CAN Library

The mikroC 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:

  Important :

Library Routines

CANxSetOperationMode

Prototype // for Stellaris MCUs :

void CANxSetOperationMode(unsigned long mode, unsigned long wait_flag);

// for ST MCUs :

unsigned char CANxSetOperationMode(unsigned char CAN_OperatingMode);

Description

Sets the CAN module to requested mode.

Parameters
  • mode: CAN module operation mode. Valid values: CAN_OP_MODE constants. See CAN_OP_MODE constants.
  • CAN_OperatingMode: CAN module operation mode for ST MCUs. Valid values: CAN_OP_MODE constants. See CAN_OP_MODE constants.
  • wait_flag: CAN mode switching verification request. If WAIT == 0, the call is non-blocking. The function does not verify if the CAN module is switched to requested mode or not. Caller must use CANxGetOperationMode to verify correct operation mode before performing mode specific operation. If WAIT != 0, the call is blocking – the function won’t “return” until the requested mode is set.
Returns
  • 0: if operation was successul,
  • 1: if operation has failed.
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
  • CAN library routine 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.

CANxGetOperationMode

Prototype

unsigned int CANxGetOperationMode();

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) {
  ...
}
Notes
  • CAN library routine 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.
  • Valid only for Stellaris devices.

CANxInitialize

Prototype // for Stellaris MCUs with dedicated PORT functions and ST MCUs:

void CANxInitialize(unsigned int SJW, unsigned int BRP, unsigned int PHSEG1, unsigned int PHSEG2, unsigned int PROPSEG, unsigned long flags);

// for Stellaris MCUs with alternative PORT functions on GPIO pins :

void CANxInitialize(unsigned int SJW, BRP, PHSEG1, PHSEG2, PROPSEG, unsigned long flags, const Module_Struct *module);

Description

Initializes the CAN module.

The internal CAN module is set to :

  • Disable CAN capture
  • Continue CAN operation in Idle mode
  • Do not abort pending transmissions
  • Fcan clock : 4*Tcy (Fosc)
  • Baud rate is set according to given parameters
  • CAN mode is set to Normal
  • Filter and mask registers IDs are set to zero
  • Filter and mask message frame type is set according to CAN_CONFIG_FLAGS value

SAM, SEG2PHTS, WAKFIL and DBEN bits are set according to CAN_CONFIG_FLAGS value.

Parameters
  • SJW as defined in MCU's datasheet (CAN Module)
  • BRP as defined in MCU's datasheet (CAN Module)
  • PHSEG1 as defined in MCU's datasheet (CAN Module)
  • PHSEG2 as defined in MCU's datasheet (CAN Module)
  • PROPSEG as defined in MCU's datasheet (CAN Module)
  • flags is formed from predefined constants. See CAN_CONFIG_FLAGS constants.
  • module: appropriate module pinout. Use Code Assistant to list available module pinouts by typing _GPIO_MODULE_CAN and pressing Ctrl + Space.
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
unsigned int can_config_flags;
...  
Can_Init_Flags = _CAN_CONFIG_SAMPLE_THRICE &            // Form value to be used
                   _CAN_CONFIG_PHSEG2_PRG_ON &            // with CAN1Initialize
                   _CAN_CONFIG_XTD_MSG &
                   _CAN_CONFIG_MATCH_MSG_TYPE &
                   _CAN_CONFIG_LINE_FILTER_OFF;

CAN1Initialize(1,3,3,3,1,Can_Init_Flags);              // initialize the CAN1 module
Notes
  • CAN mode NORMAL will be set on exit.
  • CAN library routine 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.

CANxInitializeAdvanced

Prototype

void CAN1InitializeAdvanced(unsigned int SJW, BRP, PHSEG1, PHSEG2, PROPSEG, unsigned long flags, const Module_Struct *module);

Description

Initializes the CAN with desired CAN module pinout for ST devices.

The internal CAN module is set to :

  • Disable CAN capture
  • Continue CAN operation in Idle mode
  • Do not abort pending transmissions
  • Fcan clock : 4*Tcy (Fosc)
  • Baud rate is set according to given parameters
  • CAN mode is set to Normal
  • Filter and mask registers IDs are set to zero
  • Filter and mask message frame type is set according to CAN_CONFIG_FLAGS value

SAM, SEG2PHTS, WAKFIL and DBEN bits are set according to CAN_CONFIG_FLAGS value.

