| Kinetis SDK API Reference Manual 1.0.0-beta Freescale Semiconductor, Inc.

- Introduction
- Related Pages
- API Usage

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Data Structures | Macros | Enumerations
FlexCAN Driver
Controller Area Network (FlexCAN)

The section describes the programming interface of the FlexCAN Peripheral driver. More...

## Data Structures
struct | flexcan_bitrate_table_t
| FlexCAN bit rate and the related timing segments structure. More...
struct | flexcan_data_info_t
| FlexCAN data info from user. More...

## Macros
#define | FSL_CAN_INTERRUPT_COUNT (4)

## Enumerations
enum | flexcan_bitrate_t { kFlexCanBitrate_125k = 125000, kFlexCanBitrate_250k = 250000, kFlexCanBitrate_500k = 500000, kFlexCanBitrate_750k = 750000, kFlexCanBitrate_1M = 1000000 }
| FlexCAN bitrates supported. More...

## Bit rate
flexcan_status_t | flexcan_set_bitrate (uint8_t instance, flexcan_bitrate_t bitrate)
| Sets FlexCAN bit rate. More...
flexcan_status_t | flexcan_get_bitrate (uint8_t instance, flexcan_bitrate_t *bitrate)
| Gets FlexCAN bit rate. More...

## Global mask
void | flexcan_set_mask_type (uint8_t instance, flexcan_rx_mask_type_t type)
| Sets the RX masking type. More...
flexcan_status_t | flexcan_set_rx_fifo_global_mask (uint8_t instance, flexcan_mb_id_type_t id_type, uint32_t mask)
| Sets the FlexCAN RX FIFO global standard or extended mask. More...
flexcan_status_t | flexcan_set_rx_mb_global_mask (uint8_t instance, flexcan_mb_id_type_t id_type, uint32_t mask)
| Sets the FlexCAN RX MB global standard or extended mask. More...
flexcan_status_t | flexcan_set_rx_individual_mask (uint8_t instance, const flexcan_user_config_t *data, flexcan_mb_id_type_t id_type, uint32_t mb_idx, uint32_t mask)
| Sets the FlexCAN RX individual standard or extended mask. More...

## Init and Shutdown
flexcan_status_t | flexcan_init (uint8_t instance, const flexcan_user_config_t *data, bool enable_err_interrupts)
| Initializes the FlexCAN peripheral. More...
uint32_t | flexcan_shutdown (uint8_t instance)
| Shut down a FlexCAN instance. More...

