AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
isobus::MCP2515CANInterface Class Reference

A CAN Driver for the MCP2515 CAN controller. More...

#include <mcp2515_can_interface.hpp>

Inheritance diagram for isobus::MCP2515CANInterface:
[legend]
Collaboration diagram for isobus::MCP2515CANInterface:
[legend]

Public Member Functions

 MCP2515CANInterface (SPIHardwarePlugin *transactionHandler, const std::uint8_t cfg1, const std::uint8_t cfg2, const std::uint8_t cfg3)
 Constructor for the socket CAN driver.
 
virtual ~MCP2515CANInterface ()
 The destructor for SocketCANInterface.
 
bool get_is_valid () const override
 Returns if the socket connection is valid.
 
void close () override
 Closes the socket.
 
void open () override
 Connects to the socket.
 
bool reset ()
 Resets the MCP2515.
 
bool read_frame (isobus::CANMessageFrame &canFrame) override
 Returns a frame from the hardware (synchronous), or false if no frame can be read.
 
bool write_frame (const isobus::CANMessageFrame &canFrame) override
 Writes a frame to the bus (synchronous)
 

Private Types

enum class  MCPInstruction : std::uint8_t {
  WRITE = 0x02 , READ = 0x03 , BITMOD = 0x05 , RX_STATUS = 0xB0 ,
  READ_STATUS = 0xA0 , RESET = 0xC0
}
 Some essential instructions of the MCP2515. More...
 
enum class  MCPRegister : std::uint8_t {
  CANSTAT = 0x0E , CANCTRL = 0x0F , CNF3 = 0x28 , CNF2 = 0x29 ,
  CNF1 = 0x2A , CANINTE = 0x2B , CANINTF = 0x2C , TXB0CTRL = 0x30 ,
  TXB0SIDH = 0x31 , TXB1CTRL = 0x40 , TXB1SIDH = 0x41 , TXB2CTRL = 0x50 ,
  TXB2SIDH = 0x51 , RXB0CTRL = 0x60 , RXB0DATA = 0x66 , RXB1CTRL = 0x70 ,
  RXB1DATA = 0x76
}
 Some essential registers of the MCP2515. More...
 
enum class  MCPMode : std::uint8_t {
  NORMAL = 0x00 , SLEEP = 0x20 , LOOPBACK = 0x40 , LISTEN_ONLY = 0x60 ,
  CONFIG = 0x80
}
 The different modes of the MCP2515 associated with their internal bits. More...
 

Private Member Functions

bool get_read_status (std::uint8_t &status)
 Read the rx status of the mcp2515.
 
bool read_register (const MCPRegister address, std::uint8_t &data)
 read a single byte register of the mcp2515
 
bool read_register (const MCPRegister address, std::uint8_t *data, const std::size_t length)
 read multiple byte register 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
 
bool write_register (const MCPRegister address, const std::uint8_t data)
 write a single byte register of the mcp2515
 
bool write_register (const MCPRegister address, const std::uint8_t data[], const std::size_t length)
 write multiple byte register of the mcp2515
 
bool write_reset ()
 Reset the mcp2515 internally.
 
bool set_mode (const MCPMode mode)
 set the mode of the mcp2515
 
bool read_frame (isobus::CANMessageFrame &canFrame, const MCPRegister ctrlRegister, const MCPRegister dataRegister, const std::uint8_t intfMask)
 Read a frame from a buffer on the mcp2515.
 
bool write_frame (const isobus::CANMessageFrame &canFrame, const MCPRegister ctrlRegister, const MCPRegister sidhRegister)
 Write a frame to a buffer on the mcp2515.
 

Private Attributes

SPIHardwarePlugintransactionHandler
 The SPI transaction handler.
 
std::uint8_t rxIndex = 0
 The index of the rx buffer to read from next.
 
std::uint8_t txIndex = 2
 The index of the tx buffer to write to next, start with 2 as it is the buffer with the highest priority.
 
std::uint8_t txPriority = 3
 The priority of the next tx frame.
 
const std::uint8_t cfg1
 Configuration value for CFG1 register.
 
const std::uint8_t cfg2
 Configuration value for CFG2 register.
 
const std::uint8_t cfg3
 Configuration value for CFG3 register.
 
bool initialized = false
 If the mcp2515 has been initialized and no errors have occurred.
 

Static Private Attributes

static constexpr std::uint32_t RECEIVE_MESSAGE_READ_RATE = 10
 Hardcoded time in ms between polling the MCP2515 module for new messages, mostly arbitrary.
 

Detailed Description

A CAN Driver for the MCP2515 CAN controller.

Definition at line 29 of file mcp2515_can_interface.hpp.

Member Enumeration Documentation

◆ MCPInstruction

