EVE 1.0
MCP2562x CAN chip driver
Collaboration diagram for MCP2562x CAN chip driver:

Data Structures

struct  can_t
 CAN driver descriptor: static structure for configuration of driver instance. More...
 
struct  can_state_t
 holds current CAN instance state variables. More...
 
struct  can_frame_t
 holds CAN frame and is used in driver's API to transfer frames between the driver and an application More...
 
struct  can_bittime_t
 CAN timing initialisation structure definition. More...
 

Typedefs

typedef void(* can_rx_callback_t) (const struct can_t *Can, enum can_xbuffer_t BufNum)
 The callback is called on CAN frame reception. More...
 
typedef void(* can_tx_callback_t) (const struct can_t *Can, enum can_xbuffer_t BufNum, uint8_t TXBnCTRL)
 The callback is called at CAN transmission ready and transmission error. More...
 
typedef void(* can_wake_callback_t) (const struct can_t *Can)
 The callback is called at CAN chip wakeup. More...
 
typedef void(* can_err_callback_t) (const struct can_t *Can, uint8_t EREG)
 The callback is called at CAN chip error. More...
 

Enumerations

Functions

void CanInterruptCb (const struct can_t *Can)
 Driver's HW interrupt callback to be exposed to EVE config. More...
 
bool CanInit (const struct can_t *Can)
 Initialises CAN instance structure, connects it to SPI.
Powers up MCP2562x if supported. More...
 
bool CanDisable (const struct can_t *Can)
 Disables interrupts from MCP2562x and releases SPI.
Powers down MCP2562x if supported by product HW. More...
 
void CanReset (const struct can_t *Can)
 Performs MCP2562x HW reset. MCP2562x switches to configuration mode after reset.
NB! It should be verified that MCP2562x is in configuration mode with CanGetMode() function after reset before any subsequent operation. More...
 
bool CanSetup (const struct can_t *Can)
 Sets up MCP2562x registers according to preconfigured parameters.
NB! MCP2562x should be in configuration mode. More...
 
bool CanSetBitTime (const struct can_t *Can, struct can_bittime_t *BitTime)
 Sets up MCP2562x CAN timing parameters.
NB! MCP2562x should be in configuration mode. More...
 
bool CanSetFilter (const struct can_t *Can, uint32_t Id, enum can_filter_t Filter)
 Sets up MCP2562x acceptance filters.
NB! MCP2562x should be in configuration mode. More...
 
enum can_mode_t CanGetMode (const struct can_t *Can)
 Reads current MCP2562x mode of operation. More...
 
bool CanSetMode (const struct can_t *Can, enum can_mode_t Mode)
 Sets current MCP2562x mode of operation.
NB! After setting new mode it should be verified that MCP2562x is in desired mode (in configuration mode after SW reset) with CanGetMode() function before any subsequent operation. More...
 
enum can_xbuffer_t CanUploadFrame (const struct can_t *Can, struct can_frame_t *Frame, enum can_xbuffer_t Buffer)
 Loads 1 full CAN frame to MCP2562x TX register. More...
 
bool CanSendFrame (const struct can_t *Can, enum can_xbuffer_t Buffer, enum can_priority_t Priority, can_tx_callback_t TransmitReadyCb)
 Sets priority and RTS flag to one of MCP2562x TX registers initiating transfer. More...
 
enum can_xbuffer_t CanDownloadFrame (const struct can_t *Can, struct can_frame_t *Frame, enum can_xbuffer_t Buffer)
 Receives 1 full CAN frame from MCP2562x after Rx1/Rx2 is ready. More...
 
bool CanReadStatus (const struct can_t *Can, uint8_t *Status)
 Gets MCP2562x TX/RX buffers status (IF/RTS). More...
 
bool CanClearIF (const struct can_t *Can, uint8_t Mask)
 Clear MCP2562x interrupt flag(s) by mask. More...
 
bool CanInterrupts (const struct can_t *Can, bool Enable)
 Sets/resets all the preconfigured interrupts. More...
 

MCP2562x "Read Status" command result bit masks

#define STAT_RX0IF   (1 << 0)
 Rx0 buffer interrupt flag (Rx0 is ready to be read)
 
#define STAT_RX1IF   (1 << 1)
 Rx1 buffer interrupt flag (Rx1 is ready to be read)
 
#define STAT_TX0REQ   (1 << 2)
 Tx0 RTS flag (Tx0 is pending)
 
#define STAT_TX0IF   (1 << 3)
 Tx0 buffer interrupt flag (Tx0 being sent)
 
#define STAT_TX1REQ   (1 << 4)
 Tx1 RTS flag (Tx1 is pending)
 
#define STAT_TX1IF   (1 << 5)
 Tx1 buffer interrupt flag (Tx1 being sent)
 
#define STAT_TX2REQ   (1 << 6)
 Tx2 RTS flag (Tx2 is pending)
 
#define STAT_TX2IF   (1 << 7)
 Tx2 buffer interrupt flag (Tx2 being sent)
 

MCP2562x TXBnCTRL register (Transmit Buffer [n] Control) bit masks

#define TXB_TXP_MASK   (3 << 0)
 Transmit Buffer Priority mask.
 
#define TXB_TXP0   (1 << 0)
 Transmit Buffer Priority low bit.
 
#define TXB_TXP1   (1 << 1)
 Transmit Buffer Priority high bit.
 
#define TXB_TXREQ   (1 << 3)
 Message Transmit Request.
 
#define TXB_TXERR   (1 << 4)
 Transmission Error Detected.
 
#define TXB_MLOA   (1 << 5)
 Message Lost Arbitration.
 
#define TXB_ABTF   (1 << 6)
 Message Aborted Flag.
 

MCP2562x interrupt bit masks

#define INT_RX0IF   (1 << 0)
 Rx0 Full Interrupt Flag bit - must be cleared by MCU to reset interrupt condition.
 
#define INT_RX1IF   (1 << 1)
 Rx1 Full Interrupt Flag bit - must be cleared by MCU to reset interrupt condition.
 
#define INT_TX0IF   (1 << 2)
 Tx0 Empty Interrupt Flag bit - must be cleared by MCU to reset interrupt condition.
 
#define INT_TX1IF   (1 << 3)
 Tx1 Empty Interrupt Flag bit - must be cleared by MCU to reset interrupt condition.
 
#define INT_TX2IF   (1 << 4)
 Tx2 Empty Interrupt Flag bit - must be cleared by MCU to reset interrupt condition.
 
#define INT_ERRIF   (1 << 5)
 Error Interrupt Flag bit (multiple sources in the EFLG register) - must be cleared by MCU to reset interrupt condition.
 
#define INT_WAKIF   (1 << 6)
 Wake-up Interrupt Flag bit - must be cleared by MCU to reset interrupt condition.
 
#define INT_MERRF   (1 << 7)
 Message Error Interrupt Flag bit - must be cleared by MCU to reset interrupt condition.
 

Detailed Description

This CAN driver supports MCP2562x CAN chip connected with EVE SPI driver.

Public functionality included:

Typedef Documentation

typedef void(* can_rx_callback_t) (const struct can_t *Can, enum can_xbuffer_t BufNum)

The callback is called on CAN frame reception.

Parameters
CanCAN driver descriptor
BufNumBuffer with received data (Rx0/Rx1)

Definition at line 168 of file can.h.

typedef void(* can_tx_callback_t) (const struct can_t *Can, enum can_xbuffer_t BufNum, uint8_t TXBnCTRL)

The callback is called at CAN transmission ready and transmission error.

Parameters
CanCAN driver descriptor
BufNumBuffer transmitted (Tx0/Tx1/Tx2)
TXBnCTRLMCP2562x Transmit buffer control register Masks: TXB_TXP0 - Transmit Buffer Priority low bit TXB_TXP1 - Transmit Buffer Priority high bit TXB_TXREQ - Message Transmit Request TXB_TXERR - Transmission Error Detected TXB_MLOA - Message Lost Arbitration TXB_ABTF - Message Aborted Flag

Definition at line 183 of file can.h.

typedef void(* can_wake_callback_t) (const struct can_t *Can)

The callback is called at CAN chip wakeup.

Parameters
CanCAN driver descriptor

