Implement CAN communication using ESP-IDF in freeRTOA

VincentChan
Posts: 4
Joined: Sun Sep 18, 2022 5:54 am

Implement CAN communication using ESP-IDF in freeRTOA

Postby VincentChan » Sun Sep 18, 2022 6:02 am

Code: Select all

void can_code_transmit()
{
    twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(12,14,TWAI_MODE_NORMAL);
    twai_timing_config_t t_config = TWAI_TIMING_CONFIG_25KBITS();
    twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();

    if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
        printf("Driver has been successfully installed\n");
    } else {
        printf("Failure in installing CAN driver\n");
        return;
    }

    if (twai_start() == ESP_OK) {
        printf("Driver started\n");
    } else {
        printf("Driver failed\n");
        return;
    }

    while (1)
    {
        twai_message_t message;
        message.identifier = 0x8;
        message.extd = 1;
        // message.flags = 0x00;
        message.data_length_code = 8;

        printf("Transmitting message from: 0x%d, ", message.identifier);
        printf("DLC: %d, ", message.data_length_code);
        printf("Message: "); 
        for (int i=0; i<message.data_length_code; i++) {
            unsigned char array[8] = {0x1,0x2,0x3,0x4,0xf,0xa,0xc,0xd};
            // unsigned char array[8] = {1,2,3,4,15,10,12,13};
            message.data[i] = array[i];
            // message.data[i] = 0;
            printf("0x%X ",message.data[i]); //%x lower case; %X upper case
        }printf(", ");
        if (twai_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) {            
            printf("Message queued for transmission\n");
        } else {
            printf("Failed to queue message for transmission\n");
        }
        
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }    
}

Hi guys, I am to implement CAN communication in freeRTOS using esp-idf. By following the espressif 32 documentation, I realized that the CAN message is only sent one time. I wonder how to make CAN message sending constantly although I inserted the code within the while loop(). The output console displays CAN message failed to queue for transmission. Can anyone help? Thank you.

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: Implement CAN communication using ESP-IDF in freeRTOA

Postby ESP_Dazz » Mon Sep 19, 2022 8:02 am

VincentChan wrote: The output console displays CAN message failed to queue for transmission. Can anyone help? Thank you.
Can you check what the error code is?

VincentChan
Posts: 4
Joined: Sun Sep 18, 2022 5:54 am

Re: Implement CAN communication using ESP-IDF in freeRTOA

Postby VincentChan » Mon Oct 30, 2023 5:48 am

Hi ESP_Dazz,

The error had been solved.
The solutions of this error include
1) The improper connection between the CAN transceiver and the MCU
2) The header files (driver/twai.h, hal/twai_types.h) must be well included
3) Using ESP-IDF V4.0 and above
4) Global declare the twai config settings and put it to static

static const twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)12,(gpio_num_t)14,TWAI_MODE_NORMAL);
static const twai_timing_config_t t_config = TWAI_TIMING_CONFIG_25KBITS();
static const twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();

Do not forget to declare a handler for CAN
twai_message_t rx_message;

5) remember to call twai_driver_install and twai start in the user-defined func
twai_driver_install(&g_config, &t_config, &f_config);
twai_start();

6) put twai_receive in the while(1) loop if you are to receive repetitively the CAN message. I used freertos for multitasking
7) remember to set the CAN ID type whether it is extended or non-extended
rx_message.extd==1

Who is online

Users browsing this forum: No registered users and 54 guests