ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
mcp251xfd.cpp
Go to the documentation of this file.
1#include "mcp251xfd.h"
2
3#include <algorithm>
4#include <array>
5#include <cinttypes>
6#include <cstring>
7
9#include "esphome/core/hal.h"
11#include "esphome/core/log.h"
13
15
16ESPHOME_LOG_TAG(TAG, "mcp251xfd");
17
18// Indexed by CanMode, which is the CON REQOP/OPMOD value.
19PROGMEM_STRING_TABLE(CanModeStrings, "NORMAL", "SLEEP", "LOOPBACK", "LISTENONLY", "CONFIGURATION", "EXTERNAL LOOPBACK",
20 "CLASSIC", "UNKNOWN");
21
23 this->spi_setup();
24 return this->configure_();
25}
26
28 // A 4 MHz oscillator starts without the PLL. Keep SPI below 0.85 * SYSCLK / 2 until it locks.
29 const uint32_t operating_data_rate = this->data_rate_;
30 const bool slow_start = this->mcp_clock_ == CanClock::CAN_CLOCK_4MHZ && operating_data_rate > 1000000;
31 if (slow_start) {
32 this->spi_teardown();
33 this->set_data_rate(1000000);
34 this->spi_setup();
35 }
36 const bool clock_ready = this->reset_() && this->configure_clock_();
37 // Restore the rate even on failure, so a later reconfiguration starts from it
38 if (slow_start) {
39 this->spi_teardown();
40 this->set_data_rate(operating_data_rate);
41 this->spi_setup();
42 }
43 if (!clock_ready)
44 return false;
45
46 // Leave TEF, TXQ, automatic RTR responses and timestamps disabled. ESI follows the controller's error state.
47 // Leave RTXAT clear for unlimited retransmissions, as in the MCP2515 driver.
48 std::array<uint8_t, MAX_REGISTER_WRITE_SIZE> config_data;
49 uint8_t *out =
50 put_le32(config_data.data(), (static_cast<uint32_t>(CanMode::CAN_MODE_CONFIGURATION) << 24) | CON_ISO_CRC_ENABLE);
52 out = put_le32(out, this->data_bit_timing_);
54 this->write_registers_(REG_CON, config_data.data(), config_data.size());
55
56 // NBTCFG, DBTCFG and TDC follow CON
57 std::array<uint8_t, 12> timing_data;
58 if (!this->read_registers_(REG_NBTCFG, timing_data.data(), timing_data.size()) ||
59 std::memcmp(timing_data.data(), config_data.data() + 4, timing_data.size()) != 0) {
60 ESP_LOGE(TAG, "Bit timing register verification failed");
61 return false;
62 }
63
64 // Enable the local overflow summary for polling; only RX-not-empty is enabled on the INT pin.
65 const uint32_t payload_size = this->classic_() ? FIFO_PAYLOAD_8 : FIFO_PAYLOAD_64;
66 this->write_register_(REG_TX_FIFO_CON, payload_size | ((TX_FIFO_DEPTH - 1) << 24) | FIFO_TX_ENABLE);
67 this->write_register_(REG_RX_FIFO_CON, payload_size | ((this->rx_fifo_depth_() - 1) << 24) | FIFO_RX_OVERFLOW_ENABLE |
68 (this->interrupt_pin_ != nullptr ? FIFO_RX_NOT_EMPTY_ENABLE : 0));
69
70 // Filter 0 accepts both standard and extended IDs and routes them to FIFO 2.
71 const std::array<uint8_t, 8> zeros{};
72 this->write_registers_(REG_FLTOBJ0, zeros.data(), zeros.size());
73 this->write_byte_(REG_FLTCON, FILTER_ENABLE_RX_FIFO);
74
75 if (!this->set_mode_(this->mcp_mode_) || !this->wait_for_register_(REG_TX_FIFO_CON + 1, FIFO_RESET, 0) ||
76 !this->wait_for_register_(REG_RX_FIFO_CON + 1, FIFO_RESET, 0)) {
77 // Back to Configuration, so a later mode check sees the reset and retries all of the setup
79 return false;
80 }
81
82 if (this->interrupt_pin_ != nullptr) {
83 this->interrupt_pin_->setup();
84 this->write_byte_(REG_INT + 2, INT_RX_ENABLE);
85 }
86
87 ESP_LOGV(TAG, "Setup complete");
88 return true;
89}
90
93 ESP_LOGCONFIG(TAG,
94 "MCP251XFD (CAN FD):\n"
95 " Oscillator: %u MHz\n"
96 " Mode: %s\n"
97 " Nominal Bit Timing: 0x%08" PRIX32 "\n"
98 " Data Bit Timing: 0x%08" PRIX32 "\n"
99 " Transmitter Delay Compensation: 0x%08" PRIX32,
100 static_cast<uint8_t>(this->mcp_clock_), LOG_STR_ARG(CanModeStrings::get_log_str(this->mcp_mode_)),
102 LOG_PIN(" CS Pin: ", this->cs_);
103 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
104}
105
107 // Configuration mode waits for the bus to go idle, so a reset does not cut a frame on the bus.
109 ESP_LOGW(TAG, "Config mode not confirmed before reset");
110 }
111
112 const std::array<uint8_t, 2> command{INSTRUCTION_RESET, 0};
113 this->enable();
114 this->write_array(command);
115 this->disable();
116 delay(10);
117
119}
120
122 uint8_t ready = OSC_READY;
124 // Keep the reset CLKO divider; only PLLEN changes.
125 this->write_byte_(REG_OSC, OSC_CLKODIV_10 | OSC_PLL_ENABLE);
126 ready |= OSC_PLL_READY;
127 }
128 return this->wait_for_register_(REG_OSC + 1, ready, ready);
129}
130
131bool MCP251XFD::wait_for_register_(uint16_t address, uint8_t mask, uint8_t expected, uint16_t timeout_ms) {
132 for (uint16_t attempt = 0; attempt < timeout_ms; attempt++) {
133 uint8_t value;
134 if (!this->read_registers_(address, &value, 1))
135 return false;
136 if ((value & mask) == expected)
137 return true;
138 delay(1);
139 }
140 ESP_LOGW(TAG, "Timeout waiting for register 0x%03X", address);
141 return false;
142}
143
144bool MCP251XFD::set_mode_(CanMode mode, uint16_t timeout_ms) {
145 // The rest of this byte is ABAT and TXBWS, which the driver leaves clear.
146 this->write_byte_(REG_CON + 3, static_cast<uint8_t>(mode));
147 return this->wait_for_register_(REG_CON + 2, CON_MODE_MASK << CON_MODE_SHIFT,
148 static_cast<uint8_t>(mode) << CON_MODE_SHIFT, timeout_ms);
149}
150
151bool MCP251XFD::read_registers_(uint16_t address, uint8_t *data, size_t length) {
152 std::array<uint8_t, 3> command;
153 put_be16(command.data(), (INSTRUCTION_READ_CRC << 8) | address);
154 command[2] = address >= RAM_START && address < RAM_START + RAM_SIZE ? length / 4 : length;
155
156 // Errata DS80000792, section 1: SPI pauses during a RAM read can cost a frame (SERRIF), so each read is one
157 // transfer. Plain READ can return corrupt data, so READ_CRC is retried on a mismatch.
158 // ESP32 SPI DMA writes in 4-byte units, so the buffer is aligned and padded.
159 alignas(4) std::array<uint8_t, (MAX_READ_SIZE + 5 + 3) & ~size_t{3}> transfer;
160 for (uint8_t attempt = 0; attempt < 3; attempt++) {
161 std::fill_n(std::copy(command.begin(), command.end(), transfer.begin()), length + 2, 0);
162
163 this->enable();
164 this->transfer_array(transfer.data(), length + command.size() + 2);
165 this->disable();
166
167 uint16_t crc = crc16be(command.data(), command.size(), 0xFFFF, 0x8005);
168 crc = crc16be(transfer.data() + command.size(), length, crc, 0x8005);
169 const size_t crc_offset = command.size() + length;
170 const uint16_t received_crc = encode_uint16(transfer[crc_offset], transfer[crc_offset + 1]);
171 if (crc == received_crc) {
172 std::memcpy(data, transfer.data() + command.size(), length);
173 // The only place the driver's own warning clears; the canbus base owns the bus-off one.
174 if (this->comm_fault_active_) {
175 this->comm_fault_active_ = false;
176 if (!this->bus_off_)
177 this->status_clear_warning();
178 }
179 return true;
180 }
181 }
182 return this->set_comm_fault_(LOG_STR("SPI CRC mismatch"));
183}
184
185bool MCP251XFD::set_comm_fault_(const LogString *reason) {
186 this->comm_fault_active_ = true;
187 this->status_set_warning(reason);
188 return false;
189}
190
192 std::array<uint8_t, 4> data;
193 if (!this->read_registers_(address, data.data(), data.size()))
194 return false;
195 value = encode_value_le<uint32_t>(data.data());
196 return true;
197}
198
200 if (now - this->last_fault_log_time_ < FAULT_LOG_INTERVAL_MS)
201 return false;
202 this->last_fault_log_time_ = now;
203 return true;
204}
205
206bool MCP251XFD::recover_mode_if_needed_(bool bus_off, bool retry_due) {
207 // OPMOD is in byte 2 of CON and REQOP in byte 3. A refused request leaves REQOP pending, which stalls TX.
208 std::array<uint8_t, 2> con;
209 if (!this->read_registers_(REG_CON + 2, con.data(), con.size()))
210 return false;
211 const uint8_t current_mode = (con[0] >> CON_MODE_SHIFT) & CON_MODE_MASK;
212 const uint8_t requested_mode = con[1] & CON_MODE_MASK;
213 const uint8_t target_mode = static_cast<uint8_t>(this->mcp_mode_);
214 if (current_mode == target_mode && requested_mode == target_mode) {
215 if (this->mode_fault_active_) {
216 this->mode_fault_active_ = false;
217 ESP_LOGI(TAG, "Mode %s restored", LOG_STR_ARG(CanModeStrings::get_log_str(target_mode)));
218 }
219 return true;
220 }
221
222 const bool first_attempt = !this->mode_fault_active_;
223 if (first_attempt) {
224 this->mode_fault_active_ = true;
225 ESP_LOGW(TAG, "Unexpected mode %s, requested %s, expected %s",
226 LOG_STR_ARG(CanModeStrings::get_log_str(current_mode)),
227 LOG_STR_ARG(CanModeStrings::get_log_str(requested_mode)),
228 LOG_STR_ARG(CanModeStrings::get_log_str(target_mode)));
229 } else if (!retry_due) {
230 // The first fault is fixed at once; the check interval spaces out later attempts.
231 return true;
232 }
233 // The driver never requests Configuration mode and leaves it there, so this is a chip reset, which cleared the bit
234 // timing, FIFOs and filter. NBTCFG cannot show it: its reset value is also the common 500 kbps timing.
235 constexpr uint8_t configuration = static_cast<uint8_t>(CanMode::CAN_MODE_CONFIGURATION);
236 const bool chip_reset = current_mode == configuration && requested_mode == configuration;
238 // A reset chip reports bus-off (TXBO) until it is configured again, so only a running controller waits it out
239 if (bus_off && !chip_reset) {
240 // Retry as soon as the bus recovers rather than on the next interval
241 this->last_mode_check_time_ = now - MODE_RECOVERY_INTERVAL_MS;
242 return true;
243 }
244 this->last_mode_check_time_ = now;
245
246 if (chip_reset) {
247 ESP_LOGW(TAG, "Configuration lost, reconfiguring");
248 this->configure_();
249 return true;
250 }
251 // A direct request keeps the FIFOs, as after a TX MAB underflow (errata DS80000792). The controller refuses some
252 // direct changes, such as LOOPBACK to CLASSIC, so a retry goes through Configuration, which resets the FIFOs.
253 if (first_attempt || this->set_mode_(CanMode::CAN_MODE_CONFIGURATION))
254 this->write_byte_(REG_CON + 3, target_mode);
255 return true;
256}
257
258void MCP251XFD::write_transfer_(uint16_t address, uint8_t *transfer, size_t length) {
259 put_be16(transfer, (INSTRUCTION_WRITE << 8) | address);
260 // One buffered transfer keeps the CS-low window tight (errata DS80000792, section 1).
261 this->enable();
262 this->write_array(transfer, length + 2);
263 this->disable();
264}
265
266void MCP251XFD::write_registers_(uint16_t address, const uint8_t *data, size_t length) {
267 std::array<uint8_t, 2 + MAX_REGISTER_WRITE_SIZE> transfer;
268 std::memcpy(transfer.data() + 2, data, length);
269 this->write_transfer_(address, transfer.data(), length);
270}
271
273 std::array<uint8_t, 4> data;
274 put_le32(data.data(), value);
275 this->write_registers_(address, data.data(), data.size());
276}
277
278void MCP251XFD::write_byte_(uint16_t address, uint8_t value) { this->write_registers_(address, &value, 1); }
279
281 const uint32_t message_size = this->message_size_();
282 if (offset < start || offset >= start + depth * message_size || (offset - start) % message_size != 0) {
283 return this->set_comm_fault_(LOG_STR("Invalid FIFO address"));
284 }
285 address = RAM_START + offset;
286 return true;
287}
288
290 if (this->is_failed() || this->mcp_mode_ == CanMode::CAN_MODE_LISTENONLY)
292 if (frame->can_data_length_code > MESSAGE_DLC_MASK)
294
295 std::array<uint8_t, 8> fifo;
296 if (!this->read_registers_(REG_TX_FIFO_STA, fifo.data(), fifo.size()))
298 if ((fifo[0] & FIFO_NOT_FULL_NOT_EMPTY) == 0)
300
301 uint16_t address;
302 if (!this->validate_fifo_address_(encode_value_le<uint32_t>(fifo.data() + 4), 0, TX_FIFO_DEPTH, address))
304
305 // The object follows the two write command bytes, so it goes out without a copy.
306 std::array<uint8_t, 2 + MAX_MESSAGE_SIZE> transfer;
307 const uint32_t id =
308 frame->use_extended_id ? (frame->can_id >> 18) | ((frame->can_id & EXTENDED_ID_LOW_MASK) << 11) : frame->can_id;
310 assign_bits(flags, MESSAGE_IDE, frame->use_extended_id);
311 assign_bits(flags, MESSAGE_RTR, frame->remote_transmission_request);
312#ifdef USE_CANBUS_FD
313 assign_bits(flags, MESSAGE_FDF, frame->fd);
314 assign_bits(flags, MESSAGE_BRS, frame->bit_rate_switch);
315#endif
316 uint8_t *payload = put_le32(put_le32(transfer.data() + 2, id), flags);
317 const size_t payload_length = frame->remote_transmission_request ? 0 : frame->payload_length();
318 // RAM writes must contain whole words. Unused bytes in the last word are zero.
319 const size_t padded_length = (payload_length + 3) & ~size_t{3};
320 std::memcpy(payload, frame->data, payload_length);
321 std::fill(payload + payload_length, payload + padded_length, 0);
322 this->write_transfer_(address, transfer.data(), MESSAGE_HEADER_SIZE + padded_length);
323 this->write_byte_(REG_TX_FIFO_CON + 1, FIFO_INCREMENT | FIFO_TX_REQUEST);
324 return canbus::ERROR_OK;
325}
326
328 // INT stays low until the RX FIFO is empty, so a high pin means there is nothing to read.
329 if (this->interrupt_pin_ != nullptr && this->interrupt_pin_->digital_read())
330 return canbus::ERROR_NOMSG;
331 std::array<uint8_t, 8> fifo;
332 if (!this->read_registers_(REG_RX_FIFO_STA, fifo.data(), fifo.size()))
333 return canbus::ERROR_FAIL;
334 if ((fifo[0] & FIFO_NOT_FULL_NOT_EMPTY) == 0)
335 return canbus::ERROR_NOMSG;
336
337 uint16_t address;
338 if (!this->validate_fifo_address_(encode_value_le<uint32_t>(fifo.data() + 4), TX_FIFO_DEPTH * this->message_size_(),
339 this->rx_fifo_depth_(), address))
340 return canbus::ERROR_FAIL;
341
342 // Most frames carry at most 8 bytes, so read the header and 8 data bytes first and read the rest of a longer
343 // CAN FD payload straight into the frame, rather than always reading the whole 72-byte object.
344 std::array<uint8_t, MESSAGE_HEADER_SIZE + canbus::CAN_MAX_DATA_LENGTH> message;
345 if (!this->read_registers_(address, message.data(), message.size()))
346 return canbus::ERROR_FAIL;
347 const bool fd = (message[4] & MESSAGE_FDF) != 0;
348#ifdef USE_CANBUS_FD
349 // CAN FD lengths above 8 are 8 plus whole words, as RAM reads need.
350 const uint8_t length = fd ? canbus::can_dlc_to_len(message[4] & MESSAGE_DLC_MASK) : 0;
351 if (length > canbus::CAN_MAX_DATA_LENGTH &&
352 !this->read_registers_(address + message.size(), frame->data + canbus::CAN_MAX_DATA_LENGTH,
353 length - canbus::CAN_MAX_DATA_LENGTH))
354 return canbus::ERROR_FAIL;
355#endif
356 this->write_byte_(REG_RX_FIFO_CON + 1, FIFO_INCREMENT);
357 if (fd && this->classic_())
358 return canbus::ERROR_FAIL;
359
360 const uint32_t id = encode_value_le<uint32_t>(message.data());
361 frame->use_extended_id = (message[4] & MESSAGE_IDE) != 0;
362 if (frame->use_extended_id) {
363 frame->can_id = ((id & canbus::CAN_STANDARD_ID_MAX) << 18) | ((id >> 11) & EXTENDED_ID_LOW_MASK);
364 } else {
365 frame->can_id = id & canbus::CAN_STANDARD_ID_MAX;
366 }
367 // In a CAN FD frame the RTR position is the reserved RRS bit
368 frame->remote_transmission_request = !fd && (message[4] & MESSAGE_RTR);
369#ifdef USE_CANBUS_FD
370 // The base clears the other CAN FD flags before each read
371 frame->fd = fd;
372 if (fd) {
373 frame->bit_rate_switch = (message[4] & MESSAGE_BRS) != 0;
374 frame->error_state_indicator = (message[5] & MESSAGE_ESI) != 0;
375 }
376#endif
377 frame->can_data_length_code = message[4] & MESSAGE_DLC_MASK;
378 const size_t payload_length = std::min<size_t>(frame->payload_length(), canbus::CAN_MAX_DATA_LENGTH);
379 // A remote request carries no data, so zero its bytes rather than keep the previous frame's
380 if (frame->remote_transmission_request) {
381 std::memset(frame->data, 0, payload_length);
382 } else {
383 std::memcpy(frame->data, message.data() + MESSAGE_HEADER_SIZE, payload_length);
384 }
385 return canbus::ERROR_OK;
386}
387
390 if (!this->is_failed())
391 this->read_register_(REG_TREC, this->last_trec_);
392 status.rx_error_counter = this->last_trec_;
393 status.tx_error_counter = this->last_trec_ >> 8;
394 status.bus_off = ((this->last_trec_ >> 16) & ERROR_BUS_OFF) != 0;
395 status.rx_overrun_count = this->rx_overrun_count_;
396 status.bus_error_count = this->bus_error_count_;
397 return status;
398}
399
402 // A refused mode request sets no flag, so the mode is also checked on an interval.
403 const bool mode_check_due = now - this->last_mode_check_time_ >= MODE_RECOVERY_INTERVAL_MS;
404
405 std::array<uint8_t, 2> interrupt_flags;
406 const bool comm_fault_persisted = this->comm_fault_active_;
407 if (!this->read_registers_(REG_INT, interrupt_flags.data(), interrupt_flags.size())) {
408 // A chip reset turns the PLL off, which leaves a 4 MHz SYSCLK too slow for the SPI rate, so every read fails;
409 // a fault that outlived an earlier poll tells that apart from one burst of noise
410 if (this->mcp_clock_ == CanClock::CAN_CLOCK_4MHZ && mode_check_due && comm_fault_persisted) {
411 this->last_mode_check_time_ = now;
412 ESP_LOGW(TAG, "SPI reads failing, reconfiguring");
413 this->configure_();
414 }
415 return (this->last_error_flags_ & ERROR_BUS_OFF) != 0 ? canbus::CAN_EVENT_BUS_OFF : canbus::CanEventFlags{};
416 }
417 // Only the handled flags, so a stray latched one cannot force a TREC read on every poll
418 const uint8_t controller_flags =
419 interrupt_flags[1] & (INT_SYSTEM_ERROR | INT_CAN_ERROR | INT_INVALID_MESSAGE | INT_RX_OVERFLOW);
420 // Keep polling TREC through error recovery.
421 const bool trec_needed = controller_flags != 0 || this->last_error_flags_ != 0 || (this->last_trec_ >> 16) != 0;
422 const bool trec_read = trec_needed && this->read_register_(REG_TREC, this->last_trec_);
423 const uint8_t error_flags = trec_read ? this->last_trec_ >> 16 : this->last_error_flags_;
424 uint8_t events = 0;
425 const bool was_passive = (this->last_error_flags_ & (ERROR_RX_PASSIVE | ERROR_TX_PASSIVE)) != 0;
426 const bool is_passive = (error_flags & (ERROR_RX_PASSIVE | ERROR_TX_PASSIVE)) != 0;
427 const bool is_bus_off = (error_flags & ERROR_BUS_OFF) != 0;
428 if (was_passive != is_passive)
430 if (is_bus_off)
432 this->last_error_flags_ = error_flags;
433
434 if (controller_flags & INT_RX_OVERFLOW) {
435 uint8_t fifo_status;
436 if (this->read_registers_(REG_RX_FIFO_STA, &fifo_status, 1) && (fifo_status & FIFO_RX_OVERFLOW))
437 this->write_byte_(REG_RX_FIFO_STA, static_cast<uint8_t>(~FIFO_RX_OVERFLOW));
439 this->rx_overrun_count_++;
440 }
441 if (controller_flags & (INT_CAN_ERROR | INT_INVALID_MESSAGE))
442 this->bus_error_count_++;
443
444 uint8_t direct_flags = controller_flags & (INT_SYSTEM_ERROR | INT_CAN_ERROR | INT_INVALID_MESSAGE);
445 // Leave CERRIF pending when its error-state read failed, so the next poll retries it.
446 if (!trec_read)
447 direct_flags &= ~INT_CAN_ERROR;
448 uint8_t mode_flags = interrupt_flags[0] & INT_MODE_CHANGE;
449 const bool mode_check_needed = mode_check_due || mode_flags != 0 || (controller_flags & INT_SYSTEM_ERROR);
450 if (mode_check_needed &&
451 ((trec_needed && !trec_read) || !this->recover_mode_if_needed_(is_bus_off, mode_check_due))) {
452 mode_flags = 0;
453 direct_flags &= ~INT_SYSTEM_ERROR;
454 } else if (mode_check_due && !is_bus_off) {
455 // While bus-off the check repeats each poll, so a retry follows as soon as the bus recovers.
456 this->last_mode_check_time_ = now;
457 }
458 if (mode_flags != 0 || direct_flags != 0) {
459 // Both bytes have write-zero-to-clear flags; ones preserve unrelated and newly latched flags.
460 const std::array<uint8_t, 2> clear_flags{static_cast<uint8_t>(~mode_flags), static_cast<uint8_t>(~direct_flags)};
461 this->write_registers_(REG_INT, clear_flags.data(), clear_flags.size());
462 }
463 if ((controller_flags & INT_SYSTEM_ERROR) && this->should_log_fault_(now)) {
464 ESP_LOGW(TAG, "Controller system error, RX errors=%u, TX errors=%u", static_cast<uint8_t>(this->last_trec_),
465 static_cast<uint8_t>(this->last_trec_ >> 8));
466 }
467
468 return static_cast<canbus::CanEventFlags>(events);
469}
470
471} // namespace esphome::mcp251xfd
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
bool is_failed() const
Definition component.h:287
void status_clear_warning()
Definition component.h:317
void dump_config() override
bool recover_mode_if_needed_(bool bus_off, bool retry_due)
bool set_comm_fault_(const LogString *reason)
uint32_t rx_fifo_depth_() const
Definition mcp251xfd.h:76
InternalGPIOPin * interrupt_pin_
Definition mcp251xfd.h:28
bool validate_fifo_address_(uint32_t offset, uint32_t start, uint32_t depth, uint16_t &address)
canbus::CanStatus get_status() override
canbus::CanEventFlags get_events() override
void write_registers_(uint16_t address, const uint8_t *data, size_t length)
void write_transfer_(uint16_t address, uint8_t *transfer, size_t length)
uint32_t transmitter_delay_compensation_
Definition mcp251xfd.h:31
void write_byte_(uint16_t address, uint8_t value)
bool read_registers_(uint16_t address, uint8_t *data, size_t length)
void write_register_(uint16_t address, uint32_t value)
canbus::Error read_message(canbus::CanFrame *frame) override
bool should_log_fault_(uint32_t now)
canbus::Error send_message(canbus::CanFrame *frame) override
uint32_t message_size_() const
Definition mcp251xfd.h:75
bool wait_for_register_(uint16_t address, uint8_t mask, uint8_t expected, uint16_t timeout_ms=100)
bool set_mode_(CanMode mode, uint16_t timeout_ms=100)
bool read_register_(uint16_t address, uint32_t &value)
bool setup_internal() override
Definition mcp251xfd.cpp:22
uint32_t data_rate_
Definition spi.h:424
const LogString * message
Definition component.cpp:35
uint16_t flags
std::vector< uint8_t > & out
Definition gif.cpp:49
constexpr uint8_t can_dlc_to_len(uint8_t dlc)
Payload length of a CAN FD DLC, or CAN_INVALID_DLC above 15.
Definition canbus.h:95
@ CAN_EVENT_RX_QUEUE_FULL
Definition canbus.h:177
ESPHOME_LOG_TAG(TAG, "mcp251xfd")
PROGMEM_STRING_TABLE(CanModeStrings, "NORMAL", "SLEEP", "LOOPBACK", "LISTENONLY", "CONFIGURATION", "EXTERNAL LOOPBACK", "CLASSIC", "UNKNOWN")
uint32_t value
Definition hal.cpp:59
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.
Definition helpers.h:1306
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1240
uint16_t crc16be(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t poly, bool refin, bool refout)
Definition helpers.cpp:154
constexpr uint8_t * put_be16(uint8_t *dst, uint16_t value)
Write the low 16, 24 or 32 bits of value to dst, most significant byte first (be) or least significan...
Definition helpers.h:1268
constexpr uint8_t * put_le32(uint8_t *dst, uint32_t value)
Definition helpers.h:1291
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1199
void HOT delay(uint32_t ms)
Definition hal.cpp:85
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
One CAN frame as passed between the bus and its driver.
Definition canbus.h:143
uint8_t payload_length() const
Payload bytes for the DLC: at most 8 for a classic frame, 0 for an invalid CAN FD DLC.
Definition canbus.h:156
uint32_t payload_size()
uint16_t length
Definition tt21100.cpp:0