ESPHome 2025.10.4
Loading...
Searching...
No Matches
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
12namespace esphome {
13namespace esp32_can {
14
15static const char *const TAG = "esp32_can";
16
17static 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_ESP32H2)
22 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_1KBITS();
23 return true;
25 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_5KBITS();
26 return true;
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;
34 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_16KBITS();
35 return true;
37 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_20KBITS();
38 return true;
39#endif
41 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_25KBITS();
42 return true;
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 static int next_twai_ctrl_num = 0;
71 if (static_cast<unsigned>(next_twai_ctrl_num) >= SOC_TWAI_CONTROLLER_NUM) {
72 ESP_LOGW(TAG, "Maximum number of esp32_can components created already");
73 this->mark_failed();
74 return false;
75 }
76
77 twai_general_config_t g_config =
78 TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t) this->tx_, (gpio_num_t) this->rx_, TWAI_MODE_NORMAL);
79 g_config.controller_id = next_twai_ctrl_num++;
80 if (this->tx_queue_len_.has_value()) {
81 g_config.tx_queue_len = this->tx_queue_len_.value();
82 }
83 if (this->rx_queue_len_.has_value()) {
84 g_config.rx_queue_len = this->rx_queue_len_.value();
85 }
86
87 twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
88 twai_timing_config_t t_config;
89
90 if (!get_bitrate(this->bit_rate_, &t_config)) {
91 // invalid bit rate
92 this->mark_failed();
93 return false;
94 }
95
96 // Install TWAI driver
97 if (twai_driver_install_v2(&g_config, &t_config, &f_config, &(this->twai_handle_)) != ESP_OK) {
98 // Failed to install driver
99 this->mark_failed();
100 return false;
101 }
102
103 // Start TWAI driver
104 if (twai_start_v2(this->twai_handle_) != ESP_OK) {
105 // Failed to start driver
106 this->mark_failed();
107 return false;
108 }
109 return true;
110}
111
113 if (this->twai_handle_ == nullptr) {
114 // not setup yet or setup failed
115 return canbus::ERROR_FAIL;
116 }
117
118 if (frame->can_data_length_code > canbus::CAN_MAX_DATA_LENGTH) {
120 }
121
122 uint32_t flags = TWAI_MSG_FLAG_NONE;
123 if (frame->use_extended_id) {
124 flags |= TWAI_MSG_FLAG_EXTD;
125 }
126 if (frame->remote_transmission_request) {
127 flags |= TWAI_MSG_FLAG_RTR;
128 }
129
130 twai_message_t message = {
131 .flags = flags,
132 .identifier = frame->can_id,
133 .data_length_code = frame->can_data_length_code,
134 .data = {}, // to suppress warning, data is initialized properly below
135 };
136 if (!frame->remote_transmission_request) {
137 memcpy(message.data, frame->data, frame->can_data_length_code);
138 }
139
140 if (twai_transmit_v2(this->twai_handle_, &message, this->tx_enqueue_timeout_ticks_) == ESP_OK) {
141 return canbus::ERROR_OK;
142 } else {
144 }
145}
146
148 if (this->twai_handle_ == nullptr) {
149 // not setup yet or setup failed
150 return canbus::ERROR_FAIL;
151 }
152
153 twai_message_t message;
154
155 if (twai_receive_v2(this->twai_handle_, &message, 0) != ESP_OK) {
156 return canbus::ERROR_NOMSG;
157 }
158
159 frame->can_id = message.identifier;
160 frame->use_extended_id = message.flags & TWAI_MSG_FLAG_EXTD;
161 frame->remote_transmission_request = message.flags & TWAI_MSG_FLAG_RTR;
162 frame->can_data_length_code = message.data_length_code;
163
164 if (!frame->remote_transmission_request) {
165 size_t dlc =
166 message.data_length_code < canbus::CAN_MAX_DATA_LENGTH ? message.data_length_code : canbus::CAN_MAX_DATA_LENGTH;
167 memcpy(frame->data, message.data, dlc);
168 }
169
170 return canbus::ERROR_OK;
171}
172
173} // namespace esp32_can
174} // namespace esphome
175
176#endif
virtual void mark_failed()
Mark this component as failed.
canbus::Error read_message(struct canbus::CanFrame *frame) override
optional< uint32_t > rx_queue_len_
Definition esp32_can.h:33
TickType_t tx_enqueue_timeout_ticks_
Definition esp32_can.h:31
optional< uint32_t > tx_queue_len_
Definition esp32_can.h:32
canbus::Error send_message(struct canbus::CanFrame *frame) override
bool setup_internal() override
Definition esp32_can.cpp:69
bool has_value() const
Definition optional.h:92
value_type const & value() const
Definition optional.h:94
const char * message
Definition component.cpp:38
uint16_t flags
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint8_t can_data_length_code
Definition canbus.h:61