esp-idf版本是4.3
- //下面是主函数里的测试用代码
- {
- //SPI测试代码
- luat_spi_t spi = {
- .id =2,
- .CPHA = 0,
- .CPOL = 0,
- .bit_dict = 8,
- .master = 0,
- .mode = 0,
- .cs = 5,
- .bandrate = 40*1000*1000
- };
- if (luat_spi_setup(&spi) == 0)
- {
- printf("spi setup success\n");
- }
- char* recv_buf = "";
- printf("send: %d\n",luat_spi_send(2,"0x9f",8));
- printf("send: %d\n",luat_spi_send(2,"0x00",8));
- luat_spi_recv(2,recv_buf,8);
- printf("recv: %s\n",recv_buf);
- printf("send: %d\n",luat_spi_send(2,"0x00",8));
- luat_spi_recv(2,recv_buf,8);
- printf("recv: %s\n",recv_buf);
- // printf("luat_spi_transfer %d\n",luat_spi_transfer(2,"0x00",recv_buf,8));
- // printf(recv_buf);
- printf("\n");
- // printf("luat_spi_transfer %d\n",luat_spi_transfer(2,"0x00",recv_buf,8));
- printf("\n");
- // printf(recv_buf);
- }
- //该函数将十六进制字符串转成十进制uint8
- uint8_t convert(char * src)
- {
- luat_log_log(1,LUAT_LOG_TAG," hex str: %s\n",src);
- char* src0;
- uint8_t ret;
- ret = strtol(src,&src0,16);
- luat_log_log(1,LUAT_LOG_TAG,"ret: %x\n",ret);
- return ret;
- }
- //收发函数
- int luat_spi_recv(int spi_id, char *recv_buf, size_t length)
- {
- if (spi_id == 1 || spi_id == 2)
- {
- spi_transaction_t t;
- memset(&t,0,sizeof(t));
- t.length = sizeof(uint8_t) * 8;
- t.rx_buffer = recv_buf;
- t.tx_buffer = NULL;
- esp_err_t ret = spi_device_transmit(spi_h, &t);
- luat_log_log(1,LUAT_LOG_TAG,"SPI recv error check: %d\n",ret);
- if (ret == ESP_OK)
- return 0;
- }
- return -1;
- }
- //发函数
- int luat_spi_send(int spi_id, const char *send_buf, size_t length)
- {
- if (spi_id == 1 || spi_id == 2)
- {
- spi_transaction_t t;
- memset(&t,0,sizeof(t));
- uint8_t hsend_buf = convert(send_buf);
- t.length = sizeof(uint8_t) * 8;
- t.tx_buffer = &hsend_buf;
- //下面一行代码报错 直接崩溃重启
- esp_err_t ret = spi_device_transmit(spi_h, &t);
- luat_log_log(1,LUAT_LOG_TAG,"SPI recv error check: %d\n",ret);
- if(ret == ESP_OK)
- return 0;
- }
- return -1;
- }
- //收函数
- int luat_spi_recv(int spi_id, char *recv_buf, size_t length)
- {
- if (spi_id == 1 || spi_id == 2)
- {
- spi_transaction_t t;
- memset(&t,0,sizeof(t));
- t.length = sizeof(uint8_t) * 8;
- t.rx_buffer = recv_buf;
- t.tx_buffer = NULL;
- //下面一行代码报错 直接崩溃重启
- esp_err_t ret = spi_device_transmit(spi_h, &t);
- luat_log_log(1,LUAT_LOG_TAG,"SPI recv error check: %d\n",ret);
- if (ret == ESP_OK)
- return 0;
- }
- return -1;
- }
我猜可能是我的spi_transaction_t结构体有问题,但是IDF示例里面没有看到有用过buffer,基本上用的都是data来发送数据
如果您想查看全部代码可以到仓库查看,这是仓库链接 https://gitee.com/dreamcmi/LuatOS-ESP32