Definition at line 191 of file can.h.

typedef void(* can_err_callback_t) (const struct can_t *Can, uint8_t EREG)

The callback is called at CAN chip error.

Parameters
CanCAN handling structure
EREGMCP2562x EREG register

Definition at line 198 of file can.h.

Enumeration Type Documentation

enum can_mode_t

defines possible MCP2562x modes of operation for CanSetMode() function.
NB: values correspond to REQOP bits in CANCTRL register (except CAN_MODE_RESET and CAN_MODE_SPI_BUSY)

Enumerator
CAN_MODE_NORMAL 

Switch to normal operational mode.

CAN_MODE_SLEEP 

Switch to sleep mode.

CAN_MODE_LOOPBACK 

Switch to loopback mode.

CAN_MODE_LISTENONLY 

Switch to listen-only mode.

CAN_MODE_CONFIGURE 

Switch to configuration mode.

CAN_MODE_RESET 

Perform SW reset; after that MCP is in configuration mode.

CAN_MODE_SPI_BUSY 

SPI failed to communicate with MCP.

Definition at line 110 of file can.h.

defines MCP2562x TX/RX buffers for data transmission/reception.

Enumerator
CAN_TX0 

Write data to / get status of TX0.

CAN_TX1 

Write data to / get status of TX1.

CAN_TX2 

Write data to / get status of TX2.

CAN_RX0 

Read data from / get status of RX0.

CAN_RX1 

Read data from / get status of RX1.

CAN_FIFO 

Write data to the first available TX / Read data from the RX which came first.

Definition at line 124 of file can.h.

defines MCP2562x Rx filter Ids and Masks.

Enumerator
CAN_RXF0 

Identifier of filter #0 for RX0.

CAN_RXF1 

Identifier of filter #1 for RX0.

CAN_RXF2 

Identifier of filter #0 for RX1.

CAN_RXF3 

Identifier of filter #1 for RX1.

CAN_RXF4 

Identifier of filter #2 for RX1.

CAN_RXF5 

Identifier of filter #3 for RX1.

CAN_RXM0 

Mask of filter for RX0.

CAN_RXM1 

Mask of filter for RX1.

Definition at line 137 of file can.h.

Function Documentation

void CanInterruptCb ( const struct can_t Can)

Driver's HW interrupt callback to be exposed to EVE config.

Parameters
CanPointer to CAN instance structure
bool CanInit ( const struct can_t Can)

Initialises CAN instance structure, connects it to SPI.
Powers up MCP2562x if supported.

Parameters
CanCAN driver descriptor
bool CanDisable ( const struct can_t Can)

Disables interrupts from MCP2562x and releases SPI.
Powers down MCP2562x if supported by product HW.

Parameters
CanCAN driver descriptor
void CanReset ( const struct can_t Can)

Performs MCP2562x HW reset. MCP2562x switches to configuration mode after reset.
NB! It should be verified that MCP2562x is in configuration mode with CanGetMode() function after reset before any subsequent operation.

Parameters
CanCAN driver descriptor
bool CanSetup ( const struct can_t Can)

Sets up MCP2562x registers according to preconfigured parameters.
NB! MCP2562x should be in configuration mode.

Parameters
CanCAN driver descriptor
bool CanSetBitTime ( const struct can_t Can,
struct can_bittime_t BitTime 
)

Sets up MCP2562x CAN timing parameters.
NB! MCP2562x should be in configuration mode.

Parameters
CanCAN driver descriptor
BitTimePointer to can_bittime_t structure containing the configuration information.
bool CanSetFilter ( const struct can_t Can,
uint32_t  Id,
enum can_filter_t  Filter 
)

Sets up MCP2562x acceptance filters.
NB! MCP2562x should be in configuration mode.

Parameters
CanCAN driver descriptor
IdFilter identification number / mask number (11 or 29 bit)
FilterRX filter Id or Mask
enum can_mode_t CanGetMode ( const struct can_t Can)

Reads current MCP2562x mode of operation.

Parameters
CanCAN driver descriptor
Returns
Current mode of operation..
bool CanSetMode ( const struct can_t Can,
enum can_mode_t  Mode 
)

