CAN
This module loads the Controller Area Network (CAN) driver of the mcp2518.
A Controller Area Network (CAN bus) is a robust vehicle bus standard designed to allow microcontrollers and devices to communicate with each other’s applications without a host computer. It is a message-based protocol, designed originally for multiplex electrical wiring within automobiles to save on copper, but can also be used in many other contexts. For each device the data in a frame is transmitted sequentially but in such a way that if more than one device transmits at the same time the highest priority device is able to continue while the others back off. Frames are received by all devices, including by the transmitting device.
Only CAN2.0 is supported right now.
function init
init(nss, spi=SPI0, spi_clk=15000000)
Initialize the mcp2518 CAN controlled by the SPI peripheral. This function must be called to be able to use other CAN's APIs.
-
nssis the chip select pin use to communicate with the CAN device. -
spiis the SPI peripheral connected to the mcp2518. DefaultSPI0 -
spi_clkis the clock speed of the SPI.
function conf
conf(cdiv=3, iso_crc_en=False, tef_en=False, txq_en=False, btime=10, sysclk=0)
Configure main parameter of the CAN communication. This includes clock speed, crcs and if Transmit Event Fifo and Transmit Queue channels are active.
Must be called before start is called.
-
cdivis the clock division of the CAN internal clock. Clock division parameter follows the table below.cdivClock division 0 1 1 2 2 4 3 10 Default value is 3.
-
iso_crc_enenables the iso crc check on messages. Default isFalse. -
tef_enenables Transmit Event Fifo and its APIs. This allows to keep track the completed transmit events. Default isFalse. -
txq_enenables the Transmit Queue. This will always be on channel 0. Diffrently from the Transmit Fifos, the Queue will transmit messages following ID priority (lower ID -> higher priority). Default isFalse. -
btimeis the data rate of the CAN.btimeparameter follows the table below.btimeData rate (kbps) 0 500 10 250 15 1000 17 125 Default value is 10.
-
sysclkis the CAN internal clock speed.sysclkfollows the table below.sysclkClock speed (MHz) 0 40 1 20 2 10 Default value is 0.
function tef_conf
tef_conf(queue_size)
Configure the Transmit Event Fifo.
Must be called before start is called.
queue_sizeis the size of the Transmit Event Fifo. The TEF will keep track of the lastqueue_sizecompleted transmissions.
function txq_conf
txq_conf(queue_size, prio, tx_attempts=0)
Configure the Transmit Queue. This will be always configured to channel 0, which is reserved for it. Diffrently from the Transmit Fifos, the Queue will transmit messages following ID priority (lower ID -> higher priority).
-
queue_sizeis the size of the Transmit Queue. -
priois the TXQ channel priority. Lower value -> higher priority. -
tx_attemptsis the number of retransmission attempts if transmissions are not successful. This follows the table below.tx_attemptsRetransmissions 0 Disabled 1 3 2 Unlimited Default value is 0.
function txf_conf
txf_conf(ch, queue_size, prio, rtr_en=False, tx_attempts=0)
Configure a Transmit Fifo Channel. More than one channel can be configured as TXF.
Must be called before start is called.
-
chis the channel to configure as TXF. Possible channels go from 1 to 32. -
queue_sizeis the size of the Transmit Fifo. -
priois the TXF channel priority. Lower value -> higher priority. -
rtr_enenables the remote request frames. Default isFalse. -
tx_attemptsis the number of retransmission attempts if transmissions are not successful. This follows the table below.tx_attemptsRetransmissions 0 Disabled 1 3 2 Unlimited Default value is 0.
function rxf_conf
rx_conf(ch, queue_size)
Configure a Receive Fifo Channel. More than one channel can be configured as RXF.
Must be called before start is called.
-
chis the channel to configure as RXF. Possible channels go from 1 to 32. -
queue_sizeis the size of the Receive Fifo.
function start
start()
Starts the configured CAN channels. If configured channels require to much RAM of the CAN, PERIPHERAL_ERROR exception is raised. Try to reduce queue sizes is this case to reduce RAM requirements.
function transmit
transmit(ch, tx, sid, rtr=False, seq=0)
Send a message on a target TXF channel. Tx messages are automatically transmitted by the Fifo.
-
chis the target TXF channel. -
txis the message to transmit. Max size 8 bytes. -
sidis the message standard 11b ID. -
rtrdefines if the message is a RTR. Default isFalse. -
seqis the sequence number of the message. Default is 0.
If the ch queue is full, PERIPHERAL_ERROR exception is raised.
function stop_transmit
stop_transmit(ch)
Stop transmissions on a target channel.
chTarget channel.
function add_filter
add_filter(filter, ch, sid, msid)
Add a filter on received messages ID on a specified channel. Each RXF channel requires an active filter.
-
filteris the number of the filter. Up to 32 filters. -
chis the RXF channel to link the filter to. -
sidis the filtered standard ID. -
msidis the mask of ignored bits or the filter.
function rm_filter
rm_filter(filter)
Removes a target filter. This
filterfilter number to remove.
function receive
receive(ch)
Receive a message from a RXF channel if the channel is not empty.
chis the channel to receive from.
Returns sid, msg
function stop_receive
stop_receive(ch)
Stop receiving from a RXF channel.
chis the channel to stop receiving from.
function tef_get
tef_get()
Get the next messege from the Transmit Event Fifo.
function en_rx_intr
en_rx_intr(ch)
Enables the alert pin for a specific channel RX channel. If the target channel will receive a message, the alert pin will be lowered.
chis the channel to activate the alert on.
function dis_rx_intr
dis_rx_intr(ch)
Disable the alert pin for a specific RX channel.
chis the channel to deactivate.
Example
from bsp import board
import can
nss_can_pin = D10
tx_ch = 1
can.init(nss_can_pin)
can.conf()
can.txf_conf(tx_ch, 5, 1)
can.start()
tx_buff = bytearray("Test msg")
can.transmit(tx_ch, tx_buff, 0x300)