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)
-
nss
is the chip select pin use to communicate with the CAN device. -
spi
is the SPI peripheral connected to the mcp2518. DefaultSPI0
-
spi_clk
is 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)
start
is called. -
cdiv
is the clock division of the CAN internal clock. Clock division parameter follows the table below.cdiv
Clock division 0 1 1 2 2 4 3 10 Default value is 3.
-
iso_crc_en
enables the iso crc check on messages. Default isFalse
. -
tef_en
enables Transmit Event Fifo and its APIs. This allows to keep track the completed transmit events. Default isFalse
. -
txq_en
enables 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
. -
btime
is the data rate of the CAN.btime
parameter follows the table below.btime
Data rate (kbps) 0 500 10 250 15 1000 17 125 Default value is 10.
-
sysclk
is the CAN internal clock speed.sysclk
follows the table below.sysclk
Clock speed (MHz) 0 40 1 20 2 10 Default value is 0.
function tef_conf
¶
tef_conf(queue_size)
start
is called. queue_size
is the size of the Transmit Event Fifo. The TEF will keep track of the lastqueue_size
completed transmissions.
function txq_conf
¶
txq_conf(queue_size, prio, tx_attempts=0)
-
queue_size
is the size of the Transmit Queue. -
prio
is the TXQ channel priority. Lower value -> higher priority. -
tx_attempts
is the number of retransmission attempts if transmissions are not successful. This follows the table below.tx_attempts
Retransmissions 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)
start
is called. -
ch
is the channel to configure as TXF. Possible channels go from 1 to 32. -
queue_size
is the size of the Transmit Fifo. -
prio
is the TXF channel priority. Lower value -> higher priority. -
rtr_en
enables the remote request frames. Default isFalse
. -
tx_attempts
is the number of retransmission attempts if transmissions are not successful. This follows the table below.tx_attempts
Retransmissions 0 Disabled 1 3 2 Unlimited Default value is 0.
function rxf_conf
¶
rx_conf(ch, queue_size)
start
is called. -
ch
is the channel to configure as RXF. Possible channels go from 1 to 32. -
queue_size
is the size of the Receive Fifo.
function start
¶
start()
function transmit
¶
transmit(ch, tx, sid, rtr=False, seq=0)
-
ch
is the target TXF channel. -
tx
is the message to transmit. Max size 8 bytes. -
sid
is the message standard 11b ID. -
rtr
defines if the message is a RTR. Default isFalse
. -
seq
is 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)
ch
Target channel.
function add_filter
¶
add_filter(filter, ch, sid, msid)
-
filter
is the number of the filter. Up to 32 filters. -
ch
is the RXF channel to link the filter to. -
sid
is the filtered standard ID. -
msid
is the mask of ignored bits or the filter.
function rm_filter
¶
rm_filter(filter)
filter
filter number to remove.
function receive
¶
receive(ch)
ch
is the channel to receive from.
Returns sid, msg
function stop_receive
¶
stop_receive(ch)
ch
is the channel to stop receiving from.
function tef_get
¶
tef_get()
function en_rx_intr
¶
en_rx_intr(ch)
ch
is the channel to activate the alert on.
function dis_rx_intr
¶
dis_rx_intr(ch)
ch
is 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)