Modified for BG96

Fork of mbed-dev by mbed official

Revision:
182:a56a73fd2a6f
Parent:
181:57724642e740
Child:
187:0387e8f68319
--- a/targets/TARGET_STM/serial_api.c	Fri Feb 16 16:09:33 2018 +0000
+++ b/targets/TARGET_STM/serial_api.c	Tue Mar 20 16:56:18 2018 +0000
@@ -369,32 +369,30 @@
     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 @@
         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);
 }