ESPHome  2024.4.1
esp32_can.cpp
Go to the documentation of this file.
1 #ifdef USE_ESP32
2 #include "esp32_can.h"
3 #include "esphome/core/log.h"
4 
5 #include <driver/twai.h>
6 
7 // WORKAROUND, because CAN_IO_UNUSED is just defined as (-1) in this version
8 // of the framework which does not work with -fpermissive
9 #undef CAN_IO_UNUSED
10 #define CAN_IO_UNUSED ((gpio_num_t) -1)
11 
12 namespace esphome {
13 namespace esp32_can {
14 
15 static const char *const TAG = "esp32_can";
16 
17 static bool get_bitrate(canbus::CanSpeed bitrate, twai_timing_config_t *t_config) {
18  switch (bitrate) {
19 #if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32C3) || \
20  defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32H6)
21  case canbus::CAN_1KBPS:
22  *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_1KBITS();
23  return true;
24  case canbus::CAN_5KBPS:
25  *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_5KBITS();
26  return true;
27  case canbus::CAN_10KBPS:
28  *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_10KBITS();
29  return true;
31  *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_12_5KBITS();
32  return true;
33  case canbus::CAN_16KBPS:
34  *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_16KBITS();
35  return true;
36  case canbus::CAN_20KBPS:
37  *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_20KBITS();
38  return true;
39 #endif
40  case canbus::CAN_25KBPS:
41  *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_25KBITS();
42  return true;
43  case canbus::CAN_50KBPS:
44  *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_50KBITS();
45  return true;
47  *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_100KBITS();
48  return true;
50  *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_125KBITS();
51  return true;
53  *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_250KBITS();
54  return true;
56  *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_500KBITS();
57  return true;
59  *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_800KBITS();
60  return true;
62  *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_1MBITS();
63  return true;
64  default:
65  return false;
66  }
67 }
68 
70  twai_general_config_t g_config =
71  TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t) this->tx_, (gpio_num_t) this->rx_, TWAI_MODE_NORMAL);
72  twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
73  twai_timing_config_t t_config;
74 
75  if (!get_bitrate(this->bit_rate_, &t_config)) {
76  // invalid bit rate
77  this->mark_failed();
78  return false;
79  }
80 
81  // Install TWAI driver
82  if (twai_driver_install(&g_config, &t_config, &f_config) != ESP_OK) {
83  // Failed to install driver
84  this->mark_failed();
85  return false;
86  }
87 
88  // Start TWAI driver
89  if (twai_start() != ESP_OK) {
90  // Failed to start driver
91  this->mark_failed();
92  return false;
93  }
94  return true;
95 }
96 
98  if (frame->can_data_length_code > canbus::CAN_MAX_DATA_LENGTH) {
99  return canbus::ERROR_FAILTX;
100  }
101 
102  uint32_t flags = TWAI_MSG_FLAG_NONE;
103  if (frame->use_extended_id) {
104  flags |= TWAI_MSG_FLAG_EXTD;
105  }
106  if (frame->remote_transmission_request) {
107  flags |= TWAI_MSG_FLAG_RTR;
108  }
109 
110  twai_message_t message = {
111  .flags = flags,
112  .identifier = frame->can_id,
113  .data_length_code = frame->can_data_length_code,
114  };
115  if (!frame->remote_transmission_request) {
116  memcpy(message.data, frame->data, frame->can_data_length_code);
117  }
118 
119  if (twai_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) {
120  return canbus::ERROR_OK;
121  } else {
123  }
124 }
125 
127  twai_message_t message;
128 
129  if (twai_receive(&message, 0) != ESP_OK) {
130  return canbus::ERROR_NOMSG;
131  }
132 
133  frame->can_id = message.identifier;
134  frame->use_extended_id = message.flags & TWAI_MSG_FLAG_EXTD;
135  frame->remote_transmission_request = message.flags & TWAI_MSG_FLAG_RTR;
136  frame->can_data_length_code = message.data_length_code;
137 
138  if (!frame->remote_transmission_request) {
139  size_t dlc =
140  message.data_length_code < canbus::CAN_MAX_DATA_LENGTH ? message.data_length_code : canbus::CAN_MAX_DATA_LENGTH;
141  memcpy(frame->data, message.data, dlc);
142  }
143 
144  return canbus::ERROR_OK;
145 }
146 
147 } // namespace esp32_can
148 } // namespace esphome
149 
150 #endif
bool setup_internal() override
Definition: esp32_can.cpp:69
uint8_t can_data_length_code
Definition: canbus.h:61
CanSpeed bit_rate_
Definition: canbus.h:90
bool remote_transmission_request
Definition: canbus.h:59
const uint32_t flags
Definition: stm32flash.h:85
canbus::Error read_message(struct canbus::CanFrame *frame) override
Definition: esp32_can.cpp:126
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
canbus::Error send_message(struct canbus::CanFrame *frame) override
Definition: esp32_can.cpp:97