PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Public Types | Public Member Functions | Static Public Attributes | List of all members
PropWare::MCP2515 Class Reference

Control and communicate with the Microchip MCP2515 CAN bus controller. More...

#include <PropWare/serial/can/mcp2515.h>

+ Collaboration diagram for PropWare::MCP2515:

Public Types

enum  RegisterAddress {
  RXF0SIDH = 0x00,
  RXF0SIDL = 0x01,
  RXF0EID8 = 0x02,
  RXF0EID0 = 0x03,
  RXF1SIDH = 0x04,
  RXF1SIDL = 0x05,
  RXF1EID8 = 0x06,
  RXF1EID0 = 0x07,
  RXF2SIDH = 0x08,
  RXF2SIDL = 0x09,
  RXF2EID8 = 0x0A,
  RXF2EID0 = 0x0B,
  CANSTAT = 0x0E,
  CANCTRL = 0x0F,
  RXF3SIDH = 0x10,
  RXF3SIDL = 0x11,
  RXF3EID8 = 0x12,
  RXF3EID0 = 0x13,
  RXF4SIDH = 0x14,
  RXF4SIDL = 0x15,
  RXF4EID8 = 0x16,
  RXF4EID0 = 0x17,
  RXF5SIDH = 0x18,
  RXF5SIDL = 0x19,
  RXF5EID8 = 0x1A,
  RXF5EID0 = 0x1B,
  TEC = 0x1C,
  REC = 0x1D,
  RXM0SIDH = 0x20,
  RXM0SIDL = 0x21,
  RXM0EID8 = 0x22,
  RXM0EID0 = 0x23,
  RXM1SIDH = 0x24,
  RXM1SIDL = 0x25,
  RXM1EID8 = 0x26,
  RXM1EID0 = 0x27,
  CNF3 = 0x28,
  CNF2 = 0x29,
  CNF1 = 0x2A,
  CANINTE = 0x2B,
  CANINTF = 0x2C,
  EFLG = 0x2D,
  TXB0CTRL = 0x30,
  TXB1CTRL = 0x40,
  TXB2CTRL = 0x50,
  RXB0CTRL = 0x60,
  RXB0SIDH = 0x61,
  RXB1CTRL = 0x70,
  RXB1SIDH = 0x71
}
 
enum  CANINTFBits {
  RX0IF = BIT_0,
  RX1IF = BIT_1,
  TX0IF = BIT_2,
  TX1IF = BIT_3,
  TX2IF = BIT_4,
  ERRIF = BIT_5,
  WAKIF = BIT_6,
  MERRF = BIT_7
}
 
enum  SPIInstructionSet {
  WRITE = 0x02,
  READ = 0x03,
  BITMOD = 0x05,
  LOAD_TX0 = 0x40,
  LOAD_TX1 = 0x42,
  LOAD_TX2 = 0x44,
  RTS_TX0 = 0x81,
  RTS_TX1 = 0x82,
  RTS_TX2 = 0x84,
  RTS_ALL = 0x87,
  READ_RX0 = 0x90,
  READ_RX1 = 0x94,
  READ_STATUS = 0xA0,
  RX_STATUS = 0xB0,
  RESET = 0xC0
}
 
enum  BufferNumber {
  BUFFER_0,
  BUFFER_1
}
 
enum  FilterNumber {
  FILTER_0,
  FILTER_1,
  FILTER_2,
  FILTER_3,
  FILTER_4,
  FILTER_5
}
 
enum  Mode {
  NORMAL = 0,
  SLEEP = BIT_5,
  LOOPBACK = BIT_6,
  LISTENONLY = BIT_6 | BIT_5,
  CONFIG = BIT_7,
  POWERUP = BIT_7 | BIT_6 | BIT_5
}
 
enum  ErrorCode {
  NO_ERROR,
  MODE_SET_FAILURE,
  NO_MESSAGE,
  CONTROL_ERROR,
  GET_TX_BUFFER_TIMEOUT,
  SEND_MESSAGE_TIMEOUT,
  MESSAGE_TOO_LONG,
  ALLTXBUSY
}
 
