CAN Library
The mikroPascal PRO for ARM provides a library (driver) for working with the ARM CAN module.
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.
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
Important :
- Consult the CAN standard about CAN bus termination resistance.
- CAN library routines require you to specify the module you want to use. To use the desired CAN module, simply change the letter x in the routine prototype for a number from 0 to 3.
- Number of CAN modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
Library Routines
- CANxSetOperationMode
- CANxGetOperationMode
- CANxInitialize
- CANxInitializeAdvanced
- CANxSetBaudRate
- CANxSetMask
- CANSetMask
- CANxSetFilter
- CANSetFilterScale16
- CANSetFilterScale32
- CANxReadMessage
- CANxRead
- CANxWriteMessage
- CANxWrite
- CANxConfigureMessage
- CANxBitRateSet
- CANSlaveStartBank
- CANx_EnableOperationMode
- CANx_ExitOperationMode
- CANx_Init
- CANx_Init_Advanced
- CANx_SetBitRate
- CANx_SetRxMaskType
- CANx_SetRxIndividualMask
- CANx_GetReceiveStatus
- CANx_SetRxMbGlobalMask
- CANx_ConfigRxMb
- CANx_ConfigTxMb
- CANx_Write
- CANx_Read
- CANx_InterruptHandler
CANxSetOperationMode
| Prototype |
// for Stellaris MCUs :
procedure CANxSetOperationMode(mode, wait_flag : dword); // for ST MCUs :function CANxSetOperationMode(CAN_OperatingMode : byte) : byte; |
|---|---|
| Description |
Sets the CAN module to requested mode. |
| Parameters |
|
| Returns |
|
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
| Example |
// set the CAN1 module into configuration mode (wait inside CAN1SetOperationMode until this mode is set) CAN1SetOperationMode(_CAN_MODE_CONFIG, 0xFF); |
| Notes |
|
CANxGetOperationMode
| Prototype |
function CANxGetOperationMode(): dword; |
|---|---|
| Description |
The function returns current operation mode of the CAN module. See CAN_OP_MODE constants or device datasheet for operation mode codes. Valid only for Stellaris devices. |
| Parameters |
None. |
| Returns |
Current operation mode. |
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
| Example |
// check whether the CAN1 module is in Normal mode and if it is then do something. if (CAN1GetOperationMode() = _CAN_MODE_NORMAL) begin ... end; |
| Notes |
|
CANxInitialize
| Prototype |
// for Stellaris MCUs with dedicated PORT functions and ST MCUs:
procedure CANxInitialize(SJW, BRP, PHSEG1, PHSEG2, PROPSEG : word; flags : dword); // for Stellaris MCUs with alternative PORT functions on GPIO pins :procedure CANxInitialize(SJW, BRP, PHSEG1, PHSEG2, PROPSEG : word; flags : dword; module : ^const Module_Struct); |
|---|---|
| Description |
Initializes the CAN module. The internal CAN module is set to :
|
| Parameters |
|
| Returns |
Nothing. |
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
| Example |
// initialize the CAN1 module with appropriate baud rate and message acceptance flags along with the sampling rules
var Can_Init_Flags : dword;
...
Can_Init_Flags := _CAN_MODE_NORMAL and // form value to be used
_CAN_MODE_LOOP and // with CAN1Initialize
_CAN_MODE_SILENT;
CAN1Initialize(1,3,3,3,1,Can_Init_Flags); // initialize the CAN1 module
|
| Notes |
|
CANxInitializeAdvanced
| Prototype |
procedure CANxInitializeAdvanced(SJW : word; BRP : word; PHSEG1 : word; PHSEG2 : word; PROPSEG : word; flags : dword; module : ^ const module_Struct); |
|---|---|
| Description |
Initializes the CAN with desired CAN module pinout for ST devices. The internal CAN module is set to :
|
| Parameters |
|
| Returns |
Nothing. |
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
| Example |
// initialize the CAN1 module with appropriate baud rate and message acceptance flags along with the sampling rules
var Can_Init_Flags : dword;
...
Can_Init_Flags := _CAN_MODE_NORMAL and // form value to be used
_CAN_MODE_LOOP and // with CAN1Initialize
_CAN_MODE_SILENT;
CAN1InitializeAdvanced(1,3,3,3,1,Can_Init_Flags, @_GPIO_MODULE_CAN1_PA11_12); // initialize the CAN1 module
|
| Notes |
|
CANxSetBaudRate
| Prototype |
procedure CANxSetBaudRate(SJW, BRP, PHSEG1, PHSEG2, PROPSEG : word); |
|---|---|
| Description |
Sets CAN baud rate. Due to complexity of the CAN protocol, you can not simply force a bps value. Instead, use this function when CAN is in Config mode. Refer to datasheet for details.
Valid only for Stellaris devices. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. CAN must be in Config mode, otherwise the function will be ignored. See CANxSetOperationMode. |
| Example |
CAN1SetBaudRate(1,3,3,3,1); // set the CAN1 module baud rate |
| Notes |
|
CANxSetMask
| Prototype |
procedure CANxSetMask(objID, maskValue, flags : dword); |
|---|---|
| Description |
The function configures appropriate mask for advanced message filtering. Valid only for Stellaris devices. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. CAN must be in Config mode, otherwise the function will be ignored. See CANxSetOperationMode. |
| Example |
// set appropriate filter mask and message type value CAN1SetOperationMode(_CAN_MODE_CONFIG,0xFF); // set CONFIGURATION mode (CAN1 module must be in config mode for mask settings) CAN1SetMask(1, -1, _CAN_CONFIG_USE_DIR_FILTER); // set all mask1 bits to ones |
| Notes |
|
CANSetMask
| Prototype |
procedure CANSetMask(Filter_Number : byte; maskValue : dword; CAN_FILTAR_FLAGS : byte); |
|---|---|
| Description |
The function configures appropriate mask for advanced message filtering. Valid only for ST devices. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. CAN must be in Config mode, otherwise the function will be ignored. See CANxSetOperationMode. |
| Example |
// set appropriate filter mask and message type value CAN1SetOperationMode(_CAN_MODE_CONFIG,0xFF); // set CONFIGURATION mode (CAN1 module must be in config mode for mask settings) CANSetMask(1, -1, _CAN_FILTER_ENABLED); // set all mask1 bits to ones |
| Notes |
|
CANxSetFilter
| Prototype |
procedure CANxSetFilter(objID, filterValue, flags : dword); |
|---|---|
| Description |
Function sets message filter. Given Valid only for Stellaris devices. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. CAN must be in Config mode, otherwise the function will be ignored. See CANxSetOperationMode. |
| Example |
// set appropriate filter value and message type CAN1SetOperationMode(_CAN_MODE_CONFIG,0xFF); // set CONFIGURATION mode (CAN1 module must be in config mode for filter settings) CAN1SetFilter(1, -1, _CAN_CONFIG_XTD_MSG); |
| Notes |
|
CANSetFilter
| Prototype |
procedure CANSetFilter(Filter_Number : byte; ID : dword; CAN_FILTAR_FLAGS : dword); |
|---|---|
| Description |
Function sets message filter. Given Valid only for ST devices. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. CAN must be in Config mode, otherwise the function will be ignored. See CANxSetOperationMode. |
| Example |
|
| Notes |
|
CANSetFilterScale16
| Prototype |
procedure CANSetFilterScale16(Filter_Number : byte; CAN_FILTAR_FLAGS : byte; ID : dword; mask_or_ID : dword; ID1 : dword; mask1_or_ID1 : dword)); |
|---|---|
| Description |
To optimize and adapt the filters to the application needs, each filter can be scaled independently. This routine applies two 16-bit filters to the STDID[10:0], RTR and IDE bits. Valid only for ST devices. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. CAN must be in Config mode, otherwise the function will be ignored. See CANxSetOperationMode. |
| Example |
|
| Notes |
|
CANSetFilterScale32
| Prototype |
procedure CANSetFilterScale32(Filter_Number : byte; CAN_FILTAR_FLAGS : byte; ID : dword; mask_or_ID : dword); |
|---|---|
| Description |
To optimize and adapt the filters to the application needs, each filter can be scaled independently. This routine applies one 32-bit filter to the STDID[10:0], IDE, EXTID[17:0] and RTR bits. Valid only for ST devices. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. CAN must be in Config mode, otherwise the function will be ignored. See CANxSetOperationMode. |
| Example |
|
| Notes |
|
CANxReadMessage
| Prototype |
function CANxReadMessage(objID : dword; var msgId : dword; var pMsgData : array[8] of byte; var msgLen, flags : dword) : dword; |
|---|---|
| Description |
The function reads message from the desired message object and processes it in the following way :
Valid only for Stellaris devices. |
| Parameters |
|
| Returns |
|
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. The CAN module must be in a mode in which receiving is possible. See CANxSetOperationMode. |
| Example |
if (msg_rcvd = CAN1ReadMessage(objID, @msgId, pMsgData, @msgLen, @flags)) then begin ... end; |
| Notes |
|
CANxRead
| Prototype |
// for Stellaris devices
function CANxRead(var msgId : dword; var pMsgData : array[8] of byte; var msgLen, flags : dword) : dword; // for ST devicesfunction CANxRead(FIFONumber : byte; var id : dword; var data_ : array[8] of byte; var datalen : char; var CAN_RX_MSG_FLAGS : byte) : byte;
|
|---|---|
| Description |
The function reads and processes the message from the first message object configured for reception that has received data in the following way :
|
| Parameters |
|
| Returns |
|
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. The CAN module must be in a mode in which receiving is possible. See CANxSetOperationMode. |
| Example |
// check the CAN1 module for received messages. If any was received do something.
var msg_rcvd, rx_flags, data_len : word;
data : array[8] of byte;
msg_id : longint;
...
CAN1SetOperationMode(_CAN_MODE_NORMAL,0xFF); // set NORMAL mode (CAN1 module must be in mode in which receive is possible)
...
rx_flags := 0; // clear message flags
if (msg_rcvd = CAN1Read(@msg_id, @pMsgData, @msgLen, @flags)) then
begin
...
end;
|
| Notes |
|
CANxWriteMessage
| Prototype |
function CANxWriteMessage(objID, msgID : dword; var pMsgData : array[8] of byte; msgLen, flags : dword) : dword; |
|---|---|
| Description |
The function writes the message to the desired message object. Valid only for Stellaris devices. |
| Parameters |
|
| Returns |
|
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. The CAN module must be in mode in which transmission is possible. See CANxSetOperationMode. |
| Example |
// send message extended CAN message with appropriate ID and data
var tx_flags: word;
data: array[8] of byte;
msg_id : longint;
...
CAN1SetOperationMode(_CAN_MODE_NORMAL,0xFF); // set NORMAL mode (CAN1 must be in mode in which transmission is possible)
tx_flags := _CAN_TX_PRIORITY_0 and
_CAN_TX_XTD_FRAME and
_CAN_TX_NO_RTR_FRAME; // set message flags
CAN1WriteMessage(msg_id, data, _CAN_BUFFER_0, 1, tx_flags);
|
| Notes |
|
CANxWrite
| Prototype |
function CANxWrite(id : dword; var Data_ : array[8] of byte; dataLen, CAN_TX_MSG_FLAGS : word) : word; |
|---|---|
| Description |
The function writes message in the first available message object configured for transmission. |
| Parameters |
|
| Returns |
|
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. The CAN module must be in mode in which transmission is possible. See CANxSetOperationMode. |
| Example |
// send message extended CAN message with appropriate ID and data
svar tx_flags: word;
data: array[8] of byte;
msg_id : longint;
...
CAN1SetOperationMode(_CAN_MODE_NORMAL,0xFF); // set NORMAL mode (CAN1 must be in mode in which transmission is possible)
tx_flags := _CAN_TX_PRIORITY_0 and
_CAN_TX_XTD_FRAME and
_CAN_TX_NO_RTR_FRAME; // set message flags
CAN1Write(msg_id, data, 1, tx_flags);
|
| Notes |
|
CANxConfigureMessage
| Prototype |
procedure CANxConfigureMessage(objID, flags : dword); |
|---|---|
| Description |
The function configures message object. Valid only for Stellaris devices. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
| Example |
|
| Notes |
|
CANxBitRateSet
| Prototype |
function CANxBitRateSet(ulSourceClock, bitRate : dword) : dword; |
|---|---|
| Description |
This function is used to set the CAN bit timing values to a nominal setting based on a desired bit rate. Valid only for Stellaris devices. |
| Parameters |
|
| Returns |
|
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
| Example |
|
| Notes |
|
CANSlaveStartBank
| Prototype |
procedure CANSlaveStartBank(CAN_BankNumber : byte); |
|---|---|
| Description |
This routine is used to set the starting bank filter for the CAN slave module. Valid only for ST devices. |
| Parameters |
|
| Returns |
|
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
| Example |
|
| Notes |
|
CANx_EnableOperationMode
| Prototype |
procedure CANx_EnableOperationMode(operationMode : dword); |
||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Description |
This routine is used to enable a CAN operation mode. |
||||||||||||
| Parameters |
|
||||||||||||
| Returns |
Nothing. |
||||||||||||
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
||||||||||||
| Example |
|
||||||||||||
| Notes |
|
CANx_ExitOperationMode
| Prototype |
function CANx_ExitOperationMode(operationMode : dword) : byte; |
||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Description |
This routine is used to disable a CAN operation mode. |
||||||||||||
| Parameters |
|
||||||||||||
| Returns |
|
||||||||||||
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
||||||||||||
| Example |
|
||||||||||||
| Notes |
|
CANx_Init
| Prototype |
function CANx_Init(config : dword; maxMsgBuff : dword) : byte; |
||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Description |
This routine is used to initialize CAN module. This function will enable CAN clock, reset module to the default state, set max number for message buffer and set operational mode. |
||||||||||||||||
| Parameters |
|
||||||||||||||||
| Returns |
|
||||||||||||||||
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
||||||||||||||||
| Example |
|
||||||||||||||||
| Notes |
|
CANx_Init_Advanced
| Prototype |
function CANx_Init_Advanced(config : dword; maxMsgBuff : dword; module : ^Module_Struct) : byte; |
||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Description |
This routine is used to initialize CAN module. This function will enable CAN clock, reset module to the default state, set max number for message buffer, set operational mode and initialize it on PB19 and PB18 pins. |
||||||||||||||||
| Parameters |
|
||||||||||||||||
| Returns |
|
||||||||||||||||
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
||||||||||||||||
| Example |
|
||||||||||||||||
| Notes |
|
CANx_SetBitRate
| Prototype |
procedure CANx_SetBitRate(propSeg : dword; phaseSeg2 : dword; phaseSeg1 : dword; preDivider : dword; RJW : dword); |
|---|---|
| Description |
This function will set all CAN time segments which define the length of Propagation Segment in the bit time, the length of Phase Buffer Segment 2 in the bit time, the length of Phase Buffer Segment 1 in the bit time, the ratio between the PE clock frequency and the Serial Clock (Sclock) frequency, and the maximum number of time quanta that a bit time can be changed by one resynchronization. (One time quantum is equal to the Sclock period.). |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
| Example |
|
| Notes |
|
CANx_SetRxMaskType
| Prototype |
procedure CANx_SetRxMaskType(type : dword); |
||||||
|---|---|---|---|---|---|---|---|
| Description |
This function will set the RX masking type. |
||||||
| Parameters |
|
||||||
| Returns |
Nothing. |
||||||
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
||||||
| Example |
|
||||||
| Notes |
|
CANx_SetRxIndividualMask
| Prototype |
procedure CANx_SetRxIndividualMask(msgBuffIdType : dword; msgBuffIdx : dword; mask : dword); |
||||||
|---|---|---|---|---|---|---|---|
| Description |
This function will Set Rx individual mask as the 11-bit standard mask or the 29-bit extended mask. |
||||||
| Parameters |
|
||||||
| Returns |
|
||||||
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
||||||
| Example |
|
||||||
| Notes |
|
CANx_GetReceiveStatus
| Prototype |
function CANx_GetReceiveStatus() : byte; |
|---|---|
| Description |
This function will check if CAN is receiving message. |
| Parameters |
None. |
| Returns |
|
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
| Example |
|
| Notes |
|
CANx_SetRxMbGlobalMask
| Prototype |
function CANx_SetRxMbGlobalMask(idType : dword; mask : dword) : byte; |
||||||
|---|---|---|---|---|---|---|---|
| Description |
This function will set Rx message buffer global mask as the 11-bit standard mask or the 29-bit extended mask. |
||||||
| Parameters |
|
||||||
| Returns |
|
||||||
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
||||||
| Example |
|
||||||
| Notes |
|
CANx_ConfigRxMb
| Prototype |
function CANx_ConfigRxMb(mbIndex : dword; msgIdType : dword; dataLength : dword; msgId : dword) : byte; |
||||||
|---|---|---|---|---|---|---|---|
| Description |
This function will set configure a Rx message buffer. |
||||||
| Parameters |
|
||||||
| Returns |
|
||||||
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
||||||
| Example |
|
||||||
| Notes |
|
CANx_ConfigTxMb
| Prototype |
function CANx_ConfigTxMb(mbIndex : dword; msgIdType : dword; dataLength : dword; msgId : dword) : byte; |
||||||
|---|---|---|---|---|---|---|---|
| Description |
This function will set configure a Tx message buffer. |
||||||
| Parameters |
|
||||||
| Returns |
|
||||||
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
||||||
| Example |
|
||||||
| Notes |
|
CANx_Write
| Prototype |
function CANx_Write(mbIndex : dword; msgIdType : dword; dataLength : dword; msgId : dword; dataBuffer : ^byte) : byte; |
||||||
|---|---|---|---|---|---|---|---|
| Description |
This function will initiate (start) a transmit by beginning the process of sending data. |
||||||
| Parameters |
|
||||||
| Returns |
|
||||||
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
||||||
| Example |
|
||||||
| Notes |
|
CANx_Read
| Prototype |
function CANx_Read(msgBuffIdx : dword; msgId : ^dword; dataReceived : ^byte) : byte; |
|---|---|
| Description |
This function will check for received message. If there is a message, get received data and ID. |
| Parameters |
|
| Returns |
|
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
| Example |
|
| Notes |
|
CANx_InterruptHandler
| Prototype |
procedure CANx_InterruptHandler(); |
|---|---|
| Description |
Interrupt handler for CAN. This handler read data from MB or FIFO, and then clears the interrupt flags. |
| Parameters |
None. |
| Returns |
|
| Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
| Example |
|
| Notes |
|
CAN Constants
There is a number of constants predefined in CAN library. To be able to use the library effectively, you need to be familiar with these. You might want to check the example at the end of the chapter.
CAN_OP_MODE Constants
CAN_OP_MODE constants define CAN operation mode. Function CANxSetOperationMode expects one of these as its argument:
// Stellaris Constants // Stellaris Constants _CAN_MODE_NORMAL _CAN_MODE_DISABLE _CAN_MODE_LOOP _CAN_MODE_SILENT _CAN_MODE_CONFIG _CAN_MODE_BASIC _CAN_MODE_LOOP_WITH_SILENT // ST Constants _CAN_OperatingMode_Initialization _CAN_OperatingMode_Normal _CAN_OperatingMode_Sleep
CAN_CONFIG_FLAGS Constants
CAN_CONFIG_FLAGS constants define flags related to CAN module configuration. Functions CANxInitialize and CANxSetBaudRate expect one of these (or a bitwise combination) as their argument:
_CAN_CONFIG_AUTO_RETRY_ENABLED _CAN_CONFIG_AUTO_RETRY_DISABLED
CAN_TX_MSG_FLAGS Constants
CAN_TX_MSG_FLAGS are flags related to transmission of a CAN message:
_CAN_TX_STD_FRAME _CAN_TX_XTD_FRAME _CAN_TX_NO_RTR_FRAME _CAN_TX_RTR_FRAME
You may use bitwise and to adjust the appropriate flags. For example:
// form value to be used with CANSendMessage:
send_config := _CAN_TX_XTD_FRAME and
_CAN_TX_NO_RTR_FRAME;
...
CAN1Write(id, data, 1, send_config);
CAN_RX_MSG_FLAGS Constants
CAN_RX_MSG_FLAGS are flags related to reception of CAN message. If a particular bit is set; corresponding meaning is TRUE or else it will be FALSE.
// Stellaris Constants _CAN_RX_REMOTE_FRAME // Set if this is a remote frame _CAN_RX_DATA_LOST // Set if some data is lost _CAN_RX_NEW_DATA // Indicate that there is new data in this message _CAN_RX_EXTENDED_ID // Set if extended message _CAN_RX_USE_ID_FILTER // Set the flag to indicate if ID masking was used _CAN_RX_USE_EXT_FILTER // Set if extended bit was used in filtering _CAN_RX_USE_DIR_FILTER // Set if direction filtering was enabled // ST Constants _CAN_RX_XTD_FRAME _CAN_RX_RTR_FRAME
You may use bitwise and to adjust the appropriate flags. For example:
if (MsgFlag & _CAN_RX_DATA_LOST <> 0) begin ... // We have lost our previous message. end;
CAN_MESSAGE_OBJECT Constants
CAN_MESSAGE_OBJECT constants define CAN message object configuration. Function CANxConfigureMessage expects one of these as its argument:
_CAN_CONFIG_TYPE_TX _CAN_CONFIG_TYPE_TX_REMOTE _CAN_CONFIG_TYPE_RX _CAN_CONFIG_TYPE_RX_REMOTE _CAN_CONFIG_TYPE_RXTX_REMOTE _CAN_CONFIG_FIFO _CAN_CONFIG_TX_INT_ENABLE _CAN_CONFIG_RX_INT_ENABLE _CAN_CONFIG_XTD_MSG _CAN_CONFIG_STD_MSG
CAN_MASK Constants
CAN_MASK constants define mask codes. Function CANxSetMask expects one of these as its argument:
// Stellaris Constants _CAN_CONFIG_XTD_MSG _CAN_CONFIG_STD_MSG _CAN_CONFIG_USE_DIR_FILTER // ST Constants _CAN_FILTER_ID_MASK_MODE _CAN_FILTER_ID_LIST_MODE _CAN_FILTER_USE_FIFO0 _CAN_FILTER_USE_FIFO1 _CAN_FILTER_STD_MSG _CAN_FILTER_XTD_MSG _CAN_FILTER_DISABLED _CAN_FILTER_ENABLED
CAN_FILTER Constants
CAN_FILTER constants define filter codes. Function CANxSetFilter expects one of these as its argument:
_CAN_CONFIG_XTD_MSG _CAN_CONFIG_STD_MSG // ST Constants _CAN_FILTER_ID_MASK_MODE _CAN_FILTER_ID_LIST_MODE _CAN_FILTER_USE_FIFO1 _CAN_FILTER_DISABLED _CAN_FILTER_ENABLED
Library Example
The example demonstrates CAN protocol. The 1st 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. The 1st node then does the same and sends incremented data back to the 2nd node, etc.
Code for the first CAN node:
Stellaris
program CAN_1st;
var Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags, Rx_Data_Len : dword;
RxTx_Data : array[8] of byte;
Rx_ID : longint;
Msg_Rcvd : dword;
const ID_1st : longint = 12111;
const ID_2nd : longint = 3; // node IDs
begin
GPIO_Digital_Output(@GPIO_PORTJ, _GPIO_PINMASK_ALL);
GPIO_PORTJ_DATA := 0;
Can_Init_Flags := _CAN_CONFIG_AUTO_RETRY_ENABLED; // CAN init flags
Can_Send_Flags := _CAN_TX_XTD_FRAME and _CAN_TX_NO_RTR_FRAME;// form value to be used with CAN0Write
Can_Rcv_Flags := 0;
CAN0Initialize(1,3,3,3,1,Can_Init_Flags); // initialize CAN
CAN0SetOperationMode(_CAN_MODE_CONFIG,0xFF); // set CONFIGURATION mode
CAN0ConfigureMessage(1, _CAN_CONFIG_XTD_MSG and _CAN_CONFIG_TYPE_TX); // configure message object for transmitting
CAN0ConfigureMessage(2, _CAN_CONFIG_XTD_MSG and _CAN_CONFIG_TYPE_RX); // congigure message object for receiving
CAN0SetMask(2, 0xFFFFFFFF, _CAN_CONFIG_XTD_MSG); // set all mask bits to ones
CAN0SetFilter(2, ID_2nd, _CAN_CONFIG_XTD_MSG); // set filter
CAN0SetOperationMode(_CAN_MODE_NORMAL,0xFF); // set NORMAL mode
RxTx_Data[0] := 9;
CAN0WriteMessage(1, ID_1st, RxTx_Data, 1, Can_Send_Flags);
while TRUE do
begin
Msg_Rcvd := CAN0ReadMessage(2, Rx_ID , RxTx_Data , Rx_Data_Len, Can_Rcv_Flags); // receive message
if ((Rx_ID = ID_2nd) and (Msg_Rcvd <> 0)) <> 0 then // if message received check id
begin
GPIO_PORTJ_DATA := RxTx_Data[0]; // id correct, output data at PORTJ
RxTx_Data[0] := RxTx_Data[0] + 1; // increment received data
Delay_ms(10);
CAN0WriteMessage(1, ID_1st, RxTx_Data, 1, Can_Send_Flags); // send incremented data back
end;
end;
end.
STM32
program CAN1;
{ Declarations section }
var
Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags : dword; // can flags
Rx_Data_Len : byte; // received data length in bytes
RxTx_Data: array[8] of byte; // can rx/tx data buffer
Msg_Rcvd : byte; // reception flag
Rx_ID : dword;
const ID_1st : dword = 12111;
const ID_2nd : dword = 3; // node IDs
begin
GPIO_Digital_Output(@GPIOE_BASE, 0xFF00);
GPIOE_ODR := 0;
Can_Init_Flags := 0; //
Can_Send_Flags := 0; // clear flags
Can_Rcv_Flags := 0; //
Can_Send_Flags := _CAN_TX_XTD_FRAME and // with CANWrite
_CAN_TX_NO_RTR_FRAME;
Can_Init_Flags := _CAN_CONFIG_AUTOMATIC_RETRANSMISSION and // form value to be used
_CAN_CONFIG_RX_FIFO_NOT_LOCKED_ON_OVERRUN and // with CANInit
_CAN_CONFIG_TIME_TRIGGERED_MODE_DISABLED and
_CAN_CONFIG_TX_FIFO_PRIORITY_BY_IDINTIFIER and
_CAN_CONFIG_WAKE_UP;
CAN1InitializeAdvanced(1,5,4,4,1,Can_Init_Flags, @_GPIO_MODULE_CAN1_PD01); // Initialize CAN module
CAN1SetOperationMode(_CAN_OperatingMode_Initialization); // set CONFIGURATION mode
CANSetFilter(0, ID_2nd,_CAN_FILTER_ENABLED and _CAN_FILTER_ID_MASK_MODE and _CAN_FILTER_XTD_MSG);
CANSetMask(0, 0xFFFFFFFF,_CAN_FILTER_ENABLED and _CAN_FILTER_ID_MASK_MODE and _CAN_FILTER_XTD_MSG);
CAN1SetOperationMode(_CAN_OperatingMode_Normal); // set NORMAL mode
RxTx_Data[0] := 9; // set initial data to be sent
CAN1Write(ID_1st, RxTx_Data, 1, Can_Send_Flags); // send initial message
while(true) do
begin // endless loop
Msg_Rcvd := CAN1Read(0, Rx_ID , RxTx_Data , Rx_Data_Len, Can_Rcv_Flags); // receive message
if ((Rx_ID = ID_2nd) and (Msg_Rcvd <> 0)) then
begin // if message received check id
GPIOE_ODR := word(RxTx_Data[0]) shl 8; // id correct, output data at PORTE
inc(RxTx_Data[0]) ; // increment received data
Delay_ms(10);
CAN1Write(ID_1st, RxTx_Data, 1, Can_Send_Flags); // send incremented data back
end;
end;
end.
Code for the second CAN node:
Stellaris
program Can_2nd;
var Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags, Rx_Data_Len : dword;
RxTx_Data : array[8] of byte;
Rx_ID : longint;
Msg_Rcvd : dword;
const ID_1st : longint = 12111;
const ID_2nd : longint = 3; // node IDs
begin
GPIO_Digital_Output(@GPIO_PORTJ, _GPIO_PINMASK_ALL);
GPIO_PORTJ_DATA := 0;
Can_Init_Flags := _CAN_CONFIG_AUTO_RETRY_ENABLED; // CAN init flags
Can_Send_Flags := _CAN_TX_XTD_FRAME and _CAN_TX_NO_RTR_FRAME;// form value to be used with CAN0Write
Can_Rcv_Flags := 0;
CAN0Initialize(1,3,3,3,1,Can_Init_Flags); // initialize CAN
CAN0SetOperationMode(_CAN_MODE_CONFIG,0xFF); // set CONFIGURATION mode
CAN0ConfigureMessage(1, _CAN_CONFIG_XTD_MSG and _CAN_CONFIG_TYPE_TX); // configure message object for transmitting
CAN0ConfigureMessage(2, _CAN_CONFIG_XTD_MSG and _CAN_CONFIG_TYPE_RX); // congigure message object for receiving
CAN0SetMask(2, 0xFFFFFFFF, _CAN_CONFIG_XTD_MSG); // set all mask bits to ones
CAN0SetFilter(2, ID_1st, _CAN_CONFIG_XTD_MSG); // set filter
CAN0SetOperationMode(_CAN_MODE_NORMAL,0xFF); // set NORMAL mode
while TRUE do
begin
Msg_Rcvd := CAN0ReadMessage(2, Rx_ID , RxTx_Data , Rx_Data_Len, Can_Rcv_Flags);// receive message
if ((Rx_ID = ID_1st) and (Msg_Rcvd <> 0)) <> 0 then // if message received check id
begin
GPIO_PORTJ_DATA := RxTx_Data[0]; // id correct, output data at PORTJ
RxTx_Data[0] := RxTx_Data[0] + 1; // increment received data
CAN0WriteMessage(1, ID_2nd, RxTx_Data, 1, Can_Send_Flags); // send incremented data back
end;
end;
end.
STM32
program CAN2;
{ Declarations section }
var
Can_Init_Flags : dword;
Can_Send_Flags, Can_Rcv_Flags : byte; // can flags
Rx_Data_Len : byte; // received data length in bytes
RxTx_Data: array[8] of byte; // can rx/tx data buffer
Msg_Rcvd : byte; // reception flag
Rx_ID : dword;
const ID_1st : dword = 12111;
const ID_2nd : dword = 3; // node IDs
begin
GPIO_Digital_Output(@GPIOE_BASE, 0xFF00);
GPIOE_ODR := 0;
Can_Init_Flags := 0; //
Can_Send_Flags := 0; // clear flags
Can_Rcv_Flags := 0; //
Can_Send_Flags := _CAN_TX_XTD_FRAME and // with CANWrite
_CAN_TX_NO_RTR_FRAME;
Can_Init_Flags := _CAN_CONFIG_AUTOMATIC_RETRANSMISSION and // form value to be used
_CAN_CONFIG_RX_FIFO_NOT_LOCKED_ON_OVERRUN and // with CANInit
_CAN_CONFIG_TIME_TRIGGERED_MODE_DISABLED and
_CAN_CONFIG_TX_FIFO_PRIORITY_BY_IDINTIFIER and
_CAN_CONFIG_WAKE_UP;
CAN1InitializeAdvanced(1,5,4,4,1,Can_Init_Flags, @_GPIO_MODULE_CAN1_PD01); // Initialize CAN module
CAN1SetOperationMode(_CAN_OperatingMode_Initialization); // set CONFIGURATION mode
CANSetFilter(0, ID_1st,_CAN_FILTER_ENABLED and _CAN_FILTER_ID_MASK_MODE and _CAN_FILTER_XTD_MSG);
CANSetMask(0, 0xFFFFFFFF,_CAN_FILTER_ENABLED and _CAN_FILTER_ID_MASK_MODE and _CAN_FILTER_XTD_MSG);
CAN1SetOperationMode(_CAN_OperatingMode_Normal); // set NORMAL mode
while(true) do
begin // endless loop
Msg_Rcvd := CAN1Read(0, Rx_ID , RxTx_Data , Rx_Data_Len, Can_Rcv_Flags); // receive message
if ((Rx_ID = ID_1st) and (Msg_Rcvd <> 0)) then
begin // if message received check id
GPIOE_ODR := word(RxTx_Data[0]) shl 8; // id correct, output data at PORTE
inc(RxTx_Data[0]) ; // increment received data
CAN1Write(ID_2nd, RxTx_Data, 1, Can_Send_Flags); // send incremented data back
end;
end;
end.

