idf的spi还有问题
This commit is contained in:
+91
-11
@@ -1,15 +1,8 @@
|
||||
#include "spi.h"
|
||||
|
||||
|
||||
#include "driver/spi_master.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#define VSPI_MISO -1 //接收引脚不使用,仅发送
|
||||
#define VSPI_MOSI 3
|
||||
#define VSPI_SCLK 2
|
||||
|
||||
#define LCD_CS 7
|
||||
#define LCD_DS 6
|
||||
|
||||
static const char *TAG = "SPI_2";
|
||||
|
||||
@@ -17,6 +10,39 @@ spi_device_handle_t lcd_spi;
|
||||
|
||||
void spi_init()
|
||||
{
|
||||
|
||||
ESP_LOGI(TAG, "配置GPIO");
|
||||
|
||||
// 1. 配置GPIO
|
||||
gpio_config_t io_conf = {
|
||||
.pin_bit_mask = (1ULL << LCD_DS), // LCD_DS
|
||||
.mode = GPIO_MODE_OUTPUT, // 输出模式
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE, //
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE, //
|
||||
.intr_type = GPIO_INTR_DISABLE // 禁用中断
|
||||
};
|
||||
|
||||
// 2. 应用配置
|
||||
gpio_config(&io_conf);
|
||||
|
||||
// io_conf.pin_bit_mask=LCD_CS;
|
||||
// gpio_config(&io_conf);
|
||||
|
||||
// io_conf.pin_bit_mask=VSPI_SCLK;
|
||||
// gpio_config(&io_conf);
|
||||
|
||||
// io_conf.pin_bit_mask=VSPI_MOSI;
|
||||
// gpio_config(&io_conf);
|
||||
|
||||
// 3. 设置初始电平
|
||||
gpio_set_level(LCD_DS, 0); // 输出低电平
|
||||
// gpio_set_level(LCD_CS,0);
|
||||
// gpio_set_level(VSPI_SCLK,0);
|
||||
// gpio_set_level(VSPI_MOSI,0);
|
||||
|
||||
|
||||
ESP_LOGI(TAG, "配置GPIO完成");
|
||||
|
||||
ESP_LOGI(TAG,"初始化SPI IO总线");
|
||||
esp_err_t ret;
|
||||
|
||||
@@ -31,7 +57,7 @@ void spi_init()
|
||||
};
|
||||
|
||||
// 2. 初始化 SPI 总线
|
||||
ret = spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO);
|
||||
ret = spi_bus_initialize(SPI2_HOST, &buscfg,SPI_DMA_DISABLED );
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "SPI2 IO总线初始化失败");
|
||||
return;
|
||||
@@ -43,11 +69,11 @@ void spi_init()
|
||||
|
||||
spi_device_interface_config_t devcfg = {
|
||||
.mode = 0, // SPI 模式 0
|
||||
.clock_speed_hz = 80 * 1000 * 1000, //
|
||||
.clock_speed_hz = 40 * 1000 * 1000, //
|
||||
.spics_io_num = LCD_CS,
|
||||
.queue_size = 7, // 队列深度
|
||||
.command_bits = 8, // 命令 8 位
|
||||
.address_bits = 8,
|
||||
.flags = 0, // 可选标志
|
||||
|
||||
};
|
||||
|
||||
ret = spi_bus_add_device(SPI2_HOST, &devcfg, &lcd_spi);
|
||||
@@ -56,7 +82,61 @@ void spi_init()
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// SPI 写数据
|
||||
esp_err_t lcd_spi_write8(uint8_t data) {
|
||||
spi_transaction_t trans = {
|
||||
.length = 8, // 数据位数
|
||||
.tx_buffer = &data, // 发送缓冲区
|
||||
.rx_buffer = NULL, // 不接收
|
||||
};
|
||||
|
||||
return spi_device_transmit(lcd_spi, &trans);
|
||||
}
|
||||
|
||||
// SPI 写数据
|
||||
esp_err_t lcd_spi_write16(uint16_t data) {
|
||||
spi_transaction_t trans = {
|
||||
.length = 16, // 数据位数
|
||||
.tx_buffer = &data, // 发送缓冲区
|
||||
.rx_buffer = NULL, // 不接收
|
||||
};
|
||||
|
||||
return spi_device_transmit(lcd_spi, &trans);
|
||||
}
|
||||
|
||||
// SPI 写命令
|
||||
esp_err_t lcd_spi_reg(uint8_t data) {
|
||||
esp_err_t err;
|
||||
gpio_set_level(LCD_DS, 0);
|
||||
err=lcd_spi_write8(data);
|
||||
gpio_set_level(LCD_DS, 1);
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "命令发送失败");
|
||||
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
void lcd_ds_test()
|
||||
{
|
||||
esp_err_t err;
|
||||
static uint8_t this_flag=0;
|
||||
if(this_flag)
|
||||
{
|
||||
this_flag=0;
|
||||
err=gpio_set_level(LCD_DS, 0);
|
||||
}else{
|
||||
this_flag=1;
|
||||
err=gpio_set_level(LCD_DS, 1);
|
||||
}
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "GPIO失败");
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user