[Shanghai Hangxin ACM32F070 development board + touch function evaluation board] 05. How to configure the CAN communication baud rate
[Copy link]
Have you ever encountered a situation where the same CAN communication baud rate has many different configuration parameters, but in actual testing, there is a situation where they cannot communicate with each other? I have encountered it...
First, let's take a look at the bit timing of CAN. A bit can be divided into four segments, namely synchronization segment (SS), propagation time segment (PTS), phase buffer segment 1 (PBS1) and phase buffer segment 2 (PBS2). These segments are composed of the smallest unit called Time Quantum (TQ).
Based on the composition of the bit segment, we can determine a sampling point during CAN communication. This position is fixed and is set at the end of PBS1.
The CAN bit timing diagram is also given in the ACM32F070 user manual:
From the above figure, it consists of 3 segments, namely the synchronization segment (SYNC_SEG), time period 1 (TSEG1) and time period 2 (TSEG1). Time period 1 (TSEG1) includes the propagation time period and phase buffer segment 1 in the CAN standard.
Regarding the setting of CAN communication baud rate, CAN is mounted on the APB summary, so the CAN clock frequency is related to the APB clock, divided by BRP, and then determined by the settings of TSEG1 and TSEG2 to determine the final communication baud rate. The calculation formula is as follows:
Baudrate == PCLK / ((1 + (tTSEG1 + 1) + (tTSEG2 + 1)) * 2 * (BRP + 1))
The calculation formula of the sampling point is as follows:
Sample Point = (1 + (tTSEG1 + 1)) / (1 + (tTSEG1 + 1) + (tTSEG2 + 1)) * 100%
According to the value ranges of the three register bits BRP, TSEG1 and TSEG2 in the user manual, we can use the function to exhaustively enumerate all BRP, TSEG1 and TSEG2 values that meet the baud rate setting value, and then calculate the corresponding sampling point percentage through the values of TSEG1 and TSEG2. The function implementation is as follows:
/*******************************************************************************
* [url=home.php?mod=space&uid=159083]@brief[/url] * @param
* @retval
* [url=home.php?mod=space&uid=1020061]@attention[/url] *******************************************************************************/
void CAN_ConfigParam(uint32_t Baudrate)
{
char *tTSEG1_Param[16] =
{
"CAN_TSEG1_1tq ", "CAN_TSEG1_2tq ", "CAN_TSEG1_3tq ", "CAN_TSEG1_4tq ",
"CAN_TSEG1_5tq ", "CAN_TSEG1_6tq ", "CAN_TSEG1_7tq ", "CAN_TSEG1_8tq ",
"CAN_TSEG1_9tq ", "CAN_TSEG1_10tq", "CAN_TSEG1_11tq", "CAN_TSEG1_12tq",
"CAN_TSEG1_13tq", "CAN_TSEG1_14tq", "CAN_TSEG1_15tq", "CAN_TSEG1_16tq",
};
char *tTSEG2_Param[8] =
{
"CAN_TSEG2_1tq", "CAN_TSEG2_2tq", "CAN_TSEG2_3tq", "CAN_TSEG2_4tq",
"CAN_TSEG2_5tq", "CAN_TSEG2_6tq", "CAN_TSEG2_7tq", "CAN_TSEG2_8tq",
};
uint32_t PCLK = System_Get_APBClock();
for(uint32_t BRP = 0; BRP < 32; BRP++)
{
for(uint32_t tTSEG1 = 0; tTSEG1 < 16; tTSEG1++)
{
for(uint32_t tTSEG2 = 0; tTSEG2 < 8; tTSEG2++)
{
if(Baudrate == PCLK / ((1 + (tTSEG1 + 1) + (tTSEG2 + 1)) * 2 * (BRP + 1)))
{
printf("\r\nBRP:%02d, tTSEG1: %s, tTSEG2:%s, %0.1f%%", BRP, tTSEG1_Param[tTSEG1], tTSEG2_Param[tTSEG2], (float)(1 + (tTSEG1 + 1)) / (float)(1 + (tTSEG1 + 1) + (tTSEG2 + 1)) * 100.0);
}
}
}
}
}
When we need to set the communication baud rate to 500k, the running results are as follows:
Combined with the CAN parameters configured with reference to the official example in the previous article, when PCLK=64MHz, BRP=7, TSEG1=CAN_TSEG1_3tq, TSEG2=CAN_TSEG2_4tq, the current CAN communication baud rate is 500kHz, and the sampling point percentage is 50%.
Let's go back to the original question. One of the situations is that the same baud rate but different sampling points will cause communication abnormalities. Therefore, we need to ensure the same communication baud rate and set the same sampling points on a CAN bus. In addition, it is recommended to set the sampling point to around 75%. Setting the sampling point too early may lead to incorrect judgment due to unstable communication, and setting it too early may also miss the judgment of the sampling point.
|