RA Flexible Software Package Documentation  Release v5.2.0

 
CAN FD (r_canfd)

Functions

fsp_err_t R_CANFD_Open (can_ctrl_t *const p_api_ctrl, can_cfg_t const *const p_cfg)
 
fsp_err_t R_CANFD_Close (can_ctrl_t *const p_api_ctrl)
 
fsp_err_t R_CANFD_Write (can_ctrl_t *const p_api_ctrl, uint32_t buffer, can_frame_t *const p_frame)
 
fsp_err_t R_CANFD_Read (can_ctrl_t *const p_api_ctrl, uint32_t buffer, can_frame_t *const p_frame)
 
fsp_err_t R_CANFD_ModeTransition (can_ctrl_t *const p_api_ctrl, can_operation_mode_t operation_mode, can_test_mode_t test_mode)
 
fsp_err_t R_CANFD_InfoGet (can_ctrl_t *const p_api_ctrl, can_info_t *const p_info)
 
fsp_err_t R_CANFD_CallbackSet (can_ctrl_t *const p_api_ctrl, void(*p_callback)(can_callback_args_t *), void const *const p_context, can_callback_args_t *const p_callback_memory)
 

Detailed Description

Driver for the CANFD peripheral on RA MCUs. This module implements the CAN Interface.

Overview

The CANFD module can be used to communicate over CAN networks, optionally using Flexible Data (CAN-FD) to accelerate the data phase. A variety of message filtering and buffer options are available.

Features

Common Features

Per-MCU Specifications

RA6M5 RA6T2 RA8M1
Channels 2 1 2
Max nominal bitrate 1 Mbps 1 Mbps 1 Mbps
Max data bitrate 5 Mbps 5 Mbps 8 Mbps
Filter rules 128 32 16/ch
TX message buffers 16/ch 4 4/ch
RX message buffers 32 32 16/ch
RX FIFOs 8 2 2/ch
RX Buffer RAM 4864 bytes 1216 bytes 1216 bytes
Common FIFOs 3/ch 1 1/ch
Note
Each message buffer comprises 12 header bytes plus data length (8-64 bytes). The above buffer RAM values therefore correspond to the following capacities:
RA6M5 RA6T2 RA8M1
Max 64-byte storage 64 messages 16 messages 16 messages
Max 8-byte storage 243 messages 60 messages 60 messages
Each Common FIFO buffer can support the following message capacities:
RA6M5 RA6T2 RA8M1
Maximum payload size 64 bytes 64 bytes 64 bytes
Maximum FIFO depth 128 messages 48 messages 48 messages

Configuration

Build Time Configurations for r_canfd

The following build time configurations are defined in fsp_cfg/r_canfd_cfg.h:

ConfigurationOptionsDefaultDescription
Global Error Interrupt
Callback ChannelMCU Specific OptionsSpecify which channel callback should be called to handle global errors. When starting the driver this channel must be opened first.
PriorityMCU Specific OptionsThis interrupt is fired for each of the error sources selected below.
Sources
  • DLC Check
  • Message Lost
  • FD Payload Overflow
0U Select which errors should trigger an interrupt.
Reception
Reception > FIFOs
Reception > FIFOs > FIFO 0
Enable
  • Enabled
  • Disabled
Enabled Enable or disable RX FIFO 0.
Interrupt ModeMCU Specific OptionsSet the interrupt mode for RX FIFO 0. Threshold mode will only fire an interrupt each time an incoming message crosses the threshold value set below.
Interrupt ThresholdMCU Specific OptionsSet the interrupt threshold value for RX FIFO 0. This setting is only applicable when the Interrupt Mode is set to 'At Threshold Value'.
Payload SizeMCU Specific OptionsSelect the message payload size for RX FIFO 0.
DepthMCU Specific OptionsSelect the number of stages for RX FIFO 0.
Reception > FIFOs > FIFO 1
Enable
  • Enabled
  • Disabled
Disabled Enable or disable RX FIFO 1.
Interrupt ModeMCU Specific OptionsSet the interrupt mode for RX FIFO 1. Threshold mode will only fire an interrupt each time an incoming message crosses the threshold value set below.
Interrupt ThresholdMCU Specific OptionsSet the interrupt threshold value for RX FIFO 1. This setting is only applicable when the Interrupt Mode is set to 'At Threshold Value'.
Payload SizeMCU Specific OptionsSelect the message payload size for RX FIFO 1.
DepthMCU Specific OptionsSelect the number of stages for RX FIFO 1.
Reception > FIFOs > FIFO 2
Enable
  • Enabled
  • Disabled
Disabled Enable or disable RX FIFO 2.
Interrupt ModeMCU Specific OptionsSet the interrupt mode for RX FIFO 2. Threshold mode will only fire an interrupt each time an incoming message crosses the threshold value set below.
Interrupt ThresholdMCU Specific OptionsSet the interrupt threshold value for RX FIFO 2. This setting is only applicable when the Interrupt Mode is set to 'At Threshold Value'.
Payload SizeMCU Specific OptionsSelect the message payload size for RX FIFO 2.
DepthMCU Specific OptionsSelect the number of stages for RX FIFO 2.
Reception > FIFOs > FIFO 3
Enable
  • Enabled
  • Disabled
Disabled Enable or disable RX FIFO 3.
Interrupt ModeMCU Specific OptionsSet the interrupt mode for RX FIFO 3. Threshold mode will only fire an interrupt each time an incoming message crosses the threshold value set below.
Interrupt ThresholdMCU Specific OptionsSet the interrupt threshold value for RX FIFO 3. This setting is only applicable when the Interrupt Mode is set to 'At Threshold Value'.
Payload SizeMCU Specific OptionsSelect the message payload size for RX FIFO 3.
DepthMCU Specific OptionsSelect the number of stages for RX FIFO 3.
Reception > FIFOs > FIFO 4
Enable
  • Enabled
  • Disabled
