Skip to content

Commit 07e71d6

Browse files
committed
STM32 LPUART : update clock source depending on expected baudrate
1 parent 6d64c2f commit 07e71d6

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

targets/TARGET_STM/serial_api.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,32 @@ void serial_baud(serial_t *obj, int baudrate)
384384

385385
obj_s->baudrate = baudrate;
386386
if (init_uart(obj) != HAL_OK) {
387-
debug("Cannot initialize UART with baud rate %u\n", baudrate);
387+
388+
#if defined(LPUART1_BASE)
388389
/* Note that LPUART clock source must be in the range [3 x baud rate, 4096 x baud rate], check Ref Manual */
390+
if (obj_s->uart == LPUART_1) {
391+
/* Try to change LPUART clock source */
392+
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
393+
if (baudrate == 9600) {
394+
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
395+
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_LSE;
396+
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
397+
if (init_uart(obj) == HAL_OK){
398+
return;
399+
}
400+
}
401+
else {
402+
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
403+
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK;
404+
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
405+
if (init_uart(obj) == HAL_OK){
406+
return;
407+
}
408+
}
409+
}
410+
#endif /* LPUART1_BASE */
411+
412+
debug("Cannot initialize UART with baud rate %u\n", baudrate);
389413
}
390414
}
391415

0 commit comments

Comments
 (0)