@@ -416,12 +416,7 @@ bool uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
416
416
417
417
/* Configure UART Clock Prescaler */
418
418
#if defined(UART_PRESCALER_DIV1 )
419
- // Default Value
420
- uint32_t clock_prescaler = UART_PRESCALER_DIV1 ;
421
-
422
- uint32_t pclk = uart_getPCLK (huart );
423
- clock_prescaler = calculatePresc (pclk , baudrate , huart -> Init .OverSampling );
424
- huart -> Init .ClockPrescaler = clock_prescaler ;
419
+ huart -> Init .ClockPrescaler = uart_compute_prescaler (huart );
425
420
#endif
426
421
427
422
#if defined(UART_ADVFEATURE_NO_INIT )
@@ -1429,28 +1424,29 @@ void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)
1429
1424
1430
1425
/**
1431
1426
* @brief Function called to set the uart clock prescaler
1432
- * @param pclk : supplied clock rate to related uart
1427
+ * @param huart : uart handle structure
1433
1428
* @retval uint32_t clock prescaler
1434
1429
*/
1435
1430
#if defined(UART_PRESCALER_DIV1 )
1436
- uint32_t calculatePresc ( uint32_t pclk , uint32_t baudrate , uint32_t oversampling )
1431
+ uint32_t uart_compute_prescaler ( UART_HandleTypeDef * huart )
1437
1432
{
1438
1433
static const uint16_t presc_div [12 ] = {1 , 2 , 4 , 6 , 8 , 10 , 12 , 16 , 32 , 64 , 128 , 256 };
1434
+ uint32_t freq = uart_get_clock_source_freq (huart );
1439
1435
1440
1436
uint32_t condition = 0 ;
1441
- if (oversampling == UART_OVERSAMPLING_16 ) {
1437
+ if (huart -> Init . OverSampling == UART_OVERSAMPLING_16 ) {
1442
1438
condition = 16U ;
1443
1439
} else {
1444
1440
condition = 8U ;
1445
1441
}
1446
1442
1447
- for (uint32_t idx = 0 ; idx < 8 ; idx ++ ) {
1448
- uint32_t uartclk = pclk / presc_div [idx ];
1443
+ for (uint32_t idx = 0 ; idx < 12 ; idx ++ ) {
1444
+ uint32_t uartclk = freq / presc_div [idx ];
1449
1445
uint32_t brr = 0 ;
1450
- if (oversampling == UART_OVERSAMPLING_16 ) {
1451
- brr = (uartclk + (baudrate / 2U )) / baudrate ;
1446
+ if (huart -> Init . OverSampling == UART_OVERSAMPLING_16 ) {
1447
+ brr = (uartclk + (huart -> Init . BaudRate / 2U )) / huart -> Init . BaudRate ;
1452
1448
} else {
1453
- brr = ((2U * uartclk ) + (baudrate / 2U )) / baudrate ;
1449
+ brr = ((2U * uartclk ) + (huart -> Init . BaudRate / 2U )) / huart -> Init . BaudRate ;
1454
1450
}
1455
1451
1456
1452
if (brr >= condition && brr <= 0xFFFU ) {
@@ -1459,14 +1455,13 @@ uint32_t calculatePresc(uint32_t pclk, uint32_t baudrate, uint32_t oversampling)
1459
1455
}
1460
1456
return UART_PRESCALER_DIV1 ;
1461
1457
}
1462
- #endif
1463
1458
1464
1459
/**
1465
1460
* @brief Function called to get the clock source frequency of the uart
1466
1461
* @param huart : uart handle structure
1467
1462
* @retval uint32_t clock source frequency
1468
1463
*/
1469
- uint32_t uart_getPCLK (UART_HandleTypeDef * huart )
1464
+ uint32_t uart_get_clock_source_freq (UART_HandleTypeDef * huart )
1470
1465
{
1471
1466
#if defined(LPUART1 )
1472
1467
if (huart -> Instance == LPUART1 ) {
@@ -1582,6 +1577,7 @@ uint32_t uart_getPCLK(UART_HandleTypeDef *huart)
1582
1577
1583
1578
return 0 ;
1584
1579
}
1580
+ #endif /* UART_PRESCALER_DIV1 */
1585
1581
1586
1582
#endif /* HAL_UART_MODULE_ENABLED && !HAL_UART_MODULE_ONLY */
1587
1583
0 commit comments