Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
MCP2515Can Class Reference

Specialization of CAN driver for Tiva CAN. More...

#include <MCP2515Can.hxx>

Inheritance diagram for MCP2515Can:
Can OSThread Atomic NonBlockNode Node Device FileIO

Classes

class  Buffer
 CAN TX and RX buffer structure. More...
 
class  BufferRead
 Setup a buffer read transfer structure. More...
 
class  BufferWrite
 Setup a buffer read transfer structure. More...
 
struct  Config1
 Configuration resgister 1 structure. More...
 
struct  Config2
 Configuration resgister 2 structure. More...
 
struct  Config3
 Configuration resgister 3 structure. More...
 
struct  MCP2515Baud
 Baud rate table entry. More...
 

Public Member Functions

 MCP2515Can (const char *name, void(*interrupt_enable)(), void(*interrupt_disable)())
 Constructor.
 
 ~MCP2515Can ()
 Destructor.
 
void init (const char *spi_name, uint32_t freq, uint32_t baud)
 Initialize CAN device settings.
 
void interrupt_handler ()
 Handle an interrupt.
 
OSMutexget_spi_bus_lock ()
 Return a mutex that can be used by another SPI driver instance sharing the same bus as its bus lock.
 
- Public Member Functions inherited from Can
int available ()
 
int availableForWrite ()
 
int read (struct can_frame *frame)
 Read a frame if there is one available.
 
int write (const struct can_frame *frame)
 Send a frame if there is space available.
 
- Public Member Functions inherited from Device
 Device (const char *name)
 Constructor.
 
virtual ~Device ()
 Destructor.
 
- Public Member Functions inherited from OSThread
 OSThread (const char *name, int priority, size_t stack_size, void *(*start_routine)(void *), void *arg)
 Create a thread.
 
 OSThread ()
 Creates a thread via inheritance.
 
void start (const char *name, int priority, size_t stack_size)
 Starts the thread.
 
virtual ~OSThread ()
 Default destructor.
 
bool is_created ()
 
void inherit ()
 Inherits the current thread.
 
os_thread_t get_handle ()
 
void lock_to_thread ()
 Sets the thread handle to the current calling thread's.
 
void unlock_from_thread ()
 Resets the thread handle to none.
 

Private Types

enum  Registers {
  RXF0SIDH = 0 , RXF0SIDL , RXF0SEID8 , RXF0EID0 ,
  RXF1SIDH , RXF1SIDL , RXF1SEID8 , RXF1EID0 ,
  RXF2SIDH , RXF2SIDL , RXF2SEID8 , RXF2EID0 ,
  BFPCTRL , TXRTSCTRL , CANSTAT , CANCTRL ,
  RXF3SIDH = 16 , RXF3SIDL , RXF3EID8 , RXF3EID0 ,
  RXF4SIDH , RXF4SIDL , RXF4EID8 , RXF4EID0 ,
  RXF5SIDH , RXF5SIDL , RXF5EID8 , RXF5EID0 ,
  TEC , RED , RXM0SIDH = 32 , RXM0SIDL ,
  RXM0EID8 , RXM0EID0 , RXM1SIDH , RXM1SIDL ,
  RXM1EID8 , RXM1EID0 , CNF3 , CNF2 ,
  CNF1 , CANINTE , CANINTF , EFLG ,
  TXB0CTRL = 48 , TXB0SIDH , TXB0SIDL , TXB0EID8 ,
  TXB0EID0 , TXB0DLC , TXB0D0 , TXB0D1 ,
  TXB0D2 , TXB0D3 , TXB0D4 , TXB0D5 ,
  TXB0D6 , TXB0D7 , TXB1CTRL = 64 , TXB1SIDH ,
  TXB1SIDL , TXB1EID8 , TXB1EID0 , TXB1DLC ,
  TXB1D0 , TXB1D1 , TXB1D2 , TXB1D3 ,
  TXB1D4 , TXB1D5 , TXB1D6 , TXB1D7 ,
  TXB2CTRL = 80 , TXB2SIDH , TXB2SIDL , TXB2EID8 ,
  TXB2EID0 , TXB2DLC , TXB2D0 , TXB2D1 ,
  TXB2D2 , TXB2D3 , TXB2D4 , TXB2D5 ,
  TXB2D6 , TXB2D7 , RXB0CTRL = 96 , RXB0SIDH ,
  RXB0SIDL , RXB0EID8 , RXB0EID0 , RXB0DLC ,
  RXB0D0 , RXB0D1 , RXB0D2 , RXB0D3 ,
  RXB0D4 , RXB0D5 , RXB0D6 , RXB0D7 ,
  RXB1CTRL = 112 , RXB1SIDH , RXB1SIDL , RXB1EID8 ,
  RXB1EID0 , RXB1DLC , RXB1D0 , RXB1D1 ,
  RXB1D2 , RXB1D3 , RXB1D4 , RXB1D5 ,
  RXB1D6 , RXB1D7
}
 SPI registers. More...
 
