STM32 Lab by RedCan

CAN FD on STM32 (FDCAN): bit timing, sample point, transceiver delay compensation and bus-off

5 min read

Illustration: can fd embedded electronics workbench

CAN FD carries up to 64 bytes per frame and speeds the data phase up to several Mbit/s. In practice, moving from classic CAN to FD exposes issues that did not matter at 500 kbit/s: an insufficiently accurate clock, different sample points across nodes, and transceiver delay. Here is how we configure the FDCAN peripheral in STM32 microcontrollers, based on ST’s AN5348.

1. The peripheral clock

All timing is derived from the FDCAN kernel clock (fdcan_ker_ck). The rules we follow:

  • a frequency that divides evenly into both bit rates – in practice 40 or 80 MHz (the values CiA also recommends for CAN FD networks);
  • a crystal-based source (HSE, or the PLL from HSE), not the internal RC oscillator – the HSI’s tolerance of around 1% over temperature is too much for CAN, and CAN FD is stricter than classic CAN;
  • the same prescaler in the arbitration and data phases – the time quantum is then identical, which simplifies synchronisation.

2. Bit segments and the sample point

A bit is divided into time quanta (tq): the synchronisation segment (always 1 tq), segment 1 (propagation + phase 1) and segment 2. The sample point is at the end of segment 1:

bit_rate     = fdcan_ker_ck / (Prescaler × (1 + TimeSeg1 + TimeSeg2))
sample_point = (1 + TimeSeg1) / (1 + TimeSeg1 + TimeSeg2)

In HAL, the TimeSeg1 and TimeSeg2 fields take the actual number of quanta – the library subtracts 1 when writing the registers.

Example: 500 kbit/s / 2 Mbit/s from an 80 MHz clock.

Arbitration phase: Prescaler = 1, 160 tq per bit → TimeSeg1 = 127, TimeSeg2 = 32. Rate 80 MHz / 160 = 500 kbit/s, sample point (1 + 127) / 160 = 80%.

Data phase: Prescaler = 1, 40 tq per bit → TimeSeg1 = 29, TimeSeg2 = 10. Rate 80 MHz / 40 = 2 Mbit/s, sample point (1 + 29) / 40 = 75%.

hfdcan1.Init.FrameFormat          = FDCAN_FRAME_FD_BRS;
hfdcan1.Init.NominalPrescaler     = 1;
hfdcan1.Init.NominalTimeSeg1      = 127;
hfdcan1.Init.NominalTimeSeg2      = 32;
hfdcan1.Init.NominalSyncJumpWidth = 32;
hfdcan1.Init.DataPrescaler        = 1;
hfdcan1.Init.DataTimeSeg1         = 29;
hfdcan1.Init.DataTimeSeg2         = 10;
hfdcan1.Init.DataSyncJumpWidth    = 10;

These values fit the FDCAN register ranges (e.g. on STM32G4 data-phase segment 1 up to 32 tq, segment 2 up to 16 tq). We set SJW equal to segment 2 – this gives the largest tolerance to clock differences.

The key rule: all nodes on the network should use the same sample point, especially in the data phase. Typical values are 75–87.5% in the arbitration phase and about 70–80% in the data phase; the actual values are set for the whole network (often by the system specification or CiA recommendations).

3. Transceiver delay compensation (TDC)

A CAN transmitter reads its own bits back from the bus to detect errors. The loop through the transceiver and the bus adds a delay in the order of 100–200 ns. At 500 kbit/s (a 2 µs bit) that is irrelevant, but at 2 Mbit/s a bit lasts 500 ns and the delay takes up a large part of it – the transmitter would compare the wrong bit. FDCAN solves this by measuring the delay and sampling at an additional, shifted point (the secondary sample point). We enable compensation for data-phase rates above about 1 Mbit/s:

HAL_FDCAN_ConfigTxDelayCompensation(&hfdcan1,
        hfdcan1.Init.DataPrescaler * hfdcan1.Init.DataTimeSeg1,  /* offset */
        0);                                                       /* filter */
HAL_FDCAN_EnableTxDelayCompensation(&hfdcan1);

