Actual UART Baudrate is off from what is specified
I am using UART over RS-485 on an STM32MP153AAB3.
The communication fails because the actual baudrate is off from the specified baudrate. With a specified baudrate of 9600 Bits/s, I am getting an actual baudrate of 9970 Bits/s, as you can see here (time per Bit is 100.3µs instead of 104µs):
Changing the baudrate to 115200 Bits/s results in a time per Bit of 8.33µs, i. e. 120000 Bits/s:
I set the USART2,4 Clock in the .ioc file to 64 MHz and chose PCLK1 as the source.
The value in the BRR register is 0x1A0B which, if I understand it correctly, should correspond to a value for USARTDIV of 416, giving a baudrate of 9615 Bits/s (64,000,000/(16*416) = 9615).
My settings are as follows:
static void MX_UART4_Init(void)
{
huart4.Instance = UART4;
huart4.Init.BaudRate = 9600;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_RTS;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart4) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart4, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart4, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart4) != HAL_OK)
{
Error_Handler();
}
}And I'm calling the UART in interrupt mode:
uint8_t uartData[6] = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55};
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
if (HAL_UART_Transmit_IT(&huart4, &uartData[0], sizeof(uartData)) != HAL_OK)
{
Error_Handler();
}
}Does anyone have an idea what the problem could be?
Edit: Updated the values for 115200 Bits/s.
