CANSPI Library
The SPI module is available with a number of the dsPIC30/33 and PIC24 MCUs. The mikroBasic PRO for dsPIC30/33 and PIC24 provides a library (driver) for working with mikroElektronika's CANSPI Add-on boards (with MCP2515 or MCP2510) via SPI interface.
The CAN is a very robust protocol that has error detection and signalization, self–checking and fault confinement. Faulty CAN data and remote frames are re-transmitted automatically, similar to the Ethernet.
In the mikroBasic PRO for dsPIC30/33 and PIC24, each routine of the CAN library has its own CANSPI counterpart with identical syntax. For more information on Controller Area Network, consult the CAN Library. Note that an effective communication speed depends on SPI and certainly is slower than “real” CAN.
CAN supports two message formats:
- Standard format, with 11 identifier bits and
- Extended format, with 29 identifier bits

- Consult the CAN standard about CAN bus termination resistance.
- An effective CANSPI communication speed depends on SPI and certainly is slower than “real” CAN.
- The library uses the SPI module for communication. User must initialize appropriate SPI module before using the CANSPI Library.
- For MCUs with multiple SPI modules it is possible to initialize both of them and then switch by using the SPI_Set_Active routine.
- Number of SPI modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
- CANSPI module refers to mikroElektronika's CANSPI Add-on board connected to SPI module of MCU.
Library Dependency Tree

