2022-08-22 11:42 AM
Hello.
With a preset baud rate of 19200, UART in LIN mode works fine, but the baud rate of the master node is not always known, so there is a need for automatic baud rate detection.
If the UART is set to automatic baud rate detection, then the data transfer rate is determined incorrectly, what could be the reason for this, what am I doing wrong?
Master node baud rate of 19200
UART initialization code
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_AUTOBAUDRATE_INIT;
huart1.AdvancedInit.AutoBaudRateEnable = UART_ADVFEATURE_AUTOBAUDRATE_ENABLE;
huart1.AdvancedInit.AutoBaudRateMode = UART_ADVFEATURE_AUTOBAUDRATE_ONSTARTBIT;
if (HAL_LIN_Init(&huart1, UART_LINBREAKDETECTLENGTH_11B) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
{
Error_Handler();
}
printf("StartBaudrate = %d \n", huart1.Instance->BRR);
while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_REACK) == RESET){}
while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_TEACK) == RESET){}
while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_ABRF) == RESET){}
if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_ABRE)!= RESET)
{
Error_Handler();
}
else
{
while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_RXNE) == RESET){}
}
printf("NowBaudrate = %d \n", huart1.Instance->BRR);
}
SystemCoreClock: 64000000
StartBaudrate = 115107, Instance->BRR = 556
NowBaudrate = 1484, Instance->BRR = 43102
UART Status Flags
UART_FLAG_TXFT: 0
UART_FLAG_RXFT: 0
UART_FLAG_RXFF: 0
UART_FLAG_TXFE: 0
UART_FLAG_REACK: 1
UART_FLAG_TEACK: 1
UART_FLAG_WUF: 0
UART_FLAG_RWU: 0
UART_FLAG_SBKF: 0
UART_FLAG_CMF: 0
UART_FLAG_BUSY: 0
UART_FLAG_ABRF: 1
UART_FLAG_ABRE: 0
UART_FLAG_RTOF: 0
UART_FLAG_CTS: 0
UART_FLAG_CTSIF: 0
UART_FLAG_LBDF: 0
UART_FLAG_TXE: 1
UART_FLAG_TXFNF: 1
UART_FLAG_TC: 1
UART_FLAG_RXNE: 1
UART_FLAG_RXFNE: 1
UART_FLAG_IDLE: 1
UART_FLAG_ORE: 1
UART_FLAG_NE: 1
UART_FLAG_FE: 0
UART_FLAG_PE: 0