Skip to content
Snippets Groups Projects
Commit aefa8183 authored by Thomas Eichinger's avatar Thomas Eichinger
Browse files

stm32f4: be UART0 aware

parent 8fc2e61e
No related branches found
No related tags found
No related merge requests found
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
*/ */
#define STDIO UART_0 #define STDIO UART_0
#define STDIO_BAUDRATE (115200U) #define STDIO_BAUDRATE (115200U)
#define STDIO_BUFSIZE (64U)
/** @} */ /** @} */
/** /**
......
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
/* guard file in case no UART device was specified */ /* guard file in case no UART device was specified */
#if UART_NUMOF #if UART_NUMOF
/** /**
* @brief Each UART device has to store two callbacks. * @brief Each UART device has to store two callbacks.
*/ */
......
...@@ -34,18 +34,54 @@ ...@@ -34,18 +34,54 @@
#include "irq.h" #include "irq.h"
#include "periph/uart.h" #include "periph/uart.h"
#ifdef MODULE_UART0
#include "board_uart0.h"
#endif
/** /**
* manage the heap * manage the heap
*/ */
extern uint32_t _end; /* address of last used memory cell */ extern uint32_t _end; /* address of last used memory cell */
caddr_t heap_top = (caddr_t)&_end + 4; 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 * @brief Initialize NewLib, called by __libc_init_array() from the startup script
*/ */
void _init(void) 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) ...@@ -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) int _read_r(struct _reent *r, int fd, void *buffer, unsigned int count)
{ {
char c; #ifndef MODULE_UART0
char *buff = (char*)buffer; while (rx_buf.avail == 0) {
uart_read_blocking(STDIO, &c); mutex_lock(&uart_rx_mutex);
buff[0] = c; }
return 1; 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) ...@@ -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) int _write_r(struct _reent *r, int fd, const void *data, unsigned int count)
{ {
char *c = (char*)data; int i = 0;
for (int i = 0; i < count; i++) {
uart_write_blocking(STDIO, c[i]); while (i < count) {
uart_write_blocking(STDIO, ((char*)data)[i++]);
} }
return count;
return i;
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment