@@ -413,6 +413,17 @@ bool uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t par
413413 huart -> Init .Mode = UART_MODE_TX_RX ;
414414 huart -> Init .HwFlowCtl = flow_control ;
415415 huart -> Init .OverSampling = UART_OVERSAMPLING_16 ;
416+
417+ /* Configure UART Clock Prescaler */
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 ;
425+ #endif
426+
416427#if defined(UART_ADVFEATURE_NO_INIT )
417428 // Default value
418429 huart -> AdvancedInit .AdvFeatureInit = UART_ADVFEATURE_NO_INIT ;
@@ -1415,6 +1426,133 @@ void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)
14151426 serial_t * obj = get_serial_obj (huart );
14161427 HAL_UART_Receive_IT (huart , & (obj -> recv ), 1 );
14171428}
1429+
1430+ /**
1431+ * @brief Function called to set the uart clock prescaler
1432+ * @param pclk : supplied clock rate to related uart
1433+ * @retval uint32_t clock prescaler
1434+ */
1435+ #if defined(UART_PRESCALER_DIV1 )
1436+ uint32_t calculatePresc (uint32_t pclk , uint32_t baudrate , uint32_t oversampling )
1437+ {
1438+ static const uint16_t presc_div [12 ] = {1 , 2 , 4 , 6 , 8 , 10 , 12 , 16 , 32 , 64 , 128 , 256 };
1439+
1440+ uint32_t condition = 0 ;
1441+ if (oversampling == UART_OVERSAMPLING_16 ) {
1442+ condition = 16U ;
1443+ } else {
1444+ condition = 8U ;
1445+ }
1446+
1447+ for (uint32_t idx = 0 ; idx < 8 ; idx ++ ) {
1448+ uint32_t uartclk = pclk / presc_div [idx ];
1449+ uint32_t brr = 0 ;
1450+ if (oversampling == UART_OVERSAMPLING_16 ) {
1451+ brr = (uartclk + (baudrate / 2U )) / baudrate ;
1452+ } else {
1453+ brr = ((2U * uartclk ) + (baudrate / 2U )) / baudrate ;
1454+ }
1455+
1456+ if (brr >= condition && brr <= 0xFFFU ) {
1457+ return UART_PRESCALER_DIV1 + idx ;
1458+ }
1459+ }
1460+ return UART_PRESCALER_DIV1 ;
1461+ }
1462+ #endif
1463+
1464+ /**
1465+ * @brief Function called to get the clock source frequency of the uart
1466+ * @param huart : uart handle structure
1467+ * @retval uint32_t clock source frequency
1468+ */
1469+ uint32_t uart_getPCLK (UART_HandleTypeDef * huart )
1470+ {
1471+ #if defined(LPUART1 )
1472+ if (huart -> Instance == LPUART1 ) {
1473+ #if defined(STM32H5 ) || defined(STM32U3 ) || defined(STM32U5 )
1474+ return HAL_RCC_GetPCLK3Freq ();
1475+ #elif defined(STM32H7 )
1476+ uint32_t sysclk = HAL_RCC_GetSysClockFreq ();
1477+ uint32_t prescaler = (RCC -> D2CFGR & 18 ) >> 0x7 ;
1478+
1479+ uint32_t apb4 = 1 ;
1480+
1481+ switch (prescaler )
1482+ {
1483+ case 0b000 : prescaler = 1 ; break ;
1484+ case 0b100 : prescaler = 2 ; break ;
1485+ case 0b101 : prescaler = 4 ; break ;
1486+ case 0b110 : prescaler = 8 ; break ;
1487+ case 0b111 : prescaler = 16 ; break ;
1488+ default : break ;
1489+ }
1490+
1491+ return (sysclk / prescaler );
1492+ #elif defined(STM32G4 ) || defined(STM32L4 ) || defined(STM32L5 ) || defined(STM32WB )
1493+ return HAL_RCC_GetPCLK1Freq ();
1494+ #elif defined(STM32WL ) || defined(STM32WB0 )
1495+ return HAL_RCC_GetPCLK2Freq ();
1496+ #elif defined(STM32WBA )
1497+ return HAL_RCC_GetPCLK7Freq ();
1498+ #endif
1499+ }
1500+ #endif
1501+
1502+ #if defined(LPUART2 )
1503+ if (huart -> Instance == LPUART2 ) {
1504+ return HAL_RCC_GetPCLK1Freq ();
1505+ }
1506+ #endif
1507+
1508+ #if defined(LPUART3 )
1509+ if (huart -> Instance == LPUART3 ) {
1510+ return HAL_RCC_GetPCLK1Freq ();
1511+ }
1512+ #endif
1513+
1514+ #if defined(STM32F0 ) || defined(STM32G0 ) || defined(STM32L0 ) || defined(STM32C0 ) \
1515+ || defined(STM32WB )
1516+ return HAL_RCC_GetPCLK1Freq ();
1517+ #endif
1518+
1519+ #if defined(STM32WL ) || defined(STM32WB0 )
1520+ return HAL_RCC_GetPCLK2Freq ();
1521+ #endif
1522+
1523+ #if defined(STM32H7 )
1524+ if (huart -> Instance == USART1
1525+ #if defined(USART10 )
1526+ || huart -> Instance == USART10
1527+ #endif
1528+ #if defined(USART6 )
1529+ || huart -> Instance == USART6
1530+ #endif
1531+ #if defined(UART9 )
1532+ || huart -> Instance == UART9
1533+ #endif
1534+ ) {
1535+ return HAL_RCC_GetPCLK2Freq ();
1536+ }
1537+ return HAL_RCC_GetPCLK1Freq ();
1538+ #endif
1539+
1540+ #if defined(STM32F7 ) || defined(STM32F2 ) || defined(STM32F4 ) || defined(STM32F1 ) \
1541+ || defined(STM32C0 ) || defined(STM32F3 ) || defined(STM32H5 ) || defined(STM32G4 ) \
1542+ || defined(STM32L4 ) || defined(STM32L5 ) || defined(STM32WBA )
1543+ if (huart -> Instance == USART1
1544+ #if defined(USART6 ) && !defined(STM32H5 ) && !defined(STM32U5 )
1545+ || huart -> Instance == USART6
1546+ #endif
1547+ ) {
1548+ return HAL_RCC_GetPCLK2Freq ();
1549+ }
1550+ return HAL_RCC_GetPCLK1Freq ();
1551+ #endif
1552+
1553+ return 0 ;
1554+ }
1555+
14181556#endif /* HAL_UART_MODULE_ENABLED && !HAL_UART_MODULE_ONLY */
14191557
14201558#ifdef __cplusplus
0 commit comments