* initial posix_io support
parent
02e26536f0
commit
948e430148
@ -0,0 +1,8 @@
|
||||
#ifndef __UART0_H
|
||||
#define __UART0_H
|
||||
|
||||
#define UART0_BUFSIZE 32
|
||||
|
||||
extern int uart0_handler_pid;
|
||||
|
||||
#endif /* __UART0_H */
|
@ -0,0 +1,101 @@
|
||||
#include <thread.h>
|
||||
#include <kernel.h>
|
||||
#include <msg.h>
|
||||
#include <board.h>
|
||||
#include <ringbuffer.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <irq.h>
|
||||
#include <posix_io.h>
|
||||
#include "uart0.h"
|
||||
|
||||
//#define ENABLE_DEBUG
|
||||
#include <debug.h>
|
||||
|
||||
static char buffer[UART0_BUFSIZE];
|
||||
ringbuffer uart0_ringbuffer;
|
||||
|
||||
static void uart0_loop();
|
||||
|
||||
void uart0_init() {
|
||||
ringbuffer_init(&uart0_ringbuffer, buffer, UART0_BUFSIZE);
|
||||
int pid = thread_create(KERNEL_CONF_STACKSIZE_MAIN, PRIORITY_MAIN-1, CREATE_STACKTEST, uart0_loop, "uart0");
|
||||
uart0_handler_pid = pid;
|
||||
puts("uart0_init() [OK]");
|
||||
}
|
||||
|
||||
static int min(int a, int b) {
|
||||
if (b>a) return a;
|
||||
else return b;
|
||||
}
|
||||
|
||||
static void uart0_loop() {
|
||||
msg m;
|
||||
|
||||
int pid = thread_getpid();
|
||||
|
||||
int reader_pid = -1;
|
||||
struct posix_iop *r = NULL;
|
||||
|
||||
puts("UART0 thread started.");
|
||||
|
||||
while (1) {
|
||||
msg_receive(&m);
|
||||
|
||||
if (m.sender_pid == pid) {
|
||||
} else {
|
||||
switch (m.type) {
|
||||
case OPEN:
|
||||
if (reader_pid == -1) {
|
||||
reader_pid = m.sender_pid;
|
||||
m.content.value = 0; // no error
|
||||
} else {
|
||||
m.content.value = -EBUSY;
|
||||
}
|
||||
msg_reply(&m,&m);
|
||||
break;
|
||||
case READ:
|
||||
if (m.sender_pid != reader_pid) {
|
||||
m.content.value = -EINVAL;
|
||||
msg_reply(&m, &m);
|
||||
} else {
|
||||
r = (struct posix_iop *)m.content.ptr;
|
||||
}
|
||||
break;
|
||||
case CLOSE:
|
||||
if (m.sender_pid == reader_pid) {
|
||||
DEBUG("uart0_thread: closing file from %i\n", reader_pid);
|
||||
reader_pid = -1;
|
||||
r = NULL;
|
||||
m.content.value = 0;
|
||||
} else {
|
||||
m.content.value = -EINVAL;
|
||||
}
|
||||
msg_reply(&m,&m);
|
||||
break;
|
||||
default:
|
||||
m.content.value = -EINVAL;
|
||||
msg_reply(&m, &m);
|
||||
}
|
||||
}
|
||||
|
||||
if (uart0_ringbuffer.avail && (r != NULL)) {
|
||||
int state = disableIRQ();
|
||||
int nbytes = min(r->nbytes, uart0_ringbuffer.avail);
|
||||
DEBUG("uart0_thread: sending %i bytes to pid %i\n", nbytes, reader_pid);
|
||||
rb_get_elements(&uart0_ringbuffer, r->buffer, nbytes);
|
||||
r->nbytes = nbytes;
|
||||
|
||||
m.sender_pid = reader_pid;
|
||||
m.type = OPEN;
|
||||
m.content.ptr = (char*)r;
|
||||
|
||||
msg_reply(&m, &m);
|
||||
// DEBUG("uart0_thread: sending res=%i\n", res);
|
||||
|
||||
r = NULL;
|
||||
restoreIRQ(state);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
#ifndef __READ_H
|
||||
#define __READ_H
|
||||
|
||||
#define OPEN 0
|
||||
#define CLOSE 1
|
||||
#define READ 2
|
||||
#define WRITE 3
|
||||
|
||||
struct posix_iop {
|
||||
int nbytes;
|
||||
char *buffer;
|
||||
};
|
||||
|
||||
int posix_open(int pid, int flags);
|
||||
int posix_close(int pid);
|
||||
int posix_read(int pid, char* buffer, int bufsize);
|
||||
int posix_write(int pid, char* buffer, int bufsize);
|
||||
|
||||
#endif /* __READ_H */
|
@ -0,0 +1,42 @@
|
||||
#include <msg.h>
|
||||
|
||||
#include <posix_io.h>
|
||||
|
||||
|
||||
static int _posix_fileop(int pid, int op, int flags) {
|
||||
msg m;
|
||||
m.type = op;
|
||||
m.content.value = flags;
|
||||
msg_send_receive(&m, &m, pid);
|
||||
return m.content.value;
|
||||
}
|
||||
|
||||
static int _posix_fileop_data(int pid, int op, char* buffer, int nbytes) {
|
||||
struct posix_iop r;
|
||||
r.nbytes = nbytes;
|
||||
r.buffer = buffer;
|
||||
|
||||
msg m;
|
||||
m.type = op;
|
||||
m.content.ptr = (char*) &r;
|
||||
|
||||
msg_send_receive(&m, &m, pid);
|
||||
|
||||
return r.nbytes;
|
||||
}
|
||||
|
||||
int posix_open(int pid, int flags) {
|
||||
return _posix_fileop(pid, OPEN, flags);
|
||||
}
|
||||
|
||||
int posix_close(int pid) {
|
||||
return _posix_fileop(pid, CLOSE, 0);
|
||||
}
|
||||
|
||||
int posix_read(int pid, char* buffer, int bufsize) {
|
||||
return _posix_fileop_data(pid, READ, buffer, bufsize);
|
||||
}
|
||||
|
||||
int posix_write(int pid, char* buffer, int bufsize) {
|
||||
return _posix_fileop_data(pid, WRITE, buffer, bufsize);
|
||||
}
|
@ -1,98 +0,0 @@
|
||||
/******************************************************************************
|
||||
Copyright 2009, Freie Universitaet Berlin (FUB). All rights reserved.
|
||||
|
||||
These sources were developed at the Freie Universitaet Berlin, Computer Systems
|
||||
and Telematics group (http://cst.mi.fu-berlin.de).
|
||||
-------------------------------------------------------------------------------
|
||||
This file is part of FeuerWare.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify it under
|
||||
the terms of the GNU General Public License as published by the Free Software
|
||||
Foundation, either version 3 of the License, or (at your option) any later
|
||||
version.
|
||||
|
||||
FeuerWare is distributed in the hope that it will be useful, but WITHOUT
|
||||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along with
|
||||
this program. If not, see http://www.gnu.org/licenses/ .
|
||||
--------------------------------------------------------------------------------
|
||||
For further information and questions please use the web site
|
||||
http://scatterweb.mi.fu-berlin.de
|
||||
and the mailinglist (subscription via web site)
|
||||
scatterweb@lists.spline.inf.fu-berlin.de
|
||||
*******************************************************************************/
|
||||
#include <ringbuffer.h>
|
||||
#include <mutex.h>
|
||||
#include <stdbool.h>
|
||||
#include <kernel.h>
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @ingroup simple_shell
|
||||
* @brief contains implementation of synchronous read functions,
|
||||
* @brief hardcoded to uart0
|
||||
* @author Kaspar Schleiser
|
||||
*
|
||||
*/
|
||||
|
||||
extern void (*uart0_callback)(int);
|
||||
|
||||
#define UART0_BUFSIZE 255
|
||||
static ringbuffer uart0_rb;
|
||||
static char uart0_buffer[UART0_BUFSIZE];
|
||||
static char uart0_waiting = 0;
|
||||
|
||||
static mutex_t uart0_mutex;
|
||||
|
||||
void uart0_process_byte(int c) {
|
||||
rb_add_element(&uart0_rb, c);
|
||||
|
||||
if (uart0_waiting) {
|
||||
uart0_waiting = 0;
|
||||
mutex_unlock(&uart0_mutex, false);
|
||||
}
|
||||
}
|
||||
|
||||
void uart0_init() {
|
||||
dINT();
|
||||
mutex_init(&uart0_mutex);
|
||||
mutex_lock(&uart0_mutex);
|
||||
ringbuffer_init(&uart0_rb, uart0_buffer, UART0_BUFSIZE);
|
||||
uart0_callback = uart0_process_byte;
|
||||
eINT();
|
||||
}
|
||||
|
||||
/** @brief read a char from uart0. Block if none available. */
|
||||
int uart0_readc() {
|
||||
dINT();
|
||||
|
||||
if (uart0_rb.avail == 0) {
|
||||
uart0_waiting++;
|
||||
mutex_lock(&uart0_mutex);
|
||||
}
|
||||
|
||||
int c = rb_get_element(&uart0_rb);
|
||||
|
||||
eINT();
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
/** @brief read a char from uart0. return -1 if none available */
|
||||
int uart0_readc_nb() {
|
||||
dINT();
|
||||
|
||||
if (uart0_rb.avail == 0) {
|
||||
eINT();
|
||||
return -1;
|
||||
}
|
||||
|
||||
int c = rb_get_element(&uart0_rb);
|
||||
|
||||
eINT();
|
||||
|
||||
return c;
|
||||
}
|
||||
|
Loading…
Reference in New Issue