STMicroelectronics / STM32CubeL4

STM32Cube MCU Full Package for the STM32L4 series - (HAL + LL Drivers, CMSIS Core, CMSIS Device, MW libraries plus a set of Projects running on all boards provided by ST (Nucleo, Evaluation and Discovery Kits))
Other
269 stars 154 forks source link

HAL UART transmits 9 data bits when configured to 8 data bits #53

Closed IvanVnucec closed 2 years ago

IvanVnucec commented 2 years ago

Describe the set-up

Describe the bug When USART1 is configured as:

HAL_UART_Transmit(&huart1, pData, 4, 1000) function transmits 4 packets, with each having 9 data bits instead of 8.

How To Reproduce

  1. Create new STM32 Project in STM32CubeIDE
  2. Configure USART1 as Asynchronous with configuration as stated in Describe the bug heading
  3. Send the data over UART with the HAL_UART_Transmit function
  4. Examine data on the TX pin.

Screenshots Here is the screenshot of the Saleae Logic software with the "bok\0" data being sent. image Clearly, the UART is sending 9 data bits instead of 8.

Furthermore, when I send "0xFF, 0xFF, 0xFF, 0xFF" then this happens: image (in the above screenshot, I have set the Saleae Logic to decode UART with 9 data bits and the UART configuration is left unchanged)

HAL UART initialization code

static void MX_USART1_UART_Init(void) {
  /* USER CODE BEGIN USART1_Init 0 */

  /* USER CODE END USART1_Init 0 */

  /* USER CODE BEGIN USART1_Init 1 */

  /* USER CODE END USART1_Init 1 */
  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.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart1) != HAL_OK) {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */

  /* USER CODE END USART1_Init 2 */
}
IvanVnucec commented 2 years ago

I think I'was wrong, so let me explain why I think that:

To avoid a possible bug in HAL library, I have initialized UART by directly writing values into registers with the same configuration as stated in the comment above and the same problem appeared.
I was thinking that maybe the baud rate is different from the one configured. If you look closely at the first screenshot in the comment above, you could see that some sample points of the logic analyzer software are exactly on the signal edge, which is wrong. The sample point should be in the middle of the two signal edges, where the signal is most stable.

Then I was tinkering a bit with the baud rate in the logic analyzer decoder and with the 108000 bits/s I have managed to set sample points exactly in the middle of edges. See picture below: image As you can see, UART is transmitting "bok\0" without any issues.

One of the main concerns why baud rate might differ from the one configured is that the UART clock source set to the HSI internal oscillator. 108000 bits/s differs by about 7% from the wanted 115200 bits/s.