## Send configuration
flexcan_status_t | flexcan_tx_mb_config (uint8_t instance, const flexcan_user_config_t *data, 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_send (uint8_t instance, const flexcan_user_config_t *data, uint32_t mb_idx, flexcan_data_info_t *tx_info, uint32_t msg_id, uint32_t num_bytes, uint8_t *mb_data)
| Sends FlexCAN messages. More...

## Receive configuration
flexcan_status_t | flexcan_rx_mb_config (uint8_t instance, const flexcan_user_config_t *data, 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_rx_fifo_config (uint8_t instance, const flexcan_user_config_t *data, 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_start_receive (uint8_t instance, const flexcan_user_config_t *data, uint32_t mb_idx, uint32_t receiveDataCount, bool *is_rx_mb_data, bool *is_rx_fifo_data, flexcan_mb_t *rx_mb, flexcan_mb_t *rx_fifo)
| FlexCAN is waiting to receive data. More...
flexcan_status_t | flexcan_receive (uint8_t instance, const flexcan_user_config_t *data, uint32_t mb_idx, flexcan_data_info_t *rx_info, uint32_t msg_id, flexcan_rx_fifo_id_element_format_t id_format, flexcan_id_table_t *id_filter_table, uint32_t receiveDataCount, flexcan_mb_t *rx_mb, flexcan_mb_t *rx_fifo)
| FlexCAN is preparing to receive data. More...

### FlexCAN Driver

# 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.

# Initialization

To initialize the FlexCAN driver, call the flexcan_init() function and pass the instance number of the FlexCAN you want to use. For instance, to use FlexCAN0, pass a value of 0 to the flexcan_init function. In addition, you should also pass a user configuration structure #flexcan_config_t, as shown here:
// FlexCAN configuration structure for user
typedef struct FLEXCANConfig {
uint32_t num_mb;
uint32_t max_num_mb;
flexcan_rx_fifo_id_filter_num_t num_id_filters;
bool is_rx_fifo_needed;
bool is_rx_mb_needed;
} flexcan_config_t;
Typically, the user configures the #flexcan_config_t instantiation as 16 message buffers needed, 16 message buffers available, 8 RX FIFO ID filters, set to true when RX FIFO is needed and when RX Message Buffers are needed. The user can easily modify the #flexcan_config_t instantiation to configure the FlexCAN peripheral to a different number of message buffers, which are used with/without RX FIFO and RX MB. This is a code example to set up a FlexCAN configuration instantiation:
flexcan_config_t flexcan1_data;
flexcan1_data.num_mb = 16;
flexcan1_data.max_num_mb = 16;
flexcan1_data.num_id_filters = kFlexCanRxFifoIDFilters_8;
flexcan1_data.is_rx_fifo_needed = true;
flexcan1_data.is_rx_mb_needed = true;

# 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). The following 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 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.

# Transfers

To transmit a FlexCAN frame, the CPU must prepare a message buffer for transmission by calling the flexcan_send() function. To receive the FlexCAN frames into a message buffer, the CPU must also prepare it for reception by calling the flexcan_receive() function. The FlexCAN driver implements send and receive configurations and prepares to send and receive data from TX MBs to RX MBs or/and RX FIFO. The FlexCAN uses only the interrupt-driven process to transfer data.

## Data Structure Documentation

struct flexcan_bitrate_table_t

### Data Fields
flexcan_bitrate_t | bit_rate
| bit rate
uint32_t | propseg
| Propagation segment.
uint32_t | pseg1
| Phase segment 1.
uint32_t | pseg2
| Phase segment 2.
uint32_t | pre_divider
| Clock pre divider.
uint32_t | rjw
| Re-sync jump width.

struct flexcan_data_info_t

### Data Fields
flexcan_mb_id_type_t | msg_id_type
| Type of message ID (standard or extended)
uint32_t | data_length
| Length of Data in Bytes.

## Enumeration Type Documentation

enum flexcan_bitrate_t

Enumerator
kFlexCanBitrate_125k | 125 kHz
kFlexCanBitrate_250k | 250 kHz
kFlexCanBitrate_500k | 500 kHz
kFlexCanBitrate_750k | 750 kHz
kFlexCanBitrate_1M | 1 MHz

## Function Documentation

flexcan_status_t flexcan_set_bitrate | ( | uint8_t | instance,
|  | flexcan_bitrate_t | bitrate
| ) |  |

Parameters

  instance | A FlexCAN instance number
  bit | rate Selects a FlexCAN bit rate in the bit_rate_table.

Returns
  0 if successful; non-zero failed

flexcan_status_t flexcan_get_bitrate | ( | uint8_t | instance,
|  | flexcan_bitrate_t * | bitrate
| ) |  |

Parameters

  instance | A FlexCAN instance number
  bit | rate A pointer to a variable for returning the FlexCAN bit rate in the bit_rate_table.

Returns
  0 if successful; non-zero failed

void flexcan_set_mask_type | ( | uint8_t | instance,
|  | flexcan_rx_mask_type_t | type
| ) |  |

Parameters

  instance | A FlexCAN instance number
  type | The FlexCAN RX mask type

flexcan_status_t flexcan_set_rx_fifo_global_mask | ( | uint8_t | instance,
|  | flexcan_mb_id_type_t | id_type,
|  | uint32_t | mask
| ) |  |

Parameters

  instance | A FlexCAN instance number
  id_type | Standard ID or extended ID
  mask | Mask value

Returns
  0 if successful; non-zero failed

flexcan_status_t flexcan_set_rx_mb_global_mask | ( | uint8_t | instance,
|  | flexcan_mb_id_type_t | id_type,
|  | uint32_t | mask
| ) |  |

Parameters

  instance | A FlexCAN instance number
  id_type | Standard ID or extended ID
  mask | Mask value

Returns
  0 if successful; non-zero failed

flexcan_status_t flexcan_set_rx_individual_mask | ( | uint8_t | instance,
|  | const flexcan_user_config_t * | data,
|  | flexcan_mb_id_type_t | id_type,
|  | uint32_t | mb_idx,
|  | uint32_t | mask
| ) |  |

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.

flexcan_status_t flexcan_init | ( | uint8_t | instance,
|  | const flexcan_user_config_t * | data,
|  | bool | enable_err_interrupts
| ) |  |

This function initializes

Parameters

  instance | A FlexCAN instance number
  data | The FlexCAN platform data
  enable_err_interrupts | 1 if enabled, 0 if not

Returns
  0 if successful; non-zero failed

uint32_t flexcan_shutdown | ( | uint8_t | instance | ) |

Parameters

  instance | A FlexCAN instance number

Returns
  0 if successful; non-zero failed

flexcan_status_t flexcan_tx_mb_config | ( | uint8_t | instance,
|  | const flexcan_user_config_t * | data,
|  | uint32_t | mb_idx,
|  | flexcan_data_info_t * | tx_info,
|  | uint32_t | msg_id
| ) |  |

Parameters

  instance | A FlexCAN instance number
  data | The FlexCAN platform data
  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

flexcan_status_t flexcan_send | ( | uint8_t | instance,
|  | const flexcan_user_config_t * | data,
|  | uint32_t | mb_idx,
|  | flexcan_data_info_t * | tx_info,
|  | uint32_t | msg_id,
|  | uint32_t | num_bytes,
|  | uint8_t * | mb_data
| ) |  |

Parameters

  instance | A FlexCAN instance number
  data | The FlexCAN platform data
  mb_idx | Index of the message buffer
  tx_info | Data info
  msg_id | ID of the message to transmit
  num_bytes | The number of bytes in mb_data
  mb_data | Bytes of the FlexCAN message

Returns
  0 if successful; non-zero failed

flexcan_status_t flexcan_rx_mb_config | ( | uint8_t | instance,
|  | const flexcan_user_config_t * | data,
|  | uint32_t | mb_idx,
|  | flexcan_data_info_t * | rx_info,
|  | uint32_t | msg_id
| ) |  |

Parameters

  instance | A FlexCAN instance number
  data | The FlexCAN platform data
  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

flexcan_status_t flexcan_rx_fifo_config | ( | uint8_t | instance,
|  | const flexcan_user_config_t * | data,
|  | flexcan_rx_fifo_id_element_format_t | id_format,
|  | flexcan_id_table_t * | id_filter_table
| ) |  |

Parameters

  instance | A FlexCAN instance number
  data | The FlexCAN platform data
  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.

flexcan_status_t flexcan_start_receive | ( | uint8_t | instance,
|  | const flexcan_user_config_t * | data,
|  | uint32_t | mb_idx,
|  | uint32_t | receiveDataCount,
|  | bool * | is_rx_mb_data,
|  | bool * | is_rx_fifo_data,
|  | flexcan_mb_t * | rx_mb,
|  | flexcan_mb_t * | rx_fifo
| ) |  |

Parameters

  instance | A FlexCAN instance number
  data | The FlexCAN platform data
  mb_idx | Index of the message buffer
  msg_id | ID of the message to transmit
  receiveDataCount | The number of data to be received
  rx_mb | The FlexCAN receive message buffer data.
  rx_fifo | The FlexCAN receive FIFO data.

Returns
  0 if successful; non-zero failed

flexcan_status_t flexcan_receive | ( | uint8_t | instance,
|  | const flexcan_user_config_t * | data,
|  | uint32_t | mb_idx,
|  | flexcan_data_info_t * | rx_info,
|  | uint32_t | msg_id,
|  | flexcan_rx_fifo_id_element_format_t | id_format,
|  | flexcan_id_table_t * | id_filter_table,
|  | uint32_t | receiveDataCount,
|  | flexcan_mb_t * | rx_mb,
|  | flexcan_mb_t * | rx_fifo
| ) |  |

Parameters

  instance | A FlexCAN instance number
  data | The FlexCAN platform data
  mb_idx | Index of the message buffer
  cs | CODE/status values (RX)
  msg_id | ID of the message to transmit
  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
  receiveDataCount | The number of data to be received
  rx_mb | The FlexCAN receive message buffer data
  rx_fifo | The FlexCAN receive FIFO data

Returns
  0 if successful; non-zero failed