External dependencies of CANSPI Library
The following variables must be defined in all projects using CANSPI Library: | Description : | Example : |
---|---|---|
dim CanSpi_CS as sbit sfr external |
Chip Select line. | dim CanSpi_CS as sbit at LATF0_bit |
dim CanSpi_Rst as sbit sfr external |
Reset line. | dim CanSpi_Rst as sbit at LATF1_bit |
dim CanSpi_CS_Direction as sbit sfr external |
Direction of the Chip Select pin. | dim CanSpi_CS_Direction as sbit at TRISF0_bit |
dim CanSpi_Rst_Direction as sbit sfr external |
Direction of the Reset pin. | dim CanSpi_Rst_Direction as sbit at TRISF1_bit |
Library Routines
- CANSPISetOperationMode
- CANSPIGetOperationMode
- CANSPIInit
- CANSPISetBaudRate
- CANSPISetMask
- CANSPISetFilter
- CANSPIRead
- CANSPIWrite
CANSPISetOperationMode
Prototype |
sub procedure CANSPISetOperationMode(dim mode as byte, dim WAIT as byte) |
---|---|
Description |
Sets the CANSPI module to requested mode. |
Parameters |
|
Returns |
Nothing. |
Requires |
The CANSPI routines are supported only by MCUs with the SPI module. MCU has to be properly connected to mikroElektronika's CANSPI Extra Board or similar hardware. See connection example at the bottom of this page. |
Example |
' set the CANSPI module into configuration mode (wait inside CANSPISetOperationMode until this mode is set) CANSPISetOperationMode(_CANSPI_MODE_CONFIG, 0xFF) |
Notes |
None. |
CANSPIGetOperationMode
Prototype |
sub function CANSPIGetOperationMode() as byte |
---|---|
Description |
The function returns current operation mode of the CANSPI module. Check CANSPI_OP_MODE constants or device datasheet for operation mode codes. |
Parameters |
None. |
Returns |
Current operation mode. |
Requires |
The CANSPI routines are supported only by MCUs with the SPI module. MCU has to be properly connected to mikroElektronika's CANSPI Extra Board or similar hardware. See connection example at the bottom of this page. |
Example |
' check whether the CANSPI module is in Normal mode and if it is do something. if (CANSPIGetOperationMode() = _CANSPI_MODE_NORMAL) then ... end if |
Notes |
None. |
CANSPIInit
Prototype |
sub procedure CANSPIInit(dim SJW, BRP, PHSEG1, PHSEG2, PROPSEG, CANSPI_CONFIG_FLAGS as char) |
---|---|
Description |
Initializes the CANSPI module. Stand-Alone CAN controller in the CANSPI module is set to:
|
Parameters |
|
Returns |
Nothing. |
Requires |
Global variables :
The CANSPI routines are supported only by MCUs with the SPI module. The SPI module needs to be initialized. See the SPIx_Init and SPIx_Init_Advanced routines. MCU has to be properly connected to mikroElektronika's CANSPI Extra Board or similar hardware. See connection example at the bottom of this page. |
Example |
' CANSPI module connections dim CanSpi_CS as sbit at LATF0_bit CanSpi_CS_Direction as sbit at TRISF0_bit CanSpi_Rst as sbit at LATF12_bit CanSpi_Rst_Direction as sbit at TRISF1_bit ' End CANSPI module connections dim CANSPI_Init_Flags as word ... CANSPI_Init_Flags = _CANSPI_CONFIG_SAMPLE_THRICE and _CANSPI_CONFIG_PHSEG2_PRG_ON and _CANSPI_CONFIG_STD_MSG and _CANSPI_CONFIG_DBL_BUFFER_ON and _CANSPI_CONFIG_VALID_XTD_MSG and _CANSPI_CONFIG_LINE_FILTER_OFF ... SPI1_Init() ' initialize SPI1 module CANSPI(1,3,3,3,1,CANSPI_Init_Flags) ' initialize CANSPI |
Notes |
|
CANSPISetBaudRate
Prototype |
sub procedure CANSPISetBaudRate(dim SJW, BRP, PHSEG1, PHSEG2, PROPSEG, CANSPI_CONFIG_FLAGS as byte) |
---|---|
Returns |
Nothing. |
Description |
Sets the CANSPI module baud rate. Due to complexity of the CAN protocol, you can not simply force a bps value. Instead, use this function when the CANSPI module is in Config mode.
|
Parameters |
|
Returns |
Nothing. |
Requires |
The CANSPI module must be in Config mode, otherwise the function will be ignored. See CANSPISetOperationMode. The CANSPI routines are supported only by MCUs with the SPI module. MCU has to be properly connected to mikroElektronika's CANSPI Extra Board or similar hardware. See connection example at the bottom of this page. |
Example |
' set required baud rate and sampling rules dim CANSPI_CONFIG_FLAGS as byte ... CANSPISetOperationMode(_CANSPI_MODE_CONFIG,0xFF) ' set CONFIGURATION mode (CANSPI1 module mast be in config mode for baud rate settings) CANSPI_CONFIG_FLAGS = _CANSPI_CONFIG_SAMPLE_THRICE and _CANSPI_CONFIG_PHSEG2_PRG_ON and _CANSPI_CONFIG_STD_MSG and _CANSPI_CONFIG_DBL_BUFFER_ON and _CANSPI_CONFIG_VALID_XTD_MSG and _CANSPI_CONFIG_LINE_FILTER_OFF CANSPISetBaudRate(1, 1, 3, 3, 1, CANSPI_CONFIG_FLAGS) |
Notes |
None. |
CANSPISetMask
Prototype |
sub procedure CANSPISetMask(dim CANSPI_MASK as byte, dim val as longint, dim CANSPI_CONFIG_FLAGS as byte) |
---|---|
Description |
Configures mask for advanced filtering of messages. The parameter |
Parameters |
|
Returns |
Nothing. |
Requires |
The CANSPI module must be in Config mode, otherwise the function will be ignored. See CANSPISetOperationMode. The CANSPI routines are supported only by MCUs with the SPI module. MCU has to be properly connected to mikroElektronika's CANSPI Extra Board or similar hardware. See connection example at the bottom of this page. |
Example |
' set the appropriate filter mask and message type value CANSPISetOperationMode(_CANSPI_MODE_CONFIG,0xFF) ' set CONFIGURATION mode (CANSPI1 module must be in config mode for mask settings) ' Set all B1 mask bits to 1 (all filtered bits are relevant): ' Note that -1 is just a cheaper way to write 0xFFFFFFFF. ' Complement will do the trick and fill it up with ones. CANSPISetMask(_CANSPI_MASK_B1, -1, _CANSPI_CONFIG_MATCH_MSG_TYPE and _CANSPI_CONFIG_XTD_MSG) |
Notes |
None. |
CANSPISetFilter
Prototype |
sub procedure CANSPISetFilter(dim CAN_FILTER as byte, dim val as longint, dim CANSPI_CONFIG_FLAGS as byte) |
---|---|
Description |
Configures message filter. The parameter |
Parameters |
|
Returns |
Nothing. |
Requires |
The CANSPI module must be in Config mode, otherwise the function will be ignored. See CANSPISetOperationMode. The CANSPI routines are supported only by MCUs with the SPI module. MCU has to be properly connected to mikroElektronika's CANSPI Extra Board or similar hardware. See connection example at the bottom of this page. |
Example |
' set the appropriate filter value and message type CANSPI1SetOperationMode(_CANSPI_MODE_CONFIG,0xFF) ' set CONFIGURATION mode (CANSPI1 module must be in config mode for filter settings) ' Set id of filter B1_F1 to 3: CANSPI1SetFilter(_CANSPI_FILTER_B1_F1, 3, _CANSPI_CONFIG_XTD_MSG) |
Notes |
None. |
CANSPIRead
Prototype |
sub function CANSPIRead(dim byref id as longint, dim byref Data_ as byte[8], dim byref DataLen as byte, dim byref CAN_RX_MSG_FLAGS as byte) as byte |
---|---|
Description |
If at least one full Receive Buffer is found, it will be processed in the following way:
|
Parameters |
|
Returns |
|
Requires |
The CANSPI module must be in a mode in which receiving is possible. See CANSPIxSetOperationMode. The CANSPI routines are supported only by MCUs with the SPI module. MCU has to be properly connected to mikroElektronika's CANSPI Extra Board or similar hardware. See connection example at the bottom of this page. |
Example |
' check the CANSPI1 module for received messages. If any was received do something. dim msg_rcvd, rx_flags, data_len as byte data as byte[8] msg_id as longint ... CANSPISetOperationMode(_CANSPI_MODE_NORMAL,0xFF) ' set NORMAL mode (CANSPI1 module must be in mode in which receive is possible) ... rx_flags = 0 ' clear message flags if (msg_rcvd = CANSPIRead(msg_id, data, data_len, rx_flags)) then ... end if |
Notes |
None. |
CANSPIWrite
Prototype |
sub function CANSPIWrite(dim id as longint, dim byref Data_ as byte[8], dim DataLen, CANSPI_TX_MSG_FLAGS as byte) as byte |
---|---|
Description |
If at least one empty Transmit Buffer is found, the function sends message in the queue for transmission. |
Parameters |
|
Returns |
|
Requires |
The CANSPI module must be in mode in which transmission is possible. See CANSPISetOperationMode. The CANSPI routines are supported only by MCUs with the SPI module. MCU has to be properly connected to mikroElektronika's CANSPI Extra Board or similar hardware. See connection example at the bottom of this page. |
Example |
' send message extended CAN message with the appropriate ID and data dim tx_flags as byte data as byte[8] msg_id as longint ... CANSPISetOperationMode(_CAN_MODE_NORMAL,0xFF) ' set NORMAL mode (CANSPI1 must be in mode in which transmission is possible) tx_flags = _CANSPI_TX_PRIORITY_0 and _CANSPI_TX_XTD_FRAME ' set message flags CANSPIWrite(msg_id, data, 2, tx_flags) |
Notes |
None. |
CANSPI Constants
There is a number of constants predefined in the CANSPI library. You need to be familiar with them in order to be able to use the library effectively. Check the example at the end of the chapter.
CANSPI_OP_MODE Constants
The CANSPI_OP_MODE
constants define CANSPI operation mode. Function CANSPISetOperationMode expects one of these as it's argument:
const _CANSPI_MODE_BITS as byte = $E0 Use this to access opmode bits _CANSPI_MODE_NORMAL as byte = 0 _CANSPI_MODE_SLEEP as byte = $20 _CANSPI_MODE_LOOP as byte = $40 _CANSPI_MODE_LISTEN as byte = $60 _CANSPI_MODE_CONFIG as byte = $80
CANSPI_CONFIG_FLAGS Constants
The CANSPI_CONFIG_FLAGS
constants define flags related to the CANSPI module configuration. The functions CANSPIInit, CANSPISetBaudRate, CANSPISetMask and CANSPISetFilter expect one of these (or a bitwise combination) as their argument:
const _CANSPI_CONFIG_DEFAULT as byte = $FF ' 11111111 _CANSPI_CONFIG_PHSEG2_PRG_BIT as byte = $01 _CANSPI_CONFIG_PHSEG2_PRG_ON as byte = $FF ' XXXXXXX1 _CANSPI_CONFIG_PHSEG2_PRG_OFF as byte = $FE ' XXXXXXX0 _CANSPI_CONFIG_LINE_FILTER_BIT as byte = $02 _CANSPI_CONFIG_LINE_FILTER_ON as byte = $FF ' XXXXXX1X _CANSPI_CONFIG_LINE_FILTER_OFF as byte = $FD ' XXXXXX0X _CANSPI_CONFIG_SAMPLE_BIT as byte = $04 _CANSPI_CONFIG_SAMPLE_ONCE as byte = $FF ' XXXXX1XX _CANSPI_CONFIG_SAMPLE_THRICE as byte = $FB ' XXXXX0XX _CANSPI_CONFIG_MSG_TYPE_BIT as byte = $08 _CANSPI_CONFIG_STD_MSG as byte = $FF ' XXXX1XXX _CANSPI_CONFIG_XTD_MSG as byte = $F7 ' XXXX0XXX _CANSPI_CONFIG_DBL_BUFFER_BIT as byte = $10 _CANSPI_CONFIG_DBL_BUFFER_ON as byte = $FF ' XXX1XXXX _CANSPI_CONFIG_DBL_BUFFER_OFF as byte = $EF ' XXX0XXXX _CANSPI_CONFIG_MSG_BITS as byte = $60 _CANSPI_CONFIG_ALL_MSG as byte = $FF ' X11XXXXX _CANSPI_CONFIG_VALID_XTD_MSG as byte = $DF ' X10XXXXX _CANSPI_CONFIG_VALID_STD_MSG as byte = $BF ' X01XXXXX _CANSPI_CONFIG_ALL_VALID_MSG as byte = $9F ' X00XXXXX
You may use bitwise and
to form config byte out of these values. For example:
init = _CANSPI_CONFIG_SAMPLE_THRICE and _CANSPI_CONFIG_PHSEG2_PRG_ON and _CANSPI_CONFIG_STD_MSG and _CANSPI_CONFIG_DBL_BUFFER_ON and _CANSPI_CONFIG_VALID_XTD_MSG and _CANSPI_CONFIG_LINE_FILTER_OFF ... CANSPIInit(1, 1, 3, 3, 1, init) ' initialize CANSPI
CANSPI_TX_MSG_FLAGS Constants
CANSPI_TX_MSG_FLAGS
are flags related to transmission of a CANSPI message:
const _CANSPI_TX_PRIORITY_BITS as byte = $03 _CANSPI_TX_PRIORITY_0 as byte = $FC ' XXXXXX00 _CANSPI_TX_PRIORITY_1 as byte = $FD ' XXXXXX01 _CANSPI_TX_PRIORITY_2 as byte = $FE ' XXXXXX10 _CANSPI_TX_PRIORITY_3 as byte = $FF ' XXXXXX11 _CANSPI_TX_FRAME_BIT as byte = $08 _CANSPI_TX_STD_FRAME as byte = $FF ' XXXXX1XX _CANSPI_TX_XTD_FRAME as byte = $F7 ' XXXXX0XX _CANSPI_TX_RTR_BIT as byte = $40 _CANSPI_TX_NO_RTR_FRAME as byte = $FF ' X1XXXXXX _CANSPI_TX_RTR_FRAME as byte = $BF ' X0XXXXXX
You may use bitwise and
to adjust the appropriate flags. For example:
' form value to be used with CANSendMessage: send_config = _CANSPI_TX_PRIORITY_0 and _CANSPI_TX_XTD_FRAME and _CANSPI_TX_NO_RTR_FRAME ... CANSPIWrite(id, data, 1, send_config)
CANSPI_RX_MSG_FLAGS Constants
CANSPI_RX_MSG_FLAGS
are flags related to reception of CANSPI message. If a particular bit is set then corresponding meaning is TRUE or else it will be FALSE.
const _CANSPI_RX_FILTER_BITS as byte = $07 ' Use this to access filter bits _CANSPI_RX_FILTER_1 as byte = $00 _CANSPI_RX_FILTER_2 as byte = $01 _CANSPI_RX_FILTER_3 as byte = $02 _CANSPI_RX_FILTER_4 as byte = $03 _CANSPI_RX_FILTER_5 as byte = $04 _CANSPI_RX_FILTER_6 as byte = $05 _CANSPI_RX_OVERFLOW as byte = $08 ' Set if Overflowed else cleared _CANSPI_RX_INVALID_MSG as byte = $10 ' Set if invalid else cleared _CANSPI_RX_XTD_FRAME as byte = $20 ' Set if XTD message else cleared _CANSPI_RX_RTR_FRAME as byte = $40 ' Set if RTR message else cleared _CANSPI_RX_DBL_BUFFERED as byte = $80 ' Set if this message was hardware double-buffered
You may use bitwise and
to adjust the appropriate flags. For example:
if (MsgFlag and _CANSPI_RX_OVERFLOW) <> 0 then ... ' Receiver overflow has occurred. ' We have lost our previous message. end if
CANSPI_MASK Constants
The CANSPI_MASK
constants define mask codes. Function CANSPISetMask expects one of these as it's argument:
const _CANSPI_MASK_B1 as byte = 0 _CANSPI_MASK_B2 as byte = 1
CANSPI_FILTER Constants
The CANSPI_FILTER
constants define filter codes. Functions
CANSPISetFilter expects one of these as
it's argument:
const _CANSPI_FILTER_B1_F1 as byte = 0 _CANSPI_FILTER_B1_F2 as byte = 1 _CANSPI_FILTER_B2_F1 as byte = 2 _CANSPI_FILTER_B2_F2 as byte = 3 _CANSPI_FILTER_B2_F3 as byte = 4 _CANSPI_FILTER_B2_F4 as byte = 5
Library Example
The code is a simple demonstration of CANSPI protocol. This node initiates the communication with the 2nd node by sending some data to its address. The 2nd node responds by sending back the data incremented by 1. This (1st) node then does the same and sends incremented data back to the 2nd node, etc.
Code for the first CANSPI node:
program Can_Spi_1st const ID_1st as longint = 12111 const ID_2nd as longint = 3 dim Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags as word ' can flags Rx_Data_Len as word ' received data length in bytes RxTx_Data as byte[8] ' can rx/tx data buffer Msg_Rcvd as byte ' reception flag Tx_ID, Rx_ID as longword ' can rx and tx ID ' CANSPI module connections dim CanSpi_CS as sbit at LATF0_bit CanSpi_CS_Direction as sbit at TRISF0_bit CanSpi_Rst as sbit at LATF1_bit CanSpi_Rst_Direction as sbit at TRISF1_bit ' End CANSPI module connections ADPCFG = 0xFFFF ' Configure AN pins as digital I/O PORTB = 0 ' clear PORTB TRISB = 0 ' set PORTB as output Can_Init_Flags = 0 ' Can_Send_Flags = 0 ' clear flags Can_Rcv_Flags = 0 ' Can_Send_Flags = _CANSPI_TX_PRIORITY_0 and ' form value to be used _CANSPI_TX_XTD_FRAME and ' with CANSPIWrite _CANSPI_TX_NO_RTR_FRAME Can_Init_Flags = _CANSPI_CONFIG_SAMPLE_THRICE and ' Form value to be used _CANSPI_CONFIG_PHSEG2_PRG_ON and ' with CANSPIInit _CANSPI_CONFIG_XTD_MSG and _CANSPI_CONFIG_DBL_BUFFER_ON and _CANSPI_CONFIG_VALID_XTD_MSG ' Initialize SPI1 module SPI1_Init() CANSPIInitialize(1,3,3,3,1,Can_Init_Flags) ' initialize external CANSPI module CANSPISetOperationMode(_CANSPI_MODE_CONFIG,0xFF) ' set CONFIGURATION mode CANSPISetMask(_CANSPI_MASK_B1,-1,_CANSPI_CONFIG_XTD_MSG) ' set all mask1 bits to ones CANSPISetMask(_CANSPI_MASK_B2,-1,_CANSPI_CONFIG_XTD_MSG) ' set all mask2 bits to ones CANSPISetFilter(_CANSPI_FILTER_B2_F4,ID_2nd,_CANSPI_CONFIG_XTD_MSG)' set id of filter B2_F4 to 2nd node ID CANSPISetOperationMode(_CANSPI_MODE_NORMAL,0xFF) ' set NORMAL mode ' Set initial data to be sent RxTx_Data[0] = 9 CANSPIWrite(ID_1st, RxTx_Data, 1, Can_Send_Flags) ' send initial message while (TRUE) ' endless loop Msg_Rcvd = CANSPIRead(Rx_ID , RxTx_Data , Rx_Data_Len, Can_Rcv_Flags) ' receive message if ((Rx_ID = ID_2nd) and Msg_Rcvd) then ' if message received check id PORTB = RxTx_Data[0] ' id correct, output data at PORTD Inc(RxTx_Data[0]) ' increment received data Delay_ms(10) CANSPIWrite(ID_1st, RxTx_Data, 1, Can_Send_Flags) ' send incremented data back end if wend end.
Code for the second CANSPI node:
program Can_Spi_2nd const ID_1st as longint = 12111 const ID_2nd as longint = 3 dim Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags as word ' can flags Rx_Data_Len as word ' received data length in bytes RxTx_Data as byte[8] ' can rx/tx data buffer Msg_Rcvd as byte ' reception flag Tx_ID, Rx_ID as longword ' can rx and tx ID ' CANSPI module connections dim CanSpi_CS as sbit at LATF0_bit CanSpi_CS_Direction as sbit at TRISF0_bit CanSpi_Rst as sbit at LATF1_bit CanSpi_Rst_Direction as sbit at TRISF1_bit ' End CANSPI module connections ADPCFG = 0xFFFF ' Configure AN pins as digital I/O PORTB = 0 ' clear PORTB TRISB = 0 ' set PORTB as output Can_Init_Flags = 0 ' Can_Send_Flags = 0 ' clear flags Can_Rcv_Flags = 0 ' Can_Send_Flags = _CANSPI_TX_PRIORITY_0 and ' form value to be used _CANSPI_TX_XTD_FRAME and ' with CANSPIWrite _CANSPI_TX_NO_RTR_FRAME Can_Init_Flags = _CANSPI_CONFIG_SAMPLE_THRICE and ' Form value to be used _CANSPI_CONFIG_PHSEG2_PRG_ON and ' with CANSPIInit _CANSPI_CONFIG_XTD_MSG and _CANSPI_CONFIG_DBL_BUFFER_ON and _CANSPI_CONFIG_VALID_XTD_MSG and _CANSPI_CONFIG_LINE_FILTER_OFF ' Initialize SPI1 module SPI1_Init() CANSPIInitialize(1,3,3,3,1,Can_Init_Flags) ' initialize external CANSPI module CANSPISetOperationMode(_CANSPI_MODE_CONFIG,0xFF) ' set CONFIGURATION mode CANSPISetMask(_CANSPI_MASK_B1,-1,_CANSPI_CONFIG_XTD_MSG) ' set all mask1 bits to ones CANSPISetMask(_CANSPI_MASK_B2,-1,_CANSPI_CONFIG_XTD_MSG) ' set all mask2 bits to ones CANSPISetFilter(_CANSPI_FILTER_B2_F3,ID_1st,_CANSPI_CONFIG_XTD_MSG)' set id of filter B2_F3 to 1st node ID CANSPISetOperationMode(_CANSPI_MODE_NORMAL,0xFF) ' set NORMAL mode while (TRUE) ' endless loop Msg_Rcvd = CANSPIRead(Rx_ID , RxTx_Data , Rx_Data_Len, Can_Rcv_Flags) ' receive message if ((Rx_ID = ID_1st) and Msg_Rcvd) then ' if message received check id PORTB = RxTx_Data[0] ' id correct, output data at PORTB Inc(RxTx_Data[0]) ' increment received data CANSPIWrite(ID_2nd, RxTx_Data, 1, Can_Send_Flags) ' send incremented data back end if wend end.
HW Connection
Example of interfacing CAN transceiver MCP2510 with MCU via SPI interface
What do you think about this topic ? Send us feedback!
Find them on
