ESP32做SPI主机通信时的若干问题

文章讲述了spi_master中出现的重复定义问题以及检查传输有效性的错误,涉及了DMA使用、传输缓冲区大小、最大传输数据长度配置等,提供了相应的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. spi_master: check_trans_valid(698): rxdata transfer > host maximum

问题原因是spi_transaction_t事件多次重复定义,解决方法是将

spi_transaction_t t;

转移到while(1)循环外面,不要每次都重复定义:

static void spi_task(void *pvParameters){

	//Configuration for the SPI bus
	spi_bus_config_t buscfg = {
        .mosi_io_num = GPIO_BB_MOSI,
        .miso_io_num = GPIO_BB_MISO,
        .sclk_io_num = GPIO_BB_SCLK,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1
		//.max_transfer_sz = 81920
    };

	//Configuration for the SPI device on the other side of the bus
	spi_device_interface_config_t devcfg = {
        .command_bits = 0,
        .address_bits = 0,
        .dummy_bits = 0,
        .clock_speed_hz = 20000000, //20MHz时钟频率
		.input_delay_ns = 0,
        .duty_cycle_pos = 128,        //50% duty cycle
		.mode = 2,
        .spics_io_num = GPIO_BB_CS,
        .cs_ena_posttrans = 3,
		.queue_size = 3
    };

	//注册发送CMOS控制指令的SPI 通道
	static esp_err_t ret_cmos;
	//Initialize the SPI bus and add the device we want to send stuff to.
	ret_cmos = spi_bus_initialize(CMOSCTRL_HOST, &buscfg, SPI_DMA_CH_AUTO);
	assert(ret_cmos==ESP_OK);
	ret_cmos = spi_bus_add_device(CMOSCTRL_HOST, &devcfg, &handle_cmos);
	assert(ret_cmos==ESP_OK);

	spi_transaction_t t;

	while(1){

		t.length = sizeof(cmos_sendbuf)*8;
		t.tx_buffer = cmos_sendbuf;
		t.rx_buffer = cmos_recvbuf;

		ESP_ERROR_CHECK(spi_device_transmit(handle_cmos, &t));

		vTaskDelay(1 / portTICK_PERIOD_MS);
		
	}
}

2. spi_master: check_trans_valid(1103): txdata transfer > host maximum

问题原因可能为:

 1)传输缓冲区大小不正确

 2)当待传输数据量很大时未开启DMA

 3)传输大量数据时未更改spi_bus_config_t结构体中的最大传输数据长度

对应的解决方案:

 2)在初始化SPI总线时开启DMA,但需要注意,这时SPI模式0不支持使用

//Initialize the SPI bus and add the device we want to send stuff to.
	ret_cmos = spi_bus_initialize(CMOSCTRL_HOST, &buscfg, SPI_DMA_CH_AUTO);
	assert(ret_cmos==ESP_OK);
	ret_cmos = spi_bus_add_device(CMOSCTRL_HOST, &devcfg, &handle_cmos);
	assert(ret_cmos==ESP_OK);

3) 在spi_bus_config_t结构体中配置最大传输长度:

//Configuration for the SPI bus
	spi_bus_config_t buscfg = {
        .mosi_io_num = GPIO_BB_MOSI,
        .miso_io_num = GPIO_BB_MISO,
        .sclk_io_num = GPIO_BB_SCLK,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1
		.max_transfer_sz = 81920
    };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Anndy.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值