Parameters
  • SJW as defined in MCU's datasheet (CAN Module)
  • BRP as defined in MCU's datasheet (CAN Module)
  • PHSEG1 as defined in MCU's datasheet (CAN Module)
  • PHSEG2 as defined in MCU's datasheet (CAN Module)
  • PROPSEG as defined in MCU's datasheet (CAN Module)
  • flags is formed from predefined constants. See CAN_CONFIG_FLAGS constants.
  • module: appropriate module pinout. Use Code Assistant to list available module pinouts by typing _GPIO_MODULE_CAN and pressing Ctrl + Space.
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
unsigned int can_config_flags;
...  
Can_Init_Flags = _CAN_CONFIG_SAMPLE_THRICE &            // Form value to be used
                   _CAN_CONFIG_PHSEG2_PRG_ON &            // with CAN1Initialize
                   _CAN_CONFIG_XTD_MSG &
                   _CAN_CONFIG_MATCH_MSG_TYPE &
                   _CAN_CONFIG_LINE_FILTER_OFF;

CAN1Initialize(1,3,3,3,1,Can_Init_Flags, @_GPIO_MODULE_CAN1_PA11_12);              // initialize the CAN1 module
Notes
  • CAN mode NORMAL will be set on exit.
  • CAN library routine 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.
  • Valid only for Stellaris devices.

CANxSetBaudRate

Prototype

void CANxSetBaudRate(unsigned int SJW, unsigned int BRP, unsigned int PHSEG1, unsigned int PHSEG2, unsigned int PROPSEG);

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.

SAM, SEG2PHTS and WAKFIL bits are set according to CAN_CONFIG_FLAGS value. Refer to datasheet for details.

Valid only for Stellaris devices.

Parameters
  • SJW as defined in MCU's datasheet (CAN Module)
  • BRP as defined in MCU's datasheet (CAN Module)
  • PHSEG1 as defined in MCU's datasheet (CAN Module)
  • PHSEG2 as defined in MCU's datasheet (CAN Module)
  • PROPSEG as defined in MCU's datasheet (CAN Module)
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
  • CAN library routine 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.
  • Valid only for Stellaris devices.

CANxSetMask

Prototype

void CANxSetMask(unsigned long objID, unsigned long maskValue, unsigned long flags);

Description

The function configures appropriate mask for advanced message filtering.

Valid only for Stellaris devices.

Parameters
  • objID: message object ID values. Valid values : 1 - 32.
  • maskValue: mask value for message object.
  • flags: selects type of message to filter. See CAN_MASK constants.
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(_CAN_MASK_0, -1, _CAN_CONFIG_USE_DIR_FILTER & _CAN_CONFIG_XTD_MSG);
Notes
  • CAN library routine 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.
  • Valid only for Stellaris devices.

CANSetMask

Prototype

void CANSetMask(unsigned char Filter_Number, unsigned long maskValue, unsigned char CAN_FILTAR_FLAGS);

Description

The function configures appropriate mask for advanced message filtering.

Valid only for ST devices.

Parameters
  • Filter_Number: filter number.
  • maskValue: mask value for message object.
  • CAN_FILTAR_FLAGS: selects type of message to filter. See CAN_MASK constants.
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
  • CAN library routine 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.
  • Valid only for Stellaris devices.

CANxSetFilter

Prototype

void CANxSetFilter(unsigned long objID, unsigned long filterValue, unsigned long flags);

Description

Function sets message filter. Given filterValue is bit adjusted to appropriate buffer mask registers.

Valid only for Stellaris devices.

Parameters
  • objID: message object ID values. Valid values : 1 - 32.
  • filterValue: filter value for message object.
  • flags: selects type of message to filter. See CAN_FILTER constants.
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
  • CAN library routine 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.
  • Valid only for Stellaris devices.

CANSetFilter

Prototype

void CANSetFilter(unsigned char Filter_Number, unsigned long ID, unsigned long CAN_FILTAR_FLAGS);

Description

Function sets message filter. Given filterValue is bit adjusted to appropriate buffer mask registers.

Valid only for ST devices.

Parameters
  • Filter_Number: Filter number.
  • ID: filter ID.
  • CAN_FILTAR_FLAGS: selects type of message to filter. See CAN_FILTER constants.
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
  • CAN library routine 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.
  • Valid only for ST devices.

CANSetFilterScale16

Prototype

