|
21 | 21 | // #define IS_ARDUINO_BUILTIN // Arduino boards with built-in CAN interface (e.g. Arduino Uno R4 Minima) |
22 | 22 | // #define IS_MCP2515 // Any board with external MCP2515 based extension module. See below to configure the module. |
23 | 23 | // #define IS_STM32_BUILTIN // STM32 boards with built-in CAN interface (e.g. STM32F4 Discovery). |
| 24 | +// #define IS_ESP32_TWAI // ESP32 boards with built-in TWAI (CAN) interface. Directly uses the ESP-IDF TWAI driver. |
24 | 25 |
|
25 | 26 |
|
26 | 27 | /* Board-specific includes ---------------------------------------------------*/ |
27 | 28 |
|
28 | | -#if defined(IS_TEENSY_BUILTIN) + defined(IS_ARDUINO_BUILTIN) + defined(IS_MCP2515) + defined(IS_STM32_BUILTIN) != 1 |
| 29 | +#if defined(IS_TEENSY_BUILTIN) + defined(IS_ARDUINO_BUILTIN) + defined(IS_MCP2515) + defined(IS_STM32_BUILTIN) + defined(IS_ESP32_TWAI) != 1 |
29 | 30 | #warning "Select exactly one hardware option at the top of this file." |
30 | 31 |
|
31 | 32 | #if CAN_HOWMANY > 0 || CANFD_HOWMANY > 0 |
@@ -65,6 +66,12 @@ struct ODriveStatus; // hack to prevent teensy compile error |
65 | 66 | #include "ODriveSTM32CAN.hpp" |
66 | 67 | #endif // IS_STM32_BUILTIN |
67 | 68 |
|
| 69 | +#ifdef IS_ESP32_TWAI |
| 70 | +// See https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/twai.html |
| 71 | +#include "driver/twai.h" |
| 72 | +#include "ODriveESP32TWAI.hpp" |
| 73 | +#endif // IS_ESP32_TWAI |
| 74 | + |
68 | 75 |
|
69 | 76 |
|
70 | 77 | /* Board-specific settings ---------------------------------------------------*/ |
@@ -160,6 +167,53 @@ bool setupCan() { |
160 | 167 | #endif // IS_STM32_BUILTIN |
161 | 168 |
|
162 | 169 |
|
| 170 | +/* ESP32 boards with built-in TWAI (CAN) */ |
| 171 | + |
| 172 | +#ifdef IS_ESP32_TWAI |
| 173 | + |
| 174 | +// Pins used to connect to CAN bus transceiver |
| 175 | +#define ESP32_TWAI_TX_PIN 26 |
| 176 | +#define ESP32_TWAI_RX_PIN 25 |
| 177 | + |
| 178 | +ESP32TWAIIntf can_intf; |
| 179 | + |
| 180 | +bool setupCan() { |
| 181 | + twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT( |
| 182 | + (gpio_num_t)ESP32_TWAI_TX_PIN, |
| 183 | + (gpio_num_t)ESP32_TWAI_RX_PIN, |
| 184 | + TWAI_MODE_NORMAL |
| 185 | + ); |
| 186 | + |
| 187 | + twai_timing_config_t t_config; |
| 188 | + switch (CAN_BAUDRATE) { |
| 189 | + case 1000000: t_config = TWAI_TIMING_CONFIG_1MBITS(); break; |
| 190 | + case 800000: t_config = TWAI_TIMING_CONFIG_800KBITS(); break; |
| 191 | + case 500000: t_config = TWAI_TIMING_CONFIG_500KBITS(); break; |
| 192 | + case 250000: t_config = TWAI_TIMING_CONFIG_250KBITS(); break; |
| 193 | + case 125000: t_config = TWAI_TIMING_CONFIG_125KBITS(); break; |
| 194 | + case 100000: t_config = TWAI_TIMING_CONFIG_100KBITS(); break; |
| 195 | + case 50000: t_config = TWAI_TIMING_CONFIG_50KBITS(); break; |
| 196 | + case 25000: t_config = TWAI_TIMING_CONFIG_25KBITS(); break; |
| 197 | + default: t_config = TWAI_TIMING_CONFIG_250KBITS(); break; |
| 198 | + } |
| 199 | + |
| 200 | + twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL(); |
| 201 | + |
| 202 | + if (twai_driver_install(&g_config, &t_config, &f_config) != ESP_OK) { |
| 203 | + return false; |
| 204 | + } |
| 205 | + |
| 206 | + if (twai_start() != ESP_OK) { |
| 207 | + twai_driver_uninstall(); |
| 208 | + return false; |
| 209 | + } |
| 210 | + |
| 211 | + return true; |
| 212 | +} |
| 213 | + |
| 214 | +#endif // IS_ESP32_TWAI |
| 215 | + |
| 216 | + |
163 | 217 | /* Example sketch ------------------------------------------------------------*/ |
164 | 218 |
|
165 | 219 | // Instantiate ODrive objects |
|
0 commit comments