esp32_can icon indicating copy to clipboard operation
esp32_can copied to clipboard

Add support for single Core ESP32 / multiple TWAI controllers

Open outlandnish opened this issue 1 year ago • 4 comments

Changes

  • Runs xTaskCreate instead of xTaskCreatePinnedToCore if used on a device with CONFIG_FREERTOS_UNICORE enabled (e.g. on single core ESP32s)
  • Uses a secondary internal TWAI controller if SOC_TWAI_CONTROLLER_NUM = 2 (like on the ESP32 C6 with two CAN controllers) and IDF version >= 5.2 (which introduces the v2 TWAI API)
  • Adds a preprocessor define HAS_EXTERNAL_CAN_CONTROLLER that a user can set before including esp32_can.h to enable support for an MCP2515 / MCP2517FD controller. If SOC_TWAI_CONTROLLER_NUM = 2 and IDF version >= 5.2, the MCP device shows up as CAN2 (and CAN1 otherwise)

Example for external controller

#define HAS_EXTERNAL_CAN_CONTROLLER
#include esp3_can.h

outlandnish avatar Mar 16 '24 02:03 outlandnish

Any timeline on merging this @collin80? Seems like a great update now that the C6 modules are available.

TechOverflow avatar May 03 '24 15:05 TechOverflow

My only real trepidation about merging this is that it defaults to not loading the driver for external controllers. Since that was the old default this would break all existing programs that relied on the fact that an external controller was automatically loaded. I do realize that perhaps I should never have made that the default but that's what happened. So, I have to decide what I want to do about that. I should have done that back when this pull request was created but I've been busy. I will try to merge this in soon.

collin80 avatar Jun 11 '24 00:06 collin80

@collin80 one option for not introducing a breaking change is that I can have it default to defining HAS_EXTERNAL_CAN_CONTROLLER when the IDF version < 5.2 and SOC_TWAI_CONTROLLER_NUM is not 2. That way it should work out of the box with the existing solution.

Any thoughts?

outlandnish avatar Jun 11 '24 11:06 outlandnish

Hi @outlandnish, espressif/arduino-esp32 updated the core IDF to 5.3 in 3.1.0 RC today. When trying to compile your fork, I get these errors:

/Users/***/Documents/Arduino/libraries/esp32_can/src/esp32_can_builtin.cpp: In constructor 'ESP32CAN::ESP32CAN(gpio_num_t, gpio_num_t, uint8_t)':
/Users/***/Documents/Arduino/libraries/esp32_can/src/esp32_can_builtin.cpp:57:16: error: request for member 'controller_id' in '((ESP32CAN*)this)->ESP32CAN::bus_handle', which is of pointer type 'twai_handle_t' {aka 'twai_obj_t*'} (maybe you meant to use '->' ?)
   57 |     bus_handle.controller_id = busNumber;
      |                ^~~~~~~~~~~~~
/Users/***/Documents/Arduino/libraries/esp32_can/src/esp32_can_builtin.cpp: In member function 'virtual void ESP32CAN::enable()':
/Users/***/Documents/Arduino/libraries/esp32_can/src/esp32_can_builtin.cpp:373:33: error: 'g_config' was not declared in this scope; did you mean 'gpio_config'?
  373 |     if (twai_driver_install_v2(&g_config, &t_config, &f_config, &bus_handle) == ESP_OK) {
      |                                 ^~~~~~~~
      |                                 gpio_config
/Users/***/Documents/Arduino/libraries/esp32_can/src/esp32_can_builtin.cpp:373:44: error: 't_config' was not declared in this scope
  373 |     if (twai_driver_install_v2(&g_config, &t_config, &f_config, &bus_handle) == ESP_OK) {
      |                                            ^~~~~~~~
/Users/***/Documents/Arduino/libraries/esp32_can/src/esp32_can_builtin.cpp:373:55: error: 'f_config' was not declared in this scope
  373 |     if (twai_driver_install_v2(&g_config, &t_config, &f_config, &bus_handle) == ESP_OK) {
      |                                                       ^~~~~~~~

exit status 1

Compilation error: exit status 1

Could you recommend fixes to them?

Fyod avatar Aug 28 '24 23:08 Fyod