void CANSetFilter(unsigned char Filter_Number, unsigned char CAN_FILTAR_FLAGS, unsigned long ID, unsigned long mask_or_ID, unsigned long ID1, unsigned long mask1_or_ID1);

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
  • Filter_Number: Filter number.
  • CAN_FILTAR_FLAGS: selects type of message to filter. See CAN_FILTER constants.
  • ID: and ID1: filter IDs.
  • mask_or_ID and mask_or_ID: Id/Mask mode or Identifier list mode for the filters.
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
  • CAN library routine 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.
  • Valid only for ST devices.

CANSetFilterScale32

Prototype

void CANSetFilter(unsigned char Filter_Number, unsigned char CAN_FILTAR_FLAGS, unsigned long ID, unsigned long mask_or_ID);

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
  • Filter_Number: Filter number.
  • CAN_FILTAR_FLAGS: selects type of message to filter. See CAN_FILTER constants.
  • ID: filter ID.
  • mask_or_ID: Id/Mask mode or Identifier list mode for the filter.
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
  • CAN library routine 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.
  • When using this function and CAN2 module, used filters must be in the range from 14 to 27, while for the CAN1 module filters must be in the range from 0 to 13.
  • Valid only for ST devices.

CANxReadMessage

Prototype

unsigned long CANxReadMessage(unsigned long objID, unsigned long *msgId, unsigned char *pMsgData, unsigned long *msgLen, unsigned long *flags);

Description

The function reads message from the desired message object and processes it in the following way :

  • Object ID is retrieved and stored to location pointed by objID pointer.
  • Message ID is retrieved and stored to location pointed by msgId pointer.
  • Message data is retrieved and stored to array pointed by pMsgData pointer.
  • Message length is retrieved and stored to location pointed by msgLen pointer.
  • Message flags are retrieved and stored to location pointed by flags pointer.

Valid only for Stellaris devices.

Parameters
  • objID: message object ID to be read from. Valid values : 1 - 32.
  • msgId: message object memory address.
  • pMsgData: message data address.
  • msgLen: message length address.
  • flags: message flags address. For message receive flags format refer to CAN_RX_MSG_FLAGS constants.
Returns
  • 0 if nothing is received.
  • 0xFFFF if message is received.
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)) {
  ...
}
Notes
  • CAN library routine 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.
  • Valid only for Stellaris devices.

CANxRead

Prototype // for Stellaris devices

unsigned long CANxRead(unsigned long *msgId, unsigned char *pMsgData, unsigned long *msgLen, unsigned long *flags);

// for ST devices

unsigned char CANxRead(unsigned char FIFONumber, unsigned long *id, unsigned char *data_, unsigned long *datalen, unsigned long *CAN_RX_MSG_FLAGS);

Description

The function reads and processes the message from the first message object configured for reception that has received data in the following way :

  • Message ID is retrieved and stored to location pointed by msgId (id) pointer.
  • Message data is retrieved and stored to array pointed by pMsgData (data_) pointer.
  • Message length is retrieved and stored to location pointed by msgLen (datalen) pointer.
  • Message flags are retrieved and stored to location pointed by flags (CAN_RX_MSG_FLAGS) pointer.
Parameters
  • FIFONumber: number of FIFO register. Valid only for ST devices.
  • msgId: message object memory address.
  • pMsgData: message data address.
  • msgLen: message length address.
  • flags: message flags address. For message receive flags format refer to CAN_RX_MSG_FLAGS constants.
Returns
  • 0 if nothing is received.
  • 0xFFFF if message is received.
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. 
unsigned int msg_rcvd, rx_flags, data_len;
char data[8];
unsigned long msg_id;
...
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)) {
  ...
}
Notes
  • CAN library routine 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.

CANxWriteMessage

Prototype

unsigned long CANxWriteMessage(unsigned long objID, unsigned long msgID, char *pMsgData, unsigned long msgLen, unsigned long flags);

Description

The function writes the message to the desired message object.

Valid only for Stellaris devices.

Parameters
  • objID: message object ID to be written to. Valid values : 1 - 32.
  • msgId: message ID.
  • pMsgData: message data address.
  • msgLen: message length.
  • flags: message flags. For message transmit flags format refer to CAN_TX_MSG_FLAGS constants.
Returns
  • 0 if all message objects are busy.
  • 0xFFFF if at least one message object is available.
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
unsigned int tx_flags;
char data[8];
unsigned long msg_id;
...
CAN1SetOperationMode(_CAN_MODE_NORMAL,0xFF);                  // set NORMAL mode (CAN1 must be in mode in which transmission is possible)