Sets current MCP2562x mode of operation.
NB! After setting new mode it should be verified that MCP2562x is in desired mode (in configuration mode after SW reset) with CanGetMode() function before any subsequent operation.

Parameters
CanCAN driver descriptor
ModeMode of operation to set.
enum can_xbuffer_t CanUploadFrame ( const struct can_t Can,
struct can_frame_t Frame,
enum can_xbuffer_t  Buffer 
)

Loads 1 full CAN frame to MCP2562x TX register.

Parameters
CanCAN driver descriptor
FrameStructure holding the data to send.
BufferMCP2562x TX buffer to use.
Returns
TX buffer actually used for transmission.
CAN_FIFO is returned on busy TX buffer / busy SPI
bool CanSendFrame ( const struct can_t Can,
enum can_xbuffer_t  Buffer,
enum can_priority_t  Priority,
can_tx_callback_t  TransmitReadyCb 
)

Sets priority and RTS flag to one of MCP2562x TX registers initiating transfer.

Parameters
CanCAN driver descriptor
BufferMCP2562x TX buffer to use
PriorityMessage priority
TransmitReadyCbCallback to call when transmission is done
Returns
false on error
enum can_xbuffer_t CanDownloadFrame ( const struct can_t Can,
struct can_frame_t Frame,
enum can_xbuffer_t  Buffer 
)

Receives 1 full CAN frame from MCP2562x after Rx1/Rx2 is ready.

Parameters
CanCAN driver descriptor
FrameStructure to copy received data to
BufferMCP2562x RX buffer to read from
Returns
RX buffer actually been read.
CAN_FIFO is returned on RX buffer not ready / busy SPI
bool CanReadStatus ( const struct can_t Can,
uint8_t *  Status 
)

Gets MCP2562x TX/RX buffers status (IF/RTS).

Parameters
CanCAN driver descriptor
StatusPointer to result: TX/RX buffers status as returned by MCP2562x "READ STATUS" command.
Masks:
STAT_TX2IF - Tx2 buffer interrupt flag (Tx2 was sent)
STAT_TX2REQ - Tx2 RTS flag (Tx2 is pending)
STAT_TX1IF - Tx1 buffer interrupt flag (Tx1 was sent)
STAT_TX1REQ - Tx1 RTS flag (Tx1 is pending)
STAT_TX0IF - Tx0 buffer interrupt flag (Tx0 was sent)
STAT_TX0REQ - Tx0 RTS flag (Tx0 is pending)
STAT_RX1IF - Rx1 buffer interrupt flag (Rx1 is ready to be read)
STAT_RX0IF - Rx0 buffer interrupt flag (Rx0 is ready to be read)
Returns
false on error.
bool CanClearIF ( const struct can_t Can,
uint8_t  Mask 
)

Clear MCP2562x interrupt flag(s) by mask.

Parameters
CanCAN driver descriptor
MaskMCP2562x interrupt register mask
Masks:
INT_RX0IF - Rx0 Full Interrupt Flag bit - must be cleared by MCU to reset interrupt condition
INT_RX1IF - Rx1 Full Interrupt Flag bit - must be cleared by MCU to reset interrupt condition
INT_TX0IF - Tx0 Empty Interrupt Flag bit - must be cleared by MCU to reset interrupt condition
INT_TX1IF - Tx1 Empty Interrupt Flag bit - must be cleared by MCU to reset interrupt condition
INT_TX2IF - Tx2 Empty Interrupt Flag bit - must be cleared by MCU to reset interrupt condition
INT_ERRIF - Error Interrupt Flag bit (multiple sources in the EFLG register) - must be cleared by MCU to reset interrupt condition
INT_WAKIF - Wake-up Interrupt Flag bit - must be cleared by MCU to reset interrupt condition
INT_MERRF - Message Error Interrupt Flag bit - must be cleared by MCU to reset interrupt condition
Returns
false on error.
bool CanInterrupts ( const struct can_t Can,
bool  Enable 
)

Sets/resets all the preconfigured interrupts.

Parameters
CanCAN driver descriptor
Enabletrue - enable some (preconfigured) interrupts, false - disable all interrupts
Returns
false on error