Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

sys: newlib: use double-lock scheme for uart ringbuffer #3111

Merged
merged 1 commit into from
Jun 1, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions sys/newlib/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ caddr_t heap_top = (caddr_t)&_sheap + 4;
/**
* @brief use mutex for waiting on incoming UART chars
*/
static mutex_t uart_rx_mutex;
static mutex_t uart_rx_mutex = MUTEX_INIT;
static char rx_buf_mem[STDIO_RX_BUFSIZE];
static ringbuffer_t rx_buf;
#endif
Expand All @@ -65,16 +65,14 @@ static ringbuffer_t rx_buf;
void rx_cb(void *arg, char data)
{
(void)arg;

#ifdef MODULE_UART0
#ifndef MODULE_UART0
ringbuffer_add_one(&rx_buf, data);
mutex_unlock(&uart_rx_mutex);
#else
if (uart0_handler_pid) {
uart0_handle_incoming(data);

uart0_notify_thread();
}
#else
ringbuffer_add_one(&rx_buf, data);
mutex_unlock(&uart_rx_mutex);
#endif
}

Expand All @@ -84,7 +82,7 @@ void rx_cb(void *arg, char data)
void _init(void)
{
#ifndef MODULE_UART0
mutex_init(&uart_rx_mutex);
mutex_lock(&uart_rx_mutex);
ringbuffer_init(&rx_buf, rx_buf_mem, STDIO_RX_BUFSIZE);
#endif
uart_init(STDIO, STDIO_BAUDRATE, rx_cb, 0, 0);
Expand Down Expand Up @@ -205,10 +203,11 @@ int _open_r(struct _reent *r, const char *name, int mode)
int _read_r(struct _reent *r, int fd, void *buffer, unsigned int count)
{
#ifndef MODULE_UART0
while (rx_buf.avail == 0) {
mutex_lock(&uart_rx_mutex);
}
return ringbuffer_get(&rx_buf, (char*)buffer, rx_buf.avail);
mutex_lock(&uart_rx_mutex);

count = count < rx_buf.avail ? count : rx_buf.avail;

return ringbuffer_get(&rx_buf, (char*)buffer, count);
#else
char *res = (char*)buffer;
res[0] = (char)uart0_readc();
Expand Down