The CAN FD driver provides an easy method to access the CAN FD IP block registers and provides simple functionality for sending and receiving data between devices in the CAN FD network.
The CAN FD driver provides an API to configure the main features - mode, bit time, message buffers - and transmit and receive message in all modes:
Specification of the sample code is as follows:
The steps to generate initialization code using the ModusToolbox Device Configurator Tool:
Now, all required CAN FD initialization code and configuration prerequisites will be generated:
For the CAN FD interrupt service routine, Cy_CANFD_IrqHandler() can be used. It handles reading data from the dedicated RX buffers and RX FIFO buffers. Corresponding callback functions are called for error interrupts, RX interrupts and TX complete interrupt. Put the names of callback functions to the Callback functions parameters section. Put NULL if no callback function to be used.
Set up the interrupt handler to be called with CAN FD events. The CAN FD block has two interrupt lines which can be assigned to different interrupt sources using Cy_CANFD_SetInterruptLine(): canfd_0_interrupts0_0_IRQn and canfd_0_interrupts1_0_IRQn. Also, the CAN FD block has a consolidated interrupt canfd_0_interrupt0_IRQn. The following code shows how to set up the interrupt handler.
Call Cy_CANFD_Init() to initialize the CAN FD module. It initializes the CAN FD module with the configuration parameters, passed in the cy_stc_canfd_config_t structure. It consists of several elements to be defined first.
The Cy_CANFD_Init() function also initializes the shared context structure used later with other API functions.
Although the callback functions are optional, they are recommended for use, otherwise, there is no report to the API about any error and transmission or reception events. The example callback function sends received data back to the bus, incrementing ID by 1:
CAN FD IP block requires the configuration of a peripheral clock divider. The following code configures an 8-bit clock divider. The default peripheral clock frequency is 72 MHz. The desired divider value minus one must be passed.
The CAN FD block uses the Port 5 pins for receive (P5[0]) and transmit (P5[1]).
For the CANFD interrupt service routine, the Cy_CANFD_IrqHandler() can be used. It handles reading data from dedicated RX buffers and RX FIFO buffers. Corresponding callback functions are called for error interrupts, RX interrupts and TX complete interrupt.
Setup the interrupt handler to be called with the CAN FD events. The CAN FD block has two interrupt lines, which can be assigned to different interrupt sources using Cy_CANFD_SetInterruptLine(): canfd_0_interrupts0_0_IRQn and canfd_0_interrupts1_0_IRQn. Also, the CAN FD block has a consolidated interrupt canfd_0_interrupt0_IRQn. The following code shows how to set up the interrupt handler.
CAN FD has two bit rate settings, for arbitration and data phases. Both are configured with the same structure, containing a pre-scaler, time segment 1, time segment 2 and synchronization jump width.
The CAN time quantum (tq) may be programmed in the range of 1 to 32 CAN FD clock periods: tq = (prescaler + 1) mtq, where mtq is CAN FD block's clock period. The length of the bit time is (programmed values) [timeSegment1 + timeSegment2 + 3] tq.
The example below shows the configuration with the 100 kbps arbitration bit rate and 200 kbps data bit rate. This assumes the peripheral clock frequency of 72 MHz divided by 9 to obtain the 8 MHz clock for the CAN FD block.
CAN FD driver provides API to setup Message ID filtering. There are standard ID and extended ID filters. The desired count of the filters of each type is specified in the cy_stc_canfd_config_t structure and is set once during block initialization. It is possible to change the configured filters settings with Cy_CANFD_SidFilterSetup() and Cy_CANFD_XidFilterSetup(). Use the cy_stc_id_filter_t structure to set up one standard ID filter:
Use the cy_stc_extid_filter_t structure to set up an extended ID filter:
Message IDs that do not match any filter are received according to the global filter set up. The global filter can be set up to receive messages with standard and extended IDs to different FIFO buffers. It can be configured to reject remote frames, as shown below.
The RX FIFO buffers, FIFO 0 and FIFO 1 are configured once on block initialization using cy_en_canfd_fifo_config_t structure.
The cy_stc_canfd_config_t structure is used to pass all configuration to Cy_CANFD_Init() function. It is populated with pointers to other structures required and constants, defined before.
The Cy_CANFD_Init() function initializes the CAN FD block by writing CAN FD configuration registers. Cy_CANFD_Init() enables the RX interrupts for new message reception into the dedicated RX buffers, FIFO 0 and FIFO 1. The code example also shows the test mode configuration which can be used to enable the Loopback mode. See cy_stc_canfd_test_mode_t for details. Cy_CANFD_Init() sets test mode configuration to CY_CANFD_TEST_MODE_DISABLE. Remember to disable the echo functionality in the RX callback when using a loopback.
To send a CAN FD message, a TX buffer structure must be prepared which consists of the T0 and T1 registers and data array.
To transmit CAN FD messages, the function Cy_CANFD_UpdateAndTransmitMsgBuffer() is used. The buffer status can be retrieved by Cy_CANFD_GetTxBufferStatus(). It is possible to set a callback function which will be notified whenever a message transmission has been completed.
For more information on the CAN FD peripheral, refer to the technical reference manual (TRM).
Version | Changes | Reason for Change |
---|---|---|
1.40 | Updated APIs Cy_CANFD_Init , Cy_CANFD_CalcRxBufAdrs, Cy_CANFD_CalcRxFifoAdrs, Cy_CANFD_XidFilterSetup. | Bug Fixes. |
1.30 | Updated Cy_CANFD_Init() and Cy_CANFD_DeInit() functions. Added volatile qualifier to prevent loop optimization. | Code Enhancement. |
1.20 | Added const modifier to the functions parameters where needed. Improved error handling in Cy_CANFD_Init() and Cy_CANFD_IrqHandler(). | Source code cleanup. |
1.10 | Updated Cy_CANFD_Init() functions. | Allow initing CANFD with 0 number of SID/XID filters. |
1.0.1 | Updated description of the Cy_CANFD_Init() and Cy_CANFD_DeInit() functions. | Documentation update and clarification. |
1.0 | Initial version |
API Reference | |
Macros | |
This section describes the CAN FD Macros. | |
Functions | |
Data Structures | |
Enumerated Types | |