cancel
Showing results for 
Search instead for 
Did you mean: 

CAN (bxCAN) bit time configuration on STM32 MCUs

mƎALLEm
ST Employee

Introduction

Timing aspects are crucial in a CAN bus system design. The designer of the CAN bus system must study it carefully to prevent any communication issues on the CAN network.
This article does not cover the CAN protocol description and its frame (refer to this article), but it focuses and introduces timing definitions in the CAN communication. Additionally, the article explains how to configure the CAN bitrate and how to select the CAN prescaler, BS1 and BS2 parameters for STM32 in optimum settings with examples using STM32CubeMX.

1. The CAN bit and its generation on STM32

A CAN frame is constituted by a bit sequence. Each bit has a value of “1” or “0”. In CAN jargon, “1” and “0” are respectively called recessive bit and dominant bit. In the next sections we will describe in detail the CAN bit time, the time quanta, and the sample point concepts and what purpose they are used for.

The following figure shows the representation of the CAN bit and its timing described in the literature as well as in the STM32 documentation.

Figure 1. The CAN bit timeFigure 1. The CAN bit time

 

Figure 2 gives a view on how the CAN bit time is generated.

Figure 2. CAN bit time generation on STM32Figure 2. CAN bit time generation on STM32

On STM32, the register responsible for the CAN timing is the CAN_BTR register. Refer to the reference manual of the product/section “Controller area network (bxCAN)”.

In the next sections we describe in detail:

  • The CAN bit time
  • The time quanta/quantum
  • The CAN bit time segments
  • The sample point concepts, and their purposes 

2. Time quanta and time quantum

The time quantum (tq) is simply the basic unit of the bit time, and it is measured in seconds (see Figure 1).

The time quanta (TQ) is an integer value that represents the number of time quantum that constitutes a bit time.

In Figure 1, the time quanta TQ = 11.

On STM32, the tq is generated from the APB clock divided by the CAN prescaler (see Figure 2) that needs to be set in the CAN_BTR.BRP field:  .Figure 3. CAN prescaler field in CAN_BTR registerFigure 3. CAN prescaler field in CAN_BTR register

The time quantum tq is given by the following equation (equation 1):

mALLEm_0-1744729390319.png

Based on the HAL, the definitions of the equation are (equation 2):

mALLEm_1-1744729432525.png

 

In the case of STM32CubeMX, if we take the STM32F439 MCU as an example, and if the RCC clock configuration is as follows:

Figure 4. RCC clock configuration exampleFigure 4. RCC clock configuration example

Additionally, if the prescaler is set to 3 in the CAN bit time parameters:

Figure 5. Time quantum shown by STM32CubeMXFigure 5. Time quantum shown by STM32CubeMX

The value of time quantum calculated by STM32CubeMX is (equation 3):

mALLEm_4-1744729743886.png

Or (equation 4):

mALLEm_0-1744733149457.png

So:

mALLEm_1-1744733307340.png

Which is the value shown in STM32CubeMX (Figure 5).

As the CAN instances are located on the APB1 domain, and the CAN clock is generated from the APB1 clock. From the STM32F4xx MCU reference manual (RM0090) we can see:

Figure 6. The CAN instances are located on APB1 domainFigure 6. The CAN instances are located on APB1 domain

That is why APB1 prescaler is used in the tq calculation.

3. The CAN bit time

The CAN bit time is the period of time for which the bit is actually present on the bus. It changes slightly due to the oscillator drift/jitter of the CAN node caused by the temperature or the voltage fluctuations and the aging of the components. In the CAN world, the wording “CAN node” refers to a CAN station connected to a CAN network.

The CAN nominal bit time is the theorical/ideal value that the designers of the CAN bus system wish to assign to their network. Each CAN node connected to the same CAN bus must be configured to have a such ideal and the same value.  In the CAN literature, the CAN bit time is fictively divided in four parts: synchronization (sync), propagation segment, phase 1, and phase 2.

The CAN bit time is defined as tq x TQ.

