36
36
#define USE_LPUART_CLK_LSE 0x01
37
37
#define USE_LPUART_CLK_PCLK1 0x02
38
38
#define USE_LPUART_CLK_HSI 0x04
39
+ #define USE_LPUART_CLK_PCLK3 0x08
40
+ #define USE_LPUART_CLK_SYSCLK 0x10
39
41
40
42
int stdio_uart_inited = 0 ; // used in platform/mbed_board.c and platform/mbed_retarget.cpp
41
43
serial_t stdio_uart ;
@@ -376,8 +378,8 @@ void serial_baud(serial_t *obj, int baudrate)
376
378
if (obj_s -> uart == LPUART_1 ) {
377
379
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0 };
378
380
PeriphClkInitStruct .PeriphClockSelection = RCC_PERIPHCLK_LPUART1 ;
379
- #if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE ) & USE_LPUART_CLK_LSE )
380
- if (baudrate <= 9600 ) {
381
+ #if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE ) & USE_LPUART_CLK_LSE ) && MBED_CONF_TARGET_LSE_AVAILABLE
382
+ if (baudrate <= ( int )( LSE_VALUE / 3 ) ) {
381
383
// Enable LSE in case it is not already done
382
384
if (!__HAL_RCC_GET_FLAG (RCC_FLAG_LSERDY )) {
383
385
RCC_OscInitTypeDef RCC_OscInitStruct = {0 };
@@ -417,46 +419,50 @@ void serial_baud(serial_t *obj, int baudrate)
417
419
return ;
418
420
}
419
421
#endif
420
- #if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE ) & USE_LPUART_CLK_PCLK3 )
422
+ #if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE ) & USE_LPUART_CLK_PCLK3 ) && defined( RCC_LPUART1CLKSOURCE_PCLK3 )
421
423
PeriphClkInitStruct .Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK3 ;
422
424
HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct );
423
425
if (init_uart (obj ) == HAL_OK ) {
424
426
return ;
425
427
}
426
428
#endif
427
429
#if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE ) & USE_LPUART_CLK_HSI )
428
- // Enable HSI in case it is not already done
429
- if (!__HAL_RCC_GET_FLAG (RCC_FLAG_HSIRDY )) {
430
- RCC_OscInitTypeDef RCC_OscInitStruct = {0 };
431
- RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_HSI ;
432
- RCC_OscInitStruct .HSIState = RCC_HSI_ON ;
433
- RCC_OscInitStruct .PLL .PLLState = RCC_PLL_OFF ;
434
- RCC_OscInitStruct .HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT ;
435
- #if defined(DUAL_CORE ) && (TARGET_STM32H7 )
436
- while (LL_HSEM_1StepLock (HSEM , CFG_HW_RCC_SEMID )) {
430
+ if (baudrate <= (int )(HSI_VALUE / 3 )) {
431
+ // Enable HSI in case it is not already done
432
+ if (!__HAL_RCC_GET_FLAG (RCC_FLAG_HSIRDY )) {
433
+ RCC_OscInitTypeDef RCC_OscInitStruct = {0 };
434
+ RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_HSI ;
435
+ RCC_OscInitStruct .HSIState = RCC_HSI_ON ;
436
+ RCC_OscInitStruct .PLL .PLLState = RCC_PLL_OFF ;
437
+ RCC_OscInitStruct .HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT ;
438
+ #if defined(DUAL_CORE ) && (TARGET_STM32H7 )
439
+ while (LL_HSEM_1StepLock (HSEM , CFG_HW_RCC_SEMID )) {
440
+ }
441
+ #endif /* DUAL_CORE */
442
+ HAL_RCC_OscConfig (& RCC_OscInitStruct );
443
+ #if defined(DUAL_CORE ) && (TARGET_STM32H7 )
444
+ LL_HSEM_ReleaseLock (HSEM , CFG_HW_RCC_SEMID , HSEM_CR_COREID_CURRENT );
445
+ #endif /* DUAL_CORE */
437
446
}
438
- #endif /* DUAL_CORE */
439
- HAL_RCC_OscConfig (& RCC_OscInitStruct );
440
- #if defined(DUAL_CORE ) && (TARGET_STM32H7 )
441
- LL_HSEM_ReleaseLock (HSEM , CFG_HW_RCC_SEMID , HSEM_CR_COREID_CURRENT );
442
- #endif /* DUAL_CORE */
443
- }
444
- // Keep it to verify if HAL_RCC_OscConfig didn't exit with a timeout
445
- if (__HAL_RCC_GET_FLAG (RCC_FLAG_HSIRDY )) {
446
- PeriphClkInitStruct .Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_HSI ;
447
+ // Keep it to verify if HAL_RCC_OscConfig didn't exit with a timeout
448
+ if (__HAL_RCC_GET_FLAG (RCC_FLAG_HSIRDY )) {
449
+ PeriphClkInitStruct .Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_HSI ;
447
450
#if defined(DUAL_CORE ) && (TARGET_STM32H7 )
448
- while (LL_HSEM_1StepLock (HSEM , CFG_HW_RCC_SEMID )) {
449
- }
451
+ while (LL_HSEM_1StepLock (HSEM , CFG_HW_RCC_SEMID )) {
452
+ }
450
453
#endif /* DUAL_CORE */
451
- HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct );
454
+ HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct );
452
455
#if defined(DUAL_CORE ) && (TARGET_STM32H7 )
453
- LL_HSEM_ReleaseLock (HSEM , CFG_HW_RCC_SEMID , HSEM_CR_COREID_CURRENT );
456
+ LL_HSEM_ReleaseLock (HSEM , CFG_HW_RCC_SEMID , HSEM_CR_COREID_CURRENT );
454
457
#endif /* DUAL_CORE */
455
- if (init_uart (obj ) == HAL_OK ) {
456
- return ;
458
+ if (init_uart (obj ) == HAL_OK ) {
459
+ return ;
460
+ }
457
461
}
458
462
}
459
463
#endif
464
+
465
+ #if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE ) & USE_LPUART_CLK_SYSCLK )
460
466
// Last chance using SYSCLK
461
467
PeriphClkInitStruct .Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK ;
462
468
#if defined(DUAL_CORE ) && (TARGET_STM32H7 )
@@ -467,6 +473,15 @@ void serial_baud(serial_t *obj, int baudrate)
467
473
#if defined(DUAL_CORE ) && (TARGET_STM32H7 )
468
474
LL_HSEM_ReleaseLock (HSEM , CFG_HW_RCC_SEMID , HSEM_CR_COREID_CURRENT );
469
475
#endif /* DUAL_CORE */
476
+
477
+ if (init_uart (obj ) == HAL_OK ) {
478
+ return ;
479
+ }
480
+ else
481
+ #endif
482
+ {
483
+ debug ("Cannot initialize LPUART with baud rate %u using any enabled clock source\n" , baudrate );
484
+ }
470
485
}
471
486
#endif /* LPUART1_BASE */
472
487
@@ -653,8 +668,8 @@ HAL_StatusTypeDef init_uart(serial_t *obj)
653
668
654
669
#if defined(LPUART1_BASE )
655
670
if (huart -> Instance == LPUART1 ) {
656
- if (obj_s -> baudrate <= 9600 ) {
657
- #if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE ) & USE_LPUART_CLK_LSE ) && defined(USART_CR3_UCESM )
671
+ if (obj_s -> baudrate <= ( int )( LSE_VALUE / 3 ) ) {
672
+ #if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE ) & USE_LPUART_CLK_LSE ) && MBED_CONF_TARGET_LSE_AVAILABLE && defined(USART_CR3_UCESM )
658
673
HAL_UARTEx_EnableClockStopMode (huart );
659
674
#endif
660
675
HAL_UARTEx_EnableStopMode (huart );
0 commit comments