Skip to main content
Visitor II
November 15, 2024
Question

Ensure that Frequency clock is in the range [3 * baudrate, 4096 * baudrate]

  • November 15, 2024
  • 1 reply
  • 1056 views

Dear MCD Application Team,

in Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_uart.c you can find this check:

/* Ensure that Frequency clock is in the range [3 * baudrate, 4096 * baudrate] */
if ((lpuart_ker_ck_pres < (3U * huart->Init.BaudRate)) ||
(lpuart_ker_ck_pres > (4096U * huart->Init.BaudRate)))
{
ret = HAL_ERROR;
}

assume huart->Init.BaudRate is 28333333 and lpuart_ker_ck_pres=170000000 the result is larger than uint32_t and results in a  HAL_ERROR

You can check this with this small test application. Maybe you can fix this in the next release.

#include <stdio.h>
#include <stdint.h>
int main() {
 uint32_t BaudRate = 28333334;
 uint32_t lpuart_ker_ck_pres = 170000000;

 printf("Start;\n");
 if (lpuart_ker_ck_pres < (3U * BaudRate))
 {
 printf("Error(1);\n");
 }
 if (lpuart_ker_ck_pres > (4096U * BaudRate))
 {
 printf("Error(2);\n");
 }
 printf("End;\n");
}

 

    This topic has been closed for replies.

    1 reply

    Graduate II
    November 15, 2024

    Keep in mind the Cube is only meant as a start and will not cover  all cases.

    Graduate II
    November 15, 2024

    However instead of

    lpuart_ker_ck_pres > (4096U * huart->Init.BaudRate

    ( lpuart_ker_ck_pres / 4096) > huart->Init.Baudrate
     should avoid the overflow

    Visitor II
    November 15, 2024

    this is my preferred solution. Maybe this find a way into the next release 

    if (lpuart_ker_ck_pres > (4096U * (uint64_t)BaudRate))