Home / Reference / Tools / STM32 CAN Bus Configuration Calculator
pcbway
STM32 Calculator

STM32 CAN Bus Configuration Calculator

This CAN Bus calculator will help you calculate the configuration values given the desired bit rate and system clock frequency.

Prescaler:

Time Segments:

Understanding STM32 CAN Bus and Configuration Calculations

The Controller Area Network (CAN) bus is a robust communication protocol widely used in automotive, industrial, and embedded systems. STM32 microcontrollers support CAN communication, providing developers with a versatile platform for implementing networked solutions. Configuring the CAN bus in STM32 involves calculating specific parameters that ensure proper communication between devices. This article explains the basics of the CAN bus and details the process of calculating the configuration values for STM32.


What is the CAN Bus?

The CAN bus is a two-wire communication protocol that enables microcontrollers and devices to communicate without a host computer. Key features include:

  • Real-time communication: Ensures deterministic message delivery.
  • Error detection: Built-in mechanisms ensure reliable data transmission.
  • Priority-based messaging: Messages with higher priority are transmitted first.

In an STM32 system, the CAN peripheral handles message transmission and reception, while configuration values define the timing and operation of the CAN bus.


CAN Bus Timing and Configuration Parameters

The CAN bus operates using a clock signal divided into time quanta (TQ), the smallest unit of time used in CAN bit timing. A single bit period consists of:

  1. Synchronization Segment (Sync_Seg): Fixed at 1 TQ, marking the start of the bit.
  2. Propagation Segment (Prop_Seg): Compensates for signal propagation delay.
  3. Phase Segment 1 (Phase_Seg1): Adjusts to synchronize the clock.
  4. Phase Segment 2 (Phase_Seg2): Ensures the bit ends correctly.

The total time quanta (TQ) for a bit period is:

The configuration parameters for STM32 CAN bus include:

  • Bit Timing Prescaler (BRP): Divides the input clock to produce the CAN clock.
  • Time Segment 1 (TS1): Sum of Prop_Seg and Phase_Seg1.
  • Time Segment 2 (TS2): Corresponds to Phase_Seg2.

Steps to Calculate STM32 CAN Configuration

1. Determine the Input Clock

The CAN peripheral uses the APB clock of the STM32 microcontroller. Identify this clock frequency, typically defined in the microcontroller’s configuration settings.

2. Define the Desired Bit Rate

The desired bit rate determines how fast the CAN bus transmits data, measured in bits per second (bps). Common values are 125 kbps, 250 kbps, and 1 Mbps.

3. Calculate the Bit Period

The bit period is the reciprocal of the bit rate:

4. Choose the Total Time Quanta (TQ_total)

Select an appropriate number of TQ for the bit period. CAN standards recommend values between 8 and 25 TQ.

5. Calculate the Baud Rate Prescaler (BRP)

The BRP divides the input clock to match the CAN clock. Calculate BRP using:

Round BRP to the nearest integer, ensuring it stays within the allowable range (1 to 1024 for STM32).

6. Distribute Time Quanta

Divide the remaining TQ among Prop_Seg, Phase_Seg1, and Phase_Seg2 while adhering to the following rules:

  • TS1 (Prop_Seg + Phase_Seg1) should be 75-87.5% of TQ_total.
  • TS2 (Phase_Seg2) should be 12.5-25% of TQ_total.

Example Calculation

Suppose:

  • Input Clock = 36 MHz
  • Desired Bit Rate = 500 kbps
  1. Calculate Bit Period:
  2. Choose TQ_total: Assume 16 TQ.
  3. Calculate BRP: Round to 4.
  4. Distribute TQ:
  • TS1 = 13 TQ (includes Prop_Seg and Phase_Seg1)
  • TS2 = 2 TQ
  • Sync_Seg = 1 TQ

Verifying Configuration

STM32CubeMX or similar tools can validate your configuration. Alternatively, calculate the actual bit rate:

Ensure the calculated value closely matches the desired bit rate.


Common Pitfalls

  1. Clock Mismatches: Ensure the input clock matches the actual APB clock frequency.
  2. Invalid Configurations: Keep TS1 and TS2 within recommended ranges.
  3. Error Tolerance: The calculated bit rate should be within 1% of the desired value.

Conclusion

Configuring the CAN bus on STM32 requires careful calculation of timing parameters to ensure reliable communication. By understanding the principles behind bit timing and using tools like STM32CubeMX, you can efficiently set up the CAN peripheral for your application. Whether you’re working on automotive networks or embedded systems, mastering CAN configuration is a valuable skill for any STM32 developer.

Check Also

STM32 Calculator

STM32 PWM Calculator

This calculator will help you compute the desired PWM frequency. Just put in the clock …

Index