From 1a3fefb6bf9039cc598b9a37512ed5c871500330 Mon Sep 17 00:00:00 2001
From: Hauke Petersen <hauke.petersen@fu-berlin.de>
Date: Thu, 9 Feb 2017 11:35:28 +0100
Subject: [PATCH] cpu/sam3: uart driver enhancements

- enabled driver to run in TX only mode
- make use of gpio_init_mux() for pin intialization
- simplyfied pin configuration
- adapted pin configuartion for arduino-due and udoo
---
 boards/arduino-due/include/periph_conf.h | 58 ++++++++++--------------
 boards/udoo/include/periph_conf.h        | 37 +++++++++++++--
 cpu/sam3/include/periph_cpu.h            |  6 +--
 cpu/sam3/periph/uart.c                   | 32 +++++++------
 4 files changed, 77 insertions(+), 56 deletions(-)

diff --git a/boards/arduino-due/include/periph_conf.h b/boards/arduino-due/include/periph_conf.h
index 1dce388d69..e833c5abc5 100644
--- a/boards/arduino-due/include/periph_conf.h
+++ b/boards/arduino-due/include/periph_conf.h
@@ -68,45 +68,37 @@ static const timer_conf_t timer_config[] = {
  */
 static const uart_conf_t uart_config[] = {
     {
-        .dev     = (Uart *)UART,
-        .rx_port = PIOA,
-        .tx_port = PIOA,
-        .rx_pin  = 8,
-        .tx_pin  = 9,
-        .mux     = GPIO_MUX_A,
-        .pmc_id  = ID_UART,
-        .irqn    = UART_IRQn
+        .dev    = (Uart *)UART,
+        .rx_pin = GPIO_PIN(PA, 8),
+        .tx_pin = GPIO_PIN(PA, 9),
+        .mux    = GPIO_MUX_A,
+        .pmc_id = ID_UART,
+        .irqn   = UART_IRQn
     },
     {
-        .dev     = (Uart *)USART0,
-        .rx_port = PIOA,
-        .tx_port = PIOA,
-        .rx_pin  = 10,
-        .tx_pin  = 11,
-        .mux     = GPIO_MUX_A,
-        .pmc_id  = ID_USART0,
-        .irqn    = USART0_IRQn
+        .dev    = (Uart *)USART0,
+        .rx_pin = GPIO_PIN(PA, 10),
+        .tx_pin = GPIO_PIN(PA, 11),
+        .mux    = GPIO_MUX_A,
+        .pmc_id = ID_USART0,
+        .irqn   = USART0_IRQn
     },
     {
-        .dev     = (Uart *)USART1,
-        .rx_port = PIOA,
-        .tx_port = PIOA,
-        .rx_pin  = 12,
-        .tx_pin  = 13,
-        .mux     = GPIO_MUX_A,
-        .pmc_id  = ID_USART1,
-        .irqn    = USART1_IRQn
+        .dev    = (Uart *)USART1,
+        .rx_pin = GPIO_PIN(PA, 12),
+        .tx_pin = GPIO_PIN(PA, 13),
+        .mux    = GPIO_MUX_A,
+        .pmc_id = ID_USART1,
+        .irqn   = USART1_IRQn
     },
     {
-        .dev     = (Uart *)USART3,
-        .rx_port = PIOD,
-        .tx_port = PIOD,
-        .rx_pin  = 4,
-        .tx_pin  = 5,
-        .mux     = GPIO_MUX_B,
-        .pmc_id  = ID_USART3,
-        .irqn    = USART3_IRQn
-    },
+        .dev    = (Uart *)USART3,
+        .rx_pin = GPIO_PIN(PD, 5),
+        .tx_pin = GPIO_PIN(PD, 4),
+        .mux    = GPIO_MUX_B,
+        .pmc_id = ID_USART3,
+        .irqn   = USART3_IRQn
+    }
 };
 
 /* define interrupt vectors */
