From c3710ec139bd6740fabc675bff381834bf080b64 Mon Sep 17 00:00:00 2001 From: Florent Kermarrec Date: Wed, 28 Jun 2017 17:38:09 +0200 Subject: [PATCH] build/sim: cleanup serial2console and fix terminal mode --- .../modules/serial2console/serial2console.c | 101 +++++++----------- 1 file changed, 40 insertions(+), 61 deletions(-) diff --git a/litex/build/sim/core/modules/serial2console/serial2console.c b/litex/build/sim/core/modules/serial2console/serial2console.c index ebdffe147..8d49ff5df 100644 --- a/litex/build/sim/core/modules/serial2console/serial2console.c +++ b/litex/build/sim/core/modules/serial2console/serial2console.c @@ -25,30 +25,28 @@ struct session_s { }; struct event_base *base; -static int litex_sim_module_pads_get( struct pad_s *pads, char *name, void **signal) +static int litex_sim_module_pads_get(struct pad_s *pads, char *name, void **signal) { int ret; - void *sig=NULL; + void *sig = NULL; int i; - if(!pads || !name || !signal) - { + if(!pads || !name || !signal) { ret=RC_INVARG; goto out; } + i = 0; - while(pads[i].name) - { - if(!strcmp(pads[i].name, name)) - { - sig=(void*)pads[i].signal; + while(pads[i].name) { + if(!strcmp(pads[i].name, name)) { + sig = (void*)pads[i].signal; break; } i++; } out: - *signal=sig; + *signal = sig; return ret; } @@ -57,38 +55,34 @@ void set_conio_terminal_mode(void) struct termios new_termios; tcgetattr(0, &new_termios); - cfmakeraw(&new_termios); + new_termios.c_lflag &= ~(ECHO | ICANON); tcsetattr(0, TCSANOW, &new_termios); } static int serial2console_start(void *b) { - base =(struct event_base *)b; - printf("Loaded %p!\n", base); - // set_conio_terminal_mode(); + base = (struct event_base *)b; + set_conio_terminal_mode(); return RC_OK; } void read_handler(int fd, short event, void *arg) { - struct session_s *s= (struct session_s*)arg; + struct session_s *s = (struct session_s*)arg; char buffer[1024]; ssize_t read_len; int i; read_len = read(fd, buffer, 1024); - for(i = 0; i < read_len; i++) - { - s->databuf[(s->data_start + s->datalen ) % 2048] = buffer[i]; + for(i = 0; i < read_len; i++) { + s->databuf[(s->data_start + s->datalen ) % 2048] = buffer[i]; s->datalen++; } } static void event_handler(int fd, short event, void *arg) { - //printf("hit\n"); - if (event & EV_READ) - { + if (event & EV_READ) { read_handler(fd, event, arg); } } @@ -97,43 +91,39 @@ static int serial2console_new(void **sess, char *args) { int ret=RC_OK; struct timeval tv = {1, 0}; - struct session_s *s=NULL; + struct session_s *s = NULL; - if(!sess) - { + if(!sess) { ret = RC_INVARG; goto out; } - - s=(struct session_s*)malloc(sizeof(struct session_s)); - if(!s) - { + + s = (struct session_s*) malloc(sizeof(struct session_s)); + if(!s) { ret=RC_NOENMEM; goto out; } memset(s, 0, sizeof(struct session_s)); s->ev = event_new(base, fileno(stdin), EV_READ | EV_PERSIST , event_handler, s); event_add(s->ev, &tv); - + out: - *sess=(void*)s; + *sess = (void*) s; return ret; - } static int serial2console_add_pads(void *sess, struct pad_list_s *plist) { - int ret=RC_OK; - struct session_s *s=(struct session_s*)sess; + int ret = RC_OK; + struct session_s *s = (struct session_s*) sess; struct pad_s *pads; - if(!sess || !plist) - { + + if(!sess || !plist) { ret = RC_INVARG; goto out; } pads = plist->pads; - if(!strcmp(plist->name, "serial")) - { + if(!strcmp(plist->name, "serial")) { litex_sim_module_pads_get(pads, "sink_data", (void**)&s->rx); litex_sim_module_pads_get(pads, "sink_valid", (void**)&s->rx_valid); litex_sim_module_pads_get(pads, "sink_ready", (void**)&s->rx_ready); @@ -142,47 +132,36 @@ static int serial2console_add_pads(void *sess, struct pad_list_s *plist) litex_sim_module_pads_get(pads, "source_ready", (void**)&s->tx_ready); } - if(!strcmp(plist->name, "sys_clk")) - { - litex_sim_module_pads_get(pads, "sys_clk", (void**)&s->sys_clk); - - } + if(!strcmp(plist->name, "sys_clk")) + litex_sim_module_pads_get(pads, "sys_clk", (void**) &s->sys_clk); out: return ret; } -static int serial2console_tick(void *sess) -{ +static int serial2console_tick(void *sess) { + struct session_s *s = (struct session_s*)sess; - struct session_s *s=(struct session_s*)sess; - if(*s->sys_clk == 0) - { + if(*s->sys_clk == 0) { return RC_OK; } + *s->tx_ready = 1; - if(*s->tx_valid) - { + if(*s->tx_valid) { if(*s->tx == '\n') - { printf("\r"); - } printf("%c", *s->tx); fflush(stdout); } - - - *s->rx_valid=0; - if(s->datalen) - { - + *s->rx_valid = 0; + if(s->datalen) { *s->rx=s->databuf[s->data_start]; s->data_start = (s->data_start + 1) % 2048; s->datalen--; - *s->rx_valid=1; + *s->rx_valid = 1; } - + return RC_OK; } @@ -195,9 +174,9 @@ static struct ext_module_s ext_mod = { serial2console_tick }; -int litex_sim_ext_module_init(int (*register_module)(struct ext_module_s *)) +int litex_sim_ext_module_init(int (*register_module) (struct ext_module_s *)) { - int ret=RC_OK; + int ret = RC_OK; ret = register_module(&ext_mod); return ret; }