ESPHome 2025.5.0
Loading...
Searching...
No Matches
mcp2515.h
Go to the documentation of this file.
1#pragma once
2
6#include "mcp2515_defs.h"
7
8namespace esphome {
9namespace mcp2515 {
10static const uint32_t SPI_CLOCK = 10000000; // 10MHz
11
12static const int N_TXBUFFERS = 3;
13static const int N_RXBUFFERS = 2;
15enum MASK { MASK0, MASK1 };
16enum RXF { RXF0 = 0, RXF1 = 1, RXF2 = 2, RXF3 = 3, RXF4 = 4, RXF5 = 5 };
17enum RXBn { RXB0 = 0, RXB1 = 1 };
18enum TXBn { TXB0 = 0, TXB1 = 1, TXB2 = 2 };
19
27
28enum CANINTF : uint8_t {
36 CANINTF_MERRF = 0x80
37};
38
39enum EFLG : uint8_t {
40 EFLG_RX1OVR = (1 << 7),
41 EFLG_RX0OVR = (1 << 6),
42 EFLG_TXBO = (1 << 5),
43 EFLG_TXEP = (1 << 4),
44 EFLG_RXEP = (1 << 3),
45 EFLG_TXWAR = (1 << 2),
46 EFLG_RXWAR = (1 << 1),
47 EFLG_EWARN = (1 << 0)
48};
49
50enum STAT : uint8_t { STAT_RX0IF = (1 << 0), STAT_RX1IF = (1 << 1) };
51
52static const uint8_t STAT_RXIF_MASK = STAT_RX0IF | STAT_RX1IF;
53static const uint8_t EFLG_ERRORMASK = EFLG_RX1OVR | EFLG_RX0OVR | EFLG_TXBO | EFLG_TXEP | EFLG_RXEP;
54
55class MCP2515 : public canbus::Canbus,
56 public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
57 spi::DATA_RATE_8MHZ> {
58 public:
60 void set_mcp_clock(CanClock clock) { this->mcp_clock_ = clock; };
62 static const struct TxBnRegs {
63 REGISTER CTRL;
64 REGISTER SIDH;
65 REGISTER DATA;
66 } TXB[N_TXBUFFERS];
67
68 static const struct RxBnRegs {
69 REGISTER CTRL;
70 REGISTER SIDH;
71 REGISTER DATA;
72 CANINTF CANINTF_RXnIF;
73 } RXB[N_RXBUFFERS];
74
75 protected:
78 bool setup_internal() override;
80
81 uint8_t read_register_(REGISTER reg);
82 void read_registers_(REGISTER reg, uint8_t values[], uint8_t n);
83 void set_register_(REGISTER reg, uint8_t value);
84 void set_registers_(REGISTER reg, uint8_t values[], uint8_t n);
85 void modify_register_(REGISTER reg, uint8_t mask, uint8_t data);
86
87 void prepare_id_(uint8_t *buffer, bool extended, uint32_t id);
92 canbus::Error set_filter_mask_(MASK mask, bool extended, uint32_t ul_data);
93 canbus::Error set_filter_(RXF num, bool extended, uint32_t ul_data);
95 canbus::Error send_message(struct canbus::CanFrame *frame) override;
97 canbus::Error read_message(struct canbus::CanFrame *frame) override;
98 bool check_receive_();
99 bool check_error_();
100 uint8_t get_error_flags_();
102 uint8_t get_int_();
103 uint8_t get_int_mask_();
104 void clear_int_();
105 void clear_tx_int_();
106 uint8_t get_status_();
107 void clear_rx_n_ovr_();
108 void clear_merr_();
109 void clear_errif_();
110};
111} // namespace mcp2515
112} // namespace esphome
send_message_t send_message_
BedjetMode mode
BedJet operating mode.
canbus::Error reset_()
Definition mcp2515.cpp:30
static const struct esphome::mcp2515::MCP2515::RxBnRegs RXB[N_RXBUFFERS]
Definition mcp2515.cpp:13
void modify_register_(REGISTER reg, uint8_t mask, uint8_t data)
Definition mcp2515.cpp:98
canbus::Error set_filter_mask_(MASK mask, bool extended, uint32_t ul_data)
Definition mcp2515.cpp:168
void prepare_id_(uint8_t *buffer, bool extended, uint32_t id)
Definition mcp2515.cpp:149
void read_registers_(REGISTER reg, uint8_t values[], uint8_t n)
Definition mcp2515.cpp:67
void set_mcp_mode(const CanctrlReqopMode mode)
Definition mcp2515.h:61
uint8_t read_register_(REGISTER reg)
Definition mcp2515.cpp:57
void set_mcp_clock(CanClock clock)
Definition mcp2515.h:60
void set_registers_(REGISTER reg, uint8_t values[], uint8_t n)
Definition mcp2515.cpp:87
CanctrlReqopMode mcp_mode_
Definition mcp2515.h:77
static const struct esphome::mcp2515::MCP2515::TxBnRegs TXB[N_TXBUFFERS]
Definition mcp2515.cpp:9
canbus::Error send_message(struct canbus::CanFrame *frame) override
Definition mcp2515.cpp:247
bool setup_internal() override
Definition mcp2515.cpp:16
void set_register_(REGISTER reg, uint8_t value)
Definition mcp2515.cpp:79
canbus::Error set_filter_(RXF num, bool extended, uint32_t ul_data)
Definition mcp2515.cpp:194
canbus::Error set_mode_(CanctrlReqopMode mode)
Definition mcp2515.cpp:116
canbus::Error read_message(struct canbus::CanFrame *frame) override
Definition mcp2515.cpp:306
canbus::Error set_clk_out_(CanClkOut divisor)
Definition mcp2515.cpp:128
canbus::Error read_message_(RXBn rxbn, struct canbus::CanFrame *frame)
Definition mcp2515.cpp:264
canbus::Error set_bitrate_(canbus::CanSpeed can_speed)
Definition mcp2515.cpp:364
The SPIDevice is what components using the SPI will create.
Definition spi.h:421
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7