SPI接收报错问题

fantasy
Posts: 7
Joined: Fri Apr 23, 2021 11:41 am

SPI接收报错问题

Postby fantasy » Thu Sep 09, 2021 3:58 pm

开发板是NodeMCU ESP32
esp-idf版本是4.3
  1. //下面是主函数里的测试用代码
  2. {
  3.     //SPI测试代码
  4.     luat_spi_t spi = {
  5.             .id =2,
  6.             .CPHA = 0,
  7.             .CPOL = 0,
  8.             .bit_dict = 8,
  9.             .master = 0,
  10.             .mode = 0,
  11.             .cs = 5,
  12.             .bandrate = 40*1000*1000
  13.     };
  14.     if (luat_spi_setup(&spi) == 0)
  15.     {
  16.         printf("spi setup  success\n");
  17.     }
  18.     char* recv_buf = "";
  19.     printf("send: %d\n",luat_spi_send(2,"0x9f",8));
  20.     printf("send: %d\n",luat_spi_send(2,"0x00",8));
  21.     luat_spi_recv(2,recv_buf,8);
  22.     printf("recv: %s\n",recv_buf);
  23.     printf("send: %d\n",luat_spi_send(2,"0x00",8));
  24.     luat_spi_recv(2,recv_buf,8);
  25.     printf("recv: %s\n",recv_buf);
  26. //  printf("luat_spi_transfer %d\n",luat_spi_transfer(2,"0x00",recv_buf,8));
  27. //  printf(recv_buf);
  28.     printf("\n");
  29. //  printf("luat_spi_transfer %d\n",luat_spi_transfer(2,"0x00",recv_buf,8));
  30.     printf("\n");
  31. //  printf(recv_buf);
  32. }
  33. //该函数将十六进制字符串转成十进制uint8
  34. uint8_t convert(char * src)
  35. {
  36.     luat_log_log(1,LUAT_LOG_TAG," hex str: %s\n",src);
  37.     char* src0;
  38.     uint8_t ret;
  39.     ret = strtol(src,&src0,16);
  40.     luat_log_log(1,LUAT_LOG_TAG,"ret: %x\n",ret);
  41.     return ret;
  42. }
  43. //收发函数
  44. int luat_spi_recv(int spi_id, char *recv_buf, size_t length)
  45. {
  46.     if (spi_id == 1 || spi_id == 2)
  47.     {
  48.         spi_transaction_t t;
  49.         memset(&t,0,sizeof(t));
  50.         t.length = sizeof(uint8_t) * 8;
  51.         t.rx_buffer = recv_buf;
  52.         t.tx_buffer = NULL;
  53.         esp_err_t ret = spi_device_transmit(spi_h, &t);
  54.         luat_log_log(1,LUAT_LOG_TAG,"SPI recv error check: %d\n",ret);
  55.         if (ret == ESP_OK)
  56.             return 0;
  57.     }
  58.     return -1;
  59. }
  60. //发函数
  61. int luat_spi_send(int spi_id, const char *send_buf, size_t length)
  62. {
  63.     if (spi_id == 1 || spi_id == 2)
  64.     {
  65.         spi_transaction_t t;
  66.         memset(&t,0,sizeof(t));
  67.         uint8_t hsend_buf = convert(send_buf);
  68.         t.length = sizeof(uint8_t) * 8;
  69.         t.tx_buffer = &hsend_buf;
  70.                 //下面一行代码报错 直接崩溃重启
  71.         esp_err_t ret = spi_device_transmit(spi_h, &t);
  72.         luat_log_log(1,LUAT_LOG_TAG,"SPI recv error check: %d\n",ret);
  73.         if(ret == ESP_OK)
  74.             return 0;
  75.     }
  76.     return -1;
  77. }
  78. //收函数
  79. int luat_spi_recv(int spi_id, char *recv_buf, size_t length)
  80. {
  81.     if (spi_id == 1 || spi_id == 2)
  82.     {
  83.         spi_transaction_t t;
  84.         memset(&t,0,sizeof(t));
  85.         t.length = sizeof(uint8_t) * 8;
  86.         t.rx_buffer = recv_buf;
  87.         t.tx_buffer = NULL;
  88.                 //下面一行代码报错 直接崩溃重启
  89.         esp_err_t ret = spi_device_transmit(spi_h, &t);
  90.         luat_log_log(1,LUAT_LOG_TAG,"SPI recv error check: %d\n",ret);
  91.         if (ret == ESP_OK)
  92.             return 0;
  93.     }
  94.     return -1;
  95. }
两个报错均是unhandle error,希望能帮忙看看,错在了哪里
我猜可能是我的spi_transaction_t结构体有问题,但是IDF示例里面没有看到有用过buffer,基本上用的都是data来发送数据
如果您想查看全部代码可以到仓库查看,这是仓库链接 https://gitee.com/dreamcmi/LuatOS-ESP32

Who is online

Users browsing this forum: No registered users and 60 guests