From 01660ff5ae6d11c8ddd524e4257653f9f1abc2e2 Mon Sep 17 00:00:00 2001 From: Marc Emmers Date: Thu, 25 Jan 2018 17:23:48 +0100 Subject: [PATCH 1/2] STM32L0/4: Always try to select LSE if LPUART and baudrate <= 9600 --- targets/TARGET_STM/serial_api.c | 42 ++++++++++++++++----------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/targets/TARGET_STM/serial_api.c b/targets/TARGET_STM/serial_api.c index c1e568d733..437560934b 100644 --- a/targets/TARGET_STM/serial_api.c +++ b/targets/TARGET_STM/serial_api.c @@ -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); } } From 7881e68efeb20071772133d3b590362117951e64 Mon Sep 17 00:00:00 2001 From: Marc Emmers Date: Thu, 25 Jan 2018 17:24:57 +0100 Subject: [PATCH 2/2] STM32L0: Enable stop mode operation for the LPUART --- targets/TARGET_STM/serial_api.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/targets/TARGET_STM/serial_api.c b/targets/TARGET_STM/serial_api.c index 437560934b..a3209f1f6d 100644 --- a/targets/TARGET_STM/serial_api.c +++ b/targets/TARGET_STM/serial_api.c @@ -525,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); }