enum class isobus::MCP2515CANInterface::MCPInstruction : std::uint8_t
strongprivate

Some essential instructions of the MCP2515.

Definition at line 68 of file mcp2515_can_interface.hpp.

◆ MCPMode

enum class isobus::MCP2515CANInterface::MCPMode : std::uint8_t
strongprivate

The different modes of the MCP2515 associated with their internal bits.

Definition at line 101 of file mcp2515_can_interface.hpp.

◆ MCPRegister

enum class isobus::MCP2515CANInterface::MCPRegister : std::uint8_t
strongprivate

Some essential registers of the MCP2515.

Definition at line 79 of file mcp2515_can_interface.hpp.

Constructor & Destructor Documentation

◆ MCP2515CANInterface()

isobus::MCP2515CANInterface::MCP2515CANInterface ( SPIHardwarePlugin * transactionHandler,
const std::uint8_t cfg1,
const std::uint8_t cfg2,
const std::uint8_t cfg3 )

Constructor for the socket CAN driver.

Parameters
[in]transactionHandlerThe SPI transaction handler
[in]cfg1The configuration value for CFG register 1
[in]cfg2The configuration value for CFG register 2
[in]cfg3The configuration value for CFG register 3

Definition at line 19 of file mcp2515_can_interface.cpp.

◆ ~MCP2515CANInterface()

isobus::MCP2515CANInterface::~MCP2515CANInterface ( )
virtual

The destructor for SocketCANInterface.

Definition at line 27 of file mcp2515_can_interface.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ close()

void isobus::MCP2515CANInterface::close ( )
overridevirtual

Closes the socket.

Implements isobus::CANHardwarePlugin.

Definition at line 37 of file mcp2515_can_interface.cpp.

Here is the caller graph for this function:

◆ get_is_valid()

bool isobus::MCP2515CANInterface::get_is_valid ( ) const
overridevirtual

Returns if the socket connection is valid.

Returns
true if connected, false if not connected

Implements isobus::CANHardwarePlugin.

Definition at line 32 of file mcp2515_can_interface.cpp.

Here is the caller graph for this function:

◆ get_read_status()

bool isobus::MCP2515CANInterface::get_read_status ( std::uint8_t & status)
private

Read the rx status of the mcp2515.

Parameters
[out]statusThe status that was read
Returns
If the read was successfull

Definition at line 78 of file mcp2515_can_interface.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ modify_register()

bool isobus::MCP2515CANInterface::modify_register ( const MCPRegister address,
const std::uint8_t mask,
const std::uint8_t data )
private

modify a register of the mcp2515

Parameters
[in]addressThe address of the register to modify
[in]maskThe mask to apply to the register
[in]dataThe data to write to the register
Returns
If the write was successfull

Definition at line 136 of file mcp2515_can_interface.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ open()

void isobus::MCP2515CANInterface::open ( )
overridevirtual

Connects to the socket.

Implements isobus::CANHardwarePlugin.

Definition at line 42 of file mcp2515_can_interface.cpp.

Here is the call graph for this function:

◆ read_frame() [1/2]

bool isobus::MCP2515CANInterface::read_frame ( isobus::CANMessageFrame & canFrame)
overridevirtual

Returns a frame from the hardware (synchronous), or false if no frame can be read.

Parameters
[in,out]canFrameThe CAN frame that was read
Returns
true if a CAN frame was read, otherwise false

Implements isobus::CANHardwarePlugin.

Definition at line 263 of file mcp2515_can_interface.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_frame() [2/2]

bool isobus::MCP2515CANInterface::read_frame ( isobus::CANMessageFrame & canFrame,
const MCPRegister ctrlRegister,
const MCPRegister dataRegister,
const std::uint8_t intfMask )
private

Read a frame from a buffer on the mcp2515.

Parameters
[in,out]canFrameThe frame that was read
[in]ctrlRegisterThe control register of the buffer to read from
[in]dataRegisterThe data register of the buffer to read from
[in]intfMaskThe interrupt flag of the buffer to reset after reading
Returns
If the read was successfull

Definition at line 224 of file mcp2515_can_interface.cpp.

Here is the call graph for this function:

◆ read_register() [1/2]

bool isobus::MCP2515CANInterface::read_register ( const MCPRegister address,
std::uint8_t & data )
private

read a single byte register of the mcp2515

Parameters
[in]addressThe address of the register to read
[out]dataThe data that was read
Returns
If the read was successfull

Definition at line 95 of file mcp2515_can_interface.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_register() [2/2]

bool isobus::MCP2515CANInterface::read_register ( const MCPRegister address,
std::uint8_t * data,
const std::size_t length )
private

read multiple byte register of the mcp2515

