The S32 SDK provides a Peripheral Abstraction Layer for Controller Area Network (CAN) modules of S32 SDK devices.
The CAN PAL driver allows communication over a CAN bus. It was designed to be portable across all platforms and IPs which support CAN communication.
Unlike the other drivers, CAN PAL modules need to include a configuration file named can_pal_cfg.h, which allows the user to specify which IPSs are used and how many resources are allocated for each of them (state structures). The following code example shows how to configure one instance for each available CAN IP.
The following table contains the matching between platforms and available IPs
IP/MCU | S32K116 | S32K142 | S32K144 | S32K146 | S32K148 | S32V234 | MPC5748G | MPC5746C | MPC5744P |
---|---|---|---|---|---|---|---|---|---|
FlexCAN | YES | YES | YES | YES | YES | YES | YES | YES | YES |
Remote request frames
The following table contains the matching between platforms and available features
FEATURE/MCU | S32K116 | S32K142 | S32K144 | S32K146 | S32K148 | S32V234 | MPC5748G | MPC5746C | MPC5744P |
---|---|---|---|---|---|---|---|---|---|
FD/BRS | YES | YES | YES | YES | YES | YES | YES | YES | NO |
data length > 8B | YES | YES | YES | YES | YES | YES | YES | YES | NO |
In order to use the CAN PAL driver it must be first initialized, using CAN_Init() function. Once initialized, it cannot be initialized again for the same CAN module instance until it is de-initialized, using CAN_Deinit(). Different CAN modules instances can function independently of each other.
The can_user_config_t structure allows you to configure the following:
The bitrate is represented by a can_time_segment_t structure, with the following fields:
In order to use a buffer for transmission/reception, it has to be initialized using either CAN_ConfigRxBuff or CAN_ConfigTxBuff.
After having the buffer configured, you can start sending/receiving data by calling one of the following functions:
When used over FlexCAN, the PAL allows extending the basic configuration with an Rx FIFO feature. The Rx FIFO is receive-only and 6-message deep. The application can read the received messages sequentially, in the order they were received, by repeatedly reading the data from buffer 0 (zero). A powerful filtering scheme is provided to accept only frames intended for the target application. The FIFO and filtering criteria are configured by passing a structure of extension_flexcan_rx_fifo_t type, through the extension field of the user configuration structure.
The number of elements in the ID filter table is defined by the following formula:
Each element in the ID filter table specifies an ID to be used as acceptance criteria for the FIFO as follows:
When Rx FIFO feature is enabled, buffer 0 (zero) cannot be used for transmission or reconfigured for reception using CAN_ConfigRxBuff() and CAN_SetRxFilter() functions.
Data Structures | |
struct | can_time_segment_t |
CAN bit timing variables Implements : can_time_segment_t_Class. More... | |
struct | can_buff_config_t |
CAN buffer configuration Implements : can_buff_config_t_Class. More... | |
struct | can_message_t |
CAN message format Implements : can_message_t_Class. More... | |
struct | can_user_config_t |
CAN controller configuration Implements : can_user_config_t_Class. More... | |
Enumerations | |
enum | can_operation_modes_t { CAN_NORMAL_MODE = 0U, CAN_LOOPBACK_MODE = 2U, CAN_DISABLE_MODE = 4U } |
CAN controller operation modes Implements : can_operation_modes_t_Class. More... | |
enum | can_fd_payload_size_t { CAN_PAYLOAD_SIZE_8 = 0, CAN_PAYLOAD_SIZE_16, CAN_PAYLOAD_SIZE_32, CAN_PAYLOAD_SIZE_64 } |
CAN buffer payload sizes Implements : can_fd_payload_size_t_Class. More... | |
enum | can_bitrate_phase_t { CAN_NOMINAL_BITRATE, CAN_FD_DATA_BITRATE } |
CAN bitrate phase (nominal/data) Implements : can_bitrate_phase_t_Class. More... | |
enum | can_msg_id_type_t { CAN_MSG_ID_STD, CAN_MSG_ID_EXT } |
CAN Message Buffer ID type Implements : can_msg_id_type_t_Class. More... | |
enum | can_clk_source_t { CAN_CLK_SOURCE_OSC = 0U, CAN_CLK_SOURCE_PERIPH = 1U } |
CAN PE clock sources Implements : can_clk_source_t_Class. More... | |
Functions | |
status_t | CAN_Init (const can_instance_t *const instance, const can_user_config_t *config) |
Initializes the CAN module. More... | |
status_t | CAN_Deinit (const can_instance_t *const instance) |
De-initializes the CAN module. More... | |
status_t | CAN_SetBitrate (const can_instance_t *const instance, can_bitrate_phase_t phase, const can_time_segment_t *bitTiming) |
Configures the CAN bitrate. More... | |
status_t | CAN_GetBitrate (const can_instance_t *const instance, can_bitrate_phase_t phase, can_time_segment_t *bitTiming) |
Returns the CAN bitrate. More... | |
status_t | CAN_ConfigTxBuff (const can_instance_t *const instance, uint32_t buffIdx, const can_buff_config_t *config) |
Configures a buffer for transmission. More... | |
status_t | CAN_ConfigRemoteResponseBuff (const can_instance_t *const instance, uint32_t buffIdx, const can_buff_config_t *config, const can_message_t *message) |
Configures a transmit buffer for remote frame response. More... | |
status_t | CAN_ConfigRxBuff (const can_instance_t *const instance, uint32_t buffIdx, const can_buff_config_t *config, uint32_t acceptedId) |
Configures a buffer for reception. More... | |
status_t | CAN_Send (const can_instance_t *const instance, uint32_t buffIdx, const can_message_t *message) |
Sends a CAN frame using the specified buffer. More... | |
status_t | CAN_SendBlocking (const can_instance_t *const instance, uint32_t buffIdx, const can_message_t *message, uint32_t timeoutMs) |
Sends a CAN frame using the specified buffer, in a blocking manner. More... | |
status_t | CAN_Receive (const can_instance_t *const instance, uint32_t buffIdx, can_message_t *message) |
Receives a CAN frame using the specified message buffer. More... | |
status_t | CAN_ReceiveBlocking (const can_instance_t *const instance, uint32_t buffIdx, can_message_t *message, uint32_t timeoutMs) |
Receives a CAN frame using the specified buffer, in a blocking manner. More... | |
status_t | CAN_AbortTransfer (const can_instance_t *const instance, uint32_t buffIdx) |
Ends a non-blocking CAN transfer early. More... | |
status_t | CAN_SetRxFilter (const can_instance_t *const instance, can_msg_id_type_t idType, uint32_t buffIdx, uint32_t mask) |
Configures an ID filter for a specific reception buffer. More... | |
status_t | CAN_GetTransferStatus (const can_instance_t *const instance, uint32_t buffIdx) |
Returns the state of the previous CAN transfer. More... | |
status_t | CAN_InstallEventCallback (const can_instance_t *const instance, can_callback_t callback, void *callbackParam) |
Installs a callback function for the IRQ handler. More... | |
enum can_bitrate_phase_t |
enum can_clk_source_t |
CAN buffer payload sizes Implements : can_fd_payload_size_t_Class.
enum can_msg_id_type_t |
status_t CAN_AbortTransfer | ( | const can_instance_t *const | instance, |
uint32_t | buffIdx | ||
) |
Ends a non-blocking CAN transfer early.
[in] | instance | Instance information structure. |
[in] | buffIdx | buffer index. |
status_t CAN_ConfigRemoteResponseBuff | ( | const can_instance_t *const | instance, |
uint32_t | buffIdx, | ||
const can_buff_config_t * | config, | ||
const can_message_t * | message | ||
) |
Configures a transmit buffer for remote frame response.
[in] | instance | Instance information structure. |
[in] | buffIdx | buffer index. |
[in] | config | buffer configuration. |
[in] | message | frame to be sent as remote response. |
status_t CAN_ConfigRxBuff | ( | const can_instance_t *const | instance, |
uint32_t | buffIdx, | ||
const can_buff_config_t * | config, | ||
uint32_t | acceptedId | ||
) |
Configures a buffer for reception.
This function configures a buffer for reception.
[in] | instance | Instance information structure. |
[in] | buffIdx | buffer index. |
[in] | config | buffer configuration. |
[in] | acceptedId | ID used for accepting frames. |
status_t CAN_ConfigTxBuff | ( | const can_instance_t *const | instance, |
uint32_t | buffIdx, | ||
const can_buff_config_t * | config | ||
) |
Configures a buffer for transmission.
This function configures a buffer for transmission.
[in] | instance | Instance information structure. |
[in] | buffIdx | buffer index. |
[in] | config | buffer configuration. |
status_t CAN_Deinit | ( | const can_instance_t *const | instance | ) |
status_t CAN_GetBitrate | ( | const can_instance_t *const | instance, |
can_bitrate_phase_t | phase, | ||
can_time_segment_t * | bitTiming | ||
) |
Returns the CAN bitrate.
This function returns the CAN configured bitrate.
[in] | instance | Instance information structure. |
[in] | phase | selects between nominal/data phase bitrate. |
[out] | bitTiming | configured bit timing variables. |
status_t CAN_GetTransferStatus | ( | const can_instance_t *const | instance, |
uint32_t | buffIdx | ||
) |
Returns the state of the previous CAN transfer.
When performing an async transfer, call this function to ascertain the state of the current transfer: in progress or complete.
[in] | instance | Instance information structure. |
[in] | buffIdx | buffer index. |
status_t CAN_Init | ( | const can_instance_t *const | instance, |
const can_user_config_t * | config | ||
) |
Initializes the CAN module.
This function initializes and enables the requested CAN module.
[in] | instance | Instance information structure |
[in] | config | The configuration structure |
status_t CAN_InstallEventCallback | ( | const can_instance_t *const | instance, |
can_callback_t | callback, | ||
void * | callbackParam | ||
) |
Installs a callback function for the IRQ handler.
[in] | instance | Instance information structure. |
[in] | callback | The callback function. |
[in] | callbackParam | User parameter passed to the callback function through the state parameter. |
status_t CAN_Receive | ( | const can_instance_t *const | instance, |
uint32_t | buffIdx, | ||
can_message_t * | message | ||
) |
Receives a CAN frame using the specified message buffer.
This function receives a CAN frame using a configured buffer. The function returns immediately. If a callback is installed, it will be invoked after the frame was received and read into the specified buffer.
[in] | instance | Instance information structure. |
[in] | buffIdx | buffer index. |
[out] | message | received message. |
status_t CAN_ReceiveBlocking | ( | const can_instance_t *const | instance, |
uint32_t | buffIdx, | ||
can_message_t * | message, | ||
uint32_t | timeoutMs | ||
) |
Receives a CAN frame using the specified buffer, in a blocking manner.
This function receives a CAN frame using a configured buffer. The function blocks until either a frame was received, or the specified timeout expired.
[in] | instance | Instance information structure. |
[in] | buffIdx | buffer index. |
[out] | message | received message. |
[in] | timeoutMs | A timeout for the transfer in milliseconds. |
status_t CAN_Send | ( | const can_instance_t *const | instance, |
uint32_t | buffIdx, | ||
const can_message_t * | message | ||
) |
Sends a CAN frame using the specified buffer.
This function sends a CAN frame using a configured buffer. The function returns immediately. If a callback is installed, it will be invoked after the frame was sent.
[in] | instance | Instance information structure. |
[in] | buffIdx | buffer index. |
[in] | message | message to be sent. |
status_t CAN_SendBlocking | ( | const can_instance_t *const | instance, |
uint32_t | buffIdx, | ||
const can_message_t * | message, | ||
uint32_t | timeoutMs | ||
) |
Sends a CAN frame using the specified buffer, in a blocking manner.
This function sends a CAN frame using a configured buffer. The function blocks until either the frame was sent, or the specified timeoutMs expired.
[in] | instance | Instance information structure. |
[in] | buffIdx | buffer index. |
[in] | message | message to be sent. |
[in] | timeoutMs | A timeout for the transfer in milliseconds. |
status_t CAN_SetBitrate | ( | const can_instance_t *const | instance, |
can_bitrate_phase_t | phase, | ||
const can_time_segment_t * | bitTiming | ||
) |
Configures the CAN bitrate.
This function configures the CAN bit timing variables.
[in] | instance | Instance information structure. |
[in] | phase | selects between nominal/data phase bitrate. |
[in] | bitTiming | bit timing variables. |
status_t CAN_SetRxFilter | ( | const can_instance_t *const | instance, |
can_msg_id_type_t | idType, | ||
uint32_t | buffIdx, | ||
uint32_t | mask | ||
) |
Configures an ID filter for a specific reception buffer.
This function configures an ID filter for each reception buffer.
[in] | instance | Instance information structure. |
[in] | idType | selects between standard and extended ID. |
[in] | buffIdx | buffer index. |
[in] | mask | mask value for ID filtering. |