Implement CAN communication using ESP-IDF in freeRTOA
Posted: 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.