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)
-
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)
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)
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)
-
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)
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)
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()
function transmit¶
transmit(ch, tx, sid, rtr=False, seq=0)
-
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)
chTarget channel.
function add_filter¶
add_filter(filter, ch, sid, msid)
-
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)
filterfilter number to remove.
function receive¶
receive(ch)
chis the channel to receive from.
Returns sid, msg
function stop_receive¶
stop_receive(ch)
chis the channel to stop receiving from.
function tef_get¶
tef_get()
function en_rx_intr¶
en_rx_intr(ch)
chis the channel to activate the alert on.
function dis_rx_intr¶
dis_rx_intr(ch)
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)