Skip to content

Commit 38744b9

Browse files
author
Cruz Monrreal
authored
Merge pull request #7498 from bcostm/fix_hsi_lse_lpuart
STM32: enable HSI/LSE clocks for LPUART
2 parents 904bdf8 + 665de33 commit 38744b9

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

targets/TARGET_STM/serial_api.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,22 @@ void serial_baud(serial_t *obj, int baudrate)
379379
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
380380
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
381381
#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+
}
387398
}
388399
}
389400
#endif
@@ -395,6 +406,16 @@ void serial_baud(serial_t *obj, int baudrate)
395406
}
396407
#endif
397408
#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
398419
if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIRDY)) {
399420
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_HSI;
400421
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);

0 commit comments

Comments
 (0)