Example, in Figure 1, the time quanta (TQ) = 11. If the tq = 0,0909 µs,

à CAN bit time = 0,0909 x 11 = µs.

3.1 The synchronization slot

The synchronization segment is the first segment of the CAN bit time, and it is used to synchronize the different nodes connected on the bus. The first border (rising or falling edge) of the CAN bit should appear within this timing window. The duration of this segment is always fixed at 1xtq (see later for the tq definition).

tsync = 1 x tq

The synchronization segment should not be confused with the synchronization jump width (SJW), which is another timing parameter that is not covered by this article.

3.2 The propagation segment

As you may now the signal takes time to propagate in a medium. An electrical signal on an electrical wire takes time to propagate from point A to point B. The longer the wire, the longer the signal takes time to propagate from end to end.

The propagation segment is intended to compensate all the delays induced by the signal propagation on the medium (electrical or other). This is the type of topology adopted by the CAN system designer to develop the CAN network.

3.3 Phase segment 1 and 2

These two segment phases are used to compensate for changes of the edge position of the signals due to the frequency of the various nodes of the network. The voltage thresholds level at the input of the comparator inside the CAN transceivers etc.

In the STM32 documentation, the propagation segment and phase segment 1 are merged to form the bit segment 1 (BS1). See Figure 1.

So, the bit time segment 1 is defined as: tBS1 = tprop_seg + tph_seg1

Where tprop_seg is the duration of the propagation segment and tph_seg1 is the duration of the phase segment 1.

For the phase segment 2, its duration is defined as tBS2 or tph_seg2 and it defines, with tBS1, the bit sample point position (see section 3).

On STM32, the possible values of time quanta (TQ) for BS1 can be set between 1 and 16. For BS2, the TQ can be set between 1 and 8 in the CAN_BTR register. Note that in this TS1 and TS2 register fields, 0 corresponds to 1TQ.

Figure 7. Time segment 1 and Time segment 2 fields in CAN_BTR registerFigure 7. Time segment 1 and Time segment 2 fields in CAN_BTR register

So, the duration of tBS1 is between 1 x tq and 16 x tq and the duration of tBS2 is between 1 x tq and 8 x tq.

On the STM32, the bit time is defined as follows:

mALLEm_0-1744730198878.png

Note, 1 is added to CAN_BTR.TS1 and to CAN_BTR.TS2, 0 is actually corresponding to the value 1*TQ in these fields.

Since the synchronization phase duration is fixed to 1*TQ, the CAN nominal bit time will be (equation 5):

mALLEm_1-1744730640743.png

4. The CAN nominal bitrate

The nominal bitrate is simply defined as the inverse of the bit time so (equation 6):

mALLEm_2-1744730775504.png

And the maximum CAN bitrate of CAN2.0A/2.0B allowed is 1 Mbit/s.

Example, in Figure 1, the time quanta = 11. If the tq = 0,0909 µs, so CAN bit time = 0,0909 x 11 = 1 µs.

Therefore, CAN bitrate = 1 / 1.10-6 = 1 Mbit/s

On STM32, the CAN bitrate is defined as the following:

mALLEm_3-1744730827535.png

Using the register value and based on the equation 1, the CAN bitrate (equation 7):

mALLEm_4-1744730925147.png

Using the HAL definitions, the equation is (equation 8):

mALLEm_5-1744730959140.png

The maximum nominal bitrate that can be used for the CAN bus depends on the bus length and to the topology of the bus. The higher CAN bit rates shorten permissible line lengths. See the table 1.

CAN bitrate

Maximum bus length (m)

1 Mbit/s

35

800 kbit/s

50

500 kbit/s

100

250 kbit/s

250

125 kbit/s

500

50 kbit/s

1000

20 kbit/s

2500

10 kbit/s

5000

Table 1. Maximum bitrates versus the bus length on electrical medium

5. The sample point

The sample point is the instant where the bit is sampled by the CAN peripheral. It defines the border between phase segment 1 and phase segment 2 in the CAN bit (see Figure 1). The choice of the sample point position is essential for the correct operation and for the success of the CAN communication on the bus.