Disabled Enable or disable RX FIFO 4.
Interrupt ModeMCU Specific OptionsSet the interrupt mode for RX FIFO 4. Threshold mode will only fire an interrupt each time an incoming message crosses the threshold value set below.
Interrupt ThresholdMCU Specific OptionsSet the interrupt threshold value for RX FIFO 4. This setting is only applicable when the Interrupt Mode is set to 'At Threshold Value'.
Payload SizeMCU Specific OptionsSelect the message payload size for RX FIFO 4.
DepthMCU Specific OptionsSelect the number of stages for RX FIFO 4.
Reception > FIFOs > FIFO 5
Enable
  • Enabled
  • Disabled
Disabled Enable or disable RX FIFO 5.
Interrupt ModeMCU Specific OptionsSet the interrupt mode for RX FIFO 5. Threshold mode will only fire an interrupt each time an incoming message crosses the threshold value set below.
Interrupt ThresholdMCU Specific OptionsSet the interrupt threshold value for RX FIFO 5. This setting is only applicable when the Interrupt Mode is set to 'At Threshold Value'.
Payload SizeMCU Specific OptionsSelect the message payload size for RX FIFO 5.
DepthMCU Specific OptionsSelect the number of stages for RX FIFO 5.
Reception > FIFOs > FIFO 6
Enable
  • Enabled
  • Disabled
Disabled Enable or disable RX FIFO 6.
Interrupt ModeMCU Specific OptionsSet the interrupt mode for RX FIFO 6. Threshold mode will only fire an interrupt each time an incoming message crosses the threshold value set below.
Interrupt ThresholdMCU Specific OptionsSet the interrupt threshold value for RX FIFO 6. This setting is only applicable when the Interrupt Mode is set to 'At Threshold Value'.
Payload SizeMCU Specific OptionsSelect the message payload size for RX FIFO 6.
DepthMCU Specific OptionsSelect the number of stages for RX FIFO 6.
Reception > FIFOs > FIFO 7
Enable
  • Enabled
  • Disabled
Disabled Enable or disable RX FIFO 7.
Interrupt ModeMCU Specific OptionsSet the interrupt mode for RX FIFO 7. Threshold mode will only fire an interrupt each time an incoming message crosses the threshold value set below.
Interrupt ThresholdMCU Specific OptionsSet the interrupt threshold value for RX FIFO 7. This setting is only applicable when the Interrupt Mode is set to 'At Threshold Value'.
Payload SizeMCU Specific OptionsSelect the message payload size for RX FIFO 7.
DepthMCU Specific OptionsSelect the number of stages for RX FIFO 7.
Interrupt PriorityMCU Specific OptionsThis priority level will apply to all FIFO interrupts globally.
Reception > Message Buffers
Number of BuffersRX Message Buffer number must be an integer between 0 and 32.0 Number of message buffers available for reception.

As there is no interrupt for message buffer reception it is recommended to use RX FIFOs instead. Set this value to 0 to disable RX Message Buffers.
Payload Size
  • 8 bytes
  • 12 bytes
  • 16 bytes
  • 20 bytes
  • 24 bytes
  • 32 bytes
  • 48 bytes
  • 64 bytes
8 bytes Payload size for all RX Message Buffers.
Reception > Acceptance Filtering
Channel 0 Rule CountThe number of AFL rules must be a positive integer.32 Number of acceptance filter list rules dedicated to Channel 0.
Channel 1 Rule CountThe number of AFL rules must be a positive integer.0 Number of acceptance filter list rules dedicated to Channel 1.
Flexible Data (FD)
Protocol Exceptions
  • Enabled (ISO 11898-1)
  • Disabled
Enabled (ISO 11898-1) Select whether to enter the protocol exception handling state when a RES bit is sampled recessive as defined in ISO 11898-1.
Payload Overflow
  • Reject
  • Truncate
Reject Configure whether received messages larger than the destination buffer should be truncated or rejected.
Common FIFOs
Common FIFOs > FIFO 0
Enable
  • Enabled
  • Disabled
Disabled Enable or disable Common FIFO 0.
Mode
  • Receive
  • Transmit
Receive Select the operation mode for Common FIFO 0.
RX Interrupt Enable
  • Enabled
  • Disabled
Disabled Enable to allow interrupts on message receiption for Common FIFO 0.
TX Interrupt Enable
  • Enabled
  • Disabled
Disabled Enable to allow interrupts on message transmission for Common FIFO 0.
Interrupt ModeMCU Specific OptionsSet the interrupt mode for Common FIFO 0. Threshold mode will only fire an interrupt each time an incoming message crosses the threshold value set below or when all messages have been transmitted.
Interrupt ThresholdMCU Specific OptionsSet the interrupt threshold value for Common FIFO 0. This setting is only applicable when the Interrupt Mode is set to 'At Threshold Value'.
Payload SizeMCU Specific OptionsSelect the message payload size for Common FIFO 0.
DepthMCU Specific OptionsSelect the number of stages for Common FIFO 0.
TX Message BufferTX message buffer must be an integer between 0 and 31.0 Select the TX message buffer that participates in TX scans related to Common FIFO 0.
Interval TX DelayTX delay must be an integer between 0 and 255.0 Selects the delay between successive transmission for Common FIFO 0 in units of the Interval Timer Clock.
Interval Timer
  • Reference clock
  • Bit time clock
Reference clock Select the source of the interval timer clock for Common FIFO 0. The reference clock can be scaled by x10 below.
Interval Timer Reference Clock Resolution
  • x1
  • x10
x1 Selects the timer resolution if the reference clock is selected as the timer source for Common FIFO 0.
Common FIFOs > FIFO 1
Enable
  • Enabled
  • Disabled
Disabled Enable or disable Common FIFO 1.
Mode
  • Receive
  • Transmit
Receive Select the operation mode for Common FIFO 1.
RX Interrupt Enable
  • Enabled
  • Disabled
Disabled Enable to allow interrupts on message receiption for Common FIFO 1.
TX Interrupt Enable
  • Enabled
  • Disabled
