Kinetis SDK v.1.3 API Reference Manual  Rev. 0
Freescale Semiconductor, Inc.
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages

Overview

This section describes the programming interface of the FlexCAN Peripheral driver.

FlexCAN Overview

The FlexCAN (flexible controller area network) module is a communication controller implementing the CAN protocol according to the CAN 2.0B protocol specification. The FlexCAN module supports both standard and extended message frames. The message buffers are stored in an embedded RAM dedicated to the FlexCAN module. The CAN Protocol Engine (PE) sub-module manages the serial communication on the CAN bus by requesting RAM access for receiving and transmitting message frames, validating received messages, and performing error handling.

FlexCAN Initialization

To initialize the FlexCAN driver, call the FLEXCAN_DRV_Init() function and pass the instance number of the FlexCAN you want to use. For example, to use FlexCAN0, pass a value of 0 to the flexcan_init function. In addition, you should also pass a user configuration structure flexcan_user_config_t, as shown here:

// FlexCAN configuration structure for user
typedef struct FLEXCANUserConfig {
uint32_t max_num_mb;
bool is_rx_fifo_needed;

Typically, the user configures the flexcan_user_config_t instantiation as 16 message buffers, 8 RX FIFO ID filters and set to true when RX FIFO is needed. This is a code example to set up a FlexCAN configuration instantiation:

flexcan_config_t flexcan1_data;
flexcan1_data.max_num_mb = 16;
flexcan1_data.num_id_filters = kFlexCanRxFifoIDFilters_8;
flexcan1_data.is_rx_fifo_needed = true;

FlexCAN Module timing

FlexCAN bit rate is derived from the serial clock, which is generated by dividing the PE clock by the programmed PRESDIV value. Each serial clock period is also called a time quantum. The FlexCAN bit-rate is defined as the Sclock divided by the number of time quanta, where time quanta are further broken down segments within the bit time (time to transmit and sample a bit). This list shows the CAN bit-rates that are supported in the FlexCAN driver. 1 Mbytes/s 750 Kbytes/s 500 Kbytes/s 250 Kbytes/s 125 Kbytes/s

The FlexCAN module supports several different ways to set up the bit timing parameters that are required by the CAN protocol. The Control Register has various fields used to control bit timing parameters: PRESDIV, PROPSEG, PSEG1, PSEG2, and RJW.

To calculate the CAN bit timing parameters, use the method outlined in the Application Note AN1798, section 4.1. A maximum time for PROP_SEG is used, the remaining TQ is split equally between PSEG1 and PSEG2, provided PSEG2 >=2. RJW is set to the minimum of 4 or to the PSEG1.

FlexCAN Transfers

To transmit a FlexCAN frame, the CPU must prepare a message buffer for transmission by calling the FLEXCAN_DRV_Send() function. To receive the FlexCAN frames into a message buffer, the CPU must also prepare it for reception by first setting up the receive mask by calling FLEXCAN_DRV_SetMaskType(), FLEXCAN_DRV_SetRxFifoGlobalMask(), FLEXCAN_DRV_SetRxMbGlobalMask() and FLEXCAN_DRV_SetRxIndividualMask() functions. Then configure the RX buffers by calling either FLEXCAN_DRV_ConfigRxMb() or FLEXCAN_DRV_ConfigRxFifo() functions depending on the mode of reception. Finally, call FLEXCAN_DRV_RxMessageBuffer() or FLEXCAN_DRV_RxFifo functions depending on the mode of reception. The FlexCAN uses only the interrupt-driven process to transfer data.

Data Structures

struct  flexcan_state_t
 Internal driver state information. More...
 
struct  flexcan_data_info_t
 FlexCAN data info from user. More...
 
struct  flexcan_user_config_t
 FlexCAN configuration. More...
 

Functions

void FLEXCAN_DRV_IRQHandler (uint8_t instance)
 Interrupt handler for a FlexCAN instance. More...
 
flexcan_status_t FLEXCAN_DRV_GetTransmitStatus (uint32_t instance)
 Returns whether the previous FLEXCAN transmit has finished. More...
 
flexcan_status_t FLEXCAN_DRV_GetReceiveStatus (uint32_t instance)
 Returns whether the previous FLEXCAN receive is complete. More...
 

Variables

CAN_Type *const g_flexcanBase []
 Table of base addresses for FlexCAN instances. More...
 
const IRQn_Type g_flexcanRxWarningIrqId []
 Table to save RX Warning IRQ numbers for FlexCAN instances. More...
 
const IRQn_Type g_flexcanTxWarningIrqId []
 Table to save TX Warning IRQ numbers for FlexCAN instances. More...
 
const IRQn_Type g_flexcanWakeUpIrqId []
 Table to save wakeup IRQ numbers for FlexCAN instances. More...
 
const IRQn_Type g_flexcanErrorIrqId []
 Table to save error IRQ numbers for FlexCAN instances. More...
 
const IRQn_Type g_flexcanBusOffIrqId []
 Table to save Bus off IRQ numbers for FlexCAN instances. More...
 
const IRQn_Type g_flexcanOredMessageBufferIrqId []
 Table to save message buffer IRQ numbers for FlexCAN instances. More...
 

Bit rate

flexcan_status_t FLEXCAN_DRV_SetBitrate (uint8_t instance, flexcan_time_segment_t *bitrate)
 Sets the FlexCAN bit rate. More...
 
flexcan_status_t FLEXCAN_DRV_GetBitrate (uint8_t instance, flexcan_time_segment_t *bitrate)
 Gets the FlexCAN bit rate. More...
 

Global mask

void FLEXCAN_DRV_SetRxMaskType (uint8_t instance, flexcan_rx_mask_type_t type)
 Sets the RX masking type. More...
 
flexcan_status_t FLEXCAN_DRV_SetRxFifoGlobalMask (uint8_t instance, flexcan_msgbuff_id_type_t id_type, uint32_t mask)
 Sets the FlexCAN RX FIFO global standard or extended mask. More...
 
flexcan_status_t FLEXCAN_DRV_SetRxMbGlobalMask (uint8_t instance, flexcan_msgbuff_id_type_t id_type, uint32_t mask)
 Sets the FlexCAN RX MB global standard or extended mask. More...
 
flexcan_status_t FLEXCAN_DRV_SetRxIndividualMask (uint8_t instance, flexcan_msgbuff_id_type_t id_type, uint32_t mb_idx, uint32_t mask)
 Sets the FlexCAN RX individual standard or extended mask. More...
 

Initialization and Shutdown

flexcan_status_t FLEXCAN_DRV_Init (uint32_t instance, flexcan_state_t *state, const flexcan_user_config_t *data)
 Initializes the FlexCAN peripheral. More...
 
uint32_t FLEXCAN_DRV_Deinit (uint8_t instance)
 Shuts down a FlexCAN instance. More...
 

Send configuration

flexcan_status_t FLEXCAN_DRV_ConfigTxMb (uint8_t instance, uint32_t mb_idx, flexcan_data_info_t *tx_info, uint32_t msg_id)
 FlexCAN transmit message buffer field configuration. More...
 
flexcan_status_t FLEXCAN_DRV_SendBlocking (uint8_t instance, uint32_t mb_idx, flexcan_data_info_t *tx_info, uint32_t msg_id, uint8_t *mb_data, uint32_t timeout_ms)
 Sends FlexCAN messages. More...
 
flexcan_status_t FLEXCAN_DRV_Send (uint8_t instance, uint32_t mb_idx, flexcan_data_info_t *tx_info, uint32_t msg_id, uint8_t *mb_data)
 Sends FlexCAN messages. More...
 

Receive configuration

flexcan_status_t FLEXCAN_DRV_ConfigRxMb (uint8_t instance, uint32_t mb_idx, flexcan_data_info_t *rx_info, uint32_t msg_id)
 FlexCAN receive message buffer field configuration. More...
 
flexcan_status_t FLEXCAN_DRV_ConfigRxFifo (uint8_t instance, flexcan_rx_fifo_id_element_format_t id_format, flexcan_id_table_t *id_filter_table)
 FlexCAN RX FIFO field configuration. More...
 
flexcan_status_t FLEXCAN_DRV_RxMessageBufferBlocking (uint8_t instance, uint32_t mb_idx, flexcan_msgbuff_t *data, uint32_t timeout_ms)
 FlexCAN is waiting to receive data from the message buffer. More...
 
flexcan_status_t FLEXCAN_DRV_RxMessageBuffer (uint8_t instance, uint32_t mb_idx, flexcan_msgbuff_t *data)
 FlexCAN is waiting to receive data from the message buffer. More...
 
flexcan_status_t FLEXCAN_DRV_RxFifoBlocking (uint8_t instance, flexcan_msgbuff_t *data, uint32_t timeout_ms)
 FlexCAN is waiting to receive data from the message FIFO. More...
 
flexcan_status_t FLEXCAN_DRV_RxFifo (uint8_t instance, flexcan_msgbuff_t *data)
 FlexCAN is waiting to receive data from the message FIFO. More...
 

Data Structure Documentation

struct flexcan_state_t
Note
The contents of this structure are internal to the driver and should not be modified by users. Also, contents of the structure are subject to change in future releases.

Data Fields

flexcan_msgbuff_tfifo_message
 The FlexCAN receive FIFO data.
 
flexcan_msgbuff_tmb_message
 The FlexCAN receive MB data.
 
volatile uint32_t rx_mb_idx
 Index of the message buffer for receiving.
 
volatile uint32_t tx_mb_idx
 Index of the message buffer for transmitting.
 
semaphore_t txIrqSync
 Used to wait for ISR to complete its TX business. More...
 
semaphore_t rxIrqSync
 Used to wait for ISR to complete its RX business. More...
 
volatile bool isTxBusy
 True if there is an active transmit. More...
 
volatile bool isRxBusy
 True if there is an active receive. More...
 
volatile bool isTxBlocking
 True if transmit is blocking transaction. More...
 
volatile bool isRxBlocking
 True if receive is blocking transaction. More...
 

Field Documentation

semaphore_t flexcan_state_t::txIrqSync
semaphore_t flexcan_state_t::rxIrqSync
volatile bool flexcan_state_t::isTxBusy
volatile bool flexcan_state_t::isRxBusy
volatile bool flexcan_state_t::isTxBlocking
volatile bool flexcan_state_t::isRxBlocking
struct flexcan_data_info_t

Data Fields

flexcan_msgbuff_id_type_t msg_id_type
 Type of message ID (standard or extended)
 
uint32_t data_length
 Length of Data in Bytes.
 
struct flexcan_user_config_t

Data Fields

uint32_t max_num_mb
 The maximum number of Message Buffers.
 
flexcan_rx_fifo_id_filter_num_t num_id_filters
 The number of RX FIFO ID filters needed.
 
bool is_rx_fifo_needed
 1 if needed; 0 if not. More...
 
flexcan_operation_modes_t flexcanMode
 User configurable FlexCAN operation modes.
 

Field Documentation

bool flexcan_user_config_t::is_rx_fifo_needed

This controls whether the Rx FIFO feature is enabled or not.

Function Documentation

flexcan_status_t FLEXCAN_DRV_SetBitrate ( uint8_t  instance,
flexcan_time_segment_t bitrate 
)
Parameters
instanceA FlexCAN instance number
bitrateA pointer to the FlexCAN bit rate settings.
Returns
0 if successful; non-zero failed
flexcan_status_t FLEXCAN_DRV_GetBitrate ( uint8_t  instance,
flexcan_time_segment_t bitrate 
)
Parameters
instanceA FlexCAN instance number
bitrateA pointer to a variable for returning the FlexCAN bit rate settings
Returns
0 if successful; non-zero failed
void FLEXCAN_DRV_SetRxMaskType ( uint8_t  instance,
flexcan_rx_mask_type_t  type 
)
Parameters
instanceA FlexCAN instance number
typeThe FlexCAN RX mask type
flexcan_status_t FLEXCAN_DRV_SetRxFifoGlobalMask ( uint8_t  instance,
flexcan_msgbuff_id_type_t  id_type,
uint32_t  mask 
)
Parameters
instanceA FlexCAN instance number
id_typeStandard ID or extended ID
maskMask value
Returns
0 if successful; non-zero failed
flexcan_status_t FLEXCAN_DRV_SetRxMbGlobalMask ( uint8_t  instance,
flexcan_msgbuff_id_type_t  id_type,
uint32_t  mask 
)
Parameters
instanceA FlexCAN instance number
id_typeStandard ID or extended ID
maskMask value
Returns
0 if successful; non-zero failed
flexcan_status_t FLEXCAN_DRV_SetRxIndividualMask ( uint8_t  instance,
flexcan_msgbuff_id_type_t  id_type,
uint32_t  mb_idx,
uint32_t  mask 
)
Parameters
instanceA FlexCAN instance number
id_typeA standard ID or an extended ID
mb_idxIndex of the message buffer
maskMask value
Returns
0 if successful; non-zero failed.
flexcan_status_t FLEXCAN_DRV_Init ( uint32_t  instance,
flexcan_state_t state,
const flexcan_user_config_t data 
)

This function initializes

Parameters
instanceA FlexCAN instance number
statePointer to the FlexCAN driver state structure.
dataThe FlexCAN platform data
Returns
0 if successful; non-zero failed
uint32_t FLEXCAN_DRV_Deinit ( uint8_t  instance)
Parameters
instanceA FlexCAN instance number
Returns
0 if successful; non-zero failed
flexcan_status_t FLEXCAN_DRV_ConfigTxMb ( uint8_t  instance,
uint32_t  mb_idx,
flexcan_data_info_t tx_info,
uint32_t  msg_id 
)
Parameters
instanceA FlexCAN instance number
mb_idxIndex of the message buffer
tx_infoData info
msg_idID of the message to transmit
Returns
0 if successful; non-zero failed
flexcan_status_t FLEXCAN_DRV_SendBlocking ( uint8_t  instance,
uint32_t  mb_idx,
flexcan_data_info_t tx_info,
uint32_t  msg_id,
uint8_t *  mb_data,
uint32_t  timeout_ms 
)
Parameters
instanceA FlexCAN instance number
mb_idxIndex of the message buffer
tx_infoData info
msg_idID of the message to transmit
mb_dataBytes of the FlexCAN message
timeout_msA timeout for the transfer in milliseconds.
Returns
0 if successful; non-zero failed
flexcan_status_t FLEXCAN_DRV_Send ( uint8_t  instance,
uint32_t  mb_idx,
flexcan_data_info_t tx_info,
uint32_t  msg_id,
uint8_t *  mb_data 
)
Parameters
instanceA FlexCAN instance number
mb_idxIndex of the message buffer
tx_infoData info
msg_idID of the message to transmit
mb_dataBytes of the FlexCAN message.
Returns
0 if successful; non-zero failed
flexcan_status_t FLEXCAN_DRV_ConfigRxMb ( uint8_t  instance,
uint32_t  mb_idx,
flexcan_data_info_t rx_info,
uint32_t  msg_id 
)
Parameters
instanceA FlexCAN instance number
mb_idxIndex of the message buffer
rx_infoData info
msg_idID of the message to transmit
Returns
0 if successful; non-zero failed
flexcan_status_t FLEXCAN_DRV_ConfigRxFifo ( uint8_t  instance,
flexcan_rx_fifo_id_element_format_t  id_format,
flexcan_id_table_t id_filter_table 
)
Parameters
instanceA FlexCAN instance number
id_formatThe format of the RX FIFO ID Filter Table Elements
id_filter_tableThe ID filter table elements which contain RTR bit, IDE bit, and RX message ID
Returns
0 if successful; non-zero failed.
flexcan_status_t FLEXCAN_DRV_RxMessageBufferBlocking ( uint8_t  instance,
uint32_t  mb_idx,
flexcan_msgbuff_t data,
uint32_t  timeout_ms 
)
Parameters
instanceA FlexCAN instance number
mb_idxIndex of the message buffer
dataThe FlexCAN receive message buffer data.
timeout_msA timeout for the transfer in milliseconds.
Returns
0 if successful; non-zero failed
flexcan_status_t FLEXCAN_DRV_RxMessageBuffer ( uint8_t  instance,
uint32_t  mb_idx,
flexcan_msgbuff_t data 
)
Parameters
instanceA FlexCAN instance number
mb_idxIndex of the message buffer
dataThe FlexCAN receive message buffer data.
Returns
0 if successful; non-zero failed
flexcan_status_t FLEXCAN_DRV_RxFifoBlocking ( uint8_t  instance,
flexcan_msgbuff_t data,
uint32_t  timeout_ms 
)
Parameters
instanceA FlexCAN instance number
dataThe FlexCAN receive message buffer data.
timeout_msA timeout for the transfer in milliseconds.
Returns
0 if successful; non-zero failed
flexcan_status_t FLEXCAN_DRV_RxFifo ( uint8_t  instance,
flexcan_msgbuff_t data 
)
Parameters
instanceA FlexCAN instance number
dataThe FlexCAN receive message buffer data.
Returns
0 if successful; non-zero failed
void FLEXCAN_DRV_IRQHandler ( uint8_t  instance)
Parameters
instanceThe FlexCAN instance number.
flexcan_status_t FLEXCAN_DRV_GetTransmitStatus ( uint32_t  instance)

When performing an async transmit, call this function to ascertain the state of the current transmission: in progress (or busy) or complete (success).

Parameters
instanceThe FLEXCAN module base address.
Returns
The transmit status.
Return values
kStatus_FLEXCAN_SuccessThe transmit has completed successfully.
kStatus_FLEXCAN_TxBusyThe transmit is still in progress.
flexcan_status_t FLEXCAN_DRV_GetReceiveStatus ( uint32_t  instance)

When performing an async receive, call this function to find out the state of the current receive progress: in progress (or busy) or complete (success).

Parameters
instanceThe FLEXCAN module base address.
bytesRemainingA pointer to a value that is filled in with the number of bytes which still need to be received in the active transfer.
Returns
The receive status.
Return values
kStatus_FLEXCAN_SuccessThe receive has completed successfully.
kStatus_FLEXCAN_RxBusyThe receive is still in progress.

Variable Documentation

CAN_Type* const g_flexcanBase[]
const IRQn_Type g_flexcanRxWarningIrqId[]
const IRQn_Type g_flexcanTxWarningIrqId[]
const IRQn_Type g_flexcanWakeUpIrqId[]
const IRQn_Type g_flexcanErrorIrqId[]
const IRQn_Type g_flexcanBusOffIrqId[]
const IRQn_Type g_flexcanOredMessageBufferIrqId[]