Skip to content

Commit 28d4f18

Browse files
committed
fix(uart): fixes according to review.
Signed-off-by: baftii <[email protected]>
1 parent 7a1c8ed commit 28d4f18

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

libraries/SrcWrapper/inc/uart.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,10 @@ void uart_enable_rx(serial_t *obj);
273273
size_t uart_debug_write(uint8_t *data, uint32_t size);
274274

275275
#if defined(UART_PRESCALER_DIV1)
276-
uint32_t calculatePresc(uint32_t pclk, uint32_t baudrate, uint32_t oversampling);
276+
uint32_t uart_compute_prescaler(UART_HandleTypeDef *huart);
277+
uint32_t uart_get_clock_source_freq(UART_HandleTypeDef *huart);
277278
#endif
278279

279-
uint32_t uart_getPCLK(UART_HandleTypeDef *huart);
280-
281280
#endif /* HAL_UART_MODULE_ENABLED && !HAL_UART_MODULE_ONLY */
282281
#ifdef __cplusplus
283282
}

libraries/SrcWrapper/src/stm32/uart.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,7 @@ bool uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
416416

417417
/* Configure UART Clock Prescaler */
418418
#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);
425420
#endif
426421

427422
#if defined(UART_ADVFEATURE_NO_INIT)
@@ -1429,28 +1424,29 @@ void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)
14291424

14301425
/**
14311426
* @brief Function called to set the uart clock prescaler
1432-
* @param pclk : supplied clock rate to related uart
1427+
* @param huart : uart handle structure
14331428
* @retval uint32_t clock prescaler
14341429
*/
14351430
#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)
14371432
{
14381433
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);
14391435

14401436
uint32_t condition = 0;
1441-
if (oversampling == UART_OVERSAMPLING_16) {
1437+
if (huart->Init.OverSampling == UART_OVERSAMPLING_16) {
14421438
condition = 16U;
14431439
} else {
14441440
condition = 8U;
14451441
}
14461442

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];
14491445
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;
14521448
} else {
1453-
brr = ((2U * uartclk) + (baudrate / 2U)) / baudrate;
1449+
brr = ((2U * uartclk) + (huart->Init.BaudRate / 2U)) / huart->Init.BaudRate;
14541450
}
14551451

14561452
if (brr >= condition && brr <= 0xFFFU) {
@@ -1459,14 +1455,13 @@ uint32_t calculatePresc(uint32_t pclk, uint32_t baudrate, uint32_t oversampling)
14591455
}
14601456
return UART_PRESCALER_DIV1;
14611457
}
1462-
#endif
14631458

14641459
/**
14651460
* @brief Function called to get the clock source frequency of the uart
14661461
* @param huart : uart handle structure
14671462
* @retval uint32_t clock source frequency
14681463
*/
1469-
uint32_t uart_getPCLK(UART_HandleTypeDef *huart)
1464+
uint32_t uart_get_clock_source_freq(UART_HandleTypeDef *huart)
14701465
{
14711466
#if defined(LPUART1)
14721467
if (huart->Instance == LPUART1) {
@@ -1582,6 +1577,7 @@ uint32_t uart_getPCLK(UART_HandleTypeDef *huart)
15821577

15831578
return 0;
15841579
}
1580+
#endif /* UART_PRESCALER_DIV1 */
15851581

15861582
#endif /* HAL_UART_MODULE_ENABLED && !HAL_UART_MODULE_ONLY */
15871583

0 commit comments

Comments
 (0)