An offset of DataPrescaler × DataTimeSeg1 puts the secondary sample point at roughly the same position in the bit as the normal one; it is the starting point used in ST’s examples, which we then verify on the real bus. The transceiver must be rated for CAN FD, with a specified loop delay and bit symmetry for the chosen rate.

4. Message RAM and filters

FDCAN keeps filters, receive FIFOs and transmit buffers in a separate message RAM. On STM32G0, G4 and H5 its layout is fixed: 28 standard filters, 8 extended filters, two receive FIFOs of 3 frames each and 3 transmit buffers. On STM32H7 the message RAM (10 KB) is shared by the FDCAN instances and its partitioning is set in Init – make sure the instances’ areas do not overlap. With heavy traffic and only 3-element FIFOs, hardware filtering and fast interrupt handling decide whether frames get lost.

5. Bus-off

After too many errors a node enters bus-off and disconnects from the bus. Unlike bxCAN with automatic recovery (ABOM), FDCAN sets the INIT bit and waits for software. Only clearing INIT starts the recovery sequence: the controller must observe 128 occurrences of 11 consecutive recessive bits.

void HAL_FDCAN_ErrorStatusCallback(FDCAN_HandleTypeDef *h, uint32_t its)
{
    if (its & FDCAN_IT_BUS_OFF) {
        /* log the event, optionally wait, then: */
        CLEAR_BIT(h->Instance->CCCR, FDCAN_CCCR_INIT);
    }
}

The FDCAN_IT_BUS_OFF interrupt must be enabled with HAL_FDCAN_ActivateNotification(). Without this handling, a single disturbance on the bus silences the device until reset.

6. Physical layer and testing

  • 120 Ω termination at both ends of the bus – with power off we measure about 60 Ω between CANH and CANL. We often use split termination (2 × 60 Ω with a capacitor to ground), which reduces emissions.
  • Linear topology and stubs as short as possible – in the CAN FD data phase, reflections from long stubs and star topologies quickly degrade the signal.
  • Classic CAN 2.0 nodes on the same bus flag FD frames as errors. A mixed network needs FD-tolerant controllers or separate segments.
  • First tests in FDCAN_MODE_INTERNAL_LOOPBACK (no transceiver), then EXTERNAL_LOOPBACK, and only then with other nodes.
  • Diagnostics: error counters (ECR register) and last-error codes for the arbitration and data phases (PSR.LEC, PSR.DLEC), plus an oscilloscope with CAN FD decoding to check bit shape in the data phase.

RedCan checklist: CAN FD on STM32

  • FDCAN clock of 40 or 80 MHz from a crystal, same prescaler in both phases.
  • Bit segments calculated for both phases, SJW = segment 2.
  • Identical sample points on every node in the network.
  • TDC enabled for data phases above about 1 Mbit/s, CAN FD-rated transceiver.
  • Hardware filters and FIFO handling sized for the traffic; on H7 – message RAM partitioning.
  • Bus-off handling: interrupt enabled and INIT cleared.
  • 2 × 120 Ω termination, short stubs, no non-FD-tolerant CAN 2.0 nodes.
  • Loopback tests first, then on the bus; error counters and LEC/DLEC checked.

Related reading: HSE and LSE crystal selection and STM32 board bring-up.

Further reading

  • AN5348 – Introduction to FDCAN peripherals for STM32 MCUs (STMicroelectronics) – the CAN FD protocol, the peripheral’s architecture, message RAM, operating modes and transceiver delay compensation.
  • The family reference manual – the FDCAN chapter (NBTP, DBTP, TDCR, CCCR, ECR, PSR registers).
  • CiA (CAN in Automation) recommendations on bit-timing configuration in CAN FD networks.

We design CAN, Modbus and BLE communication together with the electronics and firmware – see firmware and embedded software development.

Written by the RedCan engineering team – we design electronics, PCBs and firmware on STM32 for device manufacturers.

Building a device on STM32?

We can help with microcontroller selection, schematic and PCB, prototype bring-up, firmware and tests.

STM32 device design →
Discuss your STM32 project

STM32 is a registered trademark of STMicroelectronics International N.V. or its affiliates in the EU and/or elsewhere. STM32 Lab by RedCan is an independent series written by RedCan engineers.