CAN bus baud rate setting - take STM32 as an example

CAN bus baud rate setting - take STM32F103 as an example

 

Baud rate setting

The first is the meaning of several nouns. The composition of one bit in CAN is as follows. Note that the location of the sampling point is in the middle of PBS1 and PBS2. Based on this bit timing, the baud rate can be calculated .

Minimum time unit (Tq, Time Quantum)

Synchronization segment (SS, Synchronization Segment) 1Tq

Propagation Time Segment (PTS, Propagation Time Segment) 1-8Tq

Phase Buffer Segment 1 (PBS1, Phase Buffer Segment1) 1-8Tq

Phase Buffer Segment 2 (PBS2, Phase Buffer Segment2) 2-8Tq

Resynchronization compensation width (SJW, reSynchronization Jump Width) 1-4Tq

Baud Rate Prescaler (BRP, Baud Rate Prescaler)

Explanation of the CAN baud rate in the Chinese reference manual

STM32 merges the propagation time segment and the phase buffer segment 1 to form a new time segment 1.

The CAN bit timing register (CAN_BTR) is used to set parameters such as TS1, TS2, BRP, SJW, etc., and directly determines the baud rate of CAN.

SJW[1:0] resynchronization compensation width;

ts1[3:0] time period 1;

ts2[2:0] time period 2;

BRP[9:0] baud rate divider;

Yeah, there is no setting of SS synchronization segment, because SS is always equal to 1 .

In addition, we also need to set the clock frequency of APB1, STM32F1 is usually 36MHz (external 8M crystal oscillator).

CAN baud rate calculation formula ( register )

The specific function of the resynchronization compensation width (SJW) is to increase or reduce the allowable deviation of the CAN baud rate, that is to say, its size has no great relationship with the value of the baud rate, and it belongs to the auxiliary category, which can be understood as wave Baud rate precision adjustment. It should be noted here that this value should be designed as large as possible to meet the requirement of greater than 3% in the bit width tolerance test.

The formula is trjw=tq*(sjw+1), which means that for resynchronization, this parameter defines the upper limit of how many time units CAN hardware can extend or shorten in each bit. When actually writing a program, this sjw is usually defined as 0. A lot of information about this resynchronization compensation width is very vague. Here I will give you an example to help you understand. The library function is used here first. The first picture below is the baud rate result detected by the USBCAN analyzer when CAN_SJW_4tq is used. There are 5 baud rates that can realize CAN communication; the second picture below is when CAN_SJW_1tq is used. As a result of the baud rate detected by the USBCAN analyzer, only 3 baud rates can realize CAN communication. That is, the fault tolerance performance becomes smaller. Whether this value should be larger or smaller will be studied in the future.

In the case of CAN_SJW_4tq, the effective baud rate segment is relatively large

In the case of CAN_SJW_1tq, the effective baud rate segment is relatively small

CAN_BTR register

Let's take an example, if SJW is 0, TS1 is 8, TS2 is 7, and BRP is 3, then the baud rate should be 36M/[(1+9+8)*4]=500kbps.

Sampling rate setting

Besides baud rate, we should also consider a formula called sample rate . This formula is as follows. In fact, we can see that the sampling rate represents the relative position of the sampling point in the entire bit. For the above example, the sampling rate is (1+8+1)/(1+8+1+7+1)=55.6%, which is low.

When the baud rate is greater than 800kbps, the recommended sampling rate is 75%; when the baud rate is 500k-800kbps, the recommended sampling rate is about 80%; when the baud rate is not greater than 500kbps, the recommended sampling rate is 87.5%. Sampling points that deviate too much from this standard can cause consistency problems.

The value that needs to be set in the library function

Just now we said that the baud rate of CAN in STM32 microcontroller is mainly determined by 4 parameters. They are the resynchronization jump time unit (tsjw), the time unit of time segment 1 (tbs1), the time unit of time segment 2 (tbs2), and the baud rate divider (brp). Corresponding to the library functions of the STM32 microcontroller , they correspond to four parameters such as CAN_SJW, CAN_BS1 (ie tbs1+1), CAN_BS2 (ie tbs2+1), and CAN_Prescaler. Only need to complete the setting of these 4 parameters in the library function. For example, set CAN_SJW=1tq, CAN_BS1=6tq, CAN_BS2=1tq, CANPrescaler=9, then the baud rate=36M/[(1+6+1)*9]=500Kbps.

Sampling rate = (1+6)/(1+6+1)=87.5%. It should be noted that CAN_SJW does not participate in the calculation of any formula . The position of the baud rate formula is the SS segment, so it is 1.

Here I recommend a very good STM32-CAN baud rate calculation software can_config.exe, if necessary, please leave a message. But in this software, the statement that CAN_SJW participates in the baud rate calculation mentioned in the formula is wrong and needs to be corrected.

Hardware Design Notes

When designing hardware, pay attention to the selection of transceiver chips. If the system is powered by 5V, you can choose NXP's 82C251, TJA1050, MCP2551, etc., and if you use 3.3V power supply, you can choose VD230. The pin 8 of some chips has a wake-up function, so you need to read the datasheet in detail. The CAN_TX of the MCU pin should be connected to the TXD of the transceiver, and the CAN_RX of the MCU should be connected to the RXD of the transceiver. The figure below illustrates the signal flow. For MCU, CAN_TX is an output ; for MCU, CAN_RX is an input .

other

Synchronization jump width (SJW) : The value of SJW directly affects the adjustable range of the phase buffer segment during resynchronization. The value of SJW can be selected between 1 and 4. We choose 3 or 4 to make the bus wider Baud rate tolerance;

Sampling times : It is divided into single sampling and three sampling. Although the three sampling is designed to filter out glitches on the bus, the use of three sampling often affects the jump of SJW, so we generally use single sampling in practical applications. .

suggestion

The baud rate selection rules are suggested as follows:

  1. TSEG2 >= SJW;
  2. BRP (baud rate prescaler) should be as small as possible, and SJW (synchronous jump width) should be as large as possible;
  3. SMP (sampling point) is selected between 75% and 85%.
  4. Take a single sample.

Attachment: The size of the system clock set by the punctual atom STM32F1 series development board

SYSCLK (system clock) = 72MHz

AHB bus clock (using SYSCLK) = 72MHz

APB1 bus clock (PCLK1) = 36MHz CAN, USB, I2C, etc.

APB2 bus clock (PCLK2) = 72MHz

PLL clock = 72MHz

Further reading:

1. Can baud rate calculation - bus and interface - Electronic Engineering World Network

CAN bus baud rate setting - take STM32F103 as an example

 

Baud rate setting

The first is the meaning of several nouns. The composition of one bit in CAN is as follows. Note that the location of the sampling point is in the middle of PBS1 and PBS2. Based on this bit timing, the baud rate can be calculated .

Minimum time unit (Tq, Time Quantum)

Synchronization segment (SS, Synchronization Segment) 1Tq

Propagation Time Segment (PTS, Propagation Time Segment) 1-8Tq

Phase Buffer Segment 1 (PBS1, Phase Buffer Segment1) 1-8Tq

Phase Buffer Segment 2 (PBS2, Phase Buffer Segment2) 2-8Tq

Resynchronization compensation width (SJW, reSynchronization Jump Width) 1-4Tq

Baud Rate Prescaler (BRP, Baud Rate Prescaler)

Explanation of the CAN baud rate in the Chinese reference manual

STM32 merges the propagation time segment and the phase buffer segment 1 to form a new time segment 1.

The CAN bit timing register (CAN_BTR) is used to set parameters such as TS1, TS2, BRP, SJW, etc., and directly determines the baud rate of CAN.

SJW[1:0] resynchronization compensation width;

ts1[3:0] time period 1;

ts2[2:0] time period 2;

BRP[9:0] baud rate divider;

Yeah, there is no setting of SS synchronization segment, because SS is always equal to 1 .

In addition, we also need to set the clock frequency of APB1, STM32F1 is usually 36MHz (external 8M crystal oscillator).

CAN baud rate calculation formula ( register )

The specific function of the resynchronization compensation width (SJW) is to increase or reduce the allowable deviation of the CAN baud rate, that is to say, its size has no great relationship with the value of the baud rate, and it belongs to the auxiliary category, which can be understood as wave Baud rate precision adjustment. It should be noted here that this value should be designed as large as possible to meet the requirement of greater than 3% in the bit width tolerance test.

The formula is trjw=tq*(sjw+1), which means that for resynchronization, this parameter defines the upper limit of how many time units CAN hardware can extend or shorten in each bit. When actually writing a program, this sjw is usually defined as 0. A lot of information about this resynchronization compensation width is very vague. Here I will give you an example to help you understand. The library function is used here first. The first picture below is the baud rate result detected by the USBCAN analyzer when CAN_SJW_4tq is used. There are 5 baud rates that can realize CAN communication; the second picture below is when CAN_SJW_1tq is used. As a result of the baud rate detected by the USBCAN analyzer, only 3 baud rates can realize CAN communication. That is, the fault tolerance performance becomes smaller. Whether this value should be larger or smaller will be studied in the future.

In the case of CAN_SJW_4tq, the effective baud rate segment is relatively large

In the case of CAN_SJW_1tq, the effective baud rate segment is relatively small

CAN_BTR register

Let's take an example, if SJW is 0, TS1 is 8, TS2 is 7, and BRP is 3, then the baud rate should be 36M/[(1+9+8)*4]=500kbps.

Sampling rate setting

Besides baud rate, we should also consider a formula called sample rate . This formula is as follows. In fact, we can see that the sampling rate represents the relative position of the sampling point in the entire bit. For the above example, the sampling rate is (1+8+1)/(1+8+1+7+1)=55.6%, which is low.

When the baud rate is greater than 800kbps, the recommended sampling rate is 75%; when the baud rate is 500k-800kbps, the recommended sampling rate is about 80%; when the baud rate is not greater than 500kbps, the recommended sampling rate is 87.5%. Sampling points that deviate too much from this standard can cause consistency problems.

The value that needs to be set in the library function

Just now we said that the baud rate of CAN in STM32 microcontroller is mainly determined by 4 parameters. They are the resynchronization jump time unit (tsjw), the time unit of time segment 1 (tbs1), the time unit of time segment 2 (tbs2), and the baud rate divider (brp). Corresponding to the library functions of the STM32 microcontroller , they correspond to four parameters such as CAN_SJW, CAN_BS1 (ie tbs1+1), CAN_BS2 (ie tbs2+1), and CAN_Prescaler. Only need to complete the setting of these 4 parameters in the library function. For example, set CAN_SJW=1tq, CAN_BS1=6tq, CAN_BS2=1tq, CANPrescaler=9, then the baud rate=36M/[(1+6+1)*9]=500Kbps.

Sampling rate = (1+6)/(1+6+1)=87.5%. It should be noted that CAN_SJW does not participate in the calculation of any formula . The position of the baud rate formula is the SS segment, so it is 1.

Here I recommend a very good STM32-CAN baud rate calculation software can_config.exe, if necessary, please leave a message. But in this software, the statement that CAN_SJW participates in the baud rate calculation mentioned in the formula is wrong and needs to be corrected.

Hardware Design Notes

When designing hardware, pay attention to the selection of transceiver chips. If the system is powered by 5V, you can choose NXP's 82C251, TJA1050, MCP2551, etc., and if you use 3.3V power supply, you can choose VD230. The pin 8 of some chips has a wake-up function, so you need to read the datasheet in detail. The CAN_TX of the MCU pin should be connected to the TXD of the transceiver, and the CAN_RX of the MCU should be connected to the RXD of the transceiver. The figure below illustrates the signal flow. For MCU, CAN_TX is an output ; for MCU, CAN_RX is an input .

other

Synchronization jump width (SJW) : The value of SJW directly affects the adjustable range of the phase buffer segment during resynchronization. The value of SJW can be selected between 1 and 4. We choose 3 or 4 to make the bus wider Baud rate tolerance;

Sampling times : It is divided into single sampling and three sampling. Although the three sampling is designed to filter out glitches on the bus, the use of three sampling often affects the jump of SJW, so we generally use single sampling in practical applications. .

suggestion

The baud rate selection rules are suggested as follows:

  1. TSEG2 >= SJW;
  2. BRP (baud rate prescaler) should be as small as possible, and SJW (synchronous jump width) should be as large as possible;
  3. SMP (sampling point) is selected between 75% and 85%.
  4. Take a single sample.

Attachment: The size of the system clock set by the punctual atom STM32F1 series development board

SYSCLK (system clock) = 72MHz

AHB bus clock (using SYSCLK) = 72MHz

APB1 bus clock (PCLK1) = 36MHz CAN, USB, I2C, etc.

APB2 bus clock (PCLK2) = 72MHz

PLL clock = 72MHz

Further reading:

1. Can baud rate calculation - bus and interface - Electronic Engineering World Network

Guess you like

Origin blog.csdn.net/piaolingyekong/article/details/124276670