Non-blocking console mode introduced
parent
17e9a5cc26
commit
3ce7042421
|
@ -0,0 +1,110 @@
|
|||
#include <uart.h>
|
||||
#include <console.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
FILE *stdin, *stdout, *stderr;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// Non-blocking output permissible
|
||||
#define CONSOLE_NON_BLOCKING 1
|
||||
#ifdef CONSOLE_NON_BLOCKING
|
||||
#define MAX_WO_RESET 80
|
||||
static int output_count = 0;
|
||||
static int console_non_blocking = 0;
|
||||
|
||||
void set_console(int non_blocking)
|
||||
{
|
||||
console_non_blocking = non_blocking;
|
||||
}
|
||||
#endif
|
||||
|
||||
int putchar(int c)
|
||||
{
|
||||
uart_write(c);
|
||||
if(write_hook != NULL)
|
||||
write_hook(c);
|
||||
if (c == '\n')
|
||||
putchar('\r');
|
||||
#ifdef CONSOLE_NON_BLOCKING
|
||||
if(console_non_blocking != 0) {
|
||||
if(++output_count > MAX_WO_RESET) {
|
||||
extern void busy_wait(int);
|
||||
busy_wait(100); // Make sure, output has been sent (115200/8 -> 14400Byte -> 55ms/80Byte)
|
||||
uart_init(); // 'Unblock' ;-)
|
||||
output_count = 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return c;
|
||||
}
|
||||
|
||||
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) {
|
||||
putchar(*s);
|
||||
s++;
|
||||
}
|
||||
putchar('\n');
|
||||
return 1;
|
||||
}
|
||||
|
||||
void putsnonl(const char *s)
|
||||
{
|
||||
while(*s) {
|
||||
putchar(*s);
|
||||
s++;
|
||||
}
|
||||
}
|
||||
|
||||
#define PRINTF_BUFFER_SIZE 256
|
||||
|
||||
int vprintf(const char *fmt, va_list args)
|
||||
{
|
||||
int len;
|
||||
char outbuf[PRINTF_BUFFER_SIZE];
|
||||
len = vscnprintf(outbuf, sizeof(outbuf), fmt, args);
|
||||
outbuf[len] = 0;
|
||||
putsnonl(outbuf);
|
||||
return len;
|
||||
}
|
||||
|
||||
int printf(const char *fmt, ...)
|
||||
{
|
||||
int len;
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
len = vprintf(fmt, args);
|
||||
va_end(args);
|
||||
return len;
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
#include <uart.h>
|
||||
#include <console.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
FILE *stdin, *stdout, *stderr;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int putchar(int c)
|
||||
{
|
||||
uart_write(c);
|
||||
if(write_hook != NULL)
|
||||
write_hook(c);
|
||||
if (c == '\n')
|
||||
putchar('\r');
|
||||
return c;
|
||||
}
|
||||
|
||||
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) {
|
||||
putchar(*s);
|
||||
s++;
|
||||
}
|
||||
putchar('\n');
|
||||
return 1;
|
||||
}
|
||||
|
||||
void putsnonl(const char *s)
|
||||
{
|
||||
while(*s) {
|
||||
putchar(*s);
|
||||
s++;
|
||||
}
|
||||
}
|
||||
|
||||
#define PRINTF_BUFFER_SIZE 256
|
||||
|
||||
int vprintf(const char *fmt, va_list args)
|
||||
{
|
||||
int len;
|
||||
char outbuf[PRINTF_BUFFER_SIZE];
|
||||
len = vscnprintf(outbuf, sizeof(outbuf), fmt, args);
|
||||
outbuf[len] = 0;
|
||||
putsnonl(outbuf);
|
||||
return len;
|
||||
}
|
||||
|
||||
int printf(const char *fmt, ...)
|
||||
{
|
||||
int len;
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
len = vprintf(fmt, args);
|
||||
va_end(args);
|
||||
return len;
|
||||
}
|
|
@ -324,7 +324,13 @@ int main(int i, char **c)
|
|||
hist_init();
|
||||
#endif
|
||||
printf("\n%s", PROMPT);
|
||||
|
||||
|
||||
#define CONSOLE_NON_BLOCKING 1
|
||||
#ifdef CONSOLE_NON_BLOCKING
|
||||
// Enable non-blocking output
|
||||
extern void set_console(int non_blocking);
|
||||
set_console(1); // 1=Non-blocking (0=standard/blocking)
|
||||
#endif
|
||||
while(1) {
|
||||
readline(buffer, CMD_LINE_BUFFER_SIZE);
|
||||
if (buffer[0] != 0) {
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -52,6 +52,11 @@ def copyjob():
|
|||
os.environ["PWD"] + relpath + "/firmware/",
|
||||
"cmd_mem.c"
|
||||
)
|
||||
copyjob_actual(
|
||||
os.path.dirname(litex.soc.__file__) + "/software/libbase/",
|
||||
os.environ["PWD"] + relpath + "/firmware/",
|
||||
"console.c"
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
_, tail = os.path.split(os.getcwd())
|
||||
|
|
|
@ -148,7 +148,5 @@ void illumination(void)
|
|||
busy_wait(500);
|
||||
enable_LEDS(0);
|
||||
printf("Finished!\n");
|
||||
busy_wait(1000);
|
||||
uart_init(); // Clear buffers ...
|
||||
}
|
||||
|
||||
|
|
|
@ -70,6 +70,11 @@ int main(int i, char **c)
|
|||
");
|
||||
if((cpc >= MAIN_RAM_BASE) && (cpc <= (MAIN_RAM_BASE + MAIN_RAM_SIZE))) {
|
||||
printf("\e[1mExecuting within RAM ...\e[0m\n");
|
||||
#define CONSOLE_NON_BLOCKING 1
|
||||
#ifdef CONSOLE_NON_BLOCKING
|
||||
extern void set_console(int non_blocking);
|
||||
set_console(1); // 1=Non-blocking (0=standard/blocking)
|
||||
#endif
|
||||
while(1)
|
||||
illumination(); // The DEMO !
|
||||
while(1) {
|
||||
|
|
Loading…
Reference in New Issue