@@ -379,11 +379,22 @@ void serial_baud(serial_t *obj, int baudrate)
379
379
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0 };
380
380
PeriphClkInitStruct .PeriphClockSelection = RCC_PERIPHCLK_LPUART1 ;
381
381
#if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE ) & USE_LPUART_CLK_LSE )
382
- if (baudrate <= 9600 && __HAL_RCC_GET_FLAG (RCC_FLAG_LSERDY )) {
383
- PeriphClkInitStruct .Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_LSE ;
384
- HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct );
385
- if (init_uart (obj ) == HAL_OK ) {
386
- return ;
382
+ if (baudrate <= 9600 ) {
383
+ // Enable LSE in case it is not already done
384
+ if (!__HAL_RCC_GET_FLAG (RCC_FLAG_LSERDY )) {
385
+ RCC_OscInitTypeDef RCC_OscInitStruct = {0 };
386
+ RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_LSE ;
387
+ RCC_OscInitStruct .HSIState = RCC_LSE_ON ;
388
+ RCC_OscInitStruct .PLL .PLLState = RCC_PLL_OFF ;
389
+ HAL_RCC_OscConfig (& RCC_OscInitStruct );
390
+ }
391
+ // Keep it to verify if HAL_RCC_OscConfig didn't exit with a timeout
392
+ if (__HAL_RCC_GET_FLAG (RCC_FLAG_LSERDY )) {
393
+ PeriphClkInitStruct .Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_LSE ;
394
+ HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct );
395
+ if (init_uart (obj ) == HAL_OK ) {
396
+ return ;
397
+ }
387
398
}
388
399
}
389
400
#endif
@@ -395,6 +406,16 @@ void serial_baud(serial_t *obj, int baudrate)
395
406
}
396
407
#endif
397
408
#if ((MBED_CONF_TARGET_LPUART_CLOCK_SOURCE ) & USE_LPUART_CLK_HSI )
409
+ // Enable HSI in case it is not already done
410
+ if (!__HAL_RCC_GET_FLAG (RCC_FLAG_HSIRDY )) {
411
+ RCC_OscInitTypeDef RCC_OscInitStruct = {0 };
412
+ RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_HSI ;
413
+ RCC_OscInitStruct .HSIState = RCC_HSI_ON ;
414
+ RCC_OscInitStruct .PLL .PLLState = RCC_PLL_OFF ;
415
+ RCC_OscInitStruct .HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT ;
416
+ HAL_RCC_OscConfig (& RCC_OscInitStruct );
417
+ }
418
+ // Keep it to verify if HAL_RCC_OscConfig didn't exit with a timeout
398
419
if (__HAL_RCC_GET_FLAG (RCC_FLAG_HSIRDY )) {
399
420
PeriphClkInitStruct .Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_HSI ;
400
421
HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct );
0 commit comments