TWAI Transmit Issue, When 2 Bytes of the Data are variable numbers. Going crazy here

Ravenholem
Posts: 19
Joined: Wed Nov 10, 2021 7:13 pm

TWAI Transmit Issue, When 2 Bytes of the Data are variable numbers. Going crazy here

Postby Ravenholem » Thu Jul 28, 2022 6:59 pm

Hello,
I have been pulling my hair out with this issue.. Seriously doesn't make any sense what so ever to me.
When I try to have two bytes of the Data[] Array be operated on the whole Twai Transmit function breaks.
Look at the below code I made a comment of where the issue happens.
This is what goes on,

Scenario 1:
RPM is a incrementing variable so always different
Data[2] = PRM; // RPM is 16bit so by doing this we get LSB
Data[3] = PRM >> 8; // Shift 8 give me the higher MSB
Results: Will get a few packets on the receiving end (Another ESP32) then start seeing 0x107 (Que Full on the sending unit)

Scenario 2:
RPM is a incrementing variable so always different
Data[2] = 43; // Just random fixed number at compile time
Data[3] = PRM >> 8; // Shift 8 give me the higher MSB
Results: Receiving unit will ACK and recive every single packet sent for as long as both devices are on and everything works normal.

Scenario 3:
Even stranger
Data[2] = PRM; // RPM is 16bit so by doing this we get LSB
Data[3] = PRM // RPM is 16bit so by doing this we get LSB
Results: Same as Scenario 1... Will get a few packets on the receiving end (Another ESP32) then start seeing 0x107 (Que Full on the sending unit)

If anyone has any insight on this please help me, I can't make heads or tails out of any of this. Because it's a basic operation that causes all this... I have no understanding of how doing any of this would cause the Twai Transmit function to start Que Full Error triggering.

Thank you

Exact Code I have compiled and running right now... I get 3 or 4 Packets ACK and showing up on the Receiving esp32, then the transmitting ESP32 just starts saying Error 0x107 (Transmit Que Full)
  1. #include <stdio.h>
  2. #include "sdkconfig.h"  // ADDED 9/7
  3. #include "string.h"
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "freertos/queue.h"
  7. #include "esp_attr.h"
  8. #include "soc/rtc.h"
  9. // CAN_BUS
  10. #include "driver/twai.h"
  11. // I/O
  12. #include "driver/gpio.h"
  13.  
  14.  
  15. void Init_CAN(){
  16.    //Initialize configuration structures using macro initializers
  17.     twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(GPIO_NUM_21, GPIO_NUM_22, TWAI_MODE_NORMAL);
  18.     g_config.intr_flags = ESP_INTR_FLAG_IRAM;
  19.     twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS();
  20.     twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
  21.  
  22.     //Install CAN driver
  23.     if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
  24.         printf("Driver installed\n");
  25.     } else {
  26.         printf("Failed to install driver\n");
  27.         return;
  28.     }
  29.  
  30.     //Start CAN driver
  31.     if (twai_start() == ESP_OK) {
  32.         printf("Driver started\n");
  33.     } else {
  34.         printf("Failed to start driver\n");
  35.         return;
  36.     }
  37. }
  38.  
  39. // CAN BUS Transmiit
  40. void CAN_Transmit(void *arg){
  41.     static uint32_t Msgs_sent = 0;
  42.     uint8_t Ticker = 0; // Used as a simple timer
  43.     uint16_t RPM = 0;   // Increments during each tick to simulate RPM rise
  44.     uint16_t ret = 0;   // Holds the error coming back from Twai que
  45.    
  46.     //Configure message to transmit
  47.     twai_message_t message = {.flags = 0, .data_length_code = 8, .identifier = 0x742, .data = {0, 0, 0, 0, 0, 0, 0, 0}};
  48.    
  49.     while(true){ // infinite loop
  50.  
  51.         if(Ticker == 100){
  52.             message.identifier = 0x315;
  53.             /*
  54.              *  This is the problem area, if you change RPM >> 8 to a fixed number then all CAN TX transmits work
  55.              *   If you leave it as it is a few Frames will get ACK and then you start seeing Transmit Que Full Errors
  56.              */
  57.             message.data[2] = (uint8_t) RPM;      // RPM "I even type cast here"
  58.             message.data[3] = RPM >> 8;             // RPM
  59.             if((ret = twai_transmit(&message,  0)) == ESP_OK) {
  60.                 printf("S\n");
  61.             }else{
  62.                 printf("%x\n", ret);
  63.             }
  64.             Ticker++;
  65.         }else if(Ticker == 200){
  66.             // Gear and Mode
  67.             message.identifier = 0x232;
  68.             message.data[3] = 3;         // Just other Testing
  69.             message.data[5] = 0xE0;   // Just other Testing
  70.             if(twai_transmit(&message, 0) == ESP_OK) {        
  71.                 printf("G\n");    
  72.             }
  73.             Ticker = 0;
  74.         }else{
  75.             Ticker++;
  76.         }
  77.  
  78.         // Pause the task for 500ms
  79.         if(RPM > 9000){
  80.             RPM = 0;
  81.         }else{
  82.             RPM++;
  83.         }
  84.  
  85.         // ets_delay_us(100);
  86.         vTaskDelay(11 / portTICK_PERIOD_MS);  
  87.     }
  88. }
  89.  
  90.  
  91. // CAN BUS Reciving
  92. void CAN_RX(void *arg){
  93.     //Wait for message to be received
  94.     while (1)
  95.     {
  96.         twai_message_t message;
  97.         if (twai_receive(&message, pdMS_TO_TICKS(1)) == ESP_OK) {
  98.             printf("Message received\n");
  99.             //Process received message
  100.             if (message.extd) {
  101.                 printf("Message is in Extended Format\n");
  102.             } else {
  103.                 printf("Message is in Standard Format\n");
  104.             }
  105.    
  106.             printf("ID is %x\n", message.identifier);
  107.    
  108.             if (!(message.rtr)) {
  109.                 for (int i = 0; i < message.data_length_code; i++) {
  110.                     printf("Data byte %d = %d\n", i, message.data[i]);
  111.                 }
  112.             }
  113.         } else {
  114.         //  printf("Failed to receive message\n");
  115.         //  return 0;
  116.         }
  117.        
  118.         vTaskDelay(500 / portTICK_PERIOD_MS);
  119.     }
  120.  
  121. }
  122.  
  123.  
  124. void app_main(void)
  125. {
  126.  
  127.     Init_CAN();
  128.     xTaskCreate(CAN_Transmit, "CAN_TX", 10000, NULL, 5, NULL);
  129. //    xTaskCreate(CAN_RX, "CAN_RX", 10000, NULL, 4, NULL); // Not doing any RX this sends only
  130.    
  131.  
  132. }

Who is online

Users browsing this forum: No registered users and 50 guests