mbed library sources

Fork of mbed-src by mbed official

Revision:
242:7074e42da0b2
Parent:
227:7bd0639b8911
--- a/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/serial_api.c	Thu Jun 26 10:30:09 2014 +0100
+++ b/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/serial_api.c	Fri Jun 27 07:30:09 2014 +0100
@@ -27,11 +27,14 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *******************************************************************************
  */
+#include "mbed_assert.h"
 #include "serial_api.h"
+
+#if DEVICE_SERIAL
+
 #include "cmsis.h"
 #include "pinmap.h"
 #include <string.h>
-#include "stm32f4xx_hal.h"
 
 static const PinMap PinMap_UART_TX[] = {
     {PA_2,  UART_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART2)},
@@ -53,7 +56,7 @@
 
 #define UART_NUM (3)
 
-static uint32_t serial_irq_ids[UART_NUM] = {0};
+static uint32_t serial_irq_ids[UART_NUM] = {0, 0, 0};
 
 static uart_irq_handler irq_handler;
 
@@ -87,12 +90,15 @@
     // Enable USART clock
     if (obj->uart == UART_1) {
         __USART1_CLK_ENABLE();
+        obj->index = 0;
     }
     if (obj->uart == UART_2) {
         __USART2_CLK_ENABLE();
+        obj->index = 1;
     }
     if (obj->uart == UART_6) {
         __USART6_CLK_ENABLE();
+        obj->index = 2;
     }
     
     // Configure the UART pins
@@ -107,12 +113,10 @@
     obj->stopbits = UART_STOPBITS_1;
     obj->parity   = UART_PARITY_NONE;
 
-    init_uart(obj);
+    obj->pin_tx = tx;
+    obj->pin_rx = rx;
 
-    // The index is used by irq
-    if (obj->uart == UART_1) obj->index = 0;
-    if (obj->uart == UART_2) obj->index = 1;
-    if (obj->uart == UART_6) obj->index = 2;
+    init_uart(obj);
     
     // For stdio management
     if (obj->uart == STDIO_UART) {
@@ -123,6 +127,27 @@
 }
 
 void serial_free(serial_t *obj) {
+    // Reset UART and disable clock
+    if (obj->uart == UART_1) {
+        __USART1_FORCE_RESET();
+        __USART1_RELEASE_RESET();
+        __USART1_CLK_DISABLE();
+    }
+    if (obj->uart == UART_2) {
+        __USART2_FORCE_RESET();
+        __USART2_RELEASE_RESET();
+        __USART2_CLK_DISABLE();
+    }
+    if (obj->uart == UART_6) {
+        __USART6_FORCE_RESET();
+        __USART6_RELEASE_RESET();
+        __USART6_CLK_DISABLE();
+    }
+
+    // Configure GPIOs
+    pin_function(obj->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
+    pin_function(obj->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
+
     serial_irq_ids[obj->index] = 0;
 }
 
@@ -134,8 +159,7 @@
 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
     if (data_bits == 8) {
         obj->databits = UART_WORDLENGTH_8B;
-    }
-    else {
+    } else {
         obj->databits = UART_WORDLENGTH_9B;
     }
 
@@ -155,8 +179,7 @@
     
     if (stop_bits == 2) {
         obj->stopbits = UART_STOPBITS_2;
-    }
-    else {
+    } else {
         obj->stopbits = UART_STOPBITS_1;
     }
 
@@ -167,10 +190,8 @@
  * INTERRUPTS HANDLING
  ******************************************************************************/
 
-// Not part of mbed api
 static void uart_irq(UARTName name, int id) {
     UartHandle.Instance = (USART_TypeDef *)name;
-      
     if (serial_irq_ids[id] != 0) {
         if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TC) != RESET) {
             irq_handler(serial_irq_ids[id], TxIrq);
@@ -183,10 +204,15 @@
     }
 }
 
-// Not part of mbed api
-static void uart1_irq(void) {uart_irq(UART_1, 0);}
-static void uart2_irq(void) {uart_irq(UART_2, 1);}
-static void uart6_irq(void) {uart_irq(UART_6, 2);}
+static void uart1_irq(void) {
+    uart_irq(UART_1, 0);
+}
+static void uart2_irq(void) {
+    uart_irq(UART_2, 1);
+}
+static void uart6_irq(void) {
+    uart_irq(UART_6, 2);
+}
 
 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) {
     irq_handler = handler;
@@ -218,8 +244,7 @@
       
         if (irq == RxIrq) {
             __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_RXNE);
-        }
-        else { // TxIrq
+        } else { // TxIrq
             __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_TC);
         }
         
@@ -234,8 +259,7 @@
              __HAL_UART_DISABLE_IT(&UartHandle, UART_IT_RXNE);
             // Check if TxIrq is disabled too
             if ((UartHandle.Instance->CR1 & USART_CR1_TXEIE) == 0) all_disabled = 1;
-        }
-        else { // TxIrq
+        } else { // TxIrq
             __HAL_UART_DISABLE_IT(&UartHandle, UART_IT_TXE);
             // Check if RxIrq is disabled too
             if ((UartHandle.Instance->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
@@ -295,3 +319,5 @@
 
 void serial_break_clear(serial_t *obj) {
 }
+
+#endif