STM32L0/4: Always try to select LSE if LPUART and baudrate <= 9600

pull/5941/head
Marc Emmers 2018-01-25 17:23:48 +01:00
parent 964e6e74fb
commit 01660ff5ae
1 changed files with 20 additions and 22 deletions

View File

@ -369,32 +369,30 @@ void serial_baud(serial_t *obj, int baudrate)
struct serial_s *obj_s = SERIAL_S(obj); struct serial_s *obj_s = SERIAL_S(obj);
obj_s->baudrate = baudrate; obj_s->baudrate = baudrate;
if (init_uart(obj) != HAL_OK) {
#if defined(LPUART1_BASE) #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 */ /* 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) { if (obj_s->uart == LPUART_1) {
/* Try to change LPUART clock source */ /* If baudrate is lower than 9600 try to change to LSE */
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
if (baudrate == 9600) { if (baudrate <= 9600 && __HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY)) {
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1; PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_LSE; PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_LSE;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
} else {
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
}
if (init_uart(obj) == HAL_OK) { if (init_uart(obj) == HAL_OK) {
return; return;
} }
} /* Change LPUART clock source and try again */
else {
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1; PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK; PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
if (init_uart(obj) == HAL_OK){
return;
}
}
} }
#endif /* LPUART1_BASE */ #endif /* LPUART1_BASE */
if (init_uart(obj) != HAL_OK) {
debug("Cannot initialize UART with baud rate %u\n", baudrate); debug("Cannot initialize UART with baud rate %u\n", baudrate);
} }
} }