2015-06-04 11:50:03 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 INRIA
|
|
|
|
* 2015 Kaspar Schleiser <kaspar@schleiser.de>
|
2016-07-11 06:33:55 +02:00
|
|
|
* 2016 Eistec AB
|
2018-07-05 14:04:17 +02:00
|
|
|
* 2018 Freie Universität Berlin
|
2015-06-04 11:50:03 +02:00
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2018-07-05 14:04:17 +02:00
|
|
|
* @ingroup sys
|
2015-06-04 11:50:03 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
2018-07-05 14:04:17 +02:00
|
|
|
* @brief STDIO over UART implementation
|
2015-06-04 11:50:03 +02:00
|
|
|
*
|
2018-07-05 14:04:17 +02:00
|
|
|
* This file implements a UART callback and the STDIO read/write functions
|
2015-06-04 11:50:03 +02:00
|
|
|
*
|
|
|
|
* @author Oliver Hahm <oliver.hahm@inria.fr>
|
2015-09-27 18:58:30 +02:00
|
|
|
* @author Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
|
2015-06-04 11:50:03 +02:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2016-07-11 06:33:55 +02:00
|
|
|
* @author Joakim Nohlgård <joakim.nohlgard@eistec.se>
|
2018-07-05 14:04:17 +02:00
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2015-06-04 11:50:03 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2016-07-11 06:33:55 +02:00
|
|
|
#if MODULE_VFS
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2018-07-05 14:04:17 +02:00
|
|
|
#include "stdio_uart.h"
|
2015-06-04 11:50:03 +02:00
|
|
|
|
|
|
|
#include "board.h"
|
2015-06-04 13:42:37 +02:00
|
|
|
#include "periph/uart.h"
|
2016-12-07 20:33:23 +01:00
|
|
|
#include "isrpipe.h"
|
2015-06-04 11:50:03 +02:00
|
|
|
|
2016-02-05 12:52:02 +01:00
|
|
|
#ifdef USE_ETHOS_FOR_STDIO
|
|
|
|
#include "ethos.h"
|
|
|
|
extern ethos_t ethos;
|
|
|
|
#endif
|
|
|
|
|
2016-07-11 06:33:55 +02:00
|
|
|
#if MODULE_VFS
|
|
|
|
#include "vfs.h"
|
|
|
|
#endif
|
|
|
|
|
2015-06-04 11:50:03 +02:00
|
|
|
#define ENABLE_DEBUG 0
|
|
|
|
#include "debug.h"
|
|
|
|
|
2018-07-05 14:04:17 +02:00
|
|
|
static char _rx_buf_mem[STDIO_UART_RX_BUFSIZE];
|
|
|
|
isrpipe_t stdio_uart_isrpipe = ISRPIPE_INIT(_rx_buf_mem);
|
2015-06-04 11:50:03 +02:00
|
|
|
|
2016-07-11 06:33:55 +02:00
|
|
|
#if MODULE_VFS
|
2018-07-05 14:04:17 +02:00
|
|
|
static ssize_t stdio_uart_vfs_read(vfs_file_t *filp, void *dest, size_t nbytes);
|
|
|
|
static ssize_t stdio_uart_vfs_write(vfs_file_t *filp, const void *src, size_t nbytes);
|
2016-07-11 06:33:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief VFS file operation table for stdin/stdout/stderr
|
|
|
|
*/
|
2018-07-05 14:04:17 +02:00
|
|
|
static vfs_file_ops_t stdio_uart_vfs_ops = {
|
|
|
|
.read = stdio_uart_vfs_read,
|
|
|
|
.write = stdio_uart_vfs_write,
|
2016-07-11 06:33:55 +02:00
|
|
|
};
|
|
|
|
|
2018-07-05 14:04:17 +02:00
|
|
|
static ssize_t stdio_uart_vfs_read(vfs_file_t *filp, void *dest, size_t nbytes)
|
2016-07-11 06:33:55 +02:00
|
|
|
{
|
|
|
|
int fd = filp->private_data.value;
|
|
|
|
if (fd != STDIN_FILENO) {
|
|
|
|
return -EBADF;
|
|
|
|
}
|
2018-07-05 14:09:08 +02:00
|
|
|
return stdio_read(dest, nbytes);
|
2016-07-11 06:33:55 +02:00
|
|
|
}
|
|
|
|
|
2018-07-05 14:04:17 +02:00
|
|
|
static ssize_t stdio_uart_vfs_write(vfs_file_t *filp, const void *src, size_t nbytes)
|
2016-07-11 06:33:55 +02:00
|
|
|
{
|
|
|
|
int fd = filp->private_data.value;
|
|
|
|
if (fd == STDIN_FILENO) {
|
|
|
|
return -EBADF;
|
|
|
|
}
|
2018-07-05 14:09:08 +02:00
|
|
|
return stdio_write(src, nbytes);
|
2016-07-11 06:33:55 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-07-05 14:04:17 +02:00
|
|
|
void stdio_init(void)
|
2015-06-04 11:50:03 +02:00
|
|
|
{
|
2016-02-05 12:52:02 +01:00
|
|
|
#ifndef USE_ETHOS_FOR_STDIO
|
2018-07-05 14:04:17 +02:00
|
|
|
uart_init(STDIO_UART_DEV, STDIO_UART_BAUDRATE, (uart_rx_cb_t) isrpipe_write_one, &stdio_uart_isrpipe);
|
2016-02-05 12:52:02 +01:00
|
|
|
#else
|
2018-07-05 14:04:17 +02:00
|
|
|
uart_init(ETHOS_UART, ETHOS_BAUDRATE, (uart_rx_cb_t) isrpipe_write_one, &stdio_uart_isrpipe);
|
2016-02-05 12:52:02 +01:00
|
|
|
#endif
|
2016-07-11 06:33:55 +02:00
|
|
|
#if MODULE_VFS
|
|
|
|
int fd;
|
2018-07-05 14:04:17 +02:00
|
|
|
fd = vfs_bind(STDIN_FILENO, O_RDONLY, &stdio_uart_vfs_ops, (void *)STDIN_FILENO);
|
2016-07-11 06:33:55 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
/* How to handle errors on init? */
|
|
|
|
}
|
2018-07-05 14:04:17 +02:00
|
|
|
fd = vfs_bind(STDOUT_FILENO, O_WRONLY, &stdio_uart_vfs_ops, (void *)STDOUT_FILENO);
|
2016-07-11 06:33:55 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
/* How to handle errors on init? */
|
|
|
|
}
|
2018-07-05 14:04:17 +02:00
|
|
|
fd = vfs_bind(STDERR_FILENO, O_WRONLY, &stdio_uart_vfs_ops, (void *)STDERR_FILENO);
|
2016-07-11 06:33:55 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
/* How to handle errors on init? */
|
|
|
|
}
|
|
|
|
#endif
|
2015-06-04 11:50:03 +02:00
|
|
|
}
|
|
|
|
|
2018-07-05 14:04:17 +02:00
|
|
|
ssize_t stdio_read(void* buffer, size_t count)
|
2015-06-04 11:50:03 +02:00
|
|
|
{
|
2018-07-05 14:04:17 +02:00
|
|
|
return (ssize_t)isrpipe_read(&stdio_uart_isrpipe, (char *)buffer, count);
|
2015-06-04 11:50:03 +02:00
|
|
|
}
|
|
|
|
|
2018-07-05 14:04:17 +02:00
|
|
|
ssize_t stdio_write(const void* buffer, size_t len)
|
2015-06-04 11:50:03 +02:00
|
|
|
{
|
2016-02-05 12:52:02 +01:00
|
|
|
#ifndef USE_ETHOS_FOR_STDIO
|
2018-07-05 14:04:17 +02:00
|
|
|
uart_write(STDIO_UART_DEV, (const uint8_t *)buffer, len);
|
2016-02-05 12:52:02 +01:00
|
|
|
#else
|
2017-06-29 13:02:02 +02:00
|
|
|
ethos_send_frame(ðos, (const uint8_t *)buffer, len, ETHOS_FRAME_TYPE_TEXT);
|
2016-02-05 12:52:02 +01:00
|
|
|
#endif
|
2015-06-04 11:50:03 +02:00
|
|
|
return len;
|
|
|
|
}
|