Disabled Enable to allow interrupts on message transmission for Common FIFO 1.
Interrupt ModeMCU Specific OptionsSet the interrupt mode for Common FIFO 1. Threshold mode will only fire an interrupt each time an incoming message crosses the threshold value set below or when all messages have been transmitted.
Interrupt ThresholdMCU Specific OptionsSet the interrupt threshold value for Common FIFO 1. This setting is only applicable when the Interrupt Mode is set to 'At Threshold Value'.
Payload SizeMCU Specific OptionsSelect the message payload size for Common FIFO 1.
DepthMCU Specific OptionsSelect the number of stages for Common FIFO 1.
TX Message BufferTX message buffer must be an integer between 0 and 31.0 Select the TX message buffer that participates in TX scans related to Common FIFO 1.
Interval TX DelayTX delay must be an integer between 0 and 255.0 Selects the delay between successive transmission for Common FIFO 1 in units of the Interval Timer Clock.
Interval Timer
  • Reference clock
  • Bit time clock
Reference clock Select the source of the interval timer clock for Common FIFO 1. The reference clock can be scaled by x10 below.
Interval Timer Reference Clock Resolution
  • x1
  • x10
x1 Selects the timer resolution if the reference clock is selected as the timer source for Common FIFO 1.
Common FIFOs > FIFO 2
Enable
  • Enabled
  • Disabled
Disabled Enable or disable Common FIFO 2.
Mode
  • Receive
  • Transmit
Receive Select the operation mode for Common FIFO 2.
RX Interrupt Enable
  • Enabled
  • Disabled
Disabled Enable to allow interrupts on message receiption for Common FIFO 2.
TX Interrupt Enable
  • Enabled
  • Disabled
Disabled Enable to allow interrupts on message transmission for Common FIFO 2.
Interrupt ModeMCU Specific OptionsSet the interrupt mode for Common FIFO 2. Threshold mode will only fire an interrupt each time an incoming message crosses the threshold value set below or when all messages have been transmitted.
Interrupt ThresholdMCU Specific OptionsSet the interrupt threshold value for Common FIFO 2. This setting is only applicable when the Interrupt Mode is set to 'At Threshold Value'.
Payload SizeMCU Specific OptionsSelect the message payload size for Common FIFO 2.
DepthMCU Specific OptionsSelect the number of stages for Common FIFO 2.
TX Message BufferTX message buffer must be an integer between 0 and 31.0 Select the TX message buffer that participates in TX scans related to Common FIFO 2.
Interval TX DelayTX delay must be an integer between 0 and 255.0 Selects the delay between successive transmission for Common FIFO 2 in units of the Interval Timer Clock.
Interval Timer
  • Reference clock
  • Bit time clock
Reference clock Select the source of the interval timer clock for Common FIFO 2. The reference clock can be scaled by x10 below.
Interval Timer Reference Clock Resolution
  • x1
  • x10
x1 Selects the timer resolution if the reference clock is selected as the timer source for Common FIFO 2.
Common FIFOs > FIFO 3
Enable
  • Enabled
  • Disabled
Disabled Enable or disable Common FIFO 3.
Mode
  • Receive
  • Transmit
Receive Select the operation mode for Common FIFO 3.
RX Interrupt Enable
  • Enabled
  • Disabled
Disabled Enable to allow interrupts on message receiption for Common FIFO 3.
TX Interrupt Enable
  • Enabled
  • Disabled
Disabled Enable to allow interrupts on message transmission for Common FIFO 3.
Interrupt ModeMCU Specific OptionsSet the interrupt mode for Common FIFO 3. Threshold mode will only fire an interrupt each time an incoming message crosses the threshold value set below or when all messages have been transmitted.
Interrupt ThresholdMCU Specific OptionsSet the interrupt threshold value for Common FIFO 3. This setting is only applicable when the Interrupt Mode is set to 'At Threshold Value'.
Payload SizeMCU Specific OptionsSelect the message payload size for Common FIFO 3.
DepthMCU Specific OptionsSelect the number of stages for Common FIFO 3.
TX Message BufferTX message buffer must be an integer between 0 and 31.0 Select the TX message buffer that participates in TX scans related to Common FIFO 3.
Interval TX DelayTX delay must be an integer between 0 and 255.0 Selects the delay between successive transmission for Common FIFO 3 in units of the Interval Timer Clock.
Interval Timer
  • Reference clock
  • Bit time clock
Reference clock Select the source of the interval timer clock for Common FIFO 3. The reference clock can be scaled by x10 below.
Interval Timer Reference Clock Resolution
  • x1
  • x10
x1 Selects the timer resolution if the reference clock is selected as the timer source for Common FIFO 3.
Common FIFOs > FIFO 4
Enable
  • Enabled
  • Disabled
Disabled Enable or disable Common FIFO 4.
Mode
  • Receive
  • Transmit
Receive Select the operation mode for Common FIFO 4.
RX Interrupt Enable
  • Enabled
  • Disabled
Disabled Enable to allow interrupts on message receiption for Common FIFO 4.
TX Interrupt Enable
  • Enabled
  • Disabled
Disabled Enable to allow interrupts on message transmission for Common FIFO 4.
Interrupt ModeMCU Specific OptionsSet the interrupt mode for Common FIFO 4. Threshold mode will only fire an interrupt each time an incoming message crosses the threshold value set below or when all messages have been transmitted.
Interrupt ThresholdMCU Specific OptionsSet the interrupt threshold value for Common FIFO 4. This setting is only applicable when the Interrupt Mode is set to 'At Threshold Value'.
Payload SizeMCU Specific OptionsSelect the message payload size for Common FIFO 4.
DepthMCU Specific OptionsSelect the number of stages for Common FIFO 4.
TX Message BufferTX message buffer must be an integer between 0 and 31.0 Select the TX message buffer that participates in TX scans related to Common FIFO 4.
Interval TX DelayTX delay must be an integer between 0 and 255.0 Selects the delay between successive transmission for Common FIFO 4 in units of the Interval Timer Clock.
Interval Timer
  • Reference clock
  • Bit time clock
Reference clock Select the source of the interval timer clock for Common FIFO 4. The reference clock can be scaled by x10 below.
Interval Timer Reference Clock Resolution
  • x1
  • x10
