Canbus RX does not work

eowesi
Posts: 11
Joined: Sat Jun 09, 2018 8:55 am

Canbus RX does not work

Postby eowesi » Fri Nov 15, 2019 11:54 am

I simply connected the TX pin to the RX pin.(So there's no Can Transreceiver.) When I look through the oscilloscope, I can see the continuous data flow.That's why I think the TX pin works. Code is as below.

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);

    }


}
Console output:

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);

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Canbus RX does not work

Postby WiFive » Fri Nov 15, 2019 12:16 pm


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

Re: Canbus RX does not work

Postby ESP_Dazz » Fri Nov 15, 2019 12:29 pm

Code: Select all

can_general_config_t g_config = CAN_GENERAL_CONFIG_DEFAULT(GPIO_NUM_5, GPIO_NUM_4, CAN_MODE_NO_ACK);
...
message.flags = CAN_MSG_FLAG_EXTD | CAN_MSG_FLAG_SELF;
Documentation can be found here, and self test example can be found here.

Who is online

Users browsing this forum: No registered users and 119 guests