// This file is Copyright (c) 2020 Florent Kermarrec // License: BSD #include #include #include #include #include #include #include #include "settings.h" #include "zest.h" /*-----------------------------------------------------------------------*/ /* Uart */ /*-----------------------------------------------------------------------*/ void _putchar( char c ){ putchar(c); } static char *readstr(void) { char c[2]; static char s[64]; static int ptr = 0; if(readchar_nonblock()) { c[0] = getchar(); c[1] = 0; switch(c[0]) { case 0x7f: case 0x08: if(ptr > 0) { ptr--; fputs("\x08 \x08", stdout); } break; case 0x07: break; case '\r': case '\n': s[ptr] = 0x00; fputs("\n", stdout); ptr = 0; return s; default: if(ptr >= (sizeof(s) - 1)) break; fputs(c, stdout); s[ptr] = c[0]; ptr++; break; } } return NULL; } static char *get_token(char **str) { char *c, *d; c = (char *)strchr(*str, ' '); if(c == NULL) { d = *str; *str = *str+strlen(*str); return d; } *c = 0; d = *str; *str = c+1; return d; } static void prompt(void) { printf("\e[92;1mlitex-i2c-app\e[0m> "); } /*-----------------------------------------------------------------------*/ /* Help */ /*-----------------------------------------------------------------------*/ static void help(void) { puts("\nLiteX minimal demo app built "__DATE__" "__TIME__"\n"); puts("Available commands:"); puts("help - Show this command"); puts("reboot - Reboot CPU"); puts("helloc - Hello C"); puts("i2c_read - I2C Read"); } /*-----------------------------------------------------------------------*/ /* Commands */ /*-----------------------------------------------------------------------*/ static void reboot_cmd(void) { ctrl_reset_write(1); } extern void helloc(void); static void helloc_cmd(void) { printf("Hello C demo...\n"); helloc(); } /*-----------------------------------------------------------------------*/ /* Console service / Main */ /*-----------------------------------------------------------------------*/ static void console_service(void) { char *str; char *token; str = readstr(); if(str == NULL) return; token = get_token(&str); if(strcmp(token, "help") == 0) help(); else if(strcmp(token, "reboot") == 0) reboot_cmd(); else if(strcmp(token, "helloc") == 0) helloc_cmd(); prompt(); } extern zest_init_t zest_init_data; int main(void) { #ifdef CONFIG_CPU_HAS_INTERRUPT irq_setmask(0); irq_setie(1); #endif uart_init(); help(); prompt(); init_zest(BASE_ZEST, &zest_init_data); while(1) { console_service(); } return 0; }