enum  ControlFields {
  CLKPRE = 0x03 , CLKEN = 0x04 , OSM = 0x08 , ABAT = 0x10 ,
  REQOP = 0xE0
}
 Fields of the control register. More...
 
enum  InterruptFlags {
  RX0I = 0x01 , RX1I = 0x02 , TX0I = 0x04 , TX1I = 0x08 ,
  TX2I = 0x10 , ERRI = 0x20 , WAKI = 0x40 , MERR = 0x80
}
 interrupt flag masks More...
 
enum  ErrorFlags {
  EWARN = 0x01 , RXWARN = 0x02 , TXWARN = 0x04 , RXEP = 0x08 ,
  TXEP = 0x10 , TXBO = 0x20 , RX0OVR = 0x40 , RX1OVR = 0x80
}
 interrupt flag masks More...
 
enum  Instructions {
  RESET = 0xC0 , READ = 0x03 , READ_RX_BUF = 0x90 , WRITE = 0x02 ,
  LOAD_TX_BUF = 0x40 , STATUS = 0xA0 , RX_STATUS = 0xB0 , RTS = 0x80 ,
  BIT_MODIFY = 0x05
}
 SPI transaction instructions. More...
 

Private Member Functions

int ioctl (File *file, unsigned long int key, unsigned long data) override
 Request an ioctl transaction.
 
void * entry () override
 User entry point for the created thread.
 
void enable () override
 function to enable device
 
void disable () override
 function to disable device
 
void tx_msg () override
 Function to try and transmit a message.
 
void tx_msg_locked ()
 Function to try and transmit a message while holding a lock.
 
void reset ()
 Reset the device.
 
uint8_t register_read (Registers address)
 Read from a SPI register.
 
void register_write (Registers address, uint8_t data)
 Write to a SPI register.
 
void bit_modify (Registers address, uint8_t data, uint8_t mask)
 Bit modify to a SPI register.
 
void buffer_read (BufferRead *buf)
 Read a message to into a receive buffer.
 
void buffer_write (BufferWrite *buf, struct can_frame *can_frame)
 Write a message to a transmit buffer.
 
void request_gpio_operation ()
 Request that the GPIO cache be refreshed.
 
 MCP2515Can ()
 Default constructor.
 
 DISALLOW_COPY_AND_ASSIGN (MCP2515Can)
 

Private Attributes

void(* interruptEnable_ )()
 enable interrupt callback
 
void(* interruptDisable_ )()
 disable interrupt callback
 
unsigned state_: 4
 present bus state
 
unsigned txPending_: 2
 transmission in flight
 
unsigned gpoData_: 2
 local copy of the I/O expansion output data
 
unsigned gpiData_: 3
 local copy of the I/O expansion input data
 
unsigned ioPending_: 1
 true if an I/O update is pending
 
int spiFd_
 SPI bus that accesses MCP2515.
 
SPIspi_
 pointer to a SPI object instance
 
OSSem sem_
 semaphore for posting events
 

Static Private Attributes

static constexpr uint32_t SPI_MAX_SPEED_HZ = 10000000
 maximum SPI clock speed in Hz
 
static const MCP2515Baud BAUD_TABLE []
 baud rate settings table
 

Friends

class MCP2515GPO
 Allow access to MCP2515Can from MCP2515GPO.
 
class MCP2515GPI
 Allow access to MCP2515Can from MCP2515GPI.
 

