mcp2515.h File Reference

Driver for the Microchip MCP2515 can controller. More...

Detailed Description

Driver for the Microchip MCP2515 can controller.

Definition of the MCP2515 CAN controller driver.

Author
Toon Stegen toon..nosp@m.steg.nosp@m.en@al.nosp@m.tran.nosp@m..com

Definition in file mcp2515.h.

#include "mcp2515_defines.h"
#include "candev_mcp2515.h"
+ Include dependency graph for mcp2515.h:

Go to the source code of this file.

#define MCP2515_WKUP_SRC_INTERNAL   1
 Wake up source.
 
enum  mcp2515_mode {
  MODE_NORMAL = MCP2515_CANSTAT_OPMOD_NORMAL, MODE_SLEEP = MCP2515_CANSTAT_OPMOD_SLEEP, MODE_LOOPBACK = MCP2515_CANSTAT_OPMOD_LOOPBACK, MODE_LISTEN_ONLY = MCP2515_CANSTAT_OPMOD_LISTEN_ONLY,
  MODE_CONFIG = MCP2515_CANSTAT_OPMOD_CONFIGURATION, MODE_UNKNOWN = -1
}
 MCP2515 mode.
 
enum  mcp2515_interrupt {
  INT_RX0 = MCP2515_CANINTF_RX0IF, INT_RX1 = MCP2515_CANINTF_RX1IF, INT_TX0 = MCP2515_CANINTF_TX0IF, INT_TX1 = MCP2515_CANINTF_TX1IF,
  INT_TX2 = MCP2515_CANINTF_TX2IF, INT_ERROR = MCP2515_CANINTF_ERRIF, INT_WAKEUP = MCP2515_CANINTF_WAKIF, INT_MESSAGE_ERROR = MCP2515_CANINTF_MERRF
}
 MCP2515 interrupt.
 
enum  mcp2515_error {
  ERR_WARNING = MCP2515_EFLG_EWARN, ERR_RX_WARNING = MCP2515_EFLG_RXWAR, ERR_TX_WARNING = MCP2515_EFLG_TXWAR, ERR_RX_PASSIVE = MCP2515_EFLG_RXEP,
  ERR_TX_PASSIVE = MCP2515_EFLG_TXEP, ERR_TX_BUS_OFF = MCP2515_EFLG_TXBO, ERR_RX_0_OVERFLOW = MCP2515_EFLG_RX0OVR, ERR_RX_1_OVERFLOW = MCP2515_EFLG_RX1OVR
}
 MCP2515 error.
 
int mcp2515_init (candev_mcp2515_t *dev, void(*irq_cb)(void *))
 Initialize pins and SPI interface. More...
 
void mcp2515_reset (candev_mcp2515_t *dev)
 Reset MCP2515 device with dedicated pin. More...
 
int mcp2515_init_irqs (candev_mcp2515_t *dev)
 Initialize MCP2515 interrupts. More...
 
int mcp2515_send (candev_mcp2515_t *dev, const struct can_frame *frame, int mailbox)
 Send frame through the corresponding tx mailbox. More...
 
int mcp2515_receive (candev_mcp2515_t *dev, struct can_frame *frame, int mailbox)
 Receive frame from the corresponding rx mailbox. More...
 
int mcp2515_abort (candev_mcp2515_t *dev, int mailbox)
 Abort communication. More...
 
enum mcp2515_mode mcp2515_get_mode (candev_mcp2515_t *dev)
 Get MCP2515 mode of operation. More...
 
enum mcp2515_mode mcp2515_set_mode (candev_mcp2515_t *dev, enum mcp2515_mode mode)
 Set MCP2515 mode of operation. More...
 
void mcp2515_wake_up (candev_mcp2515_t *dev)
 Wake up MCP2515. More...
 
enum mcp2515_interrupt mcp2515_get_irq (candev_mcp2515_t *dev)
 Get MCP2515 interrupt type. More...
 
int mcp2515_clear_irq (candev_mcp2515_t *dev, enum mcp2515_interrupt irq)
 Clear MCP2515 interrupt. More...
 
int mcp2515_tx_err_occurred (candev_mcp2515_t *dev, int mailbox)
 Get if an tx error occurred on MCP2515. More...
 
int mcp2515_configure_bittiming (candev_mcp2515_t *dev)
 Configure the bit timing of the MCP2515. More...
 
uint8_t mcp2515_get_errors (candev_mcp2515_t *dev)
 Get the error flags. More...
 
int mcp2515_set_filter (candev_mcp2515_t *dev, int filter_id, uint32_t filter)
 Set the @ filter_id to the position in the mailbox. More...
 
int mcp2515_set_mask (candev_mcp2515_t *dev, int mailbox, uint32_t mask)
 Set the @ mask to the mailbox. More...
 

Function Documentation

◆ mcp2515_abort()

int mcp2515_abort ( candev_mcp2515_t dev,
int  mailbox 
)

Abort communication.

Parameters
[in]devdevice descriptor
[in]mailboxmailbox
Returns
0 on success
<0 on error

◆ mcp2515_clear_irq()

int mcp2515_clear_irq ( candev_mcp2515_t dev,
enum mcp2515_interrupt  irq 
)

Clear MCP2515 interrupt.

Parameters
[in]devdevice descriptor
[in]irqinterrupt to clear
Returns
0 on success
<0 on error

◆ mcp2515_configure_bittiming()

int mcp2515_configure_bittiming ( candev_mcp2515_t dev)

Configure the bit timing of the MCP2515.

