TWAI Transmit Issue, When 2 Bytes of the Data are variable numbers. Going crazy here
Posted: 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)
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)
- #include <stdio.h>
- #include "sdkconfig.h" // ADDED 9/7
- #include "string.h"
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "freertos/queue.h"
- #include "esp_attr.h"
- #include "soc/rtc.h"
- // CAN_BUS
- #include "driver/twai.h"
- // I/O
- #include "driver/gpio.h"
- void Init_CAN(){
- //Initialize configuration structures using macro initializers
- twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(GPIO_NUM_21, GPIO_NUM_22, TWAI_MODE_NORMAL);
- g_config.intr_flags = ESP_INTR_FLAG_IRAM;
- twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS();
- twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
- //Install CAN driver
- if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
- printf("Driver installed\n");
- } else {
- printf("Failed to install driver\n");
- return;
- }
- //Start CAN driver
- if (twai_start() == ESP_OK) {
- printf("Driver started\n");
- } else {
- printf("Failed to start driver\n");
- return;
- }
- }
- // CAN BUS Transmiit
- void CAN_Transmit(void *arg){
- static uint32_t Msgs_sent = 0;
- uint8_t Ticker = 0; // Used as a simple timer
- uint16_t RPM = 0; // Increments during each tick to simulate RPM rise
- uint16_t ret = 0; // Holds the error coming back from Twai que
- //Configure message to transmit
- twai_message_t message = {.flags = 0, .data_length_code = 8, .identifier = 0x742, .data = {0, 0, 0, 0, 0, 0, 0, 0}};
- while(true){ // infinite loop
- if(Ticker == 100){
- message.identifier = 0x315;
- /*
- * This is the problem area, if you change RPM >> 8 to a fixed number then all CAN TX transmits work
- * If you leave it as it is a few Frames will get ACK and then you start seeing Transmit Que Full Errors
- */
- message.data[2] = (uint8_t) RPM; // RPM "I even type cast here"
- message.data[3] = RPM >> 8; // RPM
- if((ret = twai_transmit(&message, 0)) == ESP_OK) {
- printf("S\n");
- }else{
- printf("%x\n", ret);
- }
- Ticker++;
- }else if(Ticker == 200){
- // Gear and Mode
- message.identifier = 0x232;
- message.data[3] = 3; // Just other Testing
- message.data[5] = 0xE0; // Just other Testing
- if(twai_transmit(&message, 0) == ESP_OK) {
- printf("G\n");
- }
- Ticker = 0;
- }else{
- Ticker++;
- }
- // Pause the task for 500ms
- if(RPM > 9000){
- RPM = 0;
- }else{
- RPM++;
- }
- // ets_delay_us(100);
- vTaskDelay(11 / portTICK_PERIOD_MS);
- }
- }
- // CAN BUS Reciving
- void CAN_RX(void *arg){
- //Wait for message to be received
- while (1)
- {
- twai_message_t message;
- if (twai_receive(&message, pdMS_TO_TICKS(1)) == ESP_OK) {
- printf("Message received\n");
- //Process received message
- if (message.extd) {
- printf("Message is in Extended Format\n");
- } else {
- printf("Message is in Standard Format\n");
- }
- printf("ID is %x\n", message.identifier);
- if (!(message.rtr)) {
- for (int i = 0; i < message.data_length_code; i++) {
- printf("Data byte %d = %d\n", i, message.data[i]);
- }
- }
- } else {
- // printf("Failed to receive message\n");
- // return 0;
- }
- vTaskDelay(500 / portTICK_PERIOD_MS);
- }
- }
- void app_main(void)
- {
- Init_CAN();
- xTaskCreate(CAN_Transmit, "CAN_TX", 10000, NULL, 5, NULL);
- // xTaskCreate(CAN_RX, "CAN_RX", 10000, NULL, 4, NULL); // Not doing any RX this sends only
- }