STM32 LPUART : update clock source depending on expected baudrate

pull/5570/head
jeromecoutant 2017-11-23 14:52:35 +01:00
parent 6d64c2fbf4
commit 07e71d6ec8
1 changed files with 25 additions and 1 deletions

View File

@ -384,8 +384,32 @@ void serial_baud(serial_t *obj, int baudrate)
obj_s->baudrate = baudrate;
if (init_uart(obj) != HAL_OK) {
debug("Cannot initialize UART with baud rate %u\n", baudrate);
#if defined(LPUART1_BASE)
/* Note that LPUART clock source must be in the range [3 x baud rate, 4096 x baud rate], check Ref Manual */
if (obj_s->uart == LPUART_1) {
/* Try to change LPUART clock source */
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
if (baudrate == 9600) {
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_LSE;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
if (init_uart(obj) == HAL_OK){
return;
}
}
else {
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
if (init_uart(obj) == HAL_OK){
return;
}
}
}
#endif /* LPUART1_BASE */
debug("Cannot initialize UART with baud rate %u\n", baudrate);
}
}