1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00
RIOT/boards/native/drivers/native-uart0.c

98 lines
2.0 KiB
C
Raw Normal View History

2013-05-15 16:04:48 +02:00
/*
2013-06-03 19:37:22 +02:00
* native uart0 implementation
2013-05-15 16:04:48 +02:00
*/
2013-05-14 17:42:08 +02:00
#include <err.h>
#include <stdio.h>
#include <unistd.h>
2013-08-15 22:58:26 +02:00
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
2013-06-03 19:37:22 +02:00
2013-05-14 17:42:08 +02:00
#include <sys/select.h>
#include "cpu.h"
#include "debug.h"
#include "board_uart0.h"
#include "native_internal.h"
static int _native_uart_in;
2013-06-03 19:37:22 +02:00
2013-05-14 17:42:08 +02:00
fd_set _native_uart_rfds;
int uart0_puts(char *astring, int length)
2013-05-14 17:42:08 +02:00
{
2013-08-18 11:21:38 +02:00
int nwritten, offset;
nwritten = 0;
offset = 0;
while (
(length - offset > 0) && (
(nwritten = write(
STDOUT_FILENO,
astring+offset,
length-offset)
) > 0)
) {
2013-08-18 11:21:38 +02:00
offset += nwritten;
}
if (nwritten == -1) {
2013-08-15 22:58:26 +02:00
err(EXIT_FAILURE, "uart0_puts: write");
}
2013-08-18 11:21:38 +02:00
else if ((length > 0) && (nwritten == 0)) {
/* XXX: handle properly */
errx(EXIT_FAILURE, "uart0_puts: Could not write to stdout. I don't know what to do now.");
}
2013-08-15 22:58:26 +02:00
return length;
2013-05-14 17:42:08 +02:00
}
void _native_handle_uart0_input()
{
char buf[42];
int nread;
2013-07-16 17:57:52 +02:00
if (!FD_ISSET(_native_uart_in, &_native_rfds)) {
DEBUG("_native_handle_uart0_input - nothing to do\n");
return;
}
2013-06-03 19:37:22 +02:00
DEBUG("_native_handle_uart0_input\n");
2013-05-14 17:42:08 +02:00
2013-06-03 19:37:22 +02:00
nread = read(_native_uart_in, buf, sizeof(buf));
2013-05-14 17:42:08 +02:00
if (nread == -1) {
err(1, "_native_handle_uart0_input(): read()");
}
2013-10-26 20:06:35 +02:00
else if (nread == 0) {
/* XXX:
* preliminary resolution for this situation, will be coped
* with properly in #161 */
close(_native_uart_in);
_native_uart_in = -1;
printf("stdin closed");
2013-10-26 20:06:35 +02:00
}
2013-05-14 17:42:08 +02:00
for(int pos = 0; pos < nread; pos++) {
uart0_handle_incoming(buf[pos]);
}
uart0_notify_thread();
thread_yield();
2013-05-14 17:42:08 +02:00
}
2013-07-16 17:57:52 +02:00
int _native_set_uart_fds(void)
{
DEBUG("_native_set_uart_fds");
if (_native_uart_in != -1) {
FD_SET(_native_uart_in, &_native_rfds);
}
2013-07-16 17:57:52 +02:00
return _native_uart_in;
}
2013-05-14 17:42:08 +02:00
void _native_init_uart0()
{
2013-06-03 19:37:22 +02:00
_native_uart_in = STDIN_FILENO;
2013-05-14 17:42:08 +02:00
puts("RIOT native uart0 initialized.");
}