7 static const char *
const TAG =
"mcp2515";
16 bool MCP2515::setup_internal() {
21 if (this->set_bitrate_(this->bit_rate_, this->mcp_clock_) !=
canbus::ERROR_OK)
25 uint8_t err_flags = this->get_error_flags_();
26 ESP_LOGD(TAG,
"mcp2515 setup done, error_flags = %02X", err_flags);
34 ESP_LOGV(TAG,
"reset_()");
37 ESP_LOGV(TAG,
"reset() CLEAR ALL TXB registers");
40 memset(zeros, 0,
sizeof(zeros));
44 ESP_LOGV(TAG,
"reset() CLEARED TXB registers");
51 modify_register_(
MCP_RXB0CTRL, RXB_CTRL_RXM_MASK | RXB_0_CTRL_BUKT, RXB_CTRL_RXM_STDEXT | RXB_0_CTRL_BUKT);
52 modify_register_(
MCP_RXB1CTRL, RXB_CTRL_RXM_MASK, RXB_CTRL_RXM_STDEXT);
57 uint8_t MCP2515::read_register_(
const REGISTER reg) {
60 this->transfer_byte(reg);
61 uint8_t ret = this->transfer_byte(0x00);
67 void MCP2515::read_registers_(
const REGISTER reg, uint8_t values[],
const uint8_t n) {
70 this->transfer_byte(reg);
73 for (uint8_t i = 0; i < n; i++) {
74 values[i] = this->transfer_byte(0x00);
79 void MCP2515::set_register_(
const REGISTER reg,
const uint8_t value) {
82 this->transfer_byte(reg);
83 this->transfer_byte(value);
87 void MCP2515::set_registers_(
const REGISTER reg, uint8_t values[],
const uint8_t n) {
90 this->transfer_byte(reg);
92 for (uint8_t i = 0; i < n; i++) {
93 this->transfer_byte(values[i]);
98 void MCP2515::modify_register_(
const REGISTER reg,
const uint8_t mask,
const uint8_t data) {
101 this->transfer_byte(reg);
102 this->transfer_byte(mask);
103 this->transfer_byte(data);
107 uint8_t MCP2515::get_status_() {
110 uint8_t i = this->transfer_byte(0x00);
117 modify_register_(
MCP_CANCTRL, CANCTRL_REQOP, mode);
119 uint32_t end_time =
millis() + 10;
120 while (
millis() < end_time) {
121 if ((read_register_(
MCP_CANSTAT) & CANSTAT_OPMOD) == mode)
124 ESP_LOGE(TAG,
"Failed to set mode");
131 modify_register_(
MCP_CANCTRL, CANCTRL_CLKEN, 0x00);
134 modify_register_(
MCP_CNF3, CNF3_SOF, CNF3_SOF);
139 modify_register_(
MCP_CANCTRL, CANCTRL_CLKPRE, divisor);
142 modify_register_(
MCP_CANCTRL, CANCTRL_CLKEN, CANCTRL_CLKEN);
145 modify_register_(
MCP_CNF3, CNF3_SOF, 0x00);
149 void MCP2515::prepare_id_(uint8_t *buffer,
const bool extended,
const uint32_t
id) {
150 uint16_t canid = (uint16_t) (
id & 0x0FFFF);
153 buffer[MCP_EID0] = (uint8_t) (canid & 0xFF);
154 buffer[MCP_EID8] = (uint8_t) (canid >> 8);
155 canid = (uint16_t) (
id >> 16);
156 buffer[MCP_SIDL] = (uint8_t) (canid & 0x03);
157 buffer[MCP_SIDL] += (uint8_t) ((canid & 0x1C) << 3);
158 buffer[MCP_SIDL] |= TXB_EXIDE_MASK;
159 buffer[MCP_SIDH] = (uint8_t) (canid >> 5);
161 buffer[MCP_SIDH] = (uint8_t) (canid >> 3);
162 buffer[MCP_SIDL] = (uint8_t) ((canid & 0x07) << 5);
163 buffer[MCP_EID0] = 0;
164 buffer[MCP_EID8] = 0;
168 canbus::Error MCP2515::set_filter_mask_(
const MASK mask,
const bool extended,
const uint32_t ul_data) {
175 prepare_id_(tbufdata, extended, ul_data);
189 set_registers_(reg, tbufdata, 4);
194 canbus::Error MCP2515::set_filter_(
const RXF num,
const bool extended,
const uint32_t ul_data) {
226 prepare_id_(tbufdata, extended, ul_data);
227 set_registers_(reg, tbufdata, 4);
233 const struct TxBnRegs *txbuf = &TXB[txbn];
253 for (
auto &tx_buffer : tx_buffers) {
254 const struct TxBnRegs *txbuf = &TXB[tx_buffer];
255 uint8_t ctrlval = read_register_(txbuf->CTRL);
257 return send_message_(tx_buffer, frame);
265 const struct RxBnRegs *rxb = &RXB[rxbn];
269 read_registers_(rxb->SIDH, tbufdata, 5);
271 uint32_t
id = (tbufdata[MCP_SIDH] << 3) + (tbufdata[MCP_SIDL] >> 5);
272 bool use_extended_id =
false;
273 bool remote_transmission_request =
false;
275 if ((tbufdata[MCP_SIDL] & TXB_EXIDE_MASK) == TXB_EXIDE_MASK) {
276 id = (
id << 2) + (tbufdata[MCP_SIDL] & 0x03);
277 id = (
id << 8) + tbufdata[MCP_EID8];
278 id = (
id << 8) + tbufdata[MCP_EID0];
280 use_extended_id =
true;
283 uint8_t dlc = (tbufdata[MCP_DLC] & DLC_MASK);
284 if (dlc > canbus::CAN_MAX_DATA_LENGTH) {
288 uint8_t ctrl = read_register_(rxb->CTRL);
289 if (ctrl & RXB_CTRL_RTR) {
291 remote_transmission_request =
true;
299 read_registers_(rxb->DATA, frame->data, dlc);
301 modify_register_(
MCP_CANINTF, rxb->CANINTF_RXnIF, 0);
308 uint8_t stat = get_status_();
311 rc = read_message_(
RXB0, frame);
313 rc = read_message_(
RXB1, frame);
321 bool MCP2515::check_receive_() {
322 uint8_t res = get_status_();
323 return (res & STAT_RXIF_MASK) != 0;
326 bool MCP2515::check_error_() {
327 uint8_t eflg = get_error_flags_();
328 return (eflg & EFLG_ERRORMASK) != 0;
331 uint8_t MCP2515::get_error_flags_() {
return read_register_(
MCP_EFLG); }
335 uint8_t MCP2515::get_int_() {
return read_register_(
MCP_CANINTF); }
337 void MCP2515::clear_int_() { set_register_(
MCP_CANINTF, 0); }
339 uint8_t MCP2515::get_int_mask_() {
return read_register_(
MCP_CANINTE); }
343 void MCP2515::clear_rx_n_ovr_() {
344 uint8_t eflg = get_error_flags_();
346 clear_rx_n_ovr_flags_();
352 void MCP2515::clear_merr_() {
358 void MCP2515::clear_errif_() {
372 uint8_t
set, cfg1, cfg2, cfg3;
378 cfg1 = MCP_8MHZ_5KBPS_CFG1;
379 cfg2 = MCP_8MHZ_5KBPS_CFG2;
380 cfg3 = MCP_8MHZ_5KBPS_CFG3;
383 cfg1 = MCP_8MHZ_10KBPS_CFG1;
384 cfg2 = MCP_8MHZ_10KBPS_CFG2;
385 cfg3 = MCP_8MHZ_10KBPS_CFG3;
388 cfg1 = MCP_8MHZ_20KBPS_CFG1;
389 cfg2 = MCP_8MHZ_20KBPS_CFG2;
390 cfg3 = MCP_8MHZ_20KBPS_CFG3;
393 cfg1 = MCP_8MHZ_31K25BPS_CFG1;
394 cfg2 = MCP_8MHZ_31K25BPS_CFG2;
395 cfg3 = MCP_8MHZ_31K25BPS_CFG3;
398 cfg1 = MCP_8MHZ_33K3BPS_CFG1;
399 cfg2 = MCP_8MHZ_33K3BPS_CFG2;
400 cfg3 = MCP_8MHZ_33K3BPS_CFG3;
403 cfg1 = MCP_8MHZ_40KBPS_CFG1;
404 cfg2 = MCP_8MHZ_40KBPS_CFG2;
405 cfg3 = MCP_8MHZ_40KBPS_CFG3;
408 cfg1 = MCP_8MHZ_50KBPS_CFG1;
409 cfg2 = MCP_8MHZ_50KBPS_CFG2;
410 cfg3 = MCP_8MHZ_50KBPS_CFG3;
413 cfg1 = MCP_8MHZ_80KBPS_CFG1;
414 cfg2 = MCP_8MHZ_80KBPS_CFG2;
415 cfg3 = MCP_8MHZ_80KBPS_CFG3;
418 cfg1 = MCP_8MHZ_100KBPS_CFG1;
419 cfg2 = MCP_8MHZ_100KBPS_CFG2;
420 cfg3 = MCP_8MHZ_100KBPS_CFG3;
423 cfg1 = MCP_8MHZ_125KBPS_CFG1;
424 cfg2 = MCP_8MHZ_125KBPS_CFG2;
425 cfg3 = MCP_8MHZ_125KBPS_CFG3;
428 cfg1 = MCP_8MHZ_200KBPS_CFG1;
429 cfg2 = MCP_8MHZ_200KBPS_CFG2;
430 cfg3 = MCP_8MHZ_200KBPS_CFG3;
433 cfg1 = MCP_8MHZ_250KBPS_CFG1;
434 cfg2 = MCP_8MHZ_250KBPS_CFG2;
435 cfg3 = MCP_8MHZ_250KBPS_CFG3;
438 cfg1 = MCP_8MHZ_500KBPS_CFG1;
439 cfg2 = MCP_8MHZ_500KBPS_CFG2;
440 cfg3 = MCP_8MHZ_500KBPS_CFG3;
443 cfg1 = MCP_8MHZ_1000KBPS_CFG1;
444 cfg2 = MCP_8MHZ_1000KBPS_CFG2;
445 cfg3 = MCP_8MHZ_1000KBPS_CFG3;
456 cfg1 = MCP_12MHZ_5KBPS_CFG1;
457 cfg2 = MCP_12MHZ_5KBPS_CFG2;
458 cfg3 = MCP_12MHZ_5KBPS_CFG3;
461 cfg1 = MCP_12MHZ_10KBPS_CFG1;
462 cfg2 = MCP_12MHZ_10KBPS_CFG2;
463 cfg3 = MCP_12MHZ_10KBPS_CFG3;
466 cfg1 = MCP_12MHZ_20KBPS_CFG1;
467 cfg2 = MCP_12MHZ_20KBPS_CFG2;
468 cfg3 = MCP_12MHZ_20KBPS_CFG3;
471 cfg1 = MCP_12MHZ_33K3BPS_CFG1;
472 cfg2 = MCP_12MHZ_33K3BPS_CFG2;
473 cfg3 = MCP_12MHZ_33K3BPS_CFG3;
476 cfg1 = MCP_12MHZ_40KBPS_CFG1;
477 cfg2 = MCP_12MHZ_40KBPS_CFG2;
478 cfg3 = MCP_12MHZ_40KBPS_CFG3;
481 cfg2 = MCP_12MHZ_50KBPS_CFG2;
482 cfg3 = MCP_12MHZ_50KBPS_CFG3;
485 cfg1 = MCP_12MHZ_80KBPS_CFG1;
486 cfg2 = MCP_12MHZ_80KBPS_CFG2;
487 cfg3 = MCP_12MHZ_80KBPS_CFG3;
490 cfg1 = MCP_12MHZ_100KBPS_CFG1;
491 cfg2 = MCP_12MHZ_100KBPS_CFG2;
492 cfg3 = MCP_12MHZ_100KBPS_CFG3;
495 cfg1 = MCP_12MHZ_125KBPS_CFG1;
496 cfg2 = MCP_12MHZ_125KBPS_CFG2;
497 cfg3 = MCP_12MHZ_125KBPS_CFG3;
500 cfg1 = MCP_12MHZ_200KBPS_CFG1;
501 cfg2 = MCP_12MHZ_200KBPS_CFG2;
502 cfg3 = MCP_12MHZ_200KBPS_CFG3;
505 cfg1 = MCP_12MHZ_250KBPS_CFG1;
506 cfg2 = MCP_12MHZ_250KBPS_CFG2;
507 cfg3 = MCP_12MHZ_250KBPS_CFG3;
510 cfg1 = MCP_12MHZ_500KBPS_CFG1;
511 cfg2 = MCP_12MHZ_500KBPS_CFG2;
512 cfg3 = MCP_12MHZ_500KBPS_CFG3;
515 cfg1 = MCP_12MHZ_1000KBPS_CFG1;
516 cfg2 = MCP_12MHZ_1000KBPS_CFG2;
517 cfg3 = MCP_12MHZ_1000KBPS_CFG3;
528 cfg1 = MCP_16MHZ_5KBPS_CFG1;
529 cfg2 = MCP_16MHZ_5KBPS_CFG2;
530 cfg3 = MCP_16MHZ_5KBPS_CFG3;
533 cfg1 = MCP_16MHZ_10KBPS_CFG1;
534 cfg2 = MCP_16MHZ_10KBPS_CFG2;
535 cfg3 = MCP_16MHZ_10KBPS_CFG3;
538 cfg1 = MCP_16MHZ_20KBPS_CFG1;
539 cfg2 = MCP_16MHZ_20KBPS_CFG2;
540 cfg3 = MCP_16MHZ_20KBPS_CFG3;
543 cfg1 = MCP_16MHZ_33K3BPS_CFG1;
544 cfg2 = MCP_16MHZ_33K3BPS_CFG2;
545 cfg3 = MCP_16MHZ_33K3BPS_CFG3;
548 cfg1 = MCP_16MHZ_40KBPS_CFG1;
549 cfg2 = MCP_16MHZ_40KBPS_CFG2;
550 cfg3 = MCP_16MHZ_40KBPS_CFG3;
553 cfg2 = MCP_16MHZ_50KBPS_CFG2;
554 cfg3 = MCP_16MHZ_50KBPS_CFG3;
557 cfg1 = MCP_16MHZ_80KBPS_CFG1;
558 cfg2 = MCP_16MHZ_80KBPS_CFG2;
559 cfg3 = MCP_16MHZ_80KBPS_CFG3;
562 cfg1 = MCP_16MHZ_83K3BPS_CFG1;
563 cfg2 = MCP_16MHZ_83K3BPS_CFG2;
564 cfg3 = MCP_16MHZ_83K3BPS_CFG3;
567 cfg1 = MCP_16MHZ_100KBPS_CFG1;
568 cfg2 = MCP_16MHZ_100KBPS_CFG2;
569 cfg3 = MCP_16MHZ_100KBPS_CFG3;
572 cfg1 = MCP_16MHZ_125KBPS_CFG1;
573 cfg2 = MCP_16MHZ_125KBPS_CFG2;
574 cfg3 = MCP_16MHZ_125KBPS_CFG3;
577 cfg1 = MCP_16MHZ_200KBPS_CFG1;
578 cfg2 = MCP_16MHZ_200KBPS_CFG2;
579 cfg3 = MCP_16MHZ_200KBPS_CFG3;
582 cfg1 = MCP_16MHZ_250KBPS_CFG1;
583 cfg2 = MCP_16MHZ_250KBPS_CFG2;
584 cfg3 = MCP_16MHZ_250KBPS_CFG3;
587 cfg1 = MCP_16MHZ_500KBPS_CFG1;
588 cfg2 = MCP_16MHZ_500KBPS_CFG2;
589 cfg3 = MCP_16MHZ_500KBPS_CFG3;
592 cfg1 = MCP_16MHZ_1000KBPS_CFG1;
593 cfg2 = MCP_16MHZ_1000KBPS_CFG2;
594 cfg3 = MCP_16MHZ_1000KBPS_CFG3;
605 cfg1 = MCP_20MHZ_33K3BPS_CFG1;
606 cfg2 = MCP_20MHZ_33K3BPS_CFG2;
607 cfg3 = MCP_20MHZ_33K3BPS_CFG3;
610 cfg1 = MCP_20MHZ_40KBPS_CFG1;
611 cfg2 = MCP_20MHZ_40KBPS_CFG2;
612 cfg3 = MCP_20MHZ_40KBPS_CFG3;
615 cfg1 = MCP_20MHZ_50KBPS_CFG1;
616 cfg2 = MCP_20MHZ_50KBPS_CFG2;
617 cfg3 = MCP_20MHZ_50KBPS_CFG3;
620 cfg1 = MCP_20MHZ_80KBPS_CFG1;
621 cfg2 = MCP_20MHZ_80KBPS_CFG2;
622 cfg3 = MCP_20MHZ_80KBPS_CFG3;
625 cfg1 = MCP_20MHZ_83K3BPS_CFG1;
626 cfg2 = MCP_20MHZ_83K3BPS_CFG2;
627 cfg3 = MCP_20MHZ_83K3BPS_CFG3;
630 cfg1 = MCP_20MHZ_100KBPS_CFG1;
631 cfg2 = MCP_20MHZ_100KBPS_CFG2;
632 cfg3 = MCP_20MHZ_100KBPS_CFG3;
635 cfg1 = MCP_20MHZ_125KBPS_CFG1;
636 cfg2 = MCP_20MHZ_125KBPS_CFG2;
637 cfg3 = MCP_20MHZ_125KBPS_CFG3;
640 cfg1 = MCP_20MHZ_200KBPS_CFG1;
641 cfg2 = MCP_20MHZ_200KBPS_CFG2;
642 cfg3 = MCP_20MHZ_200KBPS_CFG3;
645 cfg1 = MCP_20MHZ_250KBPS_CFG1;
646 cfg2 = MCP_20MHZ_250KBPS_CFG2;
647 cfg3 = MCP_20MHZ_250KBPS_CFG3;
650 cfg1 = MCP_20MHZ_500KBPS_CFG1;
651 cfg2 = MCP_20MHZ_500KBPS_CFG2;
652 cfg3 = MCP_20MHZ_500KBPS_CFG3;
655 cfg1 = MCP_20MHZ_1000KBPS_CFG1;
656 cfg2 = MCP_20MHZ_1000KBPS_CFG2;
657 cfg3 = MCP_20MHZ_1000KBPS_CFG3;
676 ESP_LOGE(TAG,
"Invalid frequency/bitrate combination: %d/%d", can_clock, can_speed);
uint8_t can_data_length_code
T id(T value)
Helper function to make id(var) known from lambdas work in custom components.
uint32_t IRAM_ATTR HOT millis()
bool remote_transmission_request
BedjetMode mode
BedJet operating mode.
Implementation of SPI Controller mode.
void IRAM_ATTR HOT delay(uint32_t ms)