From aefa818338cb4aa90462dcd7995076b134e68ac9 Mon Sep 17 00:00:00 2001
From: Thomas Eichinger <thomas.eichinger1@gmail.com>
Date: Thu, 24 Jul 2014 22:07:04 +0200
Subject: [PATCH] stm32f4: be UART0 aware

---
 boards/stm32f4discovery/include/board.h |  1 +
 cpu/stm32f4/periph/uart.c               |  1 -
 cpu/stm32f4/syscalls.c                  | 59 ++++++++++++++++++++-----
 3 files changed, 50 insertions(+), 11 deletions(-)

diff --git a/boards/stm32f4discovery/include/board.h b/boards/stm32f4discovery/include/board.h
index e40813bc2e..294a815564 100644
--- a/boards/stm32f4discovery/include/board.h
+++ b/boards/stm32f4discovery/include/board.h
@@ -40,6 +40,7 @@
  */
 #define STDIO               UART_0
 #define STDIO_BAUDRATE      (115200U)
+#define STDIO_BUFSIZE       (64U)
 /** @} */
 
 /**
diff --git a/cpu/stm32f4/periph/uart.c b/cpu/stm32f4/periph/uart.c
index 93bddb01f7..891ffd3032 100644
--- a/cpu/stm32f4/periph/uart.c
+++ b/cpu/stm32f4/periph/uart.c
@@ -27,7 +27,6 @@
 
 /* guard file in case no UART device was specified */
 #if UART_NUMOF
-
 /**
  * @brief Each UART device has to store two callbacks.
  */
diff --git a/cpu/stm32f4/syscalls.c b/cpu/stm32f4/syscalls.c
index fa915f7b60..524bab4436 100644
--- a/cpu/stm32f4/syscalls.c
+++ b/cpu/stm32f4/syscalls.c
@@ -34,18 +34,54 @@
 #include "irq.h"
 #include "periph/uart.h"
 
+#ifdef MODULE_UART0
+#include "board_uart0.h"
+#endif
+
 /**
  * manage the heap
  */
 extern uint32_t _end;                       /* address of last used memory cell */
 caddr_t heap_top = (caddr_t)&_end + 4;
 
+#ifndef MODULE_UART0
+/**
+ * @brief use mutex for waiting on incoming UART chars
+ */
+static mutex_t uart_rx_mutex;
+static char rx_buf_mem[STDIO_BUFSIZE];
+static ringbuffer_t rx_buf;
+#endif
+
+/**
+ * @brief Receive a new character from the UART and put it into the receive buffer
+ */
+void rx_cb(void *arg, char data)
+{
+#ifndef MODULE_UART0
+    (void)arg;
+
+    ringbuffer_add_one(&rx_buf, data);
+    mutex_unlock(&uart_rx_mutex);
+#else
+    if (uart0_handler_pid) {
+        uart0_handle_incoming(data);
+
+        uart0_notify_thread();
+    }
+#endif
+}
+
 /**
  * @brief Initialize NewLib, called by __libc_init_array() from the startup script
  */
 void _init(void)
 {
-    uart_init_blocking(STDIO, STDIO_BAUDRATE);
+#ifndef MODULE_UART0
+    mutex_init(&uart_rx_mutex);
+    ringbuffer_init(&rx_buf, rx_buf_mem, STDIO_BUFSIZE);
+#endif
+    uart_init(STDIO, STDIO_BAUDRATE, rx_cb, 0, 0);
 }
 
 /**
@@ -151,11 +187,12 @@ int _open_r(struct _reent *r, const char *name, int mode)
  */
 int _read_r(struct _reent *r, int fd, void *buffer, unsigned int count)
 {
-    char c;
-    char *buff = (char*)buffer;
-    uart_read_blocking(STDIO, &c);
-    buff[0] = c;
-    return 1;
+#ifndef MODULE_UART0
+    while (rx_buf.avail == 0) {
+        mutex_lock(&uart_rx_mutex);
+    }
+    return ringbuffer_get(&rx_buf, (char*)buffer, rx_buf.avail);
+#endif
 }
 
 /**
@@ -175,11 +212,13 @@ int _read_r(struct _reent *r, int fd, void *buffer, unsigned int count)
  */
 int _write_r(struct _reent *r, int fd, const void *data, unsigned int count)
 {
-    char *c = (char*)data;
-    for (int i = 0; i < count; i++) {
-        uart_write_blocking(STDIO, c[i]);
+    int i = 0;
+
+    while (i < count) {
+        uart_write_blocking(STDIO, ((char*)data)[i++]);
     }
-    return count;
+
+    return i;
 }
 
 /**
-- 
GitLab