esphome / feature-requests

ESPHome Feature Request Tracker
https://esphome.io/
411 stars 26 forks source link

Enhance CAN BUS integration for low speed #1861

Open MaKo-RS opened 2 years ago

MaKo-RS commented 2 years ago

Describe the problem you have/What new integration you would like

I would like to have an enhancement to the existing CAN integration for esp32_can to be able to run lower baudrates like 20kbps.

Please describe your use case for this integration and alternatives you've tried:

In industrial or building automation systems which run CANopen protocol or similar, the default baudrate is 20kbps. Due to long bus lines it is often not possible to run higher baudrates. One possible solution is to use the external CAN device MCP2515 via SPI. But the internal CAN engine in ESP32 is able to do the same. To reduce hardware the idea is to use the internal engine for this.

In ESP32 devices with version lower V2 it is not possible to use low speed. The APB clock runs with 80MHz and the prescaler for the bit timing is only 6 bits wide. From Version V2 of ESP32 one can enable an additional divider by 2 and low speeds are possible. Since 2 or 3 years V3 from ESP32 is available. Enabling the divider is done with setting CONFIG_ESP32_REV_MIN to 2 or higher in ESP_IDF. With this bit 4 in the CAN interrupt enable register is set. This bit was reserved before but is used as the additional prescale divider since V2.

In YAML configuration of ESP32_CAN I would prefer to have a possibility to select baudrates as it is today. Additional it would be nice to be able to overwrite the defaults for the bittiming registers like that:

canbus:
  - platform: esp32_can
    tx_pin: GPIO5
    rx_pin: GPIO4
    can_id: 160
    bit_rate: 20KBPS
    ...

Or as an alternative for more felxibility with special bus systems:

canbus:
  - platform: esp32_can
    tx_pin: GPIO5
    rx_pin: GPIO4
    can_id: 160
    divider2: true
    btr0: xxx
    btr1: xxx
    ...

Additional context

CAN description @espressif: https://docs.espressif.com/projects/esp-idf/en/release-v3.3/api-reference/peripherals/can.html In section Bit Timing is the description.

https://github.com/miwagner/ESP32-Arduino-CAN/pull/33#issue-1145148972

bakermat commented 1 year ago

I'm also looking for this, as a few of the devices I'm working with have a 20kbps bitrate. Would be great if this can be added to esp32_can.

kala79 commented 1 year ago

This feature would also be desirable for me!