tx_flags = _CAN_TX_PRIORITY_0 &            
           _CAN_TX_XTD_FRAME &             
           _CAN_TX_NO_RTR_FRAME;                       // set message flags
CAN1WriteMessage(msg_id, data, _CAN_BUFFER_0, 1, tx_flags);
Notes
  • CAN library routine 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.
  • Valid only for Stellaris devices.

CANxWrite

Prototype

unsigned long CANxWrite(unsigned long msgID, char *pMsgData, unsigned long msgLen, unsigned long flags);

Description

The function writes message in the first available message object configured for transmission.

Parameters
  • msgId: message ID.
  • pMsgData: message data address.
  • msgLen: message length.
  • flags: message flags. For message transmit flags format refer to CAN_TX_MSG_FLAGS constants.
Returns
  • Return the number of mailbox that is used for transmission - 0, 1 or 2.
  • If there are no empty mailboxes, function will return 0xFF.
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
unsigned int tx_flags;
char data[8];
unsigned long msg_id;
...
CAN1SetOperationMode(_CAN_MODE_NORMAL,0xFF);                  // set NORMAL mode (CAN1 must be in mode in which transmission is possible)

tx_flags = _CAN_TX_PRIORITY_0 &            
           _CAN_TX_XTD_FRAME &             
           _CAN_TX_NO_RTR_FRAME;                       // set message flags
CAN1Write(msg_id, data, 1, tx_flags);
Notes
  • CAN library routine 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.

CANxConfigureMessage

Prototype

void CANxConfigureMessage(unsigned long objID, unsigned long flags);

Description

The function configures message object.

Valid only for Stellaris devices.

Parameters
  • objID: message object. Valid values : 1 - 32.
  • flags: message flags. For message flags format refer to CAN_MESSAGE_OBJECT constants.
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
  • CAN library routine 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.
  • Valid only for Stellaris devices.

CANxBitRateSet

Prototype

unsigned long CANxBitRateSet(unsigned long ulSourceClock, unsigned long bitRate);

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
  • ulSourceClock: system clock for the device in Hz.
  • BitRate: desired bit rate.
Returns
  • conifgured bit rate or 0 if the bit rate is not valid
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 library routine 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.
  • Valid only for Stellaris devices.

CANSlaveStartBank

Prototype

void CANSlaveStartBank(unsigned char CAN_BankNumber);

Description

This routine is used to set the starting bank filter for the CAN slave module.

Valid only for ST devices.

Parameters
  • CAN_BankNumber: start slave bank filter from 1..27.
Returns
  • conifgured bit rate or 0 if the bit rate is not valid
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 library routine 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.
  • Valid only for ST devices.

CANx_EnableOperationMode

Prototype

void CANx_EnableOperationMode(unsigned long operationMode);

Description

This routine is used to enable a CAN operation mode.

Parameters
  • operationMode: desiured operation mode. Valid values :
    Value Description
    _CAN_CFG_MODE_NORMAL Normal mode (User or Supervisor).
    _CAN_CFG_MODE_LISTENONLY Listen-Only mode.
    _CAN_CFG_MODE_LOOPBACK Loop-Back mode.
    _CAN_CFG_MODE_FREEZE Freeze mode.
    _CAN_CFG_MODE_DISABLE Module Disable mode.
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
  • CAN library routine 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.
  • Number of CAN modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • Valid only for Kinetis devices.

CANx_ExitOperationMode

Prototype

unsigned char CANx_ExitOperationMode(unsigned long operationMode);

Description

This routine is used to disable a CAN operation mode.

Parameters
  • operationMode: desiured operation mode. Valid values :
    Value Description
    _CAN_CFG_MODE_NORMAL Normal mode (User or Supervisor).
    _CAN_CFG_MODE_LISTENONLY Listen-Only mode.
    _CAN_CFG_MODE_LOOPBACK Loop-Back mode.
    _CAN_CFG_MODE_FREEZE Freeze mode.
    _CAN_CFG_MODE_DISABLE Module Disable mode.
Returns
  • _CAN_STATUS_OK: if operation was performed correctly,
  • _CAN_STATUS_INVALIDE_ARG: if argument is invalid.
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 library routine 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.
  • Number of CAN modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • Valid only for Kinetis devices.

CANx_Init

Prototype

