Merge pull request #5941 from marcemmers/lpuart-use-lse

STM32L0/4 Enable use of LPUART in stop mode
pull/5742/merge
Cruz Monrreal 2018-02-12 10:26:37 -06:00 committed by GitHub
commit 84ee9cc7a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 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);
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 */
/* If baudrate is lower than 9600 try to change to LSE */
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
if (baudrate == 9600) {
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;
}
}
else {
/* Change LPUART clock source and try again */
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LPUART1;
PeriphClkInitStruct.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
if (init_uart(obj) == HAL_OK){
return;
}
}
}
#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);
}