Code: Select all
TaskHandle_t _xHandleTransmit;
void canbusTransmitTask(void *pvParam) {
can_message_t message;
message.identifier = 0xAAAA;
message.flags = CAN_MSG_FLAG_EXTD;
message.data_length_code = 4;
for (int i = 0; i < 4; i++) {
message.data[i] = 0;
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
for (;;) {
vTaskDelay(5000 / portTICK_PERIOD_MS);
if (can_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) {
printf("Message queued for transmission\n");
} else {
printf("Failed to queue message for transmission\n");
}
}
}
void app_main()
{
//Initialize configuration structures using macro initializers
can_general_config_t g_config = CAN_GENERAL_CONFIG_DEFAULT(GPIO_NUM_5, GPIO_NUM_4, CAN_MODE_NORMAL);
can_timing_config_t t_config = CAN_TIMING_CONFIG_100KBITS();
can_filter_config_t f_config = CAN_FILTER_CONFIG_ACCEPT_ALL();
//Install CAN driver
if (can_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 (can_start() == ESP_OK) {
printf("Driver started\n");
} else {
printf("Failed to start driver\n");
return;
}
xTaskCreate(canbusTransmitTask, "canbusTransmitTask", 1024, NULL, 4, &_xHandleTransmit);
while ( 1 ) {
//Wait for message to be received
can_message_t message2;
if (can_receive(&message2, pdMS_TO_TICKS(10000)) == ESP_OK) {
printf("Message received\n");
} else {
printf("Failed to receive message2\n");
continue;
}
//Process received message2
if (message2.flags & CAN_MSG_FLAG_EXTD) {
printf("Message is in Extended Format\n");
} else {
printf("Message is in Standard Format\n");
}
printf("ID is %d\n", message2.identifier);
if (!(message2.flags & CAN_MSG_FLAG_RTR)) {
for (int i = 0; i < message2.data_length_code; i++) {
printf("Data byte %d = %d\n", i, message2.data[i]);
}
}
vTaskDelay(50 / portTICK_PERIOD_MS);
}
}
Code: Select all
Driver installed
Driver started
Message queued for transmission
Failed to receive message2
Message queued for transmission
Message queued for transmission
Failed to receive message2
Message queued for transmission
Message queued for transmission
Failed to receive message2
I added the code below, but it still doesn't work. (after can_driver_install)
Code: Select all
gpio_set_pull_mode(4, GPIO_FLOATING);
gpio_matrix_in(4, CAN_RX_IDX, false);
gpio_pad_select_gpio(4);
gpio_set_direction(4, GPIO_MODE_INPUT);