enum  BaudRate {
  BAUD_5KBPS,
  BAUD_10KBPS,
  BAUD_20KBPS,
  BAUD_31K25BPS,
  BAUD_40KBPS,
  BAUD_50KBPS,
  BAUD_80KBPS,
  BAUD_100KBPS,
  BAUD_125KBPS,
  BAUD_200KBPS,
  BAUD_250KBPS,
  BAUD_500KBPS,
  BAUD_1000KBPS
}
 

Public Member Functions

 MCP2515 (const Pin::Mask cs)
 
 MCP2515 (const SPI &spi, const Pin::Mask cs)
 
PropWare::ErrorCode start (const BaudRate baudRate, const Mode mode=DEFAULT_MODE)
 Initialize the controller. More...
 
PropWare::ErrorCode set_mask (const BufferNumber bufferNumber, const uint32_t id, const bool extendedID=false) const
 
PropWare::ErrorCode set_filter (const FilterNumber num, const uint32_t id, const bool extendedID=false) const
 
PropWare::ErrorCode send_message (const uint32_t id, const uint8_t len, const uint8_t buf[], const bool extendedID=false)
 Send a message. More...
 
PropWare::ErrorCode read_message (uint8_t *len, uint8_t *buf)
 Read a message from either buffer. More...
 
PropWare::ErrorCode read_message (const BufferNumber bufferNumber, uint8_t *len, uint8_t *buf)
 Read a message from a specific buffer. More...
 
bool check_receive_buffer () const
 Determine if a message is available for reading on either buffer. More...
 
bool check_receive_buffer (const BufferNumber bufferNumber) const
 Determine if a message is ready to be read from the requested buffer. More...
 
PropWare::ErrorCode check_error () const
 
uint32_t get_id () const
 

Static Public Attributes

static const Mode DEFAULT_MODE = Mode::NORMAL
 
static const uint8_t MODE_MASK = BIT_7 | BIT_6 | BIT_5
 
static const uint8_t SJW1 = NULL_BIT
 
static const uint8_t SJW2 = BIT_6
 
static const uint8_t SJW3 = BIT_7
 
static const uint8_t SJW4 = BIT_7 | BIT_6
 
static const uint8_t SAMPLE_1X = NULL_BIT
 
static const uint8_t SAMPLE_3X = BIT_6
 
static const uint8_t BTLMODE = BIT_7
 
static const uint8_t SOF_ENABLE = 0x80
 
static const uint8_t SOF_DISABLE = 0x00
 
static const uint8_t WAKFIL_ENABLE = 0x40
 
static const uint8_t WAKFIL_DISABLE = 0x00
 
static const uint8_t CNF1_16MHz_1000kBPS = SJW1
 
static const uint8_t CNF2_16MHz_1000kBPS = 0xD0
 
static const uint8_t CNF3_16MHz_1000kBPS = 0x82
 
static const uint8_t CNF1_16MHz_500kBPS = SJW1
 
static const uint8_t CNF2_16MHz_500kBPS = 0xF0
 
static const uint8_t CNF3_16MHz_500kBPS = 0x86
 
static const uint8_t CNF1_16MHz_250kBPS = SJW2 | 1
 
static const uint8_t CNF2_16MHz_250kBPS = 0xF1
 
static const uint8_t CNF3_16MHz_250kBPS = 0x85
 
static const uint8_t CNF1_16MHz_200kBPS = SJW1 | 1
 
static const uint8_t CNF2_16MHz_200kBPS = 0xFA
 
static const uint8_t CNF3_16MHz_200kBPS = 0x87
 
static const uint8_t CNF1_16MHz_125kBPS = SJW1 | 3
 
static const uint8_t CNF2_16MHz_125kBPS = 0xF0
 
static const uint8_t CNF3_16MHz_125kBPS = 0x86
 
static const uint8_t CNF1_16MHz_100kBPS = SJW1 | 3
 
static const uint8_t CNF2_16MHz_100kBPS = 0xFA
 
static const uint8_t CNF3_16MHz_100kBPS = 0x87
 
static const uint8_t CNF1_16MHz_80kBPS = SJW1 | 3
 
static const uint8_t CNF2_16MHz_80kBPS = 0xFF
 
static const uint8_t CNF3_16MHz_80kBPS = 0x87
 
static const uint8_t CNF1_16MHz_50kBPS = SJW1 | 7
 
static const uint8_t CNF2_16MHz_50kBPS = 0xFA
 
static const uint8_t CNF3_16MHz_50kBPS = 0x87
 