We need to sample the bit as late as possible of the bit time to give the maximum confidence of the bit level reading. This is due to physical phenomena such as the signal deformation of the bit and the delays caused by the signal propagation on the medium. Additionally, we must not wait for the very last moment to read the bit for two reasons:

  • Due to the overall timing tolerances in the establishment of the bit time
  • To take care about the time required in the calculating the value of the bit after sampling

Table 2 shows the recommended sample point location based on the CAN bitrate used.

CAN bitrate

Valid range for location of the sample point (%)

Recommended location of the sample point (%)

1 Mbit/s

75 to 90

87.5

800 kbit/s

75 to 90

87.5

500 kbit/s

85 to 90

87.5

250 kbit/s

85 to 90

87.5

125 kbit/s

85 to 90

87.5

50 kbit/s

85 to 90

87.5

20 kbit/s

85 to 90

87.5

10 kbit/s

85 to 90

87.5

Table 2. The sample point position versus the CAN bitrate

The simple point position in % is simply the proportion of time segment on the left of the bit. It is delimited by the sample point versus the bit time, and it is defined as (equation 9):

mALLEm_6-1744731317916.png

On STM32 (equation 9):

mALLEm_7-1744731355781.png

Or with CAN register based calculation:

mALLEm_8-1744731407683.png

Finally, (equation 10):

mALLEm_9-1744731455641.png

Or with HAL parameters (equation 11):

mALLEm_10-1744731483595.png

Note that the simple point has more sense with CAN in Normal mode with a complete bus than with CAN in Loopback mode. In Loopback mode, the constraints related to the propagation delays are almost unavailable since the signal is looped back internally in the peripheral and the length of the signal trajectory is very short. Moreover, the signal is not distorted as in Normal mode due to the transceivers, the voltage levels of each node etc. The sample point is not something to be considered in Loopback mode.

6. Setting the CAN bitrate on STM32 exemplified using STM32CubeMX

In this section, we provide an example of how to configure the CAN bit timing parameters for a given bitrate on an STM32F4 MCU. The example can be tailored to any STM32 that has bxCAN peripheral, with the respect of its clock specificities.

Let us take the STM32F439 as an example. The NUCLEO-F439 board is the target, and we use CAN1 that communicates at 500 kbit/s.

Suppose that the CAN Normal mode will be used. In that case, it is important to call that the usage of an external crystal or a crystal oscillator is mandatory or any other external precise clock. The internal oscillator such as HSI is not recommended for the CAN communication in Normal mode,  but it's acceptable in Loopback mode (read this article). In our case, we use the STLINK-MCO output as a source clock of the MCU. The STLINK-MCO is generated based on an 8MHz crystal. Please also read this article.

In STM32CubeMX, we start the project from the MCU part number STM32F439ZI (See Figure 8).

Figure 8. STM32F439ZI MCU selection in STM32CubeMXFigure 8. STM32F439ZI MCU selection in STM32CubeMX

As the STLINK-MCO clock source is used, we need to set the HSE in Bypass mode.

Figure 9. Setting HSE in Bypass mode in STM32CubeMXFigure 9. Setting HSE in Bypass mode in STM32CubeMX

Now, make sure that the input clock is set at 8 MHz. Select HSE as a PLL source and the clock source of the system clock is generated from the PLL as shown in Figure 10.

Figure 10. Configuring the system clock source in STM32CubeMXFigure 10. Configuring the system clock source in STM32CubeMX

If STM32CubeMX shows errors, select them to fix them automatically by the tool according to the system clock frequency you need to use.

In our case 180 MHz is used as a system clock frequency of the MCU. The APB1 from which the CAN peripheral is clocked, is set to 45 MHz which is its maximum value as indicated in the STM32CubeMX (see Figure 11).

Figure 11. System clock and APB1 clock valuesFigure 11. System clock and APB1 clock values

Now, we are going to enable CAN1 by activating it in CAN menu as shown in Figure 12.

Figure 12. Activating CAN1 peripheral in STM32CubeMXFigure 12. Activating CAN1 peripheral in STM32CubeMX

As shown in the Figure 13, the default CAN timing settings are not suitable for our case and need to be updated. The bitrate is set to 937500 bit/s while we need to set it to 500 kbit/s.

Figure 13. Default CAN timings in STM32CubeMXFigure 13. Default CAN timings in STM32CubeMX

At this stage, we use this CAN bit timing web tool that helps us selecting the recommended values. These values are for the CAN prescaler, Time Quanta in Bit segment 1 (BS1), and the Time Quanta in Bit segment 2 (BS2) based on our needs: CAN bitrate = 500 kbit/s with a simple point set to 87.5% as provided in the table 2.

In the web tool, we start by selecting the version of the CAN to be used. We select “STMicroelectronics bxCAN” as shown in Figure 14.

Figure 14. Selecting the correct CAN versionFigure 14. Selecting the correct CAN version

There is no need to select the CAN clock frequency in MHz. In our case, the CAN clock is the same as APB1 (see Figure 2). As the APB1 clock = 45 MHz (see Figure 11), we put 45 in the “Clock Rate field” as shown in Figure 15.

Figure 15. Putting the CAN clock frequency in MHzFigure 15. Putting the CAN clock frequency in MHz

The default value of the sample point is already set to 87.5% as shown in the Figure 16. We keep that value.

Figure 16. Sample point is already set to 87.5%Figure 16. Sample point is already set to 87.5%

As the CAN bitrate to be used is 500 kbit/s, we need to put 500 as shown in the Figure 17.

Figure 17. CAN bitrate at 500 kb/sFigure 17. CAN bitrate at 500 kb/s

For those who needs to change the SJW value, you can do it over “SJW” field. In our case, we keep the default value = 1.

Figure 18 shows the full picture of the settings in the web tool.

Figure 18. A full picture of the settingsFigure 18. A full picture of the settings

Now, we need to click the [Request Table] button (Figure 19).

Figure 19. “Request Table” buttonFigure 19. “Request Table” button

The tool generates a table where it provides many settings with these same CAN bitrates. There are different sample points as it did not find a suitable CAN parameters matching the 87.5% as a sample point. See figure 20.

Figure 20. Generated CAN timing tableFigure 20. Generated CAN timing table

In STM32CubeMX, we set these recommended values in the “Bit timings Parameters” menu as shown in Figure 21.

Figure 21. Setting the CAN timing parameters in STM32CubeMXFigure 21. Setting the CAN timing parameters in STM32CubeMX

To avoid a warning message on bitrates in STM32CubeMX, it is recommended to start by setting BS1 and BS2 then finish by setting the CAN prescaler. See Figure 20.

As shown in Figure 21, the wanted CAN bitrate is set to 500 kbit/s. At this level, the CAN timings is all set.

Important note: if you are not using STM32CubeMX, avoid the usage of low values of Time Quanta in segment 1 (BS1) and in segment 2 even if they do match the targeted bitrate. The recommended way is to decrease as CAN prescaler as much as possible. Moreover, to increase the BS1 and BS2 as much as possible with the respective of a good sample point position (see table 2).

Conclusion

This article provided a theoretical description of the CAN bit time as well as detailed bit segment timing concepts. The article also described how the CAN bit time is generated on STM32 MCUs. An example guide is provided on how to set the CAN bit timing parameters, by selecting optimum values in STM32CubeMX for a normal CAN operation.

References

  • Book: “CAN System Engineering, From Theory to Practical Applications”, second edition, Wolfhard Lawrenz, ISBN 978-1-4471-5612-3
  • Book: “Multiplexed Networks for Embedded Systems, CAN, LIN, FlexRay, Safe-by-Wire...”, Dominique Paret, ISBN 978-0-470-03416-3 (HB)

Related links

Version history
Last update:
‎2025-04-28 6:56 AM
Updated by: