The SPI module is
available with a number of the 8051 compliant MCUs. The mikroC PRO for 8051
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, selfchecking and fault
confinement. Faulty CAN data
and remote frames are re-transmitted automatically, similar to the Ethernet.
Data transfer rates depend on distance. For example, 1 Mbit/s can be achieved
at network lengths below 40m while 250 Kbit/s can be achieved at network lengths
below 250m. The greater distance the lower maximum bitrate that can be achieved.
The lowest bitrate defined by the standard is 200Kbit/s. Cables used are
shielded twisted pairs.
CAN supports two message
formats:
- Standard format, with 11 identifier bits and
- Extended format, with 29 identifier bits
Notes:
- Consult the CAN standard about CAN bus termination resistance.
- An effective CANSPI communication speed depends on SPI and certainly is slower than “real” CAN.
- CANSPI module refers to mikroElektronika's CANSPI Add-on board connected to SPI module of MCU.
External dependencies of CANSPI Library
The following variables must be defined in all projects using CANSPI Library: | Description : | Example : |
---|---|---|
extern sfr bdata sbit CanSpi_CS; |
Chip Select line. | sbit CanSpi_CS at P1_0_bit; |
extern sfr bdata sbit CanSpi_Rst; |
Reset line. | sbit CanSpi_Rst at
P1_2_bit; |
Library Routines
- CANSPISetOperationMode
- CANSPIGetOperationMode
- CANSPIInitialize
- CANSPISetBaudRate
- CANSPISetMask
- CANSPISetFilter
- CANSPIread
- CANSPIWrite
The following routines are for an internal use by the library only:
- RegsToCANSPIID
- CANSPIIDToRegs
Be sure to check CANSPI constants necessary
for using some of the functions.
CANSPISetOperationMode
Prototype | void CANSPISetOperationMode(char mode, char WAIT); |
---|---|
Returns | Nothing. |
Description |
Sets the CANSPI
module to requested mode. Parameters :
|
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); |
CANSPIGetOperationMode
Prototype | char CANSPIGetOperationMode(); |
---|---|
Returns | Current operation mode. |
Description |
The function returns current operation mode of the CANSPI
module. Check CANSPI_OP_MODE constants or device datasheet for operation
mode codes. |
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) { ... } |
CANSPIInitialize
Prototype | void CANSPIInitialize( char SJW, char BRP, char PHSEG1, char PHSEG2, char PROPSEG, char CANSPI_CONFIG_FLAGS); |
---|---|
Returns | Nothing. |
Description |
Initializes the CANSPI
module. Stand-Alone CAN controller in the CANSPI module is set to:
SAM , SEG2PHTS , WAKFIL and
DBEN bits are set according to CAN_CONFIG_FLAGS
value.Parameters:
|
Requires |
CanSpi_CS and CanSpi_Rst variables must be defined
before using this function.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 sbit CanSpi_CS at P1_0_bit; sbit CanSpi_Rst at P1_2_bit; // End CANSPI module connections // initialize the CANSPI module with the appropriate baud rate and message acceptance flags along with the sampling rules char CanSpi_Init_Flags; ... CanSpi_Init_Flags = _CANSPI_CONFIG_SAMPLE_THRICE & // form value to be used _CANSPI_CONFIG_PHSEG2_PRG_ON & // with CANSPIInitialize _CANSPI_CONFIG_XTD_MSG & _CANSPI_CONFIG_DBL_BUFFER_ON & _CANSPI_CONFIG_VALID_XTD_MSG; ... SPI1_Init(); // initialize SPI module CANSPIInitialize(1,3,3,3,1,CanSpi_Init_Flags); // initialize external CANSPI module |
CANSPISetBaudRate
Prototype | void CANSPISetBaudRate( char SJW, char BRP, char PHSEG1, char PHSEG2, char PROPSEG, char CANSPI_CONFIG_FLAGS); |
---|---|
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.SAM , SEG2PHTS and WAKFIL bits are set
according to CANSPI_CONFIG_FLAGS value. Refer to datasheet for
details.Parameters:
|
Requires |
The CANSPI
module must be in Config mode, otherwise the function will be ignored. 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 char canspi_config_flags; ... CANSPISetOperationMode(CANSPI_MODE_CONFIG,0xFF); // set CONFIGURATION mode (CANSPI module mast be in config mode for baud rate settings) canspi_config_flags = _CANSPI_CONFIG_SAMPLE_THRICE & _CANSPI_CONFIG_PHSEG2_PRG_ON & _CANSPI_CONFIG_STD_MSG & _CANSPI_CONFIG_DBL_BUFFER_ON & _CANSPI_CONFIG_VALID_XTD_MSG & _CANSPI_CONFIG_LINE_FILTER_OFF; CANSPISetBaudRate(1, 1, 3, 3, 1, canspi_config_flags); |
CANSPISetMask
Prototype | void CANSPISetMask(char CANSPI_MASK, long val, char CANSPI_CONFIG_FLAGS); |
---|---|
Returns | Nothing. |
Description |
Configures mask for advanced filtering of messages. The parameter
value is bit-adjusted to the appropriate mask registers.Parameters:
|
Requires |
The CANSPI
module must be in Config mode, otherwise the function will be ignored. 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 (CANSPI 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 & _CANSPI_CONFIG_XTD_MSG); |
CANSPISetFilter
Prototype | void CANSPISetFilter(char CANSPI_FILTER, long val, char CANSPI_CONFIG_FLAGS); |
---|---|
Returns | Nothing. |
Description |
Configures message filter. The parameter value is bit-adjusted
to the appropriate filter registers.Parameters:
|
Requires |
The CANSPI
module must be in Config mode, otherwise the function will be ignored. 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 CANSPISetOperationMode(_CANSPI_MODE_CONFIG,0xFF); // set CONFIGURATION mode (CANSPI module must be in config mode for filter settings) /* Set id of filter B1_F1 to 3: */ CANSPISetFilter(_CANSPI_FILTER_B1_F1, 3, _CANSPI_CONFIG_XTD_MSG); |
CANSPIRead
Prototype | char CANSPIRead(long *id, char *rd_data, char *data_len, char *CANSPI_RX_MSG_FLAGS); |
---|---|
Returns |
|
Description |
If at least one full Receive Buffer is found, it will be processed in the
following way:
|
Requires |
The CANSPI
module must be in a mode in which receiving is possible. 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 CANSPI module for received messages. If any was received do something. char msg_rcvd, rx_flags, data_len; char data[8]; long msg_id; ... CANSPISetOperationMode(_CANSPI_MODE_NORMAL,0xFF); // set NORMAL mode (CANSPI 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)) { ... } |
CANSPIWrite
Prototype | char CANSPIWrite(long id, char *wr_data, char data_len, char CANSPI_TX_MSG_FLAGS); |
---|---|
Returns |
|
Description |
If at least one empty Transmit Buffer is found, the function sends message in
the queue for transmission. Parameters:
|
Requires |
The CANSPI
module must be in mode in which transmission is possible. 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 char tx_flags; char data[8]; long msg_id; ... CANSPISetOperationMode(_CANSPI_MODE_NORMAL,0xFF); // set NORMAL mode (CANSPI must be in mode in which transmission is possible) tx_flags = _CANSPI_TX_PRIORITY_0 & _CANSPI_TX_XTD_FRAME; // set message flags CANSPIWrite(msg_id, data, 2, tx_flags); |
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
The
CANSPI_OP_MODE
constants define CANSPI
operation mode. Function CANSPISetOperationMode expects one of these
as it's argument:const char_CANSPI_MODE_BITS = 0xE0, // Use this to access opmode bits_CANSPI_MODE_NORMAL = 0x00,_CANSPI_MODE_SLEEP = 0x20,_CANSPI_MODE_LISTEN = 0x60,_CANSPI_MODE_LOOP = 0x40,_CANSPI_MODE_CONFIG = 0x80;
CANSPI_CONFIG_FLAGS
The
CANSPI_CONFIG_FLAGS
constants define flags related to the
CANSPI
module configuration. The functions CANSPIInitialize, CANSPISetBaudRate, CANSPISetMask and CANSPISetFilter expect one of these (or a bitwise
combination) as their argument:const char_CANSPI_CONFIG_DEFAULT = 0xFF, // 11111111_CANSPI_CONFIG_PHSEG2_PRG_BIT = 0x01,_CANSPI_CONFIG_PHSEG2_PRG_ON = 0xFF, // XXXXXXX1_CANSPI_CONFIG_PHSEG2_PRG_OFF = 0xFE, // XXXXXXX0_CANSPI_CONFIG_LINE_FILTER_BIT = 0x02,_CANSPI_CONFIG_LINE_FILTER_ON = 0xFF, // XXXXXX1X_CANSPI_CONFIG_LINE_FILTER_OFF = 0xFD, // XXXXXX0X_CANSPI_CONFIG_SAMPLE_BIT = 0x04,_CANSPI_CONFIG_SAMPLE_ONCE = 0xFF, // XXXXX1XX_CANSPI_CONFIG_SAMPLE_THRICE = 0xFB, // XXXXX0XX_CANSPI_CONFIG_MSG_TYPE_BIT = 0x08,_CANSPI_CONFIG_STD_MSG = 0xFF, // XXXX1XXX_CANSPI_CONFIG_XTD_MSG = 0xF7, // XXXX0XXX_CANSPI_CONFIG_DBL_BUFFER_BIT = 0x10,_CANSPI_CONFIG_DBL_BUFFER_ON = 0xFF, // XXX1XXXX_CANSPI_CONFIG_DBL_BUFFER_OFF = 0xEF, // XXX0XXXX_CANSPI_CONFIG_MSG_BITS = 0x60,_CANSPI_CONFIG_ALL_MSG = 0xFF, // X11XXXXX_CANSPI_CONFIG_VALID_XTD_MSG = 0xDF, // X10XXXXX_CANSPI_CONFIG_VALID_STD_MSG = 0xBF, // X01XXXXX_CANSPI_CONFIG_ALL_VALID_MSG = 0x9F; // X00XXXXX
You may use bitwise AND (
&
) to form config byte out of these
values. For example:init = _CANSPI_CONFIG_SAMPLE_THRICE &_CANSPI_CONFIG_PHSEG2_PRG_ON &_CANSPI_CONFIG_DBL_BUFFER_ON &_CANSPI_CONFIG_STD_MSG &_CANSPI_CONFIG_VALID_XTD_MSG &CANSPIInitialize(1, 1, 3, 3, 1, init); // initialize CANSPI_CANSPI_CONFIG_LINE_FILTER_OFF;...
CANSPI_TX_MSG_FLAGS
CANSPI_TX_MSG_FLAGS
are flags related to transmission of a
CAN message:const char_CANSPI_TX_PRIORITY_BITS = 0x03,_CANSPI_TX_PRIORITY_0 = 0xFC, // XXXXXX00_CANSPI_TX_PRIORITY_1 = 0xFD, // XXXXXX01_CANSPI_TX_PRIORITY_2 = 0xFE, // XXXXXX10_CANSPI_TX_PRIORITY_3 = 0xFF, // XXXXXX11_CANSPI_TX_FRAME_BIT = 0x08,_CANSPI_TX_STD_FRAME = 0xFF, // XXXXX1XX_CANSPI_TX_XTD_FRAME = 0xF7, // XXXXX0XX_CANSPI_TX_RTR_BIT = 0x40,_CANSPI_TX_NO_RTR_FRAME = 0xFF, // X1XXXXXX_CANSPI_TX_RTR_FRAME = 0xBF; // X0XXXXXX
You may use bitwise AND (
&
) to adjust the appropriate flags.
For example:/* form value to be used as sending message flag : */send_config = _CANSPI_TX_PRIORITY_0 &_CANSPI_TX_XTD_FRAME &_CANSPI_TX_NO_RTR_FRAME;...CANSPIWrite(id, data, 1, send_config);
CANSPI_RX_MSG_FLAGS
CANSPI_RX_MSG_FLAGS
are flags related to reception of CAN message. If a particular bit is
set then corresponding meaning is TRUE or else it will be FALSE.const char_CANSPI_RX_FILTER_BITS = 0x07, // Use this to access filter bits_CANSPI_RX_FILTER_1 = 0x00,_CANSPI_RX_FILTER_2 = 0x01,_CANSPI_RX_FILTER_4 = 0x03,_CANSPI_RX_FILTER_3 = 0x02,_CANSPI_RX_FILTER_5 = 0x04,_CANSPI_RX_OVERFLOW = 0x08, // Set if Overflowed else cleared_CANSPI_RX_FILTER_6 = 0x05,_CANSPI_RX_INVALID_MSG = 0x10, // Set if invalid else cleared_CANSPI_RX_XTD_FRAME = 0x20, // Set if XTD message else cleared_CANSPI_RX_RTR_FRAME = 0x40, // Set if RTR message else cleared_CANSPI_RX_DBL_BUFFERED = 0x80; // Set if this message was hardware double-buffered
You may use bitwise AND (
&
) to adjust the appropriate flags.
For example:if (MsgFlag & _CANSPI_RX_OVERFLOW != 0) {...// Receiver overflow has occurred.// We have lost our previous message.}
CANSPI_MASK
The
CANSPI_MASK
constants define mask codes. Function CANSPISetMask expects one of these as it's
argument:const char_CANSPI_MASK_B1 = 0,_CANSPI_MASK_B2 = 1;
CANSPI_FILTER
The
CANSPI_FILTER
constants define filter codes. Functions CANSPISetFilter expects one of these as it's
argument:const char_CANSPI_FILTER_B1_F1 = 0,_CANSPI_FILTER_B1_F2 = 1,_CANSPI_FILTER_B2_F2 = 3,_CANSPI_FILTER_B2_F1 = 2,_CANSPI_FILTER_B2_F3 = 4,_CANSPI_FILTER_B2_F4 = 5;
Code Example
This is a simple demonstration of CANSPI
Library routines usage. First node initiates the communication with the second
node by sending some data to its address. The second node responds by sending
back the data incremented by 1. First node then does the same and sends
incremented data back to second node, etc.
Code for the first CANSPI node:
unsigned char Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags; // can flagsunsigned char Rx_Data_Len; // received data length in byteschar RxTx_Data[8]; // can rx/tx data bufferchar Msg_Rcvd; // reception flagconst long ID_1st = 12111, ID_2nd = 3; // node IDslong Rx_ID;// CANSPI module connectionssbit CanSpi_CS at P1_0_bit;sbit CanSpi_Rst at P1_2_bit;// End CANSPI module connectionsvoid main() {Can_Init_Flags = 0; //Can_Send_Flags = 0; // clear flagsCan_Rcv_Flags = 0; //Can_Send_Flags = _CANSPI_TX_PRIORITY_0 & // form value to be used_CANSPI_TX_XTD_FRAME & // with CANSPIWrite_CANSPI_TX_NO_RTR_FRAME;Can_Init_Flags = _CANSPI_CONFIG_SAMPLE_THRICE & // Form value to be used_CANSPI_CONFIG_PHSEG2_PRG_ON & // with CANSPIInit_CANSPI_CONFIG_XTD_MSG &_CANSPI_CONFIG_DBL_BUFFER_ON &_CANSPI_CONFIG_VALID_XTD_MSG;SPI1_Init(); // initialize SPI1 moduleCANSPIInitialize(1,3,3,3,1,Can_Init_Flags); // Initialize external CANSPI moduleCANSPISetOperationMode(_CANSPI_MODE_CONFIG,0xFF); // set CONFIGURATION modeCANSPISetMask(_CANSPI_MASK_B1,-1,_CANSPI_CONFIG_XTD_MSG); // set all mask1 bits to onesCANSPISetMask(_CANSPI_MASK_B2,-1,_CANSPI_CONFIG_XTD_MSG); // set all mask2 bits to onesCANSPISetFilter(_CANSPI_FILTER_B2_F4,ID_2nd,_CANSPI_CONFIG_XTD_MSG);// set id of filter B2_F4 to 2nd node IDCANSPISetOperationMode(_CANSPI_MODE_NORMAL,0xFF); // set NORMAL modeRxTx_Data[0] = 9; // set initial data to be sentCANSPIWrite(ID_1st, RxTx_Data, 1, Can_Send_Flags); // send initial messagewhile(1) { // endless loopMsg_Rcvd = CANSPIRead(&Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags);// receive messageif ((Rx_ID == ID_2nd) && Msg_Rcvd) { // if message received check idP0 = RxTx_Data[0]; // id correct, output data at PORT0RxTx_Data[0]++ ; // increment received dataDelay_ms(10);CANSPIWrite(ID_1st, RxTx_Data, 1, Can_Send_Flags); // send incremented data back}}}
Code for the second CANSPI node:
unsigned char Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags; // can flagsunsigned char Rx_Data_Len; // received data length in byteschar RxTx_Data[8]; // can rx/tx data bufferchar Msg_Rcvd; // reception flagconst long ID_1st = 12111, ID_2nd = 3; // node IDslong Rx_ID;// CANSPI module connectionssbit CanSpi_CS at P1_0_bit;sbit CanSpi_Rst at P1_2_bit;// End CANSPI module connectionsvoid main() {Can_Init_Flags = 0; //Can_Send_Flags = 0; // clear flagsCan_Rcv_Flags = 0; //Can_Send_Flags = _CANSPI_TX_PRIORITY_0 & // form value to be used_CANSPI_TX_XTD_FRAME & // with CANSPIWrite_CANSPI_TX_NO_RTR_FRAME;Can_Init_Flags = _CANSPI_CONFIG_SAMPLE_THRICE & // Form value to be used_CANSPI_CONFIG_PHSEG2_PRG_ON & // with CANSPIInit_CANSPI_CONFIG_XTD_MSG &_CANSPI_CONFIG_DBL_BUFFER_ON &_CANSPI_CONFIG_VALID_XTD_MSG &_CANSPI_CONFIG_LINE_FILTER_OFF;SPI1_Init(); // initialize SPI1 moduleCANSPIInitialize(1,3,3,3,1,Can_Init_Flags); // initialize external CANSPI moduleCANSPISetOperationMode(_CANSPI_MODE_CONFIG,0xFF); // set CONFIGURATION modeCANSPISetMask(_CANSPI_MASK_B1,-1,_CANSPI_CONFIG_XTD_MSG); // set all mask1 bits to onesCANSPISetMask(_CANSPI_MASK_B2,-1,_CANSPI_CONFIG_XTD_MSG); // set all mask2 bits to onesCANSPISetFilter(_CANSPI_FILTER_B2_F3,ID_1st,_CANSPI_CONFIG_XTD_MSG); // set id of filter B2_F3 to 1st node IDCANSPISetOperationMode(_CANSPI_MODE_NORMAL,0xFF); // set NORMAL modewhile (1) { // endless loopMsg_Rcvd = CANSPIRead(&Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags); // receive messageif ((Rx_ID == ID_1st) && Msg_Rcvd) { // if message received check idP0 = RxTx_Data[0]; // id correct, output data at PORT0RxTx_Data[0]++ ; // increment received dataCANSPIWrite(ID_2nd, RxTx_Data, 1, Can_Send_Flags); // send incremented data back}}}
No comments:
Post a Comment