SPI发送触发看门狗自动复位

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

SPI发送触发看门狗自动复位

Postby fantasy » Wed Oct 20, 2021 3:54 pm

开发板是nodemcu esp32
esp-idf v4.3
代码如下
  1. void app_main(void)
  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 = 20*1000*1000
  13.     };
  14.     if (luat_spi_setup(&spi) == 0)
  15.     {
  16.         printf("spi setup  success\n");
  17.     }
  18.     printf("luat_spi_transfer: %d",luat_spi_transfer(2,"0x9F",recv_buf,1));
  19.     char* recv_buf0 = "";
  20.     printf("luat_spi_transfer: %d",luat_spi_transfer(2,"0x00",recv_buf0,1));
  21.     printf(recv_buf0);
  22.     char* recv_buf1 = "";
  23.     printf("luat_spi_transfer: %d",luat_spi_transfer(2,"0x00",recv_buf1,1));
  24.     printf(recv_buf1);
  25.     char* recv_buf2 = "";
  26.     printf("luat_spi_transfer: %d",luat_spi_transfer(2,"0x00",recv_buf2,1));
  27.     printf(recv_buf2);
  28. }
  29. //十六进制字符串转uint8_t
  30. uint8_t AsciiToHex8(const char * src)
  31. {
  32.     luat_log_log(1,LUAT_LOG_TAG," hex str: %s\n",src);
  33.     char* src0;
  34.     uint8_t ret;
  35.     ret = strtol(src,&src0,16);
  36.     luat_log_log(1,LUAT_LOG_TAG,"ret: %x\n",ret);
  37.     return ret;
  38. }
  39. //SPI收发
  40. int luat_spi_transfer(int spi_id, const char *send_buf, char *recv_buf, size_t length)
  41. {
  42.     //判断是否超出32位 超过续开启DMA
  43.     if(length*8 > 32)
  44.     {
  45.         luat_log_error(LUAT_LOG_TAG,"Data exceeds 32 bits");
  46.         return -1;
  47.     }
  48.     //判断长度是否为0 为0则不发送
  49.     if(length == 0)
  50.     {
  51.         luat_log_error(LUAT_LOG_TAG,"Data length can't be 0");
  52.         return -1;
  53.     }
  54.     //判断SPI ID是否合法
  55.     if (spi_id == 1 || spi_id == 2)
  56.     {
  57.         //初始化发送配珠
  58.         printf("init spi_transaction_t");
  59.         uint8_t *hex_recv_buf = NULL;
  60.         spi_transaction_t t;
  61.         memset(&t,0,sizeof(t));
  62.         luat_log_debug(LUAT_LOG_TAG,"Data is %d byte\n",length);
  63.         t.length = length*8;
  64.         //将数据转换成uint8_t
  65.         printf("convert spi_transaction_t");
  66.         uint8_t hex_send_buf = AsciiToHex8(send_buf);
  67.         luat_log_debug(LUAT_LOG_TAG,"AsciiToHex8 %x\n",hex_send_buf);
  68.         t.tx_buffer = hex_send_buf;
  69.         luat_log_debug(LUAT_LOG_TAG,"Recv Hex\n");
  70.         t.rx_buffer = hex_recv_buf;
  71.         printf("send spi_transaction_t");
  72.         esp_err_t ret = spi_device_transmit(spi_h, &t);
  73.         if (ret != ESP_OK)
  74.             return -1;
  75.         HexToAscii(hex_recv_buf,recv_buf,sizeof(hex_recv_buf));
  76.         luat_log_log(1,LUAT_LOG_TAG,"SPI recv error check: %d\n",ret);
  77.     }
  78.     return 0;
  79. }
串口输出:
`(2]IX49JF%O]U1R`@S]I6O.png
`(2]IX49JF%O]U1R`@S]I6O.png (211.81 KiB) Viewed 1825 times

Who is online

Users browsing this forum: No registered users and 91 guests