Search code examples
esp32can-bus

How to setup my CANbus message filterering acceptance code and mask?


I have no success enabling the filtering configuration of the CANbus driver. The doc is right there (CTRL+F acceptance filter): https://docs.espressif.com/projects/esp-idf/en/v3.3/api-reference/peripherals/can.html#configuration

I would like to setup the filter so that I can only catch my extended frames which bear the ID that fall within 0x18000000 to 0x18FFFFFF range.

But my code does not catch the frames which I expect (aka 0x18307001).

I probably misunderstand the documentation.

My code is as follows:


    can_general_config_t general_config = {
        .mode = 
        //CAN_MODE_NO_ACK,
        //CAN_MODE_LISTEN_ONLY,
        CAN_MODE_NORMAL,
        
        .tx_io = (gpio_num_t)TXD,
        .rx_io = (gpio_num_t)RXD,
        .clkout_io = (gpio_num_t)CAN_IO_UNUSED,
        .bus_off_io = (gpio_num_t)CAN_IO_UNUSED,
        .tx_queue_len = 100,
        .rx_queue_len = 65,

        .alerts_enabled = CAN_ALERT_NONE,
        .clkout_divider = 0
    };
    log("CAN Driver: general config done");
    can_timing_config_t timing_config = CAN_TIMING_CONFIG_500KBITS();
    log("CAN Driver: timing config done @ 500KBPS");


    can_filter_config_t filter_config = 
    {
        .acceptance_code = 0x18FFFFFF, 
        .acceptance_mask = 0x00FFFFFF, //0x18FFFFFF, 
        .single_filter = true
    };
    log("CAN Driver: filter config done");

    esp_err_t error = can_driver_install(&general_config, &timing_config, &filter_config);
    #endif

Solution

  • I finally found the solution: I have to shift left 3 bits

        const uint32_t acceptanceCode = 0x18000000U<<3;
        const uint32_t acceptanceMask = 0x00FFFFFFU<<3;