ESPHome 2025.12.3
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_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32C61) || \
20 defined(USE_ESP32_VARIANT_ESP32H2) || defined(USE_ESP32_VARIANT_ESP32P4) || defined(USE_ESP32_VARIANT_ESP32S2) || \
21 defined(USE_ESP32_VARIANT_ESP32S3)
23 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_1KBITS();
24 return true;
26 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_5KBITS();
27 return true;
29 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_10KBITS();
30 return true;
32 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_12_5KBITS();
33 return true;
35 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_16KBITS();
36 return true;
38 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_20KBITS();
39 return true;
40#endif
42 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_25KBITS();
43 return true;
45 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_50KBITS();
46 return true;
48 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_100KBITS();
49 return true;
51 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_125KBITS();
52 return true;
54 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_250KBITS();
55 return true;
57 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_500KBITS();
58 return true;
60 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_800KBITS();
61 return true;
63 *t_config = (twai_timing_config_t) TWAI_TIMING_CONFIG_1MBITS();
64 return true;
65 default:
66 return false;
67 }
68}
69
71 static int next_twai_ctrl_num = 0;
72 if (static_cast<unsigned>(next_twai_ctrl_num) >= SOC_TWAI_CONTROLLER_NUM) {
73 ESP_LOGW(TAG, "Maximum number of esp32_can components created already");
74 this->mark_failed();
75 return false;
76 }
77
78 twai_general_config_t g_config =
79 TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t) this->tx_, (gpio_num_t) this->rx_, TWAI_MODE_NORMAL);
80 g_config.controller_id = next_twai_ctrl_num++;
81 if (this->tx_queue_len_.has_value()) {
82 g_config.tx_queue_len = this->tx_queue_len_.value();
83 }
84 if (this->rx_queue_len_.has_value()) {
85 g_config.rx_queue_len = this->rx_queue_len_.value();
86 }
87
88 twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
89 twai_timing_config_t t_config;
90
91 if (!get_bitrate(this->bit_rate_, &t_config)) {
92 // invalid bit rate
93 this->mark_failed();
94 return false;
95 }
96
97 // Install TWAI driver
98 if (twai_driver_install_v2(&g_config, &t_config, &f_config, &(this->twai_handle_)) != ESP_OK) {
99 // Failed to install driver
100 this->mark_failed();
101 return false;
102 }
103
104 // Start TWAI driver
105 if (twai_start_v2(this->twai_handle_) != ESP_OK) {
106 // Failed to start driver
107 this->mark_failed();
108 return false;
109 }
110 return true;
111}
112
114 if (this->twai_handle_ == nullptr) {
115 // not setup yet or setup failed
116 return canbus::ERROR_FAIL;
117 }
118
119 if (frame->can_data_length_code > canbus::CAN_MAX_DATA_LENGTH) {
121 }
122
123 uint32_t flags = TWAI_MSG_FLAG_NONE;
124 if (frame->use_extended_id) {
125 flags |= TWAI_MSG_FLAG_EXTD;
126 }
127 if (frame->remote_transmission_request) {
128 flags |= TWAI_MSG_FLAG_RTR;
129 }
130
131 twai_message_t message = {
132 .flags = flags,
133 .identifier = frame->can_id,
134 .data_length_code = frame->can_data_length_code,
135 .data = {}, // to suppress warning, data is initialized properly below
136 };
137 if (!frame->remote_transmission_request) {
138 memcpy(message.data, frame->data, frame->can_data_length_code);
139 }
140
141 if (twai_transmit_v2(this->twai_handle_, &message, this->tx_enqueue_timeout_ticks_) == ESP_OK) {
142 return canbus::ERROR_OK;
143 } else {
145 }
146}
147
149 if (this->twai_handle_ == nullptr) {
150 // not setup yet or setup failed
151 return canbus::ERROR_FAIL;
152 }
153
154 twai_message_t message;
155
156 if (twai_receive_v2(this->twai_handle_, &message, 0) != ESP_OK) {
157 return canbus::ERROR_NOMSG;
158 }
159
160 frame->can_id = message.identifier;
161 frame->use_extended_id = message.flags & TWAI_MSG_FLAG_EXTD;
162 frame->remote_transmission_request = message.flags & TWAI_MSG_FLAG_RTR;
163 frame->can_data_length_code = message.data_length_code;
164
165 if (!frame->remote_transmission_request) {
166 size_t dlc =
167 message.data_length_code < canbus::CAN_MAX_DATA_LENGTH ? message.data_length_code : canbus::CAN_MAX_DATA_LENGTH;
168 memcpy(frame->data, message.data, dlc);
169 }
170
171 return canbus::ERROR_OK;
172}
173
174} // namespace esp32_can
175} // namespace esphome
176
177#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:70
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