AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
mcp2515_can_interface.hpp
Go to the documentation of this file.
1//================================================================================================
8//================================================================================================
9#ifndef MCP2515_CAN_INTERFACE_HPP
10#define MCP2515_CAN_INTERFACE_HPP
11
12#include <cstring>
13#include <string>
14#include <vector>
15
21
22namespace isobus
23{
24 //================================================================================================
28 //================================================================================================
30 {
31 public:
37 MCP2515CANInterface(SPIHardwarePlugin *transactionHandler, const std::uint8_t cfg1, const std::uint8_t cfg2, const std::uint8_t cfg3);
38
40 virtual ~MCP2515CANInterface();
41
44 bool get_is_valid() const override;
45
47 void close() override;
48
50 void open() override;
51
54 bool reset();
55
59 bool read_frame(isobus::CANMessageFrame &canFrame) override;
60
64 bool write_frame(const isobus::CANMessageFrame &canFrame) override;
65
66 private:
68 enum class MCPInstruction : std::uint8_t
69 {
70 WRITE = 0x02,
71 READ = 0x03,
72 BITMOD = 0x05,
73 RX_STATUS = 0xB0,
74 READ_STATUS = 0xA0,
75 RESET = 0xC0
76 };
77
79 enum class MCPRegister : std::uint8_t
80 {
81 CANSTAT = 0x0E,
82 CANCTRL = 0x0F,
83 CNF3 = 0x28,
84 CNF2 = 0x29,
85 CNF1 = 0x2A,
86 CANINTE = 0x2B,
87 CANINTF = 0x2C,
88 TXB0CTRL = 0x30,
89 TXB0SIDH = 0x31,
90 TXB1CTRL = 0x40,
91 TXB1SIDH = 0x41,
92 TXB2CTRL = 0x50,
93 TXB2SIDH = 0x51,
94 RXB0CTRL = 0x60,
95 RXB0DATA = 0x66,
96 RXB1CTRL = 0x70,
97 RXB1DATA = 0x76
98 };
99
101 enum class MCPMode : std::uint8_t
102 {
103 NORMAL = 0x00,
104 SLEEP = 0x20,
105 LOOPBACK = 0x40,
106 LISTEN_ONLY = 0x60,
107 CONFIG = 0x80,
108 };
109
110 static constexpr std::uint32_t RECEIVE_MESSAGE_READ_RATE = 10;
111
115 bool get_read_status(std::uint8_t &status);
116
121 bool read_register(const MCPRegister address, std::uint8_t &data);
122
128 bool read_register(const MCPRegister address, std::uint8_t *data, const std::size_t length);
129
135 bool modify_register(const MCPRegister address, const std::uint8_t mask, const std::uint8_t data);
136
141 bool write_register(const MCPRegister address, const std::uint8_t data);
142
148 bool write_register(const MCPRegister address, const std::uint8_t data[], const std::size_t length);
149
152 bool write_reset();
153
157 bool set_mode(const MCPMode mode);
158
165 bool read_frame(isobus::CANMessageFrame &canFrame, const MCPRegister ctrlRegister, const MCPRegister dataRegister, const std::uint8_t intfMask);
166
172 bool write_frame(const isobus::CANMessageFrame &canFrame, const MCPRegister ctrlRegister, const MCPRegister sidhRegister);
173
175 std::uint8_t rxIndex = 0;
176 std::uint8_t txIndex = 2;
177 std::uint8_t txPriority = 3;
178 const std::uint8_t cfg1;
179 const std::uint8_t cfg2;
180 const std::uint8_t cfg3;
181 bool initialized = false;
182 };
183}
184#endif // MCP2515_CAN_INTERFACE_HPP
General constants used throughout this library.
An abstraction between this CAN stack and any hardware layer.
A base class for a CAN driver. Can be derived into your platform's required interface.
A classical CAN frame, with 8 data bytes.
An abstract base class for a CAN driver.
A CAN frame for interfacing with a hardware layer, like socket CAN or other interface.
A CAN Driver for the MCP2515 CAN controller.
std::uint8_t rxIndex
The index of the rx buffer to read from next.
static constexpr std::uint32_t RECEIVE_MESSAGE_READ_RATE
Hardcoded time in ms between polling the MCP2515 module for new messages, mostly arbitrary.
bool reset()
Resets the MCP2515.
bool write_frame(const isobus::CANMessageFrame &canFrame) override
Writes a frame to the bus (synchronous)
bool write_register(const MCPRegister address, const std::uint8_t data)
write a single byte register of the mcp2515
MCPMode
The different modes of the MCP2515 associated with their internal bits.
MCPRegister
Some essential registers of the MCP2515.
const std::uint8_t cfg2
Configuration value for CFG2 register.
void close() override
Closes the socket.
bool write_reset()
Reset the mcp2515 internally.
const std::uint8_t cfg1
Configuration value for CFG1 register.
std::uint8_t txIndex
The index of the tx buffer to write to next, start with 2 as it is the buffer with the highest priori...
std::uint8_t txPriority
The priority of the next tx frame.
bool get_is_valid() const override
Returns if the socket connection is valid.
const std::uint8_t cfg3
Configuration value for CFG3 register.
bool read_register(const MCPRegister address, std::uint8_t &data)
read a single byte register of the mcp2515
bool set_mode(const MCPMode mode)
set the mode of the mcp2515
bool modify_register(const MCPRegister address, const std::uint8_t mask, const std::uint8_t data)
modify a register of the mcp2515
virtual ~MCP2515CANInterface()
The destructor for SocketCANInterface.
bool read_frame(isobus::CANMessageFrame &canFrame) override
Returns a frame from the hardware (synchronous), or false if no frame can be read.
SPIHardwarePlugin * transactionHandler
The SPI transaction handler.
bool get_read_status(std::uint8_t &status)
Read the rx status of the mcp2515.
MCP2515CANInterface(SPIHardwarePlugin *transactionHandler, const std::uint8_t cfg1, const std::uint8_t cfg2, const std::uint8_t cfg3)
Constructor for the socket CAN driver.
void open() override
Connects to the socket.
MCPInstruction
Some essential instructions of the MCP2515.
bool initialized
If the mcp2515 has been initialized and no errors have occurred.
An abstract base class for SPI communication.
This namespace encompasses all of the ISO11783 stack's functionality to reduce global namespace pollu...
A base class for SPI communication between hardware devices. Can be derived into your platform's requ...