SPI学习
前言
我个人以为开始学习一个新的单片机最好的方法就是先把他各个外设给跑一遍,整体了解一下他的功能,由此记录一下我学习ESP32外设的过程,防止以后忘记。
SPI 配置步骤
SPI总线初始化
spi_bus_config_t buscfg = {.miso_io_num = PIN_NUM_MISO,.mosi_io_num = PIN_NUM_MOSI,.sclk_io_num = PIN_NUM_CLK,.quadwp_io_num = -1,.quadhd_io_num = -1,.max_transfer_sz = PARALLEL_LINES * 320 * 2 + 8};
点击进入可以查看结构体中的内容如下
typedef struct {union {int mosi_io_num; ///< GPIO pin for Master Out Slave In (=spi_d) signal, or -1 if not used.int data0_io_num; ///< GPIO pin for spi data0 signal in quad/octal mode, or -1 if not used.};union {int miso_io_num; ///< GPIO pin for Master In Slave Out (=spi_q) signal, or -1 if not used.int data1_io_num; ///< GPIO pin for spi data1 signal in quad/octal mode, or -1 if not used.};int sclk_io_num; ///< GPIO pin for SPI Clock signal, or -1 if not used.union {int quadwp_io_num; ///< GPIO pin for WP (Write Protect) signal, or -1 if not used.int data2_io_num; ///< GPIO pin for spi data2 signal in quad/octal mode, or -1 if not used.};union {int quadhd_io_num; ///< GPIO pin for HD (Hold) signal, or -1 if not used.int data3_io_num; ///< GPIO pin for spi data3 signal in quad/octal mode, or -1 if not used.};int data4_io_num; ///< GPIO pin for spi data4 signal in octal mode, or -1 if not used.int data5_io_num; ///< GPIO pin for spi data5 signal in octal mode, or -1 if not used.int data6_io_num; ///< GPIO pin for spi data6 signal in octal mode, or -1 if not used.int data7_io_num; ///< GPIO pin for spi data7 signal in octal mode, or -1 if not used.int max_transfer_sz; ///< Maximum transfer size, in bytes. Defaults to 4092 if 0 when DMA enabled, or to `SOC_SPI_MAXIMUM_BUFFER_SIZE` if DMA is disabled.uint32_t flags; ///< Abilities of bus to be checked by the driver. Or-ed value of ``SPICOMMON_BUSFLAG_*`` flags.esp_intr_cpu_affinity_t isr_cpu_id; ///< Select cpu core to register SPI ISR.int intr_flags; /**< Interrupt flag for the bus to set the priority, and IRAM attribute, see* ``esp_intr_alloc.h``. Note that the EDGE, INTRDISABLED attribute are ignored* by the driver. Note that if ESP_INTR_FLAG_IRAM is set, ALL the callbacks of* the driver, and their callee functions, should be put in the IRAM.*/} spi_bus_config_t;
可以看到上面可以配置的内容非常多,可以配置三线四线SPI,但是我们本次主要学习两线的就可以了,用不到的管脚直接配置-1就OK了。
添加SPI设备
spi_device_interface_config_t devcfg = {#ifdef CONFIG_LCD_OVERCLOCK.clock_speed_hz = 26 * 1000 * 1000, //Clock out at 26 MHz#else.clock_speed_hz = 10 * 1000 * 1000, //Clock out at 10 MHz#endif.mode = 0, //SPI mode 0.spics_io_num = PIN_NUM_CS, //CS pin.queue_size = 7, //We want to be able to queue 7 transactions at a time.pre_cb = lcd_spi_pre_transfer_callback, //Specify pre-transfer callback to handle D/C line};
然后配置spi_device_interface_config_t
这个结构体,老规矩继续看一下这个结构体中的内容
typedef struct {uint8_t command_bits; ///< Default amount of bits in command phase (0-16), used when ``SPI_TRANS_VARIABLE_CMD`` is not used, otherwise ignored.uint8_t address_bits; ///< Default amount of bits in address phase (0-64), used when ``SPI_TRANS_VARIABLE_ADDR`` is not used, otherwise ignored.uint8_t dummy_bits; ///< Amount of dummy bits to insert between address and data phaseuint8_t mode; /**< SPI mode, representing a pair of (CPOL, CPHA) configuration:- 0: (0, 0)- 1: (0, 1)- 2: (1, 0)- 3: (1, 1)*/spi_clock_source_t clock_source;///< Select SPI clock source, `SPI_CLK_SRC_DEFAULT` by default.uint16_t duty_cycle_pos; ///< Duty cycle of positive clock, in 1/256th increments (128 = 50%/50% duty). Setting this to 0 (=not setting it) is equivalent to setting this to 128.uint16_t cs_ena_pretrans; ///< Amount of SPI bit-cycles the cs should be activated before the transmission (0-16). This only works on half-duplex transactions.uint8_t cs_ena_posttrans; ///< Amount of SPI bit-cycles the cs should stay active after the transmission (0-16)int clock_speed_hz; ///< SPI clock speed in Hz. Derived from `clock_source`.int input_delay_ns; /**< Maximum data valid time of slave. The time required between SCLK and MISOvalid, including the possible clock delay from slave to master. The driver uses this value to give an extradelay before the MISO is ready on the line. Leave at 0 unless you know you need a delay. For better timingperformance at high frequency (over 8MHz), it's suggest to have the right value.*/int spics_io_num; ///< CS GPIO pin for this device, or -1 if not useduint32_t flags; ///< Bitwise OR of SPI_DEVICE_* flagsint queue_size; ///< Transaction queue size. This sets how many transactions can be 'in the air' (queued using spi_device_queue_trans but not yet finished using spi_device_get_trans_result) at the same timetransaction_cb_t pre_cb; /**< Callback to be called before a transmission is started.** This callback is called within interrupt* context should be in IRAM for best* performance, see "Transferring Speed"* section in the SPI Master documentation for* full details. If not, the callback may crash* during flash operation when the driver is* initialized with ESP_INTR_FLAG_IRAM.*/transaction_cb_t post_cb; /**< Callback to be called after a transmission has completed.** This callback is called within interrupt* context should be in IRAM for best* performance, see "Transferring Speed"* section in the SPI Master documentation for* full details. If not, the callback may crash* during flash operation when the driver is* initialized with ESP_INTR_FLAG_IRAM.*/} spi_device_interface_config_t;
可以看到这个结构体里面的参数也是比较多的,但是我们主要关注的其实就是clock_speed_hz
,mode
,spics_io_num
,这几个参数,这几个也是我们在STM32上最熟悉的;当然如果需要驱动LCD我们可能还需要控制一个DC脚,这时候也可以关注一下pre_cb
这个参数,这个是在启用SPI传输前的回调,我们可以用它来控制下个发的是command还是data。
初始化总线
//Initialize the SPI busret = spi_bus_initialize(LCD_HOST, &buscfg, SPI_DMA_CH_AUTO);
用上面的函数就可以初始化我们配置好的SPI总线。
入参的选择
typedef enum {SPI_DMA_DISABLED = 0, ///< Do not enable DMA for SPI#if CONFIG_IDF_TARGET_ESP32SPI_DMA_CH1 = 1, ///< Enable DMA, select DMA Channel 1SPI_DMA_CH2 = 2, ///< Enable DMA, select DMA Channel 2#endifSPI_DMA_CH_AUTO = 3, ///< Enable DMA, channel is automatically selected by driver} spi_common_dma_t;
前两个其实都不用讲,都是我们配置好的。最后一个要看下如果选择了DMA通道,发送的数据就要需要是被存储在DMA可以访问的内存中,不然容易出现问题。
添加设备
//Attach the LCD to the SPI busret = spi_bus_add_device(SPI2_HOST, &devcfg, &spi);
用上面的函数就可以添加我们的SPI设备了
数据发送
数据发送的时候我们可以使用两个函数来发送我们的数据
spi_device_queue_trans 函数和spi_device_polling_transmit函数的区别
spi_device_queue_trans
和 spi_device_polling_transmit
都是ESP32 SPI主机驱动程序中用于发送SPI事务的函数,但它们在传输方式和使用场景上有所不同:
esp_err_t SPI_MASTER_ATTR spi_device_queue_trans(spi_device_handle_t handle, spi_transaction_t *trans_desc, TickType_t ticks_to_wait)esp_err_t SPI_MASTER_ISR_ATTR spi_device_polling_transmit(spi_device_handle_t handle, spi_transaction_t* trans_desc)
-
spi_device_queue_trans:
- 这个函数用于将SPI事务添加到中断传输队列中。
- 调用这个函数后,当前线程可以继续执行其他任务,而SPI传输将在后台通过中断服务程序异步处理。
- 它允许多个事务排队,适合于需要连续发送多个SPI事务的场景,或者当需要在传输之间插入其他代码时。
- 事务完成后,需要使用
spi_device_get_trans_result
函数来获取事务的结果。 - 这种方式适用于非阻塞传输,可以提高CPU的利用率。
-
spi_device_polling_transmit:
- 这个函数用于发送轮询模式下的SPI事务,调用后会等待事务完成并返回结果。
- 在轮询模式下,CPU会一直等待直到SPI事务完成,期间不能执行其他任务,这可能会导致CPU资源的浪费。
- 它适合于对实时性要求较高的场合,或者当需要在事务之间精确控制时序时。
- 如果需要在传输中间插入其他代码,可以使用
spi_device_polling_start
和spi_device_polling_end
两个函数来实现。
总结来说,spi_device_queue_trans
提供了一种非阻塞的、异步的SPI传输方式,适合于多任务环境和需要高CPU利用率的场景;而spi_device_polling_transmit
提供了一种同步的、阻塞的传输方式,适合于对时序要求严格的场合。开发者可以根据具体的应用需求选择合适的函数。
struct spi_transaction_t {uint32_t flags; ///< Bitwise OR of SPI_TRANS_* flagsuint16_t cmd; /**< Command data, of which the length is set in the ``command_bits`` of spi_device_interface_config_t.** <b>NOTE: this field, used to be "command" in ESP-IDF 2.1 and before, is re-written to be used in a new way in ESP-IDF 3.0.</b>** Example: write 0x0123 and command_bits=12 to send command 0x12, 0x3_ (in previous version, you may have to write 0x3_12).*/uint64_t addr; /**< Address data, of which the length is set in the ``address_bits`` of spi_device_interface_config_t.** <b>NOTE: this field, used to be "address" in ESP-IDF 2.1 and before, is re-written to be used in a new way in ESP-IDF3.0.</b>** Example: write 0x123400 and address_bits=24 to send address of 0x12, 0x34, 0x00 (in previous version, you may have to write 0x12340000).*/size_t length; ///< Total data length, in bitssize_t rxlength; ///< Total data length received, should be not greater than ``length`` in full-duplex mode (0 defaults this to the value of ``length``).void *user; ///< User-defined variable. Can be used to store eg transaction ID.union {const void *tx_buffer; ///< Pointer to transmit buffer, or NULL for no MOSI phaseuint8_t tx_data[4]; ///< If SPI_TRANS_USE_TXDATA is set, data set here is sent directly from this variable.};union {void *rx_buffer; ///< Pointer to receive buffer, or NULL for no MISO phase. Written by 4 bytes-unit if DMA is used.uint8_t rx_data[4]; ///< If SPI_TRANS_USE_RXDATA is set, data is received directly to this variable};} ; //the rx data should start from a 32-bit aligned address to get around dma issue.
同样的使用上面两个函数我们都需要传递这个结构体来指示一些东西
flags:SPI 事务标志位,可以使用 SPI_TRANS_*
宏定义进行设置。这些标志位用于控制事务的行为,例如是否使用 DMA、是否在事务结束后保持 CS 信号等
length:本次发送的总数据长度,以位为单位。SPI 最多一次可传输 64 字节(65536 位)数据,如果需要传输更多数据,建议使用 DMA
user:用户定义的变量,可以用于存储事务 ID 等信息
tx_buffer:发送数据缓冲区指针,如果不需要发送数据,则设置为 NULL
rx_buffer:接收数据缓冲区指针,如果不需要接收数据,则设置为 NULL,在 DMA 使用时,每次读取 4 字节
最终代码
void lcd_spi_pre_transfer_callback(spi_transaction_t *t){int dc=(int)t->user;gpio_set_level(PIN_NUM_DC, dc);// printf("DC get 1\n");}void send_non_blocking(spi_device_handle_t spi,uint8_t *data,uint16_t len){esp_err_t ret;static spi_transaction_t trans;memset(&trans, 0, sizeof(trans)); //Zero out the transactiontrans.length=8*len;trans.flags = 0; //undo SPI_TRANS_USE_TXDATA flagtrans.tx_buffer = data;ret=spi_device_queue_trans(spi, &trans, portMAX_DELAY);}spi_device_handle_t spi;uint8_t spi_send[5] DMA_ATTR = {0x23,0x12,0x37,0x44,0x55};void tx_spi_init(void){esp_err_t ret;spi_bus_config_t buscfg = {.miso_io_num = PIN_NUM_MISO,.mosi_io_num = PIN_NUM_MOSI,.sclk_io_num = PIN_NUM_CLK,.quadwp_io_num = -1,.quadhd_io_num = -1,.max_transfer_sz = 16 * 320 * 2 + 8};spi_device_interface_config_t devcfg = {#ifdef CONFIG_LCD_OVERCLOCK.clock_speed_hz = 26 * 1000 * 1000, //Clock out at 26 MHz#else.clock_speed_hz = 1 * 1000 * 1000, //Clock out at 10 MHz#endif.mode = 3, //SPI mode 0.spics_io_num = PIN_NUM_CS, //CS pin.queue_size = 7, //We want to be able to queue 7 transactions at a time.pre_cb = lcd_spi_pre_transfer_callback, //Specify pre-transfer callback to handle D/C line};//Initialize the SPI busret = spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO);ESP_ERROR_CHECK(ret);//Attach the LCD to the SPI busret = spi_bus_add_device(SPI2_HOST, &devcfg, &spi);ESP_ERROR_CHECK(ret);}void spi_test(void)
{send_non_blocking(spi,spi_send,5);
}