x1 Selects the timer resolution if the reference clock is selected as the timer source for Common FIFO 4.
Common FIFOs > FIFO 5
Enable
  • Enabled
  • Disabled
Disabled Enable or disable Common FIFO 5.
Mode
  • Receive
  • Transmit
Receive Select the operation mode for Common FIFO 5.
RX Interrupt Enable
  • Enabled
  • Disabled
Disabled Enable to allow interrupts on message receiption for Common FIFO 5.
TX Interrupt Enable
  • Enabled
  • Disabled
Disabled Enable to allow interrupts on message transmission for Common FIFO 5.
Interrupt ModeMCU Specific OptionsSet the interrupt mode for Common FIFO 5. Threshold mode will only fire an interrupt each time an incoming message crosses the threshold value set below or when all messages have been transmitted.
Interrupt ThresholdMCU Specific OptionsSet the interrupt threshold value for Common FIFO 5. This setting is only applicable when the Interrupt Mode is set to 'At Threshold Value'.
Payload SizeMCU Specific OptionsSelect the message payload size for Common FIFO 5.
DepthMCU Specific OptionsSelect the number of stages for Common FIFO 5.
TX Message BufferTX message buffer must be an integer between 0 and 31.0 Select the TX message buffer that participates in TX scans related to Common FIFO 5.
Interval TX DelayTX delay must be an integer between 0 and 255.0 Selects the delay between successive transmission for Common FIFO 5 in units of the Interval Timer Clock.
Interval Timer
  • Reference clock
  • Bit time clock
Reference clock Select the source of the interval timer clock for Common FIFO 5. The reference clock can be scaled by x10 below.
Interval Timer Reference Clock Resolution
  • x1
  • x10
x1 Selects the timer resolution if the reference clock is selected as the timer source for Common FIFO 5.
Parameter Checking
  • Default (BSP)
  • Enabled
  • Disabled
Default (BSP) If selected code for parameter checking is included in the build.
Transmission Priority
  • Message ID
  • Buffer Number
Buffer Number Select how messages should be prioritized for transmission. In either case, lower numbers indicate higher priority.
DLC Check
  • Disabled
  • Enabled
  • Enabled w/truncate
config.driver.canfd.dlc_check.disabled When enabled received messages will be rejected if their DLC field is less than the value configured in the associated AFL rule. If 'Enabled w/truncate' is selected and a message passes the DLC check the DLC field is set to the value in the associated AFL rule and any excess data is discarded.

Configurations for Connectivity > CAN FD (r_canfd)

This module can be added to the Stacks tab via New Stack > Connectivity > CAN FD (r_canfd). Non-secure callable guard functions can be generated for this module by right clicking the module in the RA Configuration tool and checking the "Non-secure Callable" box.

ConfigurationOptionsDefaultDescription
General
NameName must be a valid C symbolg_canfd0 Module name.
ChannelChannel should be 0 or 10 Specify the CAN channel to use.
Bitrate
Bitrate > Automatic
Nominal Rate (bps)Must be a valid integer with a maximum of 1MHz.500000 Specify nominal bitrate in bits per second.
FD Data Rate (bps)Must be a valid integer with a minimum of 1MHz.2000000 Specify data bitrate in bits per second. This value is not used when sending messages without the FD and BRS bits enabled and should be left at the default setting if only Classical CAN will be used.
Sample Point (%)Must be a valid integer between 60 and 99.75 Specify desired sample point.
Bitrate > Manual
Bitrate > Manual > Nominal
Prescaler (divisor)Value must be a non-negative integer between 1 and 1024.1 Specify clock divisor for nominal bitrate.
Time Segment 1 (Tq)Value must be a non-negative integer between 2 and 256.29 Select the Time Segment 1 value. Check module usage notes for how to calculate this value.
Time Segment 2 (Tq)Value must be a non-negative integer between 2 and 128.10 Select the Time Segment 2 value. Check module usage notes for how to calculate this value.
Sync Jump Width (Tq)Value must be a non-negative integer between 1 and 128.4 Select the Synchronization Jump Width value. Check module usage notes for how to calculate this value.
Bitrate > Manual > Data
Prescaler (divisor)Value must be a non-negative integer between 1 and 256.1 Specify clock divisor for data bitrate.
Time Segment 1 (Tq)Value must be a non-negative integer between 2 and 32.5 Select the Time Segment 1 value. Check module usage notes for how to calculate this value.
Time Segment 2 (Tq)Value must be a non-negative integer between 2 and 16.2 Select the Time Segment 2 value. Check module usage notes for how to calculate this value.
Sync Jump Width (Tq)Value must be a non-negative integer between 1 and 16.1 Select the Synchronization Jump Width value. Check module usage notes for how to calculate this value.
Use manual settings
  • Yes
  • No
No Select whether or not to override automatic baudrate generation and instead use the values specified here.
Delay Compensation
  • Enable
  • Disable
Enable When enabled the CANFD module will automatically compensate for any transceiver or bus delay between transmitted and received bits. When manually supplying bit timing values with delay compensation enabled be sure the data prescaler is 2 or smaller for correct operation.
Interrupts
CallbackName must be a valid C symbolcanfd0_callback A user callback function. If this callback function is provided, it is called from the interrupt service routine (ISR) each time any interrupt occurs.
Channel Interrupt Priority LevelMCU Specific OptionsChannel Error/Transmit interrupt priority.
Transmit InterruptsMCU Specific OptionsSelect which TX Message Buffers should trigger an interrupt when transmission is complete.
Channel Error Interrupts
  • Error Warning
  • Error Passive
  • Bus-Off Entry
  • Bus-Off Recovery
  • Overload
0U Select which channel error interrupt sources to enable.
Filter List ArrayName must be a valid C symbolp_canfd0_afl Acceptance Filter List (AFL) rule array symbol name.

Clock Configuration

The CANFD peripheral uses PLL or PLL2 as its clock source. The RA Configuration editor will attempt to get as close as possible to the supplied bitrate with the configured clock source. To achieve an exact bitrate the CANFD source clock or divisor may need to be adjusted to meet the criteria in the formula below:

bitrate = canfd_clock_hz / ((time_segment_1 + time_segment_2 + 1) * prescalar)

