ESPHome  2024.11.1
mcp2515.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include "mcp2515_defs.h"
7 
8 namespace esphome {
9 namespace mcp2515 {
10 static const uint32_t SPI_CLOCK = 10000000; // 10MHz
11 
12 static const int N_TXBUFFERS = 3;
13 static const int N_RXBUFFERS = 2;
15 enum MASK { MASK0, MASK1 };
16 enum RXF { RXF0 = 0, RXF1 = 1, RXF2 = 2, RXF3 = 3, RXF4 = 4, RXF5 = 5 };
17 enum RXBn { RXB0 = 0, RXB1 = 1 };
18 enum TXBn { TXB0 = 0, TXB1 = 1, TXB2 = 2 };
19 
20 enum CanClkOut {
22  CLKOUT_DIV1 = 0x0,
23  CLKOUT_DIV2 = 0x1,
24  CLKOUT_DIV4 = 0x2,
25  CLKOUT_DIV8 = 0x3,
26 };
27 
28 enum CANINTF : uint8_t {
29  CANINTF_RX0IF = 0x01,
30  CANINTF_RX1IF = 0x02,
31  CANINTF_TX0IF = 0x04,
32  CANINTF_TX1IF = 0x08,
33  CANINTF_TX2IF = 0x10,
34  CANINTF_ERRIF = 0x20,
35  CANINTF_WAKIF = 0x40,
37 };
38 
39 enum 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 
50 enum STAT : uint8_t { STAT_RX0IF = (1 << 0), STAT_RX1IF = (1 << 1) };
51 
52 static const uint8_t STAT_RXIF_MASK = STAT_RX0IF | STAT_RX1IF;
53 static const uint8_t EFLG_ERRORMASK = EFLG_RX1OVR | EFLG_RX0OVR | EFLG_TXBO | EFLG_TXEP | EFLG_RXEP;
54 
55 class 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:
59  MCP2515(){};
60  void set_mcp_clock(CanClock clock) { this->mcp_clock_ = clock; };
61  void set_mcp_mode(const CanctrlReqopMode mode) { this->mcp_mode_ = mode; }
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:
76  CanClock mcp_clock_{MCP_8MHZ};
78  bool setup_internal() override;
79  canbus::Error set_mode_(CanctrlReqopMode mode);
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);
88  canbus::Error reset_();
89  canbus::Error set_clk_out_(CanClkOut divisor);
90  canbus::Error set_bitrate_(canbus::CanSpeed can_speed);
91  canbus::Error set_bitrate_(canbus::CanSpeed can_speed, CanClock can_clock);
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);
94  canbus::Error send_message_(TXBn txbn, struct canbus::CanFrame *frame);
95  canbus::Error send_message(struct canbus::CanFrame *frame) override;
96  canbus::Error read_message_(RXBn rxbn, struct canbus::CanFrame *frame);
97  canbus::Error read_message(struct canbus::CanFrame *frame) override;
98  bool check_receive_();
99  bool check_error_();
100  uint8_t get_error_flags_();
101  void clear_rx_n_ovr_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
The SPIDevice is what components using the SPI will create.
Definition: spi.h:391
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:183
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7