34#ifndef _FREERTOS_DRIVERS_COMMON_MCP2515CAN_HXX_
35#define _FREERTOS_DRIVERS_COMMON_MCP2515CAN_HXX_
48#define MCP2515_DEBUG 0
49#define MCP2515_NULL_TX 0
65 void (*interrupt_enable)(),
void (*interrupt_disable)())
93 void init(
const char *spi_name, uint32_t freq, uint32_t baud);
321 Config2(uint8_t prseg, uint8_t prseg1, uint8_t sam, uint8_t bltmode)
350 Config3(uint8_t phseg2, uint8_t wakfil, uint8_t sof)
391 __attribute__((optimize(
"-O3")))
394 can_frame->can_eff =
exide_;
398 can_frame->can_rtr =
rtr_;
399 can_frame->can_id = ((uint32_t)
eid0_ << 0) +
400 ((uint32_t)
eid8_ << 8) +
401 ((uint32_t)
eid_ << 16) +
402 ((uint32_t)
sid_ << 18) +
403 ((uint32_t)
sidh_ << 21);
408 can_frame->can_rtr =
srr_;
409 can_frame->can_id = ((uint32_t)
sid_ << 0) +
410 ((uint32_t)
sidh_ << 3);
412 can_frame->can_err = 0;
413 can_frame->can_dlc =
dlc_;
414 static_assert(offsetof(
Buffer,
data_) == 8,
"data_ misaligned");
431 __attribute__((optimize(
"-O3")))
432 Buffer(struct can_frame *can_frame, uint8_t command)
434 ,
dlc_(can_frame->can_dlc)
435 ,
rtr_(can_frame->can_rtr)
437 if (LIKELY(can_frame->can_eff))
439 sidh_ = (can_frame->can_id & 0x1FE00000) >> 21;
440 eid_ = (can_frame->can_id & 0x00030000) >> 16;
442 sid_ = (can_frame->can_id & 0x001C0000) >> 18;
443 eid8_ = (can_frame->can_id & 0x0000FF00) >> 8;
444 eid0_ = (can_frame->can_id & 0x000000FF) >> 0;
448 sidh_ = (can_frame->can_id & 0x000007F8) >> 3;
451 sid_ = (can_frame->can_id & 0x000007F8) >> 3;
455 static_assert(offsetof(
Buffer,
data_) == 8,
"data_ misaligned");
529 int ioctl(
File *file,
unsigned long int key,
unsigned long data)
override;
534 void *
entry()
override;
540 __attribute__((optimize(
"-O3")))
552 portENTER_CRITICAL();
562 spi_ioc_transfer xfer;
565 xfer.tx_buf = (
unsigned long)&
reset;
567 xfer.len =
sizeof(
reset);
576 __attribute__((optimize(
"-O3")))
579 spi_ioc_transfer xfer;
580 uint8_t data[3] = {
READ, address, 0};
582 xfer.tx_buf = (
unsigned long)data;
583 xfer.rx_buf = (
unsigned long)data;
584 xfer.len =
sizeof(data);
595 __attribute__((optimize(
"-O3")))
598 spi_ioc_transfer xfer;
599 uint8_t payload[] = {
WRITE, address, data};
601 xfer.tx_buf = (
unsigned long)payload;
603 xfer.len =
sizeof(payload);
615 spi_ioc_transfer xfer;
616 uint8_t payload[] = {
BIT_MODIFY, address, mask, data};
618 xfer.tx_buf = (
unsigned long)payload;
620 xfer.len =
sizeof(payload);
629 spi_ioc_transfer xfer;
642 spi_ioc_transfer xfer;
646 spi_->SPI::transfer_with_cs_assert_polled(&xfer);
671 volatile uint8_t regs_[128];
712 new_state ?
set() :
clr();
729 portENTER_CRITICAL();
742 portENTER_CRITICAL();
754 HASSERT(dir == Gpio::Direction::DOUTPUT);
762 return Gpio::Direction::DOUTPUT;
806 HASSERT(dir == Gpio::Direction::DINPUT);
814 return Gpio::Direction::DINPUT;
Base class for a CAN device for the Arduino environment.
const char * name
device name
OS-independent abstraction for GPIO.
Value
Defines the options for GPIO level.
Direction
Defines the options for GPIO direction.
Setup a buffer read transfer structure.
BufferRead(int index)
Constructor.
Setup a buffer read transfer structure.
BufferWrite(int index, struct can_frame *can_frame)
Constructor.
CAN TX and RX buffer structure.
uint8_t eid_
extended identifier bits 16 and 17
uint8_t unused4_
unused bit
void * get_payload()
Get a pointer to the buffer payload.
Buffer(uint8_t command)
Constructor.
uint8_t pad_[2]
force the data to 32/64-bit allign
uint8_t eid8_
extended identifier high byte
uint8_t exide_
extended identifer enable
uint8_t unused3_
unused bits
uint8_t eid0_
extended identifier low byte
uint8_t sidh_
standard identifier high byte
uint64_t data64_
64-bit data
uint8_t sid_
standard identifier low bits
uint8_t dlc_
data length code
uint8_t unused1_
unused bit
static const size_t TRANSFER_SIZE
The SPI transfer size in bytes of a buffer transfer.
uint8_t data_[8]
all 8 data bytes
uint8_t rtr_
remote transmit request bit
void build_struct_can_frame(struct can_frame *can_frame)
Take the contents of the Buffer and fill in a can_frame structure.
uint8_t command_
the transaction command
uint8_t srr_
standard frame RTR
Specialization of CAN driver for Tiva CAN.
unsigned gpoData_
local copy of the I/O expansion output data
MCP2515Can()
Default constructor.
@ CNF2
configuration register 3
@ TEC
transmit error counter
@ CNF3
configuration register 3
@ RED
receive error counter
@ CNF1
configuration register 3
@ CANINTE
interrupt enable
@ TXRTSCTRL
TX RTS I/O control.
InterruptFlags
interrupt flag masks
@ TX0I
transmit buffer 0 interrupt bit
@ ERRI
error interrupt bit
@ RX0I
receive buffer 0 interrupt bit
@ RX1I
receive buffer 1 interrupt bit
@ WAKI
wake-up interrupt bit
@ TX2I
transmit buffer 2 interrupt bit
@ TX1I
transmit buffer 1 interrupt bit
@ MERR
message error interrupt bit
int ioctl(File *file, unsigned long int key, unsigned long data) override
Request an ioctl transaction.
void request_gpio_operation()
Request that the GPIO cache be refreshed.
SPI * spi_
pointer to a SPI object instance
void buffer_read(BufferRead *buf)
Read a message to into a receive buffer.
MCP2515Can(const char *name, void(*interrupt_enable)(), void(*interrupt_disable)())
Constructor.
void register_write(Registers address, uint8_t data)
Write to a SPI register.
static constexpr uint32_t SPI_MAX_SPEED_HZ
maximum SPI clock speed in Hz
unsigned gpiData_
local copy of the I/O expansion input data
OSSem sem_
semaphore for posting events
void tx_msg() override
Function to try and transmit a message.
unsigned state_
present bus state
void(* interruptEnable_)()
enable interrupt callback
void bit_modify(Registers address, uint8_t data, uint8_t mask)
Bit modify to a SPI register.
void tx_msg_locked()
Function to try and transmit a message while holding a lock.
void buffer_write(BufferWrite *buf, struct can_frame *can_frame)
Write a message to a transmit buffer.
void init(const char *spi_name, uint32_t freq, uint32_t baud)
Initialize CAN device settings.
ControlFields
Fields of the control register.
@ REQOP
request operation mode
@ CLKPRE
clockout pin prescaler
@ CLKEN
clockout pin enable
@ ABAT
abort all pending transmissions
unsigned ioPending_
true if an I/O update is pending
OSMutex * get_spi_bus_lock()
Return a mutex that can be used by another SPI driver instance sharing the same bus as its bus lock.
int spiFd_
SPI bus that accesses MCP2515.
void enable() override
function to enable device
ErrorFlags
interrupt flag masks
@ TXBO
set when TEC reaches 255 (bus-off)
@ TXWARN
set when TEC is equal to or greater than 96
@ RXWARN
set when REC is equal to or greater than 96
@ RXEP
set when REC is equal to or greater than 128
@ EWARN
set when TEC or REC is equal to or greater than 96
@ RX0OVR
receiver buffer 0 overflow flag
@ TXEP
set when TEC is equal to or greater than 128
@ RX1OVR
receiver buffer 1 overflow flag
void(* interruptDisable_)()
disable interrupt callback
void disable() override
function to disable device
void interrupt_handler()
Handle an interrupt.
Instructions
SPI transaction instructions.
@ READ_RX_BUF
read a receive buffer
@ RX_STATUS
read rx status
@ READ
read data from register at selected address
@ RESET
resets internal registers to default state
@ LOAD_TX_BUF
load a transmit buffer
@ WRITE
write data to register at selected address
@ BIT_MODIFY
perform a bit manipulation
@ RTS
request to send a tramsmit buffer
uint8_t register_read(Registers address)
Read from a SPI register.
void reset()
Reset the device.
unsigned txPending_
transmission in flight
static const MCP2515Baud BAUD_TABLE[]
baud rate settings table
void * entry() override
User entry point for the created thread.
General Purpose Input (GPI) instance on the MCP2515.
uint8_t bit_
bit number representative of the bit
MCP2515Can * instance_
reference to chip instance
Value read() const override
Retrieves the current Value of a GPI input pin.
void set_direction(Gpio::Direction dir) const override
Sets the GPI direction (does nothing).
Direction direction() const override
Gets the GPI direction.
MCP2515GPI(MCP2515Can *instance, uint8_t bit)
Constructor.
General Purpose Output (GPO) instance on the MCP2515.
MCP2515Can * instance_
reference to chip instance
Value read() const override
Retrieves the current Value of a GPO output sate (requested).
void set() const override
Sets the GPO pin to high.
MCP2515GPO(MCP2515Can *instance, uint8_t bit)
Constructor.
Direction direction() const override
Gets the GPO direction.
void clr() const override
Clears the GPO pin to low.
void set_direction(Gpio::Direction dir) const override
Sets the GPO direction (does nothing).
void write(Value new_state) const override
Writes a GPO pin (set or clear to a specific state).
uint8_t bit_
bit number representative of the bit
OSMutex lock_
protects internal structures.
This class provides a mutex API.
void unlock()
Unlock a mutex.
This class provides a counting semaphore API.
void post()
Post (increment) a semaphore.
This class provides a threading API.
Private data for an SPI device.
int transfer_with_cs_assert_polled(struct spi_ioc_transfer *msgs, int num=1)
Method to transmit/receive the data.
#define CAN_STATE_STOPPED
CAN bus stopped.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Configuration resgister 1 structure.
uint8_t sjw_
synchronization jump width length bits
uint8_t data_
raw data for register
uint8_t brp_
baud rate prescaler bits
Config1(uint8_t brp, uint8_t sjw)
Constructor.
Configuration resgister 2 structure.
uint8_t bltmode_
PS2 bit time length bit.
uint8_t sam_
sample point configuration bit
uint8_t data_
raw data for register
uint8_t prseg1_
PS1 length bits.
uint8_t prseg_
propagation segment length bits
Config2(uint8_t prseg, uint8_t prseg1, uint8_t sam, uint8_t bltmode)
Constructor.
Configuration resgister 3 structure.
uint8_t phseg2_
PS2 length bits.
Config3(uint8_t phseg2, uint8_t wakfil, uint8_t sof)
Constructor.
uint8_t wakfil_
wake-up filter bit
uint8_t sof_
start of frame signal bit
uint8_t unused_
unused bits
uint8_t data_
raw data for register
uint32_t baud_
target baud rate
Config3 cnf3_
Configuration registers CNF3.
Config2 cnf2_
Configuration registers CNF2.
uint32_t freq_
incoming frequency
Config1 cnf1_
Configuration registers CNF1.