HAL FDCAN How to Use

group FDCAN_How_To_Use

How to use this driver

  1. Declare a hal_fdcan_handle_t handle structure and initialize the FDCAN driver with an FDCAN instance.

  2. Initialize the FDCAN peripheral using the HAL_FDCAN_Init() function. The FDCAN clock is enabled inside HAL_FDCAN_Init() if USE_HAL_FDCAN_CLK_ENABLE_MODEL > HAL_CLK_ENABLE_NO.

  3. Configure the low-level hardware (GPIO, CLOCK, NVIC, etc.)

  • Enable the FDCAN clock if USE_HAL_FDCAN_CLK_ENABLE_MODEL = HAL_CLK_ENABLE_NO

  • FDCAN pins configuration:

    • Enable the clock for the FDCAN GPIOs

    • Configure the NVIC for interrupt processing

  1. Configure the peripheral using the HAL_FDCAN_SetConfig()

  1. Operation Modes

  1. Use the control functions to initiate Rx/Tx transfers over the FDCAN bus, either sending frames, or receiving frames, or checking and managing the whole transfer process and resources (status, buffers, events). Most of the control functions can be called in IDLE, ACTIVE, or POWER_DOWN states; however, some control functions are restricted to specific states. For example, HAL_FDCAN_Start() can be called in IDLE but not in ACTIVE or POWER_DOWN. The control functions include the following set of functions:

Call HAL_FDCAN_Start() to start the FDCAN module. At this step, the node is active on the bus:

  1. Callbacks definition in interrupt mode:

    When the compilation define USE_HAL_FDCAN_REGISTER_CALLBACKS is set to 1U, configure the driver callbacks dynamically using the registration functions below:

If one needs to unregister a callback, register the default callback via the registration function.

By default, after the HAL_FDCAN_Init() and when the state is HAL_FDCAN_STATE_INIT, all callbacks are set to the corresponding default weak functions.

  • Callbacks can be registered in HAL_FDCAN_STATE_INIT or HAL_FDCAN_STATE_IDLE states only.

  • When HAL_FDCAN_ErrorCallback() (or its registered callback) is called, check if the error code contains HAL_FDCAN_ERROR_BUS_FAULT_OFF. If a bus-off condition is detected, initiate recovery by calling HAL_FDCAN_Recover().

  • Restricted operation mode handling:

    • The hardware in some cases automatically sets the ASM bit (restricted operation). Calling HAL_FDCAN_GetMode() can return HAL_FDCAN_MODE_INVALID because the CCCR bits combination does not match a standard user-configurable mode.

    • Scenarios forcing restricted operation (ASM bit set):

      • Message RAM access failure:

        • A RAM access fault (e.g., timing violation or HW fault) sets ASM and raises HAL_FDCAN_ERROR_RAM_ACCESS_FAILURE.

        • Required actions:

    • Normal communication is suspended while ASM remains set; clear it after resolving the root cause.

  • If USE_HAL_FDCAN_REGISTER_CALLBACKS is 0U (or undefined) callback registration is not available and the default weak callbacks listed above are used.

  1. Acquire/Release the FDCAN bus

  • When the compilation flag USE_HAL_MUTEX is set to 1, it allows the user to acquire/reserve the whole FDCAN bus for executing process. The HAL_FDCAN_Acquire and HAL_FDCAN_Release are based on the HAL OS abstraction layer (stm32_hal_os.c/.h osal):

  • When the compilation flag USE_HAL_MUTEX is set to 0 or not defined, HAL_FDCAN_AcquireBus/HAL_FDCAN_ReleaseBus are not available.