mirror of https://github.com/ARMmbed/mbed-os.git
Merge pull request #5941 from marcemmers/lpuart-use-lse
STM32L0/4 Enable use of LPUART in stop modepull/5742/merge
commit
84ee9cc7a1
|
@ -369,32 +369,30 @@ void serial_baud(serial_t *obj, int baudrate)
|
|||
struct serial_s *obj_s = SERIAL_S(obj);
|
||||
|
||||
obj_s->baudrate = baudrate;
|
||||
if (init_uart(obj) != HAL_OK) {
|
||||
|
||||
#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;
|
||||
}
|
||||
}
|
||||
if (obj_s->uart == LPUART_1) {
|
||||
/* If baudrate is lower than 9600 try to change to LSE */
|
||||
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
|
||||
if (baudrate <= 9600 && __HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY)) {
|
||||
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
|
||||
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_LSE;
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
/* Change LPUART clock source and try again */
|
||||
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
|
||||
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK;
|
||||
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
|
||||
}
|
||||
#endif /* LPUART1_BASE */
|
||||
|
||||
if (init_uart(obj) != HAL_OK) {
|
||||
debug("Cannot initialize UART with baud rate %u\n", baudrate);
|
||||
}
|
||||
}
|
||||
|
@ -527,6 +525,18 @@ HAL_StatusTypeDef init_uart(serial_t *obj)
|
|||
huart->Init.Mode = UART_MODE_TX_RX;
|
||||
}
|
||||
|
||||
#if defined(LPUART1_BASE)
|
||||
if (huart->Instance == LPUART1) {
|
||||
if (obj_s->baudrate <= 9600) {
|
||||
HAL_UARTEx_EnableClockStopMode(huart);
|
||||
HAL_UARTEx_EnableStopMode(huart);
|
||||
} else {
|
||||
HAL_UARTEx_DisableClockStopMode(huart);
|
||||
HAL_UARTEx_DisableStopMode(huart);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return HAL_UART_Init(huart);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue