Skip to content

Commit 8b0a5c2

Browse files
authored
Merge pull request #12099 from J91Olivier/stm32f4_baud_rate_calculation_fix
Implemented recommended fix from https://github.com/STMicroelectronic…
2 parents 03e29df + ef5da02 commit 8b0a5c2

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_hal_uart.c

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,6 +2432,7 @@ static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
24322432
static void UART_SetConfig(UART_HandleTypeDef *huart)
24332433
{
24342434
uint32_t tmpreg = 0x00U;
2435+
uint32_t pclk;
24352436

24362437
/* Check the parameters */
24372438
assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
@@ -2484,39 +2485,58 @@ static void UART_SetConfig(UART_HandleTypeDef *huart)
24842485
if(huart->Init.OverSampling == UART_OVERSAMPLING_8)
24852486
{
24862487
/*-------------------------- USART BRR Configuration ---------------------*/
2487-
#if defined(USART6)
2488-
if((huart->Instance == USART1) || (huart->Instance == USART6))
2488+
#if defined(USART6) && defined(UART9) && defined(UART10)
2489+
if ((huart->Instance == USART1) || (huart->Instance == USART6) || (huart->Instance == UART9) || (huart->Instance == UART10))
24892490
{
2490-
huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
2491+
pclk = HAL_RCC_GetPCLK2Freq();
2492+
huart->Instance->BRR = UART_BRR_SAMPLING8(pclk, huart->Init.BaudRate);
2493+
}
2494+
#elif defined(USART6)
2495+
if ((huart->Instance == USART1) || (huart->Instance == USART6))
2496+
{
2497+
pclk = HAL_RCC_GetPCLK2Freq();
2498+
huart->Instance->BRR = UART_BRR_SAMPLING8(pclk, huart->Init.BaudRate);
24912499
}
24922500
#else
2493-
if(huart->Instance == USART1)
2501+
if (huart->Instance == USART1)
24942502
{
2495-
huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
2496-
}
2503+
pclk = HAL_RCC_GetPCLK2Freq();
2504+
huart->Instance->BRR = UART_BRR_SAMPLING8(pclk, huart->Init.BaudRate);
2505+
}
24972506
#endif /* USART6 */
24982507
else
24992508
{
2500-
huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
2509+
pclk = HAL_RCC_GetPCLK1Freq();
2510+
huart->Instance->BRR = UART_BRR_SAMPLING8(pclk, huart->Init.BaudRate);
25012511
}
25022512
}
25032513
else
25042514
{
25052515
/*-------------------------- USART BRR Configuration ---------------------*/
2506-
#if defined(USART6)
2507-
if((huart->Instance == USART1) || (huart->Instance == USART6))
2516+
#if defined(USART6) && defined(UART9) && defined(UART10)
2517+
2518+
if ((huart->Instance == USART1) || (huart->Instance == USART6) || (huart->Instance == UART9) || (huart->Instance == UART10))
25082519
{
2509-
huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
2520+
pclk = HAL_RCC_GetPCLK2Freq();
2521+
huart->Instance->BRR = UART_BRR_SAMPLING16(pclk, huart->Init.BaudRate);
2522+
}
2523+
#elif defined(USART6)
2524+
if ((huart->Instance == USART1) || (huart->Instance == USART6))
2525+
{
2526+
pclk = HAL_RCC_GetPCLK2Freq();
2527+
huart->Instance->BRR = UART_BRR_SAMPLING16(pclk, huart->Init.BaudRate);
25102528
}
25112529
#else
2512-
if(huart->Instance == USART1)
2530+
if (huart->Instance == USART1)
25132531
{
2514-
huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
2515-
}
2532+
pclk = HAL_RCC_GetPCLK2Freq();
2533+
huart->Instance->BRR = UART_BRR_SAMPLING16(pclk, huart->Init.BaudRate);
2534+
}
25162535
#endif /* USART6 */
25172536
else
25182537
{
2519-
huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
2538+
pclk = HAL_RCC_GetPCLK1Freq();
2539+
huart->Instance->BRR = UART_BRR_SAMPLING16(pclk, huart->Init.BaudRate);
25202540
}
25212541
}
25222542
}

0 commit comments

Comments
 (0)