1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

* refactored uart / chardev handling

This commit is contained in:
Kaspar Schleiser 2010-09-30 15:10:39 +02:00
parent 867fd7f293
commit b8ca7edac8
11 changed files with 147 additions and 113 deletions

View File

@ -3,4 +3,5 @@ SubDir TOP board msba2 drivers ;
Module board_cc1100 : msba2-cc1100.c ;
Module board_hal : msba2-hal.c ;
Module board_ltc4150 : msba2-ltc4150.c : gpioint ;
Module board_common : msba2-uart0.c msba2-uart0_thread.c : ringbuffer ;
Module board_common : msba2-uart0.c : ringbuffer ;
Module board_uart : msba2-uart0_thread.c : chardev_thread ;

View File

@ -110,11 +110,9 @@ int uart_active(void){
}
static void notify_handler() {
if (uart0_handler_pid) {
msg m;
m.type = 0;
msg_send_int(&m, uart0_handler_pid);
}
msg m;
m.type = 0;
msg_send_int(&m, uart0_handler_pid);
}
void stdio_flush(void)
@ -142,14 +140,15 @@ void UART0_IRQHandler(void)
case UIIR_CTI_INT: // Character Timeout Indicator
case UIIR_RDA_INT: // Receive Data Available
do {
int c = U0RBR;
rb_add_element(&uart0_ringbuffer, c);
} while (U0LSR & ULSR_RDR);
notify_handler();
break;
if (uart0_handler_pid) {
do {
int c = U0RBR;
rb_add_element(&uart0_ringbuffer, c);
} while (U0LSR & ULSR_RDR);
notify_handler();
break;
}
default:
U0LSR;
U0RBR;

View File

@ -1,101 +1,25 @@
#include <thread.h>
#include <kernel.h>
#include <msg.h>
#include <board.h>
#include <chardev_thread.h>
#include <ringbuffer.h>
#include <stdio.h>
#include <errno.h>
#include <irq.h>
#include <posix_io.h>
#include "uart0.h"
#include <thread.h>
//#define ENABLE_DEBUG
#include <debug.h>
#include <board_uart0.h>
#define UART0_BUFSIZE 32
extern ringbuffer uart0_ringbuffer;
extern int uart0_handler_pid;
static char buffer[UART0_BUFSIZE];
ringbuffer uart0_ringbuffer;
static void uart0_loop();
static void uart0_loop() {
chardev_loop(&uart0_ringbuffer);
}
void uart0_init() {
void board_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);
}
}
}

View File

@ -6,6 +6,6 @@
SubDir TOP projects test_shell ;
Module test_shell : test_shell.c : shell posix_io shell_auto_init ps ;
Module test_shell : test_shell.c : shell posix_io shell_auto_init ps board_uart ;
UseModule test_shell ;

View File

@ -6,9 +6,9 @@
#include <string.h>
#include <malloc.h>
#include <uart0.h>
#include <posix_io.h>
#include <shell.h>
#include <board_uart0.h>
void print_teststart(char* str) {
printf("[TEST_START]\n");
@ -27,16 +27,20 @@ int shell_readc() {
return c;
}
void shell_putchar(int c) {
putchar(c);
}
int main(void) {
//printf("Moin. build on %s %s SVN-Revision: %s\n", kernel_builddate, kernel_buildtime, kernel_svnrevision);
printf("test_shell.\n");
uart0_init();
board_uart0_init();
posix_open(uart0_handler_pid, 0);
shell_t shell;
shell_init(&shell, shell_readc);
shell_init(&shell, shell_readc, shell_putchar);
shell_auto_init(&shell);
shell_register_cmd(&shell, "start_test", print_teststart);

View File

@ -32,6 +32,8 @@ Module posix_io : posix_io.c ;
Module auto_init : auto_init.c ;
Module chardev_thread : chardev_thread.c : ringbuffer ;
SubInclude TOP sys net ;
SubInclude TOP sys lib ;
SubInclude TOP sys shell ;

87
sys/chardev_thread.c Normal file
View File

@ -0,0 +1,87 @@
#include <thread.h>
#include <kernel.h>
#include <msg.h>
#include <ringbuffer.h>
#include <stdio.h>
#include <errno.h>
#include <irq.h>
#include <posix_io.h>
//#define ENABLE_DEBUG
#include <debug.h>
static int min(int a, int b) {
if (b>a) return a;
else return b;
}
void chardev_loop(ringbuffer *rb) {
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 (rb->avail && (r != NULL)) {
int state = disableIRQ();
int nbytes = min(r->nbytes, rb->avail);
DEBUG("uart0_thread: sending %i bytes to pid %i\n", nbytes, reader_pid);
rb_get_elements(rb, 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);
}
}
}

View File

@ -0,0 +1,8 @@
#ifndef __BOARD_UART0_H
#define __BOARD_UART0_H
extern int uart0_handler_pid;
void board_uart0_init();
#endif /* __BOARD_UART0_H */

View File

@ -0,0 +1,8 @@
#ifndef __CHARDEV_THREAD_H
#define __CHARDEV_THREAD_H
#include <ringbuffer.h>
void chardev_loop(ringbuffer *rb);
#endif /* __CHARDEV_THREAD_H */

View File

@ -24,8 +24,8 @@ and the mailinglist (subscription via web site)
scatterweb@lists.spline.inf.fu-berlin.de
*******************************************************************************/
#ifndef __SIMPLE_SHELL_H
#define __SIMPLE_SHELL_H
#ifndef __SHELL_H
#define __SHELL_H
/**
* @defgroup shell Simple Shell Interpreter
@ -37,12 +37,13 @@ and the mailinglist (subscription via web site)
typedef struct shell_t {
struct hashtable *h;
int (*readchar)(void);
void (*put_char)(int);
} shell_t;
/**
* @brief Initialize a shell object
*/
void shell_init(shell_t *shell, int(*readchar)(void));
void shell_init(shell_t *shell, int(*read_char)(void), void (*put_char)(int));
/**
* @brief Register a new command handler for a shell.
@ -60,4 +61,4 @@ void shell_run(shell_t *shell);
void shell_auto_init(shell_t *shell);
#endif /* __SIMPLE_SHELL_H */
#endif /* __SHELL_H */

View File

@ -77,8 +77,7 @@ int readline(shell_t *shell, char* buf, int size) {
}
c = shell->readchar();
write(STDOUT_FILENO, &c, 1);
shell->put_char(c);
if (c == 13) continue;
@ -95,7 +94,7 @@ void shell_run(shell_t *shell) {
char line_buf[255];
while(1) {
write(STDOUT_FILENO, ">", 1);
shell->put_char('>');
int res = readline(shell, line_buf, sizeof(line_buf));
if (! res ) {
handle_input_line(shell, strdup(line_buf));
@ -103,9 +102,10 @@ void shell_run(shell_t *shell) {
}
}
void shell_init(shell_t *shell, int(*readchar)(void)) {
void shell_init(shell_t *shell, int(*readchar)(void), void(*put_char)(int)) {
shell->h = create_hashtable(16, (unsigned int (*)(void*)) hash_string, (int (*) (void*,void*)) cmp_string);
shell->readchar = readchar;
shell->put_char = put_char;
}
void shell_register_cmd(shell_t *shell, char* name, void (*handler)(char* args)) {