Additional Inherited Members

- Static Public Member Functions inherited from Device
static int open (struct _reent *reent, const char *path, int flags, int mode)
 Open a file or device.
 
static int close (struct _reent *reent, int fd)
 Close a file or device.
 
static int stat (struct _reent *reent, const char *path, struct stat *stat)
 Get the status information of a file or device.
 
static int select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, long long timeout)
 POSIX select().
 
static void select_clear ()
 Clears the current thread's select bits.
 
- Static Public Member Functions inherited from FileIO
static ssize_t read (struct _reent *reent, int fd, void *buf, size_t count)
 Read from a file or device.
 
static ssize_t write (struct _reent *reent, int fd, const void *buf, size_t count)
 Write to a file or device.
 
static _off_t lseek (struct _reent *reent, int fd, _off_t offset, int whence)
 Change the offset index of a file or device.
 
static int fstat (struct _reent *reent, int fd, struct stat *stat)
 Get the status information of a file or device.
 
static int ioctl (int fd, unsigned long int key, unsigned long data)
 Request and ioctl transaction.
 
static int fcntl (int fd, int cmd, unsigned long data)
 Manipulate a file descriptor.
 
static bool is_device (int fd)
 Test if the file descriptor belongs to a device.
 
- Static Public Member Functions inherited from OSThread
static int getpriority (OSThread *thread)
 Return the current thread priority.
 
static int get_priority (OSThread *thread)
 Return the current thread priority.
 
static int get_priority_min ()
 Get the minimum thread priority.
 
static int get_priority_max ()
 Get the maximum thread priority.
 
- Static Public Attributes inherited from Can
static unsigned numReceivedPackets_ {0}
 
static unsigned numTransmittedPackets_ {0}
 
- Protected Member Functions inherited from Can
 Can (const char *ignored)
 Constructor.
 
 ~Can ()
 Destructor.
 
 Can (const char *name, size_t tx_buffer_size=config_can_tx_buffer_size(), size_t rx_buffer_size=config_can_rx_buffer_size())
 Constructor.
 
 ~Can ()
 Destructor.
 
bool has_tx_buffer_space () OVERRIDE
 
bool has_rx_buffer_data () OVERRIDE
 
void flush_buffers () OVERRIDE
 called after disable
 
ssize_t read (File *file, void *buf, size_t count) OVERRIDE
 Read from a file or device.
 
ssize_t write (File *file, const void *buf, size_t count) OVERRIDE
 Write to a file or device.
 
bool select (File *file, int mode) OVERRIDE
 Device select method.
 
- Protected Member Functions inherited from Atomic
void lock ()
 
void unlock ()
 
- Protected Member Functions inherited from NonBlockNode
 NonBlockNode (const char *name)
 Constructor.
 
int ioctl (File *file, unsigned long int key, unsigned long data) OVERRIDE
 Request an ioctl transaction.
 
- Protected Member Functions inherited from Node
 Node (const char *name)
 Constructor.
 
virtual ~Node ()
 Destructor.
 
int open (File *, const char *, int, int) OVERRIDE
 Open method.
 
int close (File *) OVERRIDE
 Close method.
 
virtual int fstat (File *file, struct stat *stat) override
 Get the status information of a file or device.
 
 Device (const char *name)
 Constructor.
 
virtual ~Device ()
 Destructor.
 
- Protected Member Functions inherited from FileIO
 FileIO (const char *name)
 Constructor.
 
virtual ~FileIO ()
 Destructor.
 
virtual off_t lseek (File *f, off_t offset, int whence)
 Seek method.
 
virtual int fcntl (File *file, int cmd, unsigned long data)
 Manipulate a file descriptor.
 
- Static Protected Member Functions inherited from Device
static void select_insert (SelectInfo *info)
 Add client to list of clients needing woken.
 
static void select_wakeup (SelectInfo *info)
 Wakeup the list of clients needing woken.
 
static void select_wakeup_from_isr (SelectInfo *info, int *woken)
 Wakeup the list of clients needing woken.
 
static int open (struct _reent *reent, const char *path, int flags, int mode)
 Open a file or device.
 
static int close (struct _reent *reent, int fd)
 Close a file or device.
 