The information about the bit timing should be contained in dev descriptor.

Parameters
[in]devdevice descriptor
Returns
0 on success
<0 on error

◆ mcp2515_get_errors()

uint8_t mcp2515_get_errors ( candev_mcp2515_t dev)

Get the error flags.

Parameters
[in]devdevice descriptor
Returns
EFLG error flags

◆ mcp2515_get_irq()

enum mcp2515_interrupt mcp2515_get_irq ( candev_mcp2515_t dev)

Get MCP2515 interrupt type.

Parameters
[in]devdevice descriptor
Returns
mcp2515_interrupt enum

◆ mcp2515_get_mode()

enum mcp2515_mode mcp2515_get_mode ( candev_mcp2515_t dev)

Get MCP2515 mode of operation.

Parameters
[in]devdevice descriptor
Returns
mcp2515_mode enum

◆ mcp2515_init()

int mcp2515_init ( candev_mcp2515_t dev,
void(*)(void *)  irq_cb 
)

Initialize pins and SPI interface.

The device descriptor contains all information related to pins and SPI interface. This function initialize all corresponding fields and relies the irq_cb callback function to the pin interruption. The pin interruption should be configured in the device descriptor.

Parameters
[out]devdevice descriptor
[in]irq_cbcallback function called when an interrupt is raised from the MCP2515. The MCP2515 makes the interruption through the interruption pin.
Returns
0 on success
<0 on error

◆ mcp2515_init_irqs()

int mcp2515_init_irqs ( candev_mcp2515_t dev)

Initialize MCP2515 interrupts.

Parameters
[in]devdevice descriptor
Returns
0 on success
<0 on error

◆ mcp2515_receive()

int mcp2515_receive ( candev_mcp2515_t dev,
struct can_frame frame,
int  mailbox 
)

Receive frame from the corresponding rx mailbox.

Parameters
[in]devdevice descriptor
[out]framethe receive frame
[in]mailboxrx mailbox
Returns
0 on success
<0 on error

◆ mcp2515_reset()

void mcp2515_reset ( candev_mcp2515_t dev)

Reset MCP2515 device with dedicated pin.

The MCP2515 device is reset by toggling the rst pin.

Parameters
[in]devdevice descriptor

◆ mcp2515_send()

int mcp2515_send ( candev_mcp2515_t dev,
const struct can_frame frame,
int  mailbox 
)

Send frame through the corresponding tx mailbox.

Parameters
[in]devdevice descriptor
[in]framethe frame to send
[in]mailboxtx mailbox
Returns
0 on success
<0 on error

◆ mcp2515_set_filter()

int mcp2515_set_filter ( candev_mcp2515_t dev,
int  filter_id,
uint32_t  filter 
)

Set the @ filter_id to the position in the mailbox.

filter_id corresponds to the position in the MCP2515 mailbox as follow:

  • [0; 1]: mailbox RXB0
  • [2; 5]: mailbox RXB1

The MCP2515 managed 6 acceptance filters in 2 rx mailboxes:

  • MB0 contains 2 acceptance filters in relation with 1 acceptance mask
  • MB1 contains 4 acceptance filters in relation with 1 acceptance mask
        MB0          MB1
      +------+    +------+
    
    mask 0 | RXM0 | | RXM1 | mask 1 +======+ +======+ filter 0 | RXF0 | | RXF2 | filter 2 +---—+ +---—+ filter 1 | RXF1 | | RXF3 | filter 3 +---—+ +---—+ | RXF4 | filter 4 +---—+ | RXF5 | filter 5 +---—+
Parameters
[in]devdevice descriptor
[in]filter_idfilter identifier in the MCP2515 mailbox
[in]filteracceptance filter can identifier
Returns
0 on success
<0 on error

◆ mcp2515_set_mask()

int mcp2515_set_mask ( candev_mcp2515_t dev,
int  mailbox,
uint32_t  mask 
)

Set the @ mask to the mailbox.

The MCP2515 managed 6 acceptance filters in 2 rx mailboxes:

  • MB0 contains 2 acceptance filters in relation with 1 acceptance mask
  • MB1 contains 4 acceptance filters in relation with 1 acceptance mask
        MB0          MB1
      +------+    +------+
    
    mask 0 | RXM0 | | RXM1 | mask 1 +======+ +======+ filter 0 | RXF0 | | RXF2 | filter 2 +---—+ +---—+ filter 1 | RXF1 | | RXF3 | filter 3 +---—+ +---—+ | RXF4 | filter 4 +---—+ | RXF5 | filter 5 +---—+
Parameters
[in]devdevice descriptor
[in]mailboxrx mailbox
[in]maskacceptance mask
Returns
0 on success
<0 on error

◆ mcp2515_set_mode()

enum mcp2515_mode mcp2515_set_mode ( candev_mcp2515_t dev,
enum mcp2515_mode  mode 
)

Set MCP2515 mode of operation.

Parameters
[in]devdevice descriptor
[in]modemode of operation to set
Returns
The mode actually set

◆ mcp2515_tx_err_occurred()

int mcp2515_tx_err_occurred ( candev_mcp2515_t dev,
int  mailbox 
)

Get if an tx error occurred on MCP2515.

Parameters
[in]devdevice descriptor
[in]mailboxtx mailbox
Returns
0 on success
<0 on error

◆ mcp2515_wake_up()

void mcp2515_wake_up ( candev_mcp2515_t dev)

Wake up MCP2515.

Parameters
[in]devdevice descriptor