For CANFD, the possible values for each element are as follows:

Element Min Max (Nominal) Max (Data)
Bitrate - 1 Mbps 5-8 Mbps*
Time Segment 1 2 Tq 256 Tq 32 Tq
Time Segment 2 2 Tq 128 Tq 16 Tq
Sync Jump Width 1 Tq Time Segment 2 Time Segment 2
Prescalar 1 1024 256

Use the Clocks tab of the RA Configuration editor to configure the CANFD clock source/divisor as well as to set the frequency of PLL or PLL2. To change the clock frequency at run-time, use the CGC Interface. Refer to the CGC module guide for more information on configuring clocks.

Pin Configuration

CANFD channels each control two pins: CRX (receive) and CTX (transmit).

Usage Notes

Buffers

The CANFD driver provides four types of buffers: Transmit Message Buffers (TX MBs), Receive Message Buffers (RX MBs), Receive FIFOs (RX FIFOs), and Common FIFOs (RX/TX Common FIFOs).

TX Message Buffers

TX MBs are used for transmission only. Refer to the hardware manual for your device for information on which TX MBs are available.

Note
The CANFD peripheral continually scans TX MBs for new data. Depending on the provided clock it may be possible to write to multiple TX MBs before transmission begins. In this case, messages will be sent in the priority specified by the Transmission Priority option in the RA Configuration editor.

RX Message Buffers

RX MBs are for reception only and may only hold one message at a time. The number of available RX MBs varies per device.

On RA6M5, RX MBs are shared between channels and no interrupts are provided. Use R_CANFD_InfoGet and R_CANFD_Read to poll and read them, respectively.

RX FIFOs

RX FIFOs provide interrupt-driven queue functionality for receiving messages. All FIFOs have the following capabilities:

Once an interrupt is fired it will continue to fire until the FIFO is emptied and all messages have been passed to user code via the callback. When using the threshold interrupt mode a FIFO can be checked for data and read between interrupts by calling R_CANFD_InfoGet and R_CANFD_Read, respectively.

Note
On the RA6M5, FIFOs are shared across all channels.

RX Buffer Pool

The CANFD peripheral has a limited amount of buffer pool RAM available for allocating RX MBs and FIFO stages. The RA Configuration editor will provide a warning when the limit is exceeded.

The number of bytes used by RX MBs and individual FIFOs can be calculated as follows:

Total RX MB bytes used = (number of RX MBs enabled) * (RX MB payload size + 12 header bytes)
RX FIFO bytes used = (number of FIFO stages) * (FIFO payload size + 12 header bytes)

Common FIFOs

Common FIFOs provide interrupt-driven queue functionality for receiving or transmitting messages. Unlike RX FIFOs, Common FIFOs are individual to each channel. Refer to the hardware manual for your device for information on how many Common FIFOs are available. Common FIFOs support the following capabilities:

Once an interrupt is fired it will continue to fire until the FIFO is emptied and all messages have been passed to user code via the callback. When using the threshold interrupt mode a FIFO can be checked for data and read between interrupts by calling R_CANFD_InfoGet and R_CANFD_Read, respectively.

Users should be aware of the total memory allocated in the internal CAN-FD RAM area for Common FIFOs. Allocating too many FIFOs or entries can lead to unexpected behavior.

Message Filtering (Acceptance Filter List)

To filter messages to the desired message buffer or FIFO the CANFD peripheral uses an Acceptance Filter List (AFL). Each entry in the AFL provides a rule to check a message against along with destination and other filtering information. When a message is received the CANFD peripheral internally checks against every configured AFL rule for the channel. If a match is found the message is transferred to the destination(s) specified in the rule. The default template with one entry is shown below:

static const canfd_afl_entry_t p_canfd0_afl[CANFD_CFG_AFL_CH0_RULE_NUM] =
{
{
.id =
{
/* Specify the ID, ID type and frame type to accept. */
.id = 0x00000000,
.frame_type = CAN_FRAME_TYPE_DATA,
},
.mask =
{
/* These values mask which ID/mode bits to compare when filtering messages. */
.mask_id = 0x1FFFFFFF,
.mask_frame_type = 1,
.mask_id_mode = 1,
},
.destination =
{
/* If DLC checking is enabled any messages shorter than the below setting will be rejected. */
.minimum_dlc = CANFD_MINIMUM_DLC_0,
/* Optionally specify a Receive Message Buffer (RX MB) to store accepted frames. RX MBs do not have an
* interrupt or overwrite protection and must be checked with R_CANFD_InfoGet and R_CANFD_Read. */
.rx_buffer = CANFD_RX_MB_NONE,
/* Specify which FIFO(s) to send filtered messages to. Multiple FIFOs can be OR'd together. */
.fifo_select_flags = CANFD_RX_FIFO_0,
}
}
};

AFL templates can be easily added to a project using the Developer Assistance feature in e² studio. Once the CANFD module is added to a project, drag and drop the elements circled below to build a filter list:

r_canfd_afl_devassist.png
CANFD Developer Assistance AFL Templates

For an example configuration refer to the AFL Example below.

Flexible Data (FD)

Flexible Data is an extension of the CAN protocol allowing for messages up to 64 bytes and higher data bitrates, among other features. The CANFD driver supports the following:

To specify one or more of these options when transmitting set can_frame_t::options with combined values from canfd_frame_options_t. Received messages will automatically have this field filled, if applicable.

/* Configure a frame to write 64 bytes with bitrate switching (BRS) enabled */
g_can_tx_frame.id = CAN_EXAMPLE_ID;
g_can_tx_frame.id_mode = CAN_ID_MODE_STANDARD;
g_can_tx_frame.type = CAN_FRAME_TYPE_DATA;
g_can_tx_frame.data_length_code = CAN_EXAMPLE_64_BYTES;
Note
When using bitrate switching be sure to configure the Data Bitrate as desired in the RA Configuration editor.

Bit Rate Calculation

For convenience, the baudrate of the CANFD peripheral is automatically set through the RA Configuration editor using a best effort approach.