unsigned char CANx_Init(unsigned long config, unsigned long maxMsgBuff);

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
  • config: desiured operation mode. Valid values :
    Value Description
    _CAN_CFG_CLOCK_OSC Normal mode (User or Supervisor).
    _CAN_CFG_CLOCK_PERIPHERAL_CLK Normal mode (User or Supervisor).
    _CAN_CFG_MODE_NORMAL Normal mode (User or Supervisor).
    _CAN_CFG_MODE_LISTENONLY Listen-Only mode.
    _CAN_CFG_MODE_LOOPBACK Loop-Back mode.
    _CAN_CFG_MODE_FREEZE Freeze mode.
    _CAN_CFG_MODE_DISABLE Module Disable mode.
  • maxMsgBuff: maximum number of message buffers.
Returns
  • _CAN_STATUS_OK: if operation was performed correctly,
  • _CAN_STATUS_INVALIDE_ARG: if argument is invalid.
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 library routine 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.
  • Number of CAN modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • Valid only for Kinetis devices.

CANx_Init_Advanced

Prototype

unsigned char CANx_Init_Advanced(unsigned long config, unsigned long maxMsgBuff, Module_Struct *module);

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
  • config: desiured operation mode. Valid values :
    Value Description
    _CAN_CFG_CLOCK_OSC Normal mode (User or Supervisor).
    _CAN_CFG_CLOCK_PERIPHERAL_CLK Normal mode (User or Supervisor).
    _CAN_CFG_MODE_NORMAL Normal mode (User or Supervisor).
    _CAN_CFG_MODE_LISTENONLY Listen-Only mode.
    _CAN_CFG_MODE_LOOPBACK Loop-Back mode.
    _CAN_CFG_MODE_FREEZE Freeze mode.
    _CAN_CFG_MODE_DISABLE Module Disable mode.
  • maxMsgBuff: maximum number of message buffers.
  • module: appropriate module pinout. Use Code Assistant to list available module pinouts by typing _GPIO_MODULE_CAN and pressing Ctrl + Space.
Returns
  • _CAN_STATUS_OK: if operation was performed correctly,
  • _CAN_STATUS_INVALIDE_ARG: if argument is invalid.
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 library routine 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.
  • Number of CAN modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • Valid only for Kinetis devices.

CANx_SetBitRate

Prototype

void CANx_SetBitRate(unsigned long propSeg, unsigned long phaseSeg2, unsigned long phaseSeg1, unsigned long preDivider, unsigned long RJW);

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
  • propSeg Propagation Segment, as defined in MCU's datasheet.
  • phaseSeg2 Phase Segment 2, as defined in MCU's datasheet.
  • phaseSeg1 Phase Segment 1, as defined in MCU's datasheet.
  • preDivider Prescaler Division Factor, as defined in MCU's datasheet.
  • RJW Resync Jump Width, as defined in MCU's datasheet.
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
  • CAN library routine 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.
  • Number of CAN modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • Valid only for Kinetis devices.

CANx_SetRxMaskType

Prototype

void CANx_SetRxMaskType(unsigned long type);

Description

This function will set the RX masking type.

Parameters
  • type: RX masking type. Valid values :
    Value Description
    _CAN_RXMASK_GLOBAL Global masking.
    _CAN_RXMASK_INDIVIDUAL Individual masking.
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
  • CAN library routine 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.
  • Number of CAN modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • Valid only for Kinetis devices.

CANx_SetRxIndividualMask

Prototype

void CANx_SetRxIndividualMask(unsigned long msgBuffIdType, unsigned long msgBuffIdx, unsigned long mask);

Description

This function will Set Rx individual mask as the 11-bit standard mask or the 29-bit extended mask.

Parameters
  • msgBuffIdType: RX individual masking type. Valid values :
    Value Description
    _CAN_MSGID_STD Standard mask.
    _CAN_MSGID_EXT Extended mask.
  • msgBuffIdx: message buffer index.
  • mask: 11bit or 29bit format.
Returns
  • _CAN_STATUS_OK: if operation was performed correctly,
  • _CAN_STATUS_INVALIDE_ARG: if argument is invalid.
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 library routine 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.
  • Number of CAN modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • Valid only for Kinetis devices.

CANx_GetReceiveStatus

Prototype

unsigned char CANx_GetReceiveStatus();

Description

This function will check if CAN is receiving message.

Parameters

None.

Returns
  • 0: CAN is not receiving a message,
  • 1: CAN is receiving a message.
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 library routine 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.
  • Number of CAN modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • Valid only for Kinetis devices.

CANx_SetRxMbGlobalMask

Prototype

unsigned char CANx_SetRxMbGlobalMask(unsigned long idType, unsigned long mask);

Description

This function will set Rx message buffer global mask as the 11-bit standard mask or the 29-bit extended mask.

Parameters
  • idType: RX individual masking type. Valid values :
    Value Description
    _CAN_MSGID_STD Standard mask.
    _CAN_MSGID_EXT Extended mask.
  • mask: 11bit or 29bit format.
Returns
  • _CAN_STATUS_OK: if operation was performed correctly,
  • _CAN_STATUS_INVALIDE_ARG: if argument is invalid.
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 library routine 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.
  • Number of CAN modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • Valid only for Kinetis devices.

CANx_ConfigRxMb

Prototype

unsigned char CANx_ConfigRxMb(unsigned long mbIndex, unsigned long msgIdType, unsigned long dataLength, unsigned long msgId);

Description

This function will set configure a Rx message buffer.

Parameters
  • mbIndex: message buffer index.
  • msgIdType: RX individual masking type. Valid values :
    Value Description
    _CAN_MSGID_STD Standard mask.
    _CAN_MSGID_EXT Extended mask.
  • dataLength: length of data.
  • msgId: message ID.
Returns
  • _CAN_STATUS_OK: if operation was performed correctly,
  • _CAN_STATUS_INVALIDE_ARG: if argument is invalid.
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 library routine 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.
  • Number of CAN modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • This function will set up the message buffer fields, configure the message buffer code for Rx message buffer as NOT_USED, enable the Message Buffer interrupt,
    configure the message buffer code for Rx message buffer as INACTIVE, copy user's buffer into the message buffer data area, and configure the message buffer code for Rx message buffer as EMPTY.
  • Valid only for Kinetis devices.

CANx_ConfigTxMb

Prototype

unsigned char CANx_ConfigTxMb(unsigned long mbIndex, unsigned long msgIdType, unsigned long dataLength, unsigned long msgId);

Description

This function will set configure a Tx message buffer.

Parameters
  • mbIndex: message buffer index.
  • msgIdType: TX individual masking type. Valid values :
    Value Description
    _CAN_MSGID_STD Standard mask.
    _CAN_MSGID_EXT Extended mask.
  • dataLength: length of data.
  • msgId: message ID.
Returns
  • _CAN_STATUS_OK: if operation was performed correctly,
  • _CAN_STATUS_INVALIDE_ARG: if argument is invalid.
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 library routine 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.
  • Number of CAN modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • This function will set up the message buffer fields, configure the message buffer code for Tx buffer as INACTIVE, and enable the Message Buffer interrupt.
  • Valid only for Kinetis devices.

CANx_Write

Prototype

unsigned char CANx_Write(unsigned long mbIndex, unsigned long msgIdType, unsigned long dataLength, unsigned long msgId, unsigned char *dataBuffer);

Description

This function will initiate (start) a transmit by beginning the process of sending data.

Parameters
  • mbIndex: message buffer index.
  • msgIdType: individual masking type. Valid values :
    Value Description
    _CAN_MSGID_STD Standard mask.
    _CAN_MSGID_EXT Extended mask.
  • dataLength: length of data.
  • msgId: message ID.
  • dataBuffer: pointer to data.
Returns
  • _CAN_STATUS_OK: if operation was performed correctly,
  • _CAN_STATUS_INVALIDE_ARG: if argument is invalid.
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 library routine 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.
  • Number of CAN modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • Valid only for Kinetis devices.

CANx_Read

Prototype

unsigned char CANx_Read(unsigned long msgBuffIdx, unsigned long *msgId, unsigned char *dataReceived);

Description

This function will check for received message. If there is a message, get received data and ID.

Parameters
  • msgBuffIdx: message buffer index.
  • msgId: message ID.
  • dataBuffer: retrieved and stored to location pointed by this pointer.
Returns
  • _CAN_STATUS_OK: if operation was performed correctly,
  • _CAN_STATUS_INVALIDE_ARG: if argument is invalid.
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 library routine 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.
  • Number of CAN modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • Valid only for Kinetis devices.

CANx_InterruptHandler

Prototype

void CANx_InterruptHandler();

Description

Interrupt handler for CAN. This handler read data from MB or FIFO, and then clears the interrupt flags.

Parameters

None.

Returns
  • _CAN_STATUS_OK: if operation was performed correctly,
  • _CAN_STATUS_INVALIDE_ARG: if argument is invalid.
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 library routine 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.
  • Number of CAN modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • Valid only for Kinetis devices.

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:

Copy Code To ClipboardCopy Code To Clipboard
// 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:

Copy Code To ClipboardCopy Code To Clipboard
_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:

Copy Code To ClipboardCopy Code To Clipboard
_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:

Copy Code To ClipboardCopy Code To Clipboard
// form value to be used with CANSendMessage:
send_config = _CAN_TX_XTD_FRAME  &
              _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.

Copy Code To ClipboardCopy Code To Clipboard
// 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:

Copy Code To ClipboardCopy Code To Clipboard
if (MsgFlag & _CAN_RX_DATA_LOST != 0) {
  ...
  // We have lost our previous message.
}

CAN_MESSAGE_OBJECT Constants

CAN_MESSAGE_OBJECT constants define CAN message object configuration. Function CANxConfigureMessage expects one of these as its argument:

Copy Code To ClipboardCopy Code To Clipboard
  _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:

Copy Code To ClipboardCopy Code To Clipboard
// 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:

Copy Code To ClipboardCopy Code To Clipboard
_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

unsigned int Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags;  // can flags
unsigned int Rx_Data_Len;                                    // received data length in bytes
char RxTx_Data[8];   
char Msg_Rcvd;                                               // reception flag
const unsigned long ID_1st = 12111, ID_2nd = 3;              // node IDs
unsigned long Rx_ID;

void main() {
  GPIO_Digital_Output(&GPIO_PORTJ, _GPIO_PINMASK_ALL);
  GPIO_PORTJ_DATA = 0;

  Can_Rcv_Flags  = 0;

  Can_Send_Flags = _CAN_TX_XTD_FRAME & _CAN_TX_NO_RTR_FRAME;  // form value to be used with CAN0Write

  Can_Init_Flags = _CAN_CONFIG_AUTO_RETRY_ENABLED;           // CAN init flags

  CAN0Initialize(1,3,3,3,1,Can_Init_Flags);                   // initialize CAN0

  CAN0SetOperationMode(_CAN_MODE_CONFIG,0xFF);                // set CONFIGURATION mode

  CAN0ConfigureMessage(1, _CAN_CONFIG_XTD_MSG & _CAN_CONFIG_TYPE_TX); // configure message object for transmitting
  CAN0ConfigureMessage(2, _CAN_CONFIG_XTD_MSG & _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] = 0xFE;
  CAN0WriteMessage(1, ID_1st, RxTx_Data, 1, Can_Send_Flags);
  
  while(1){
    Msg_Rcvd = CAN0ReadMessage(2, &Rx_ID, RxTx_Data, &Rx_Data_Len, &Can_Rcv_Flags);  // receive message
    if ((Rx_ID == ID_2nd) && Msg_Rcvd) {                                   // if message received check id
      GPIO_PORTJ_DATA = RxTx_Data[0];                                      // id correct, output data at PORTJ
      RxTx_Data[0]++;                                                      // increment received data
      delay_ms(10);
      CAN0WriteMessage(1, ID_1st, RxTx_Data, 1, Can_Send_Flags);                     // send incremented data back
    }
  }

}

STM32

unsigned long Can_Init_Flags;
unsigned char Can_Send_Flags, Can_Rcv_Flags; // can flags
unsigned char Rx_Data_Len;                                   // received data length in bytes
char RxTx_Data[8];                                           // can rx/tx data buffer
char Msg_Rcvd;                                               // reception flag
const long ID_1st = 12111, ID_2nd = 3;                       // node IDs
long Rx_ID;

void main() {

  GPIO_Digital_Output(&GPIOE_BASE, 0xFF00);
  GPIOE_ODR  = 0;
    DMA1_CCR1bits.

  Can_Init_Flags = 0;                                       //
  Can_Send_Flags = 0;                                       // clear flags
  Can_Rcv_Flags  = 0;                                       //

  Can_Send_Flags = _CAN_TX_XTD_FRAME &                      //     with CANWrite
                   _CAN_TX_NO_RTR_FRAME;

  Can_Init_Flags = _CAN_CONFIG_AUTOMATIC_RETRANSMISSION &              // form value to be used
                   _CAN_CONFIG_RX_FIFO_NOT_LOCKED_ON_OVERRUN &         // with CANInit
                   _CAN_CONFIG_TIME_TRIGGERED_MODE_DISABLED &
                   _CAN_CONFIG_TX_FIFO_PRIORITY_BY_IDINTIFIER &
                   _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

  CANSetFilterScale32(0, _CAN_FILTER_ENABLED & _CAN_FILTER_ID_MASK_MODE & _CAN_FILTER_XTD_MSG, ID_2nd, -1);

  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(1) {  // endless loop
    Msg_Rcvd = CAN1Read(0, &Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags);  // receive message
    if ((Rx_ID == ID_2nd) && Msg_Rcvd) {                                        // if message received check id
      GPIOE_ODR  = RxTx_Data[0] << 8;                                           // id correct, output data at PORTE
      RxTx_Data[0]++ ;                                                          // increment received data
      Delay_ms(10);
      CAN1Write(ID_1st, RxTx_Data, 1, Can_Send_Flags);                          // send incremented data back
    }
  }
}

Code for the second CAN node:

Stellaris

unsigned int Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags;  // can flags
unsigned int Rx_Data_Len;                                    // received data length in bytes
char RxTx_Data[8];                                           // can rx/tx data buffer
char Msg_Rcvd;                                               // reception flag
const unsigned long ID_1st = 12111, ID_2nd = 3;              // node IDs
unsigned long Rx_ID;

void main() {
  GPIO_Digital_Output(&GPIO_PORTJ, _GPIO_PINMASK_ALL);
  GPIO_PORTJ_DATA = 0;

  Can_Rcv_Flags  = 0;

  Can_Send_Flags = _CAN_TX_XTD_FRAME & _CAN_TX_NO_RTR_FRAME;  // form value to be used with CAN0Write

  Can_Init_Flags = _CAN_CONFIG_AUTO_RETRY_ENABLED;           // CAN init flags

//  CAN0Initialize(1,3,3,3,1,Can_Init_Flags);                   // initialize CAN0
  CAN0InitializeAdvanced(1,3,3,3,1,Can_Init_Flags,&_GPIO_MODULE_CAN0_D01);                   // initialize CAN0

  CAN0SetOperationMode(_CAN_MODE_CONFIG,0xFF);                // set CONFIGURATION mode

  CAN0ConfigureMessage(1, _CAN_CONFIG_XTD_MSG & _CAN_CONFIG_TYPE_TX); // configure message object for transmitting
  CAN0ConfigureMessage(2, _CAN_CONFIG_XTD_MSG & _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
  RxTx_Data[0] = 0;

  while(1) {
    Msg_Rcvd = CAN0ReadMessage(2, &Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags); // receive message
    if ((Rx_ID == ID_1st) && Msg_Rcvd) {                                              // if message received check id
      GPIO_PORTJ_DATA = RxTx_Data[0];                                                 // id correct, output data at PORTJ
      RxTx_Data[0]++;                                                                 // increment received data
      CAN0WriteMessage(1, ID_2nd, RxTx_Data, 1, Can_Send_Flags);                      // send incremented data back
    }
  }

}

STM32

unsigned char Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags; // can flags
unsigned char Rx_Data_Len;                                   // received data length in bytes
char RxTx_Data[8];                                           // can rx/tx data buffer
char Msg_Rcvd;                                               // reception flag
const long ID_1st = 12111, ID_2nd = 3;                       // node IDs
long Rx_ID;

void main() {

  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 &                      //     with CANWrite
                   _CAN_TX_NO_RTR_FRAME;

  Can_Init_Flags = _CAN_CONFIG_AUTOMATIC_RETRANSMISSION &          // form value to be used
                   _CAN_CONFIG_RX_FIFO_NOT_LOCKED_ON_OVERRUN &     // with CANInit
                   _CAN_CONFIG_TIME_TRIGGERED_MODE_DISABLED &
                   _CAN_CONFIG_TX_FIFO_PRIORITY_BY_IDINTIFIER &
                   _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
  CANSetFilterScale32(0, _CAN_FILTER_ENABLED & _CAN_FILTER_ID_MASK_MODE & _CAN_FILTER_XTD_MSG, ID_1st, -1);

  CAN1SetOperationMode(_CAN_OperatingMode_Normal);               // set NORMAL mode

  while (1) {                                                                  // endless loop
    Msg_Rcvd = CAN1Read(0, &Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags); // receive message
    if ((Rx_ID == ID_1st) && Msg_Rcvd) {                                       // if message received check id
      GPIOE_ODR  = RxTx_Data[0] << 8;                                          // id correct, output data at PORTE
      RxTx_Data[0]++ ;                                                         // increment received data
      CAN1Write(ID_2nd, RxTx_Data, 1, Can_Send_Flags);                         // send incremented data back
    }
  }
}
Copyright (c) 2002-2019 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code