Bit timing is the count of time Quanta(tq,a basic unit of bit time) required to carry a single bit(i.e tour of a bit a on CAN bus from writing to reading) on CAN Bus. reciprocal of Bit timing is known as Bit rate or Nominal bit rate.

Keeping the attention on bit synchronization a bit time is divided into 4 segments and each segment has some specific programmable number of time quanta(tq) –

  1. Synchronization Segment ( SYNC_SEG )
  2. Propagation Segment ( PROP_SEG )
  3. Phase Segment 1( PHASE_SEG1 )
  4. Phase Segment 2( PHASE_SEG2 )

But what is a time quanta? Essentially is a subdivision of a one-bit in CAN communication, for instance. let’s say we configure our bus to handle a speed of 100Kpbs which means that each bit will take 10us, now we are going to divide this 10us between the period of the signal that will feed the CAN peripheral in our controller, we found to use a frequency of 8MHz with a prescaler of 10 for illustration purpose:

  • tcan = 1/ (8MHz / 10) = 1.25us
  • 1Tq = 1.25us
  • Ntq = 10us / 1.25us = 8tq

Where SYNC_SEG will always have a value of 1 tq, and we need to select the values of PROP_SEG, PHASE_SEG1, and PHASE_SEG2 keeping in mind that the sum of the values must not exceed the total amount of quantas, this case is 10tq, to get the values we need to decide the sample point, which is basically the moment of the bit period when the data bit will be detected. this value is always in terms of percentage which is recommended to be from 60% to 90%. We can calculate PHASE_SEG1 using a sample point of 75%

Where:

  • PROP_SEG = 1 (this can be any value from 1 to PHASE_SEG1)
  • PHASE_SEG1 = ( Ntq * ( 75 / 100 ) ) - ( PROP_SEG + SYNC_SEG )
  • PHASE_SEG2 = Ntq - PHASE_SEG1

HAL STM32CubeG0 Library

In the case of the library, we first need to determine the frequency that feeds the FDCAN peripheral and the clock source supplying it. If we review the datasheet, we can find the following image (Figure 10, page 166), which shows that the FDCAN module can be powered from three clock sources.

By default, PCLK is used, which is essentially the frequency of the APB bus.

If we don't configure any clock source, the default frequency used will be the frequency of the microcontroller's internal oscillator without any prescaler, such as SYSCLK (HSI16), which produces a frequency of 16MHz. Therefore, this frequency is what will be fed to the FCDAN module.

The library utilizes the structure FDCAN_HandleTypeDef to configure our peripheral. Among its elements are ClockDivider and NominalPrescaler, which further allow reducing the frequency to obtain the value of time quanta:

  • fcan = fpclk / CANHandler.Init.ClockDivider / CANHandler.Init.NominalPrescaler
  • tq = 1 / fcan

With this value, we can calculate the number of time quanta required to send a single bit. We only need to determine the speed we want to operate on our bus (125, 250, 500 kbps, etc.).

  • Ntq = ( 1 / baudrate ) / tq

The next step is to determine the sample point we will use. A value of 75% is always very suitable, but if the values do not give you integers, you can vary this value without going beyond the range of 60% - 90%. The FDCAN_HandleTypeDef structure has two elements, namely NominalTimeSeg1 and NominalTimeSeg2. The following formula can be used to determine the value of NominalTimeSeg1:

  • CANHandler.Init.NominalTimeSeg1 = ( Ntq * ( SamplePoint / 100 ) - 1
  • CANHandler.Init.NominalTimeSeg2 = Ntq - CANHandler.Init.NominalTimeSeg1

By the way NominalTimeSeg1 = PROP_SEG + PHASE_SEG1 and NominalTimeSeg2 = PHASE_SEG2

NOTE: The value of NominalTimeSeg1 should be calculated with a value of Ntq greater than 1 and less than or equal to 16. In the case of NominalTimeSeg2, it should be calculated with a value of Ntq greater than 1 and less than or equal to 8.

There is one last parameter to configure, which is the NominalSyncJumpWidth. This parameter serves as a kind of compensation for the sample point. Typically, this value is set between 1 and NominalTimeSeg2 / 2, but it should never exceed the value of NominalTimeSeg2.

As exercises, calculate the values for a speed of 100Kbps and a sample point of 75% with a frequency of 16MHz, and then the same sample point but at 500Kbps with a frequency of 32MHz.