Enabling Delay Compensation instructs the CANFD peripheral to measure TX to RX tranceiver delay and automatically adjust for it, improving the reliability of high-speed FD messages. This option may severely limit available bitrate settings depending on the source clock; it is highly recommended to check the generated values when enabled.

If the auto-generated baud settings cause deviation that is not tolerable by the application the user can override the auto-generated settings and put in manually calculated values through the RA Configuration editor. For more details on how the bitrate is calculated refer to the Clock Configuration section above.

Sync Jump Width

The Sync Jump Width option specifies the maximum number of time quanta that the sample point may be delayed by to account for differences in oscillators on the bus. It should be set to a value between 1 and the configured Time Segment 2 value depending on the maximum permissible clock error.

Error Handling

The CANFD peripheral provides two types of error interrupts: Channel and Global. As the names imply, each channel has its own Channel Error interrupt but there is only one Global Error interrupt. Only the configured channel will receive callbacks for Global Errors.

Error interrupt callbacks will pass either CAN_EVENT_ERR_CHANNEL or CAN_EVENT_ERR_GLOBAL in the can_callback_args_t::event field. A second field, can_callback_args_t::error, provides the actual error code as canfd_error_t. Cast to this enum to retrieve the error condition. See the callback in the Basic Example below for a demonstration.

DLC Checking

When DLC Checking is enabled messages are checked against the destination.minimum_dlc value of each AFL rule. If the data length of a message is less than this value the message will be rejected. When DLC checking is set to "Enabled w/truncate" in the RA Configuration editor any data in excess of the minimum DLC setting will be truncated and the DLC value for the frame will be set to match.

FD Payload Overflow

When an FD message is received with a DLC larger than the destination buffer an FD Payload Overflow interrupt is thrown (if configured). When Payload Overflow is set to "Truncate" the message will still be accepted but only data up to the buffer capacity will be preserved. The DLC value is unchanged in this case; any data beyond this value in the can_frame_t::data array should not be used.

Test Modes

The CANFD peripheral provides three basic test modes: Listen Only, Internal Loopback and External Loopback. Use R_CANFD_ModeTransition to switch to a test mode.

On some MCUs an additional "Internal Bus" test mode is available that allows connecting both CANFD channels together on an internal bus, effectively creating an internal CAN network. See the Internal Bus example below for details.

Limitations

Developers should be aware of the following limitations when using CANFD:

Examples

AFL Example

The below is an example Acceptance Filter List (AFL) declaration with two rules.

const canfd_afl_entry_t p_canfd0_afl[CANFD_CFG_AFL_CH1_RULE_NUM] =
{
/* Store all data frames with at least 4 bytes from Standard IDs 0x40-0x4F in RX FIFO 0 and RX FIFO 1 */
{
.id =
{
.id = 0x40,
.frame_type = CAN_FRAME_TYPE_DATA,
},
.mask =
{
.mask_id = 0x7F0,
.mask_frame_type = 1,
.mask_id_mode = 1
},
.destination =
{
.minimum_dlc = CANFD_MINIMUM_DLC_4,
.rx_buffer = CANFD_RX_MB_NONE,
.fifo_select_flags = (canfd_rx_fifo_t) (CANFD_RX_FIFO_0 | CANFD_RX_FIFO_1)
}
},
/* Store all frames from Extended ID 0x1100 in RX FIFO 2 and RX MB 0 */
{
.id =
{
.id = 0x1100,
.frame_type = CAN_FRAME_TYPE_DATA, // This setting is ignored by the mask
},
.mask =
{
.mask_id = 0x1FFFFFFF,
.mask_frame_type = 0,
.mask_id_mode = 1
},
.destination =
{
.minimum_dlc = CANFD_MINIMUM_DLC_0,
.rx_buffer = CANFD_RX_MB_0,
.fifo_select_flags = CANFD_RX_FIFO_2
}
}
};

Basic Example

This is a basic example of minimal use of the CANFD module in an application.