Parameters
[in]addressThe address of the register to read
[out]dataThe data that was read
[in]lengthThe length of the data to read
Returns
If the read was successfull

Definition at line 116 of file mcp2515_can_interface.cpp.

Here is the call graph for this function:

◆ reset()

bool isobus::MCP2515CANInterface::reset ( )

Resets the MCP2515.

Returns
true if the reset was successful, otherwise false

Definition at line 61 of file mcp2515_can_interface.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_mode()

bool isobus::MCP2515CANInterface::set_mode ( const MCPMode mode)
private

set the mode of the mcp2515

Parameters
[in]modeThe mode to set the mcp2515 to
Returns
If the mode was set successfully

Definition at line 201 of file mcp2515_can_interface.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_frame() [1/2]

bool isobus::MCP2515CANInterface::write_frame ( const isobus::CANMessageFrame & canFrame)
overridevirtual

Writes a frame to the bus (synchronous)

Parameters
[in]canFrameThe frame to write to the bus
Returns
true if the frame was written, otherwise false

Implements isobus::CANHardwarePlugin.

Definition at line 360 of file mcp2515_can_interface.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_frame() [2/2]

bool isobus::MCP2515CANInterface::write_frame ( const isobus::CANMessageFrame & canFrame,
const MCPRegister ctrlRegister,
const MCPRegister sidhRegister )
private

Write a frame to a buffer on the mcp2515.

Parameters
[in]canFrameThe frame to write
[in]ctrlRegisterThe control register of the buffer to write to
[in]sidhRegisterThe sidh register of the buffer to write to
Returns
If the write was successfull

Definition at line 297 of file mcp2515_can_interface.cpp.

Here is the call graph for this function:

◆ write_register() [1/2]

bool isobus::MCP2515CANInterface::write_register ( const MCPRegister address,
const std::uint8_t data )
private

write a single byte register of the mcp2515

Parameters
[in]addressThe address of the register to write
[in]dataThe data to write to the register
Returns
If the write was successfull

Definition at line 167 of file mcp2515_can_interface.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_register() [2/2]

bool isobus::MCP2515CANInterface::write_register ( const MCPRegister address,
const std::uint8_t data[],
const std::size_t length )
private

write multiple byte register of the mcp2515

Parameters
[in]addressThe address of the register to write
[in]dataThe data to write to the register
[in]lengthThe length of the data to write to the register
Returns
If the write was successfull

Definition at line 183 of file mcp2515_can_interface.cpp.

Here is the call graph for this function:

◆ write_reset()

bool isobus::MCP2515CANInterface::write_reset ( )
private

Reset the mcp2515 internally.

Returns
If the reset was successfull

Definition at line 153 of file mcp2515_can_interface.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ cfg1

const std::uint8_t isobus::MCP2515CANInterface::cfg1
private

Configuration value for CFG1 register.

Definition at line 178 of file mcp2515_can_interface.hpp.

◆ cfg2

const std::uint8_t isobus::MCP2515CANInterface::cfg2
private

Configuration value for CFG2 register.

Definition at line 179 of file mcp2515_can_interface.hpp.

◆ cfg3

const std::uint8_t isobus::MCP2515CANInterface::cfg3
private

Configuration value for CFG3 register.

Definition at line 180 of file mcp2515_can_interface.hpp.

◆ initialized

bool isobus::MCP2515CANInterface::initialized = false
private

If the mcp2515 has been initialized and no errors have occurred.

Definition at line 181 of file mcp2515_can_interface.hpp.

◆ RECEIVE_MESSAGE_READ_RATE

constexpr std::uint32_t isobus::MCP2515CANInterface::RECEIVE_MESSAGE_READ_RATE = 10
staticconstexprprivate

Hardcoded time in ms between polling the MCP2515 module for new messages, mostly arbitrary.

Definition at line 110 of file mcp2515_can_interface.hpp.

◆ rxIndex

std::uint8_t isobus::MCP2515CANInterface::rxIndex = 0
private

The index of the rx buffer to read from next.

Definition at line 175 of file mcp2515_can_interface.hpp.

◆ transactionHandler

SPIHardwarePlugin* isobus::MCP2515CANInterface::transactionHandler
private

The SPI transaction handler.

Definition at line 174 of file mcp2515_can_interface.hpp.

◆ txIndex

std::uint8_t isobus::MCP2515CANInterface::txIndex = 2
private

The index of the tx buffer to write to next, start with 2 as it is the buffer with the highest priority.

Definition at line 176 of file mcp2515_can_interface.hpp.

◆ txPriority

std::uint8_t isobus::MCP2515CANInterface::txPriority = 3
private

The priority of the next tx frame.

Definition at line 177 of file mcp2515_can_interface.hpp.


The documentation for this class was generated from the following files: