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:
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.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.
|
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...
|
|
|
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...
|
|
- 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.
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 |
struct flexcan_user_config_t |
bool flexcan_user_config_t::is_rx_fifo_needed |
This controls whether the Rx FIFO feature is enabled or not.
- Parameters
-
instance | A FlexCAN instance number |
bitrate | A pointer to the FlexCAN bit rate settings. |
- Returns
- 0 if successful; non-zero failed
- Parameters
-
instance | A FlexCAN instance number |
bitrate | A pointer to a variable for returning the FlexCAN bit rate settings |
- Returns
- 0 if successful; non-zero failed
- Parameters
-
instance | A FlexCAN instance number |
type | The FlexCAN RX mask type |
- Parameters
-
instance | A FlexCAN instance number |
id_type | Standard ID or extended ID |
mask | Mask value |
- Returns
- 0 if successful; non-zero failed
- Parameters
-
instance | A FlexCAN instance number |
id_type | Standard ID or extended ID |
mask | Mask value |
- Returns
- 0 if successful; non-zero failed
- Parameters
-
instance | A FlexCAN instance number |
id_type | A standard ID or an extended ID |
mb_idx | Index of the message buffer |
mask | Mask value |
- Returns
- 0 if successful; non-zero failed.
This function initializes
- Parameters
-
instance | A FlexCAN instance number |
state | Pointer to the FlexCAN driver state structure. |
data | The FlexCAN platform data |
- Returns
- 0 if successful; non-zero failed
uint32_t FLEXCAN_DRV_Deinit |
( |
uint8_t |
instance | ) |
|
- Parameters
-
instance | A FlexCAN instance number |
- Returns
- 0 if successful; non-zero failed
- Parameters
-
instance | A FlexCAN instance number |
mb_idx | Index of the message buffer |
tx_info | Data info |
msg_id | ID of the message to transmit |
- Returns
- 0 if successful; non-zero failed
- Parameters
-
instance | A FlexCAN instance number |
mb_idx | Index of the message buffer |
tx_info | Data info |
msg_id | ID of the message to transmit |
mb_data | Bytes of the FlexCAN message |
timeout_ms | A timeout for the transfer in milliseconds. |
- Returns
- 0 if successful; non-zero failed
- Parameters
-
instance | A FlexCAN instance number |
mb_idx | Index of the message buffer |
tx_info | Data info |
msg_id | ID of the message to transmit |
mb_data | Bytes of the FlexCAN message. |
- Returns
- 0 if successful; non-zero failed
- Parameters
-
instance | A FlexCAN instance number |
mb_idx | Index of the message buffer |
rx_info | Data info |
msg_id | ID of the message to transmit |
- Returns
- 0 if successful; non-zero failed
- Parameters
-
instance | A FlexCAN instance number |
id_format | The format of the RX FIFO ID Filter Table Elements |
id_filter_table | The ID filter table elements which contain RTR bit, IDE bit, and RX message ID |
- Returns
- 0 if successful; non-zero failed.
- Parameters
-
instance | A FlexCAN instance number |
mb_idx | Index of the message buffer |
data | The FlexCAN receive message buffer data. |
timeout_ms | A timeout for the transfer in milliseconds. |
- Returns
- 0 if successful; non-zero failed
- Parameters
-
instance | A FlexCAN instance number |
mb_idx | Index of the message buffer |
data | The FlexCAN receive message buffer data. |
- Returns
- 0 if successful; non-zero failed
- Parameters
-
instance | A FlexCAN instance number |
data | The FlexCAN receive message buffer data. |
timeout_ms | A timeout for the transfer in milliseconds. |
- Returns
- 0 if successful; non-zero failed
- Parameters
-
instance | A FlexCAN instance number |
data | The FlexCAN receive message buffer data. |
- Returns
- 0 if successful; non-zero failed
void FLEXCAN_DRV_IRQHandler |
( |
uint8_t |
instance | ) |
|
- Parameters
-
instance | The FlexCAN instance number. |
When performing an async transmit, call this function to ascertain the state of the current transmission: in progress (or busy) or complete (success).
- Parameters
-
instance | The FLEXCAN module base address. |
- Returns
- The transmit status.
- Return values
-
kStatus_FLEXCAN_Success | The transmit has completed successfully. |
kStatus_FLEXCAN_TxBusy | The transmit is still in progress. |
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
-
instance | The FLEXCAN module base address. |
bytesRemaining | A 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_Success | The receive has completed successfully. |
kStatus_FLEXCAN_RxBusy | The receive is still in progress. |
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[] |