diff --git a/boards/udoo/include/periph_conf.h b/boards/udoo/include/periph_conf.h
index c00e34921e..2dbc5770ef 100644
--- a/boards/udoo/include/periph_conf.h
+++ b/boards/udoo/include/periph_conf.h
@@ -66,11 +66,38 @@ static const timer_conf_t timer_config[] = {
  * @{
  */
 static const uart_conf_t uart_config[] = {
-    /* device, rx port, tx port, rx pin, tx pin, mux, PMC bit, IRGn line */
-    {(Uart *)UART,   PIOA, PIOA,  8,  9, GPIO_MUX_A, ID_UART,   UART_IRQn},
-    {(Uart *)USART0, PIOA, PIOA, 10, 11, GPIO_MUX_A, ID_USART0, USART0_IRQn},
-    {(Uart *)USART1, PIOA, PIOA, 12, 13, GPIO_MUX_A, ID_USART1, USART1_IRQn},
-    {(Uart *)USART3, PIOD, PIOD,  4,  5, GPIO_MUX_B, ID_USART3, USART3_IRQn}
+    {
+        .dev    = (Uart *)UART,
+        .rx_pin = GPIO_PIN(PA, 8),
+        .tx_pin = GPIO_PIN(PA, 9),
+        .mux    = GPIO_MUX_A,
+        .pmc_id = ID_UART,
+        .irqn   = UART_IRQn
+    },
+    {
+        .dev    = (Uart *)USART0,
+        .rx_pin = GPIO_PIN(PA, 10),
+        .tx_pin = GPIO_PIN(PA, 11),
+        .mux    = GPIO_MUX_A,
+        .pmc_id = ID_USART0,
+        .irqn   = USART0_IRQn
+    },
+    {
+        .dev    = (Uart *)USART1,
+        .rx_pin = GPIO_PIN(PA, 12),
+        .tx_pin = GPIO_PIN(PA, 13),
+        .mux    = GPIO_MUX_A,
+        .pmc_id = ID_USART1,
+        .irqn   = USART1_IRQn
+    },
+    {
+        .dev    = (Uart *)USART3,
+        .rx_pin = GPIO_PIN(PD, 5),
+        .tx_pin = GPIO_PIN(PD, 4),
+        .mux    = GPIO_MUX_B,
+        .pmc_id = ID_USART3,
+        .irqn   = USART3_IRQn
+    }
 };
 
 /* define interrupt vectors */
diff --git a/cpu/sam3/include/periph_cpu.h b/cpu/sam3/include/periph_cpu.h
index f8553ac08f..d82b5142d5 100644
--- a/cpu/sam3/include/periph_cpu.h
+++ b/cpu/sam3/include/periph_cpu.h
@@ -204,10 +204,8 @@ typedef struct {
  */
 typedef struct {
     Uart *dev;              /**< U(S)ART device used */
-    Pio *rx_port;           /**< port for RX pin */
-    Pio *tx_port;           /**< port for TX pin */
-    uint8_t rx_pin;         /**< RX pin */
-    uint8_t tx_pin;         /**< TX pin */
+    gpio_t rx_pin;          /**< RX pin */
+    gpio_t tx_pin;          /**< TX pin */
     gpio_mux_t mux;         /**< MUX used for pins */
     uint8_t pmc_id;         /**< bit in the PMC register of the device*/
     uint8_t irqn;           /**< interrupt number of the device */
diff --git a/cpu/sam3/periph/uart.c b/cpu/sam3/periph/uart.c
index 2cb028a9f8..5c6e0d9fd9 100644
--- a/cpu/sam3/periph/uart.c
+++ b/cpu/sam3/periph/uart.c
@@ -22,6 +22,7 @@
 #include "cpu.h"
 #include "board.h"
 #include "periph/uart.h"
+#include "periph/gpio.h"
 
 #define ENABLE_DEBUG (0)
 #include "debug.h"
@@ -50,25 +51,28 @@ int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
     /* enable clock */
     uart_poweron(uart);
 
-    /* configure pins
-       TODO: optimize once GPIO refactoring is merged */
-    uart_config[uart].rx_port->PIO_PDR = (1 << uart_config[uart].rx_pin);
-    uart_config[uart].rx_port->PIO_ABSR &= ~(1 << uart_config[uart].rx_pin);
-    uart_config[uart].tx_port->PIO_ABSR |=  (uart_config[uart].mux <<
-                                             uart_config[uart].rx_pin);
-    uart_config[uart].tx_port->PIO_PDR = (1 << uart_config[uart].tx_pin);
-    uart_config[uart].tx_port->PIO_ABSR &= ~(1 << uart_config[uart].tx_pin);
-    uart_config[uart].tx_port->PIO_ABSR |=  (uart_config[uart].mux <<
-                                             uart_config[uart].tx_pin);
+    /* reset configuration */
+    dev->UART_CR = 0;
+    dev->UART_IDR = 0x0000ffff;
+
+    /* configure pins */
+    gpio_init_mux(uart_config[uart].tx_pin, uart_config[uart].mux);
+    if (rx_cb) {
+        gpio_init_mux(uart_config[uart].rx_pin, uart_config[uart].mux);
+    }
 
     /* configure baud rate and set mode to 8N1 */
     dev->UART_BRGR = (CLOCK_CORECLOCK / (16 * baudrate));
     dev->UART_MR = UART_MR_PAR_NO | US_MR_CHRL_8_BIT;
-    dev->UART_CR = UART_CR_RXEN | UART_CR_TXEN | UART_CR_RSTSTA;
 
-    /* enable RX interrupt */
-    NVIC_EnableIRQ(uart_config[uart].irqn);
-    dev->UART_IER = UART_IER_RXRDY;
+    if (rx_cb) {
+        dev->UART_CR = UART_CR_RXEN | UART_CR_TXEN | UART_CR_RSTSTA;
+        NVIC_EnableIRQ(uart_config[uart].irqn);
+        dev->UART_IER = UART_IER_RXRDY;
+    }
+    else {
+        dev->UART_CR = UART_CR_TXEN | UART_CR_RSTSTA;
+    }
 
     return UART_OK;
 }
-- 
GitLab