static int stat (struct _reent *reent, const char *path, struct stat *stat)
 Get the status information of a file or device.
 
static int select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, long long timeout)
 POSIX select().
 
static void select_clear ()
 Clears the current thread's select bits.
 
- Static Protected Member Functions inherited from FileIO
static int fd_alloc (void)
 Allocate a free file descriptor.
 
static void fd_free (int fd)
 Free up a file descriptor.
 
static Filefile_lookup (int fd)
 Looks up a reference to a File corresponding to a given file descriptor.
 
static int fd_lookup (File *file)
 Looks up a file descriptor corresponding to a given File reference.
 
static ssize_t read (struct _reent *reent, int fd, void *buf, size_t count)
 Read from a file or device.
 
static ssize_t write (struct _reent *reent, int fd, const void *buf, size_t count)
 Write to a file or device.
 
static _off_t lseek (struct _reent *reent, int fd, _off_t offset, int whence)
 Change the offset index of a file or device.
 
static int fstat (struct _reent *reent, int fd, struct stat *stat)
 Get the status information of a file or device.
 
static int ioctl (int fd, unsigned long int key, unsigned long data)
 Request and ioctl transaction.
 
static int fcntl (int fd, int cmd, unsigned long data)
 Manipulate a file descriptor.
 
static bool is_device (int fd)
 Test if the file descriptor belongs to a device.
 
- Protected Attributes inherited from Can
DeviceBuffer< struct can_frame > * txBuf
 transmit buffer
 
DeviceBuffer< struct can_frame > * rxBuf
 receive buffer
 
unsigned int overrunCount
 overrun count
 
unsigned int busOffCount
 bus-off count
 
unsigned int softErrorCount
 soft error count
 
- Protected Attributes inherited from NonBlockNode
NotifiablereadableNotify_
 This will be notified if the device has data avilable for read.
 
NotifiablewritableNotify_
 This will be notified if the device has buffer avilable for write.
 
- Protected Attributes inherited from Node
OSMutex lock_
 protects internal structures.
 
mode_t mode_
 File open mode, such as O_NONBLOCK.
 
unsigned int references_
 number of open references
 
- Protected Attributes inherited from FileIO
const char * name
 device name
 
- Static Protected Attributes inherited from FileIO
static const unsigned int numOpenFiles = 20
 
static File files []
 File descriptor pool.
 
static OSMutex mutex
 mutual exclusion for fileio
 

Detailed Description

Specialization of CAN driver for Tiva CAN.

Definition at line 56 of file MCP2515Can.hxx.

Member Enumeration Documentation

◆ ControlFields

Fields of the control register.

Enumerator
CLKPRE 

clockout pin prescaler

CLKEN 

clockout pin enable

OSM 

one-shot mode

ABAT 

abort all pending transmissions

REQOP 

request operation mode

Definition at line 239 of file MCP2515Can.hxx.

◆ ErrorFlags

enum MCP2515Can::ErrorFlags
private

interrupt flag masks

Enumerator
EWARN 

set when TEC or REC is equal to or greater than 96

RXWARN 

set when REC is equal to or greater than 96

TXWARN 

set when TEC is equal to or greater than 96

RXEP 

set when REC is equal to or greater than 128

TXEP 

set when TEC is equal to or greater than 128

TXBO 

set when TEC reaches 255 (bus-off)

RX0OVR 

receiver buffer 0 overflow flag

RX1OVR 

receiver buffer 1 overflow flag

Definition at line 262 of file MCP2515Can.hxx.

◆ Instructions

SPI transaction instructions.

Enumerator
RESET 

resets internal registers to default state

READ 

read data from register at selected address

READ_RX_BUF 

read a receive buffer

WRITE 

write data to register at selected address

LOAD_TX_BUF 

load a transmit buffer

STATUS 

read status

RX_STATUS 

read rx status

RTS 

request to send a tramsmit buffer

BIT_MODIFY 

perform a bit manipulation

Definition at line 275 of file MCP2515Can.hxx.

◆ InterruptFlags

interrupt flag masks

Enumerator
RX0I 

receive buffer 0 interrupt bit

RX1I 

receive buffer 1 interrupt bit

TX0I 

transmit buffer 0 interrupt bit

TX1I 

transmit buffer 1 interrupt bit

TX2I 

transmit buffer 2 interrupt bit

ERRI 

error interrupt bit

WAKI 

wake-up interrupt bit

MERR 

message error interrupt bit

Definition at line 249 of file MCP2515Can.hxx.

◆ Registers

enum MCP2515Can::Registers
private

SPI registers.

Enumerator
TXRTSCTRL 

TX RTS I/O control.

CANSTAT 

status

CANCTRL 

control

TEC 

transmit error counter

RED 

receive error counter

CNF3 

configuration register 3

CNF2 

configuration register 3

CNF1 

configuration register 3

CANINTE 

interrupt enable

CANINTF 

interrupt flags

EFLG 

error flags

Definition at line 113 of file MCP2515Can.hxx.

Constructor & Destructor Documentation

◆ MCP2515Can()

MCP2515Can::MCP2515Can ( const char *  name,
void(*)()  interrupt_enable,
void(*)()  interrupt_disable 
)
inline

Constructor.

Parameters
namename of this device instance in the file system
interrupt_enablecallback to enable the interrupt
interrupt_disablecallback to disable the interrupt

Definition at line 64 of file MCP2515Can.hxx.

◆ ~MCP2515Can()

MCP2515Can::~MCP2515Can ( )
inline

Destructor.

Definition at line 83 of file MCP2515Can.hxx.

Member Function Documentation

◆ bit_modify()

void MCP2515Can::bit_modify ( Registers  address,
uint8_t  data,
uint8_t  mask 
)
inlineprivate

Bit modify to a SPI register.

Parameters
addressaddress to modify
datadata to modify
maskmask of data to modify

Definition at line 613 of file MCP2515Can.hxx.

◆ buffer_read()

void MCP2515Can::buffer_read ( BufferRead buf)
inlineprivate

Read a message to into a receive buffer.

Parameters
bufBuffeRead object to fill in

Definition at line 627 of file MCP2515Can.hxx.

◆ buffer_write()

void MCP2515Can::buffer_write ( BufferWrite buf,
struct can_frame *  can_frame 
)
inlineprivate

Write a message to a transmit buffer.

Parameters
bufBufferWrite object to write out over SPI
can_framereference to a can_frame metadata structure.

Definition at line 640 of file MCP2515Can.hxx.

◆ disable()

void MCP2515Can::disable ( )
overrideprivatevirtual

function to disable device

Implements Can.

Definition at line 171 of file MCP2515Can.cxx.

◆ enable()

void MCP2515Can::enable ( )
overrideprivatevirtual

function to enable device

Implements Can.

Definition at line 150 of file MCP2515Can.cxx.

◆ entry()

void * MCP2515Can::entry ( )
overrideprivatevirtual

User entry point for the created thread.

Returns
exit status entry point to thread

Reimplemented from OSThread.

Definition at line 261 of file MCP2515Can.cxx.

◆ get_spi_bus_lock()

OSMutex * MCP2515Can::get_spi_bus_lock ( )
inline

Return a mutex that can be used by another SPI driver instance sharing the same bus as its bus lock.

Returns
a reference to a mutex that can be used as a bus lock

Definition at line 103 of file MCP2515Can.hxx.

◆ init()

void MCP2515Can::init ( const char *  spi_name,
uint32_t  freq,
uint32_t  baud 
)

Initialize CAN device settings.

Typically called in hw_postinit(), not hw_preinit() or hw_init().

Parameters
spi_namespi interface that the MCP2515Can is on
freqfrequency in Hz that the MCP2515 clock runs at
baudtarget baud rate in Hz

Definition at line 88 of file MCP2515Can.cxx.

◆ interrupt_handler()

void MCP2515Can::interrupt_handler ( )

Handle an interrupt.

Called by user provided interrupt handler.

Definition at line 464 of file MCP2515Can.cxx.

◆ ioctl()

int MCP2515Can::ioctl ( File file,
unsigned long int  key,
unsigned long  data 
)
overrideprivatevirtual

Request an ioctl transaction.

Parameters
filefile reference for this device
keyioctl key
datakey data
Returns
>= 0 upon success, -errno upon failure