Note
On RA6M5 it is recommended to use RX FIFOs for reception as there are no interrupts for RX message buffers.
#define CAN_EXAMPLE_ID (0x20)
can_frame_t g_can_tx_frame;
can_frame_t g_can_rx_frame;
volatile canfd_error_t g_err_status = (canfd_error_t) 0;
void canfd_callback (can_callback_args_t * p_args)
{
switch (p_args->event)
{
case CAN_EVENT_RX_COMPLETE: /* Receive complete event. */
{
/* Read received frame */
memcpy(&g_can_rx_frame, &p_args->frame, sizeof(can_frame_t));
/* Handle event */
break;
}
case CAN_EVENT_TX_COMPLETE: /* Transmit complete event. */
{
/* Handle event */
break;
}
case CAN_EVENT_ERR_GLOBAL: /* Global error. */
case CAN_EVENT_ERR_CHANNEL: /* Channel error. */
{
/* Get error status */
g_err_status = (canfd_error_t) p_args->error; /* Check error code with canfd_error_t. */
/* Handle event */
break;
}
default:
{
break;
}
}
}
void canfd_basic_example (void)
{
fsp_err_t err;
/* Initialize the CAN module */
err = R_CANFD_Open(&g_canfd0_ctrl, &g_canfd0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
/* Setup frame to write to CAN ID 0x20 */
g_can_tx_frame.id = CAN_EXAMPLE_ID;
g_can_tx_frame.id_mode = CAN_ID_MODE_STANDARD;
g_can_tx_frame.type = CAN_FRAME_TYPE_DATA;
g_can_tx_frame.data_length_code = 8;
g_can_tx_frame.options = 0;
/* Write some data to the transmit frame */
for (uint32_t i = 0; i < 8; i++)
{
g_can_tx_frame.data[i] = (uint8_t) i;
}
/* Send data on the bus */
err = R_CANFD_Write(&g_canfd0_ctrl, CANFD_TX_MB_0, &g_can_tx_frame);
assert(FSP_SUCCESS == err);
/* Wait for a transmit callback event */
}

Flexible Data

This example demonstrates sending an FD message with bitrate switching over external loopback. The CTX and CRX pins must be connected when using external loopback, though if a CAN tranciever is onboard a 120 Ohm resistor should be connected across CANH and CANL instead.

#define CAN_EXAMPLE_64_BYTES 64
void canfd_fd_loopback_example (void)
{
fsp_err_t err;
err = R_CANFD_Open(&g_canfd0_ctrl, &g_canfd0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
/* Switch to external loopback mode */
assert(FSP_SUCCESS == err);
/* Configure a frame to write 64 bytes with bitrate switching (BRS) enabled */
g_can_tx_frame.id = CAN_EXAMPLE_ID;
g_can_tx_frame.id_mode = CAN_ID_MODE_STANDARD;
g_can_tx_frame.type = CAN_FRAME_TYPE_DATA;
g_can_tx_frame.data_length_code = CAN_EXAMPLE_64_BYTES;
/* Write some data to the transmit frame */
for (uint32_t i = 0; i < CAN_DATA_BUFFER_LENGTH; i++)
{
g_can_tx_frame.data[i] = (uint8_t) i;
}
/* Send data on the bus */
err = R_CANFD_Write(&g_canfd0_ctrl, CANFD_TX_MB_0, &g_can_tx_frame);
assert(FSP_SUCCESS == err);
/* Wait for a transmit and/or receive callback event */
}

Internal Bus

In this example two CANFD channels are connected to the Internal Bus test mode. API error checking has been omitted for clarity.

Note
Internal Bus mode is only available on MCUs with more than one CANFD channel. In addition, use of Global Modes for any other purpose is not recommended without consulting the device User's Manual.

Data Structures

struct  canfd_afl_entry_t
 
struct  canfd_global_cfg_t
 
struct  canfd_extended_cfg_t
 

Enumerations

enum  canfd_status_t
 
enum  canfd_error_t
 
enum  canfd_tx_buffer_t
 
enum  canfd_tx_mb_t
 
enum  canfd_rx_buffer_t
 
enum  canfd_rx_mb_t
 
enum  canfd_rx_fifo_t
 
enum  canfd_minimum_dlc_t
 
enum  canfd_frame_options_t
 

Data Structure Documentation

◆ canfd_afl_entry_t

struct canfd_afl_entry_t

AFL Entry (based on R_CANFD_CFDGAFL_Type in renesas.h)

◆ canfd_global_cfg_t

struct canfd_global_cfg_t

CANFD Global Configuration

Data Fields
uint32_t global_interrupts Global control options (CFDGCTR register setting)
uint32_t global_config Global configuration options (CFDGCFG register setting)
uint32_t rx_fifo_config[8] RX FIFO configuration (CFDRFCCn register settings)
uint32_t rx_mb_config Number and size of RX Message Buffers (CFDRMNB register setting)
uint8_t global_err_ipl Global Error interrupt priority.
uint8_t rx_fifo_ipl RX FIFO interrupt priority.
uint32_t common_fifo_config[R_CANFD_NUM_COMMON_FIFOS] Common FIFO configurations.

◆ canfd_extended_cfg_t

struct canfd_extended_cfg_t

CANFD Extended Configuration

Data Fields
canfd_afl_entry_t const * p_afl AFL rules list.
uint64_t txmb_txi_enable Array of TX Message Buffer enable bits.
uint32_t error_interrupts Error interrupt enable bits.
can_bit_timing_cfg_t * p_data_timing FD Data Rate (when bitrate switching is used)
uint8_t delay_compensation FD Transceiver Delay Compensation (enable or disable)
canfd_global_cfg_t * p_global_cfg Global configuration (global error callback channel only)

Enumeration Type Documentation

◆ canfd_status_t

CANFD Status

Enumerator
CANFD_STATUS_RESET_MODE 

Channel in Reset mode.

CANFD_STATUS_HALT_MODE 

Channel in Halt mode.

CANFD_STATUS_SLEEP_MODE 

Channel in Sleep mode.

CANFD_STATUS_ERROR_PASSIVE 

Channel in error-passive state.

CANFD_STATUS_BUS_OFF 

Channel in bus-off state.

CANFD_STATUS_TRANSMITTING 

Channel is transmitting.

CANFD_STATUS_RECEIVING 

Channel is receiving.

CANFD_STATUS_READY 

Channel is ready for communication.

CANFD_STATUS_ESI 

At least one CAN-FD message was received with the ESI flag set.

◆ canfd_error_t

CANFD Error Code

Enumerator
CANFD_ERROR_CHANNEL_BUS 

Bus Error.

CANFD_ERROR_CHANNEL_WARNING 

Error Warning (TX/RX error count over 0x5F)

CANFD_ERROR_CHANNEL_PASSIVE 

Error Passive (TX/RX error count over 0x7F)

CANFD_ERROR_CHANNEL_BUS_OFF_ENTRY 

Bus-Off State Entry.

CANFD_ERROR_CHANNEL_BUS_OFF_RECOVERY 

Recovery from Bus-Off State.

CANFD_ERROR_CHANNEL_OVERLOAD 

Overload.

CANFD_ERROR_CHANNEL_BUS_LOCK 

Bus Locked.

CANFD_ERROR_CHANNEL_ARBITRATION_LOSS 

Arbitration Lost.

CANFD_ERROR_CHANNEL_STUFF 

Stuff Error.

CANFD_ERROR_CHANNEL_FORM 

Form Error.

CANFD_ERROR_CHANNEL_ACK 

ACK Error.

CANFD_ERROR_CHANNEL_CRC 

CRC Error.

CANFD_ERROR_CHANNEL_BIT_RECESSIVE 

Bit Error (recessive) Error.

CANFD_ERROR_CHANNEL_BIT_DOMINANT 

Bit Error (dominant) Error.

CANFD_ERROR_CHANNEL_ACK_DELIMITER 

ACK Delimiter Error.

CANFD_ERROR_GLOBAL_DLC 

DLC Error.

CANFD_ERROR_GLOBAL_MESSAGE_LOST 

Message Lost.

CANFD_ERROR_GLOBAL_PAYLOAD_OVERFLOW 

FD Payload Overflow.

CANFD_ERROR_GLOBAL_TXQ_OVERWRITE 

TX Queue Message Overwrite.

CANFD_ERROR_GLOBAL_TXQ_MESSAGE_LOST 

TX Queue Message Lost.

CANFD_ERROR_GLOBAL_CH0_SCAN_FAIL 

Channel 0 RX Scan Failure.

CANFD_ERROR_GLOBAL_CH1_SCAN_FAIL 

Channel 1 RX Scan Failure.

CANFD_ERROR_GLOBAL_CH0_ECC 

Channel 0 ECC Error.

CANFD_ERROR_GLOBAL_CH1_ECC 

Channel 1 ECC Error.

◆ canfd_tx_buffer_t

CANFD Transmit Buffer (MB + CFIFO)

◆ canfd_tx_mb_t

CANFD Transmit Message Buffer (TX MB)

◆ canfd_rx_buffer_t

CANFD Receive Buffer (MB + FIFO + CFIFO)

◆ canfd_rx_mb_t

CANFD Receive Message Buffer (RX MB)

◆ canfd_rx_fifo_t

CANFD Receive FIFO (RX FIFO)

◆ canfd_minimum_dlc_t

CANFD AFL Minimum DLC settings

◆ canfd_frame_options_t

CANFD Frame Options

Enumerator
CANFD_FRAME_OPTION_ERROR 

Error state set (ESI).

CANFD_FRAME_OPTION_BRS 

Bit Rate Switching (BRS) enabled.

CANFD_FRAME_OPTION_FD 

Flexible Data frame (FDF).

Function Documentation

◆ R_CANFD_Open()

fsp_err_t R_CANFD_Open ( can_ctrl_t *const  p_api_ctrl,
can_cfg_t const *const  p_cfg 
)

Open and configure the CANFD channel for operation.

Example:

/* Initialize the CAN module */
err = R_CANFD_Open(&g_canfd0_ctrl, &g_canfd0_cfg);
Return values
FSP_SUCCESSChannel opened successfully.
FSP_ERR_ALREADY_OPENDriver already open.
FSP_ERR_IN_USEChannel is already in use.
FSP_ERR_IP_CHANNEL_NOT_PRESENTChannel does not exist on this MCU.
FSP_ERR_ASSERTIONA required pointer was NULL.
FSP_ERR_CAN_INIT_FAILEDThe provided nominal or data bitrate is invalid.
FSP_ERR_CLOCK_INACTIVECANFD source clock is disabled (PLL or PLL2).

◆ R_CANFD_Close()

fsp_err_t R_CANFD_Close ( can_ctrl_t *const  p_api_ctrl)

Close the CANFD channel.

Return values
FSP_SUCCESSChannel closed successfully.
FSP_ERR_NOT_OPENControl block not open.
FSP_ERR_ASSERTIONNull pointer presented.

◆ R_CANFD_Write()

fsp_err_t R_CANFD_Write ( can_ctrl_t *const  p_api_ctrl,
uint32_t  buffer,
can_frame_t *const  p_frame 
)

Write data to the CANFD channel.

Example:

/* Send data on the bus */
err = R_CANFD_Write(&g_canfd0_ctrl, CANFD_TX_MB_0, &g_can_tx_frame);
assert(FSP_SUCCESS == err);
Return values
FSP_SUCCESSOperation succeeded.
FSP_ERR_NOT_OPENControl block not open.
FSP_ERR_CAN_TRANSMIT_NOT_READYTransmit in progress, cannot write data at this time.
FSP_ERR_INVALID_ARGUMENTData length or buffer number invalid.
FSP_ERR_INVALID_MODEAn FD option was set on a non-FD frame.
FSP_ERR_ASSERTIONOne or more pointer arguments is NULL.
FSP_ERR_UNSUPPORTEDFD is not supported on this MCU.

◆ R_CANFD_Read()

fsp_err_t R_CANFD_Read ( can_ctrl_t *const  p_api_ctrl,
uint32_t  buffer,
can_frame_t *const  p_frame 
)

Read data from a CANFD Message Buffer or FIFO.

Example: snippet r_canfd_example.c R_CANFD_Read

Return values
FSP_SUCCESSOperation succeeded.
FSP_ERR_NOT_OPENControl block not open.
FSP_ERR_INVALID_ARGUMENTBuffer number invalid.
FSP_ERR_ASSERTIONp_api_ctrl or p_frame is NULL.
FSP_ERR_BUFFER_EMPTYBuffer or FIFO is empty.

◆ R_CANFD_ModeTransition()

fsp_err_t R_CANFD_ModeTransition ( can_ctrl_t *const  p_api_ctrl,
can_operation_mode_t  operation_mode,
can_test_mode_t  test_mode 
)

Switch to a different channel, global or test mode.

Example:

/* Switch to external loopback mode */
assert(FSP_SUCCESS == err);
Return values
FSP_SUCCESSOperation succeeded.
FSP_ERR_NOT_OPENControl block not open.
FSP_ERR_ASSERTIONNull pointer presented
FSP_ERR_INVALID_MODECannot change to the requested mode from the current global mode.

◆ R_CANFD_InfoGet()

fsp_err_t R_CANFD_InfoGet ( can_ctrl_t *const  p_api_ctrl,
can_info_t *const  p_info 
)

Get CANFD state and status information for the channel.

Return values
FSP_SUCCESSOperation succeeded.
FSP_ERR_NOT_OPENControl block not open.
FSP_ERR_ASSERTIONNull pointer presented

◆ R_CANFD_CallbackSet()

fsp_err_t R_CANFD_CallbackSet ( can_ctrl_t *const  p_api_ctrl,
void(*)(can_callback_args_t *)  p_callback,
void const *const  p_context,
can_callback_args_t *const  p_callback_memory 
)

Updates the user callback with the option to provide memory for the callback argument structure. Implements can_api_t::callbackSet.

Return values
FSP_SUCCESSCallback updated successfully.
FSP_ERR_ASSERTIONA required pointer is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened.
FSP_ERR_NO_CALLBACK_MEMORYp_callback is non-secure and p_callback_memory is either secure or NULL.