stm32f0: usart: allocate USART console object and expose FILE pointer

tags/0.1.0
S.J.R. van Schaik 8 years ago
parent 8af9ba9fa9
commit 40c0668d20
  1. 4
      include/console.h
  2. 39
      source/drivers/usart_console.c

@ -2,4 +2,6 @@
#include <stdio.h> #include <stdio.h>
void console_init(void); struct usart_console;
FILE *console_init(void);

@ -4,6 +4,7 @@
#include <libopencm3/cm3/scb.h> #include <libopencm3/cm3/scb.h>
#include <ctype.h> #include <ctype.h>
#include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -19,12 +20,12 @@
struct usart_console { struct usart_console {
char recv_buf[RECV_BUF_LEN]; char recv_buf[RECV_BUF_LEN];
void (* process_line)(void); void (* process_line)(void);
FILE *fp;
volatile size_t cur, next; volatile size_t cur, next;
uint32_t dev; uint32_t dev;
}; };
static struct usart_console *usart[4]; static struct usart_console *usart[4];
struct usart_console user_console;
static void usart_isr(struct usart_console *console) static void usart_isr(struct usart_console *console)
{ {
@ -168,32 +169,30 @@ static int usart_init(struct usart_console *console)
return 0; return 0;
} }
void console_init(void) FILE *console_init(void)
{ {
cookie_io_functions_t console_in = { cookie_io_functions_t fops = {
.read = usart_read, .read = usart_read,
.write = NULL,
.seek = NULL,
.close = NULL,
};
cookie_io_functions_t console_out = {
.read = NULL,
.write = usart_write, .write = usart_write,
.seek = NULL, .seek = NULL,
.close = NULL, .close = NULL,
}; };
struct usart_console *console;
if (!(console = malloc(sizeof *console)))
return NULL;
console->dev = USART1;
console->cur = 0;
console->next = 0;
console->process_line = NULL;
usart[0] = console;
usart_init(console);
user_console.dev = USART1; console->fp = fopencookie(console, "r+w", fops);
user_console.cur = 0;
user_console.next = 0;
usart[0] = &user_console;
usart_init(&user_console);
stdin = fopencookie(&user_console, "r", console_in); setbuf(console->fp, NULL);
stdout = fopencookie(&user_console, "w", console_out);
stderr = fopencookie(&user_console, "w", console_out);
setbuf(stdout, NULL); return console->fp;
//setlinebuf(stdout);
setbuf(stderr, NULL);
} }

Loading…
Cancel
Save