can_fd_init()

can_fd_init()

Description

Initializes a CAN interface for CAN FD operation and installs the receive buffer that frames are queued into before an rx event fires. Must be called once, before any other can_* function is used on the same interface.

Syntax

can_fd_init(can_interface, can_ctrl_mode, can_data_bit_rate, can_nominal_bit_rate, can_rx_buffer);

Parameters

Parameter

Description

can_interface

CAN interface to initialize – CAN1 or CAN2.

can_ctrl_mode

CAN_CTRL_MODE_NORMAL - Standard operating mode: the controller participates fully in bus arbitration and acknowledges frames.
CAN_CTRL_MODE_ONE_SHOT - Disables automatic retransmission: a frame that loses arbitration or is not acknowledged is not resent.
CAN_CTRL_MODE_FD_BRS - Enables CAN FD with Bit Rate Switching, so the data phase of FD frames can run at the separate data_bit_rate.

These options can be passed as a bitwise OR of one or more flags, thus controlling retransmission behavior and whether FD/BRS is enabled.

can_data_bit_rate

FD data-phase bit rate, in kbit/s (e.g. 2000 = 2 Mbit/s). Only meaningful when CAN_CTRL_MODE_FD_BRS is set.

can_nominal_bit_rate

Nominal (arbitration phase) bit rate, in bit/s (e.g. 500000 = 500 kbit/s).

can_rx_buffer

Array reserved by the script to hold incoming raw CAN frame data; sized to the maximum amount of buffering required (can_rx_buffer{4096} in the example).

Returns

Return value

Return explanation

SCRIPT_OPERATION_SUCCESS

The operation completed successfully.

SCRIPT_OPERATION_FAILED

The operation could not be completed (invalid state, hardware error, etc.).

SCRIPT_NO_ACCESS

CAN Bus already initialized.

SCRIPT_PARAM_INVALID

One or more passed parameters are invalid.

Function call example

#include <io> #include <read> #include <float> #include <string> #include <core> #include <write> #include <define> #include <socket> #include <can> #define CAN_BUS CAN1 new can_rx_buffer{4096}; main() { can_fd_init(CAN_BUS, CAN_CTRL_MODE_NORMAL | CAN_CTRL_MODE_ONE_SHOT | CAN_CTRL_MODE_FD_BRS, 2000, 500000, can_rx_buffer); for(;;) { Delay(1000); } }

Notes and Warnings

  • data_bit_rate and nominal_bit_rate use different units (kbit/s vs. bit/s). This is a common source of mistakes – the standalone can_fd_data_bit_rate_set and can_nominal_bit_rate_set functions below both expect bit/s, not kbit/s.

  • CAN_CTRL_MODE_FD_BRS must be included in ctrl_mode for data_bit_rate to take effect; otherwise the interface stays on classic CAN timing.