Signed-off-by: 吴文峰 <kevin@lmve.net>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
#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";
|
||||
|
||||
spi_device_handle_t lcd_spi;
|
||||
|
||||
void spi_init()
|
||||
{
|
||||
ESP_LOGI(TAG,"初始化SPI IO总线");
|
||||
esp_err_t ret;
|
||||
|
||||
// 1. SPI 总线配置
|
||||
spi_bus_config_t buscfg = {
|
||||
.miso_io_num = VSPI_MISO, // MISO 引脚
|
||||
.mosi_io_num = VSPI_MOSI, // MOSI 引脚
|
||||
.sclk_io_num = VSPI_SCLK, // 时钟引脚
|
||||
.quadwp_io_num = -1, // 不使用 QWP
|
||||
.quadhd_io_num = -1, // 不使用 QHD
|
||||
.max_transfer_sz = 4096, // 最大传输大小
|
||||
};
|
||||
|
||||
// 2. 初始化 SPI 总线
|
||||
ret = spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "SPI2 IO总线初始化失败");
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG,"SPI IO总线初始化成功");
|
||||
|
||||
ESP_LOGI(TAG,"挂载设备片选IO");
|
||||
|
||||
spi_device_interface_config_t devcfg = {
|
||||
.mode = 0, // SPI 模式 0
|
||||
.clock_speed_hz = 80 * 1000 * 1000, //
|
||||
.spics_io_num = LCD_CS,
|
||||
.queue_size = 7, // 队列深度
|
||||
.command_bits = 8, // 命令 8 位
|
||||
.address_bits = 8,
|
||||
};
|
||||
|
||||
ret = spi_bus_add_device(SPI2_HOST, &devcfg, &lcd_spi);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "挂载设备片选IO失败");
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user