static const uint8_t CNF1_16MHz_40kBPS = SJW1 | 7
 
static const uint8_t CNF2_16MHz_40kBPS = 0xFF
 
static const uint8_t CNF3_16MHz_40kBPS = 0x87
 
static const uint8_t CNF1_16MHz_31k25BPS = SJW1 | 15
 
static const uint8_t CNF2_16MHz_31k25BPS = 0xF1
 
static const uint8_t CNF3_16MHz_31k25BPS = 0x85
 
static const uint8_t CNF1_16MHz_20kBPS = SJW1 | 15
 
static const uint8_t CNF2_16MHz_20kBPS = 0xFF
 
static const uint8_t CNF3_16MHz_20kBPS = 0x87
 
static const uint8_t CNF1_16MHz_10kBPS = SJW1 | 31
 
static const uint8_t CNF2_16MHz_10kBPS = 0xFF
 
static const uint8_t CNF3_16MHz_10kBPS = 0x87
 
static const uint8_t CNF1_16MHz_5kBPS = SJW1 | 63
 
static const uint8_t CNF2_16MHz_5kBPS = 0xFF
 
static const uint8_t CNF3_16MHz_5kBPS = 0x87
 
static const uint8_t MAX_DATA_BYTES = 8
 

Detailed Description

Control and communicate with the Microchip MCP2515 CAN bus controller.

Examples
MCP2515_Demo.cpp.

Definition at line 111 of file mcp2515.h.

Member Function Documentation

◆ check_receive_buffer() [1/2]

bool PropWare::MCP2515::check_receive_buffer ( ) const

Determine if a message is available for reading on either buffer.

Returns
True when a message is available, false otherwise

Definition at line 497 of file mcp2515.h.

◆ check_receive_buffer() [2/2]

bool PropWare::MCP2515::check_receive_buffer ( const BufferNumber  bufferNumber) const

Determine if a message is ready to be read from the requested buffer.

Parameters
[in]bufferNumberNumber of the buffer to check
Returns
True when a message is available, false otherwise

Definition at line 508 of file mcp2515.h.

◆ read_message() [1/2]

PropWare::ErrorCode PropWare::MCP2515::read_message ( const BufferNumber  bufferNumber,
uint8_t *  len,
uint8_t *  buf 
)

Read a message from a specific buffer.

Parameters
[in]bufferNumberBuffer to read from
[out]bufAddress where incoming message will be stored
[out]lenNumber of bytes received
Returns
0 upon success. NO_MESSAGE if the requested buffer does not contain a message

Definition at line 484 of file mcp2515.h.

◆ read_message() [2/2]

PropWare::ErrorCode PropWare::MCP2515::read_message ( uint8_t *  len,
uint8_t *  buf 
)

Read a message from either buffer.

Buffer 0 is checked first. If no message is available on buffer 0, buffer 1 will be checked.

Parameters
[out]bufAddress where incoming message will be stored
[out]lenNumber of bytes received
Returns
0 upon success. NO_MESSAGE if neither buffer has a message available

Definition at line 467 of file mcp2515.h.

◆ send_message()

PropWare::ErrorCode PropWare::MCP2515::send_message ( const uint32_t  id,
const uint8_t  len,
const uint8_t  buf[],
const bool  extendedID = false 
)

Send a message.

Parameters
[in]idID of the sender
[in]lenNumber of bytes in the message. Must be less than or equal to MAX_CHAR_IN_MESSAGE
[in]bufAddress where the message data is stored
[in]extendedIDTrue for 29-bit, false for 11-bit

Definition at line 451 of file mcp2515.h.

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

◆ start()

PropWare::ErrorCode PropWare::MCP2515::start ( const BaudRate  baudRate,
const Mode  mode = DEFAULT_MODE 
)

Initialize the controller.

Parameters
[in]baudRateCAN bus baud rate assuming a 16 MHz clock frequency
[in]modeAllows selection between normal, loopback and other modes. Configuration can not be changed until the controller is restarted
Returns
Zero upon success, error code otherwise

Definition at line 367 of file mcp2515.h.

Member Data Documentation

◆ MAX_DATA_BYTES

const uint8_t PropWare::MCP2515::MAX_DATA_BYTES = 8
static

The MCP2515 only has an 8-byte data buffer

Definition at line 343 of file mcp2515.h.


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