Reimplemented from FileIO.

Definition at line 247 of file MCP2515Can.cxx.

◆ register_read()

uint8_t MCP2515Can::register_read ( Registers  address)
inlineprivate

Read from a SPI register.

Parameters
addressaddress to read from
Returns
data read

Definition at line 577 of file MCP2515Can.hxx.

◆ register_write()

void MCP2515Can::register_write ( Registers  address,
uint8_t  data 
)
inlineprivate

Write to a SPI register.

Parameters
addressaddress to write to
datadata to write

Definition at line 596 of file MCP2515Can.hxx.

◆ request_gpio_operation()

void MCP2515Can::request_gpio_operation ( )
inlineprivate

Request that the GPIO cache be refreshed.

Definition at line 651 of file MCP2515Can.hxx.

◆ reset()

void MCP2515Can::reset ( )
inlineprivate

Reset the device.

Definition at line 560 of file MCP2515Can.hxx.

◆ tx_msg()

void MCP2515Can::tx_msg ( )
inlineoverrideprivatevirtual

Function to try and transmit a message.

Implements Can.

Definition at line 541 of file MCP2515Can.hxx.

◆ tx_msg_locked()

void MCP2515Can::tx_msg_locked ( )
private

Function to try and transmit a message while holding a lock.

Definition at line 191 of file MCP2515Can.cxx.

Friends And Related Symbol Documentation

◆ MCP2515GPI

friend class MCP2515GPI
friend

Allow access to MCP2515Can from MCP2515GPI.

Definition at line 685 of file MCP2515Can.hxx.

◆ MCP2515GPO

friend class MCP2515GPO
friend

Allow access to MCP2515Can from MCP2515GPO.

Definition at line 682 of file MCP2515Can.hxx.

Member Data Documentation

◆ BAUD_TABLE

const MCP2515Can::MCP2515Baud MCP2515Can::BAUD_TABLE
staticprivate
Initial value:
=
{
{20000000, 125000, {(5 - 1), (3 - 1)},
{(7 - 1), (4 - 1), 0, 1},
{(4 - 1), 0, 0}},
{8000000, 125000, {(2 - 1), (3 - 1)},
{(7 - 1), (4 - 1), 0, 1},
{(4 - 1), 0, 0}}
}

baud rate settings table

Definition at line 41 of file MCP2515Can.hxx.

◆ gpiData_

unsigned MCP2515Can::gpiData_
private

local copy of the I/O expansion input data

Definition at line 665 of file MCP2515Can.hxx.

◆ gpoData_

unsigned MCP2515Can::gpoData_
private

local copy of the I/O expansion output data

Definition at line 664 of file MCP2515Can.hxx.

◆ interruptDisable_

void(* MCP2515Can::interruptDisable_) ()
private

disable interrupt callback

Definition at line 661 of file MCP2515Can.hxx.

◆ interruptEnable_

void(* MCP2515Can::interruptEnable_) ()
private

enable interrupt callback

Definition at line 660 of file MCP2515Can.hxx.

◆ ioPending_

unsigned MCP2515Can::ioPending_
private

true if an I/O update is pending

Definition at line 666 of file MCP2515Can.hxx.

◆ sem_

OSSem MCP2515Can::sem_
private

semaphore for posting events

Definition at line 669 of file MCP2515Can.hxx.

◆ spi_

SPI* MCP2515Can::spi_
private

pointer to a SPI object instance

Definition at line 668 of file MCP2515Can.hxx.

◆ SPI_MAX_SPEED_HZ

constexpr uint32_t MCP2515Can::SPI_MAX_SPEED_HZ = 10000000
staticconstexprprivate

maximum SPI clock speed in Hz

Definition at line 110 of file MCP2515Can.hxx.

◆ spiFd_

int MCP2515Can::spiFd_
private

SPI bus that accesses MCP2515.

Definition at line 667 of file MCP2515Can.hxx.

◆ state_

unsigned MCP2515Can::state_
private

present bus state

Definition at line 662 of file MCP2515Can.hxx.

◆ txPending_

unsigned MCP2515Can::txPending_
private

transmission in flight

Definition at line 663 of file MCP2515Can.hxx.


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