2012-02-03 06:08:17 -05:00
|
|
|
#include <uart.h>
|
|
|
|
#include <console.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2015-07-26 05:42:53 -04:00
|
|
|
FILE *stdin, *stdout, *stderr;
|
|
|
|
|
2012-02-03 06:08:17 -05:00
|
|
|
static console_write_hook write_hook;
|
|
|
|
static console_read_hook read_hook;
|
|
|
|
static console_read_nonblock_hook read_nonblock_hook;
|
|
|
|
|
|
|
|
void console_set_write_hook(console_write_hook h)
|
|
|
|
{
|
|
|
|
write_hook = h;
|
|
|
|
}
|
|
|
|
|
|
|
|
void console_set_read_hook(console_read_hook r, console_read_nonblock_hook rn)
|
|
|
|
{
|
|
|
|
read_hook = r;
|
|
|
|
read_nonblock_hook = rn;
|
|
|
|
}
|
|
|
|
|
2012-02-07 09:02:44 -05:00
|
|
|
int putchar(int c)
|
2012-02-03 06:08:17 -05:00
|
|
|
{
|
|
|
|
uart_write(c);
|
|
|
|
if(write_hook != NULL)
|
|
|
|
write_hook(c);
|
2012-02-07 09:02:44 -05:00
|
|
|
return c;
|
2012-02-03 06:08:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
char readchar(void)
|
|
|
|
{
|
|
|
|
while(1) {
|
|
|
|
if(uart_read_nonblock())
|
|
|
|
return uart_read();
|
|
|
|
if((read_nonblock_hook != NULL) && read_nonblock_hook())
|
|
|
|
return read_hook();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int readchar_nonblock(void)
|
|
|
|
{
|
|
|
|
return (uart_read_nonblock()
|
|
|
|
|| ((read_nonblock_hook != NULL) && read_nonblock_hook()));
|
|
|
|
}
|
|
|
|
|
|
|
|
int puts(const char *s)
|
|
|
|
{
|
|
|
|
while(*s) {
|
2012-02-07 09:02:44 -05:00
|
|
|
putchar(*s);
|
2012-02-03 06:08:17 -05:00
|
|
|
s++;
|
|
|
|
}
|
2012-02-07 09:02:44 -05:00
|
|
|
putchar('\n');
|
2012-02-03 06:08:17 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void putsnonl(const char *s)
|
|
|
|
{
|
|
|
|
while(*s) {
|
2012-02-07 09:02:44 -05:00
|
|
|
putchar(*s);
|
2012-02-03 06:08:17 -05:00
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-26 05:42:53 -04:00
|
|
|
#define PRINTF_BUFFER_SIZE 256
|
|
|
|
|
2012-02-03 06:08:17 -05:00
|
|
|
int printf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int len;
|
2015-07-26 05:42:53 -04:00
|
|
|
char outbuf[PRINTF_BUFFER_SIZE];
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
len = vscnprintf(outbuf, sizeof(outbuf), fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
outbuf[len] = 0;
|
|
|
|
putsnonl(outbuf);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|