Occasional errors on UART's received Bytes.
Hi, everyone,
I have been struggling with an issue for sometime. Upon reception, occasionally, a Byte is received with an error in which the most significant bit is received as a '1'. For example, if 0x55 (0101 0101) is sent, 0xD5 (1101 0101) is received (not always, though). THe UART's configuration is as follows:
static void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_2;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_RTS_CTS;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
}As a debug measure, reducing the baudrate from 115200 to 2400 had no effect. I also changed the oversampling from 16 bits to 8 bits, with no avail. One bit sampling was also enabled, with no success. The last two configurations were also set simultaneously and the problem persists.
I have set the UART with enabled interrupts. Thus, upon a received Byte, an interrupt is triggered and the Byte extracted inside the ISR. Whenever the issue occurs, the RDR (receive data register) contains the Byte with the said error on the Byte's most significant bit. This fact leads me to the suspicion that there's a kind of sampling problem, leading to the UART sampling the stop bit as the MS bit.
A curious behavior that I observed during the UART_SetConfig function was that HAL_RCC_GetPCLK1Freq() returns 16MHz, when it is configured as 170MHz in .ioc file (ClockPrescaler=0) altough, given this unexpected clock, the USARTDIV constant is well computed and stored in BRR. Does this fact relate to the issue?
I think it is relevant to say that the transmission occurs with no problem whatsoever. The bytes are correctly received on a terminal in my computer.
What might be happening?
Thanks in advance and, please, let me know if there's any detail I may have missed exposing here.
Kind regards,
Pedro Machado
