Hello! Welcome back to our course on professional embedded systems engineering.
In our last session, we took the essential first step of physically connecting the TJA1050 CAN transceiver to your STM32 board and activating the CAN peripheral in STM32CubeIDE. While the hardware is now ready, the software configuration is incomplete. All nodes on a CAN bus must agree on the exact duration of a single bit to communicate successfully. This is known as the bitrate.
Today, we will tackle this critical configuration step. Our learning outcome is to calculate and configure the CAN bit timing registers for a specific bus speed. We will delve into the theory of how a CAN bit is timed, learn the formulas that govern the bitrate, and then use professional tools to determine the optimal settings for our STM32.
1. The Anatomy of a CAN Bit
To control the bitrate, we don't just set a single frequency. Instead, we define the duration of a single bit by breaking it down into smaller, programmable segments. The fundamental unit of time in CAN timing is the time quantum (plural: time quanta), abbreviated as tq.
A single nominal bit time is composed of several such quanta, organized into three main segments for the STM32's bxCAN peripheral:
- Synchronization Segment (SYNC_SEG): This is a fixed segment of 1 tq. It's used to align the clocks of all nodes on the bus whenever a message starts (i.e., on a falling edge from recessive to dominant).
- Bit Segment 1 (BS1): This segment compensates for signal propagation delays across the bus wiring and within the transceivers. Its length is programmable.
- Bit Segment 2 (BS2): This segment compensates for phase errors and oscillator drift between nodes. Its length is also programmable.
The Sample Point is the specific instant in time where the CAN controller reads the voltage on the bus to determine if the bit is a '0' (dominant) or a '1' (recessive). It is located at the end of Bit Segment 1.
The image below, taken from an STM32 reference manual, beautifully illustrates this structure and the key formulas we'll be using.

To dive deeper into these concepts, the official STMicroelectronics documentation provides an excellent explanation.
CAN (bxCAN) bit time configuration on STM32 MCUs
Please read the following sections from the ST community article on CAN bit time configuration. This will solidify your understanding of the bit segments and the concept of a time quantum.
Read sections 1. The CAN bit and its generation on STM32, 2. Time quanta and time quantum, and 3. The CAN bit time. Focus on how the theoretical CAN bit structure (with Propagation and Phase segments) maps to the STM32's implementation (BS1 and BS2), and understand the equation for calculating the duration of a single time quantum (tq).
2. The Bitrate Calculation
Now that we understand the components, let's assemble the calculation. The final bitrate depends on three factors:
- CAN Peripheral Clock Frequency (): This is the input clock to the CAN peripheral, which on the STM32F103 is derived from the APB1 bus clock.
- Baud Rate Prescaler (BRP): An integer value (from 1 to 1024) that divides the peripheral clock to create the time quantum.
- Time Segment Registers (TS1 and TS2): These registers hold the number of time quanta for Bit Segment 1 and Bit Segment 2.
The relationships are as follows:
- Time Quantum Duration:
- Total Quanta per Bit:
(Note: The values written to theTS1andTS2fields in theCAN_BTRregister are one less than the number of quanta they represent, hence the+1in the formula). - Nominal Bit Time:
- Baud Rate:
One more crucial parameter is the Sample Point. It's calculated as a percentage of the total bit time:
For robust communication, especially at higher speeds, the CAN standard recommends a sample point around 87.5%. This allows maximum time for the signal to propagate and stabilize before it's read.
For a clear walkthrough of these formulas and the sample point's role, please review the next sections of the ST article.
CAN (bxCAN) bit time configuration on STM32 MCUs
These sections explain the final calculation for bitrate and the importance of the sample point.
Now, read sections 4. The CAN nominal bitrate and 5. The sample point. Pay attention to the table of recommended sample point locations and the final formulas for calculating the bitrate and sample point percentage.
3. Practical Configuration for 500 kbit/s
As you can see, for a given clock and desired bitrate, there can be many valid combinations of BRP, TS1, and TS2. Finding the optimal set that also yields a good sample point can be tedious. Professionals typically use specialized calculators for this task.
Let's configure our project for a common automotive bitrate: 500 kbit/s.
Step 1: Find Your CAN Clock Frequency
First, we need to know the clock frequency being fed to the CAN peripheral.
- Open your project in STM32CubeIDE.
- Open the
.iocfile to view the device configuration tool. - Navigate to the "Clock Configuration" tab.
- Look for the box labeled "APB1 peripheral clocks (MHz)". This is your . For a typical STM32F103C8 running at its maximum speed of 72MHz, the APB1 clock is usually configured to 36 MHz. Let's proceed assuming this value.
Step 2: Use an Online Calculator
We will use the same online calculator recommended in the ST article and the video below.
Controller Area Network(CAN) programming Tutorial 14: Bit timings and online calculator
This video segment quickly explains the different bit timing parameters and how an online calculator simplifies finding the correct values.
Watch from 01:29 to 04:16 to see the CAN_InitTypeDef structure members, then from 05:12 to 08:17 for an explanation of bit segments and time quanta. Finally, watch from 09:49 to 12:49 to see a demonstration of the online calculator.
Now, let's use the calculator ourselves. The following guide is based on the excellent step-by-step example in the ST article.
CAN (bxCAN) bit time configuration on STM32 MCUs
This is our primary hands-on guide. Follow it precisely to find the correct parameters and enter them into STM32CubeIDE.
Follow the steps in section 6. Setting the CAN bitrate on STM32 exemplified using STM32CubeMX. Go to the CAN bit timing web tool: http://www.bittiming.can-wiki.info/ Select STMicroelectronics bxCAN. In the Clock Rate field, enter your APB1 clock frequency (e.g., 36 MHz). Keep the Sample-Point at the recommended 87.5%. In the Bit Rate boxes, find the row for 500 kbit/s and click the [Request Table] button next to it. The tool will generate one or more possible solutions. A good choice would be one with a reasonable number of total time quanta (e.g., 8-16 TQ). For 36MHz and 500kbps, a common solution is: BRP (Prescaler) = 4 TS1 = 15 TS2 = 2 (Note: Your calculator might yield slightly different but equally valid results, like BRP=4, TS1=15, TS2=2. The important thing is that the final bitrate is correct and the sample point is near 87.5%). Finally, enter these values into STM32CubeIDE: Go to Connectivity -> CAN -> Parameter Settings -> Bit Timings Parameters and input the values for Prescaler, Time Quanta in Bit Segment 1, and Time Quanta in Bit Segment 2. Then, regenerate your code.
After entering the parameters, STM32CubeIDE should show the "Nominal Baud Rate" as 500000 bit/s, confirming your settings are correct.
Conclusion
Excellent work. You have now mastered a fundamental and often confusing aspect of CAN configuration. This meticulous process of timing configuration is what ensures every node on the bus, from a simple sensor to the engine control unit, can communicate reliably.
Key Takeaways:
- CAN bitrate is not set directly; it's a result of the peripheral clock, Baud Rate Prescaler (BRP), and the number of time quanta in Bit Segments 1 & 2 (TS1, TS2).
- The Sample Point is a critical parameter for reliable communication, with a location of ~87.5% being a common professional standard.
- While manual calculation is possible, using an online bit timing calculator is an efficient and reliable method to find optimal timing parameters.
- You have now configured your STM32's CAN peripheral for a 500 kbit/s bus speed, a standard in the automotive world.
With the physical layer connected and the timing correctly configured, our CAN node is finally ready to communicate. In our next lesson, we will write the firmware to implement a function to transmit a standard CAN message onto the bus.