build/sim: cleanup serial2console and fix terminal mode

This commit is contained in:
Florent Kermarrec 2017-06-28 17:38:09 +02:00
parent ebded4a1b9
commit c3710ec139
1 changed files with 40 additions and 61 deletions

View File

@ -25,30 +25,28 @@ struct session_s {
}; };
struct event_base *base; 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; int ret;
void *sig=NULL; void *sig = NULL;
int i; int i;
if(!pads || !name || !signal) if(!pads || !name || !signal) {
{
ret=RC_INVARG; ret=RC_INVARG;
goto out; goto out;
} }
i = 0; i = 0;
while(pads[i].name) while(pads[i].name) {
{ if(!strcmp(pads[i].name, name)) {
if(!strcmp(pads[i].name, name)) sig = (void*)pads[i].signal;
{
sig=(void*)pads[i].signal;
break; break;
} }
i++; i++;
} }
out: out:
*signal=sig; *signal = sig;
return ret; return ret;
} }
@ -57,38 +55,34 @@ void set_conio_terminal_mode(void)
struct termios new_termios; struct termios new_termios;
tcgetattr(0, &new_termios); tcgetattr(0, &new_termios);
cfmakeraw(&new_termios); new_termios.c_lflag &= ~(ECHO | ICANON);
tcsetattr(0, TCSANOW, &new_termios); tcsetattr(0, TCSANOW, &new_termios);
} }
static int serial2console_start(void *b) static int serial2console_start(void *b)
{ {
base =(struct event_base *)b; base = (struct event_base *)b;
printf("Loaded %p!\n", base); set_conio_terminal_mode();
// set_conio_terminal_mode();
return RC_OK; return RC_OK;
} }
void read_handler(int fd, short event, void *arg) 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]; char buffer[1024];
ssize_t read_len; ssize_t read_len;
int i; int i;
read_len = read(fd, buffer, 1024); read_len = read(fd, buffer, 1024);
for(i = 0; i < read_len; i++) for(i = 0; i < read_len; i++) {
{ s->databuf[(s->data_start + s->datalen ) % 2048] = buffer[i];
s->databuf[(s->data_start + s->datalen ) % 2048] = buffer[i];
s->datalen++; s->datalen++;
} }
} }
static void event_handler(int fd, short event, void *arg) 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); read_handler(fd, event, arg);
} }
} }
@ -97,17 +91,15 @@ static int serial2console_new(void **sess, char *args)
{ {
int ret=RC_OK; int ret=RC_OK;
struct timeval tv = {1, 0}; struct timeval tv = {1, 0};
struct session_s *s=NULL; struct session_s *s = NULL;
if(!sess) if(!sess) {
{
ret = RC_INVARG; ret = RC_INVARG;
goto out; goto out;
} }
s=(struct session_s*)malloc(sizeof(struct session_s)); s = (struct session_s*) malloc(sizeof(struct session_s));
if(!s) if(!s) {
{
ret=RC_NOENMEM; ret=RC_NOENMEM;
goto out; goto out;
} }
@ -116,24 +108,22 @@ static int serial2console_new(void **sess, char *args)
event_add(s->ev, &tv); event_add(s->ev, &tv);
out: out:
*sess=(void*)s; *sess = (void*) s;
return ret; return ret;
} }
static int serial2console_add_pads(void *sess, struct pad_list_s *plist) static int serial2console_add_pads(void *sess, struct pad_list_s *plist)
{ {
int ret=RC_OK; int ret = RC_OK;
struct session_s *s=(struct session_s*)sess; struct session_s *s = (struct session_s*) sess;
struct pad_s *pads; struct pad_s *pads;
if(!sess || !plist)
{ if(!sess || !plist) {
ret = RC_INVARG; ret = RC_INVARG;
goto out; goto out;
} }
pads = plist->pads; 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_data", (void**)&s->rx);
litex_sim_module_pads_get(pads, "sink_valid", (void**)&s->rx_valid); litex_sim_module_pads_get(pads, "sink_valid", (void**)&s->rx_valid);
litex_sim_module_pads_get(pads, "sink_ready", (void**)&s->rx_ready); litex_sim_module_pads_get(pads, "sink_ready", (void**)&s->rx_ready);
@ -143,44 +133,33 @@ static int serial2console_add_pads(void *sess, struct pad_list_s *plist)
} }
if(!strcmp(plist->name, "sys_clk")) if(!strcmp(plist->name, "sys_clk"))
{ litex_sim_module_pads_get(pads, "sys_clk", (void**) &s->sys_clk);
litex_sim_module_pads_get(pads, "sys_clk", (void**)&s->sys_clk);
}
out: out:
return ret; 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; return RC_OK;
} }
*s->tx_ready = 1; *s->tx_ready = 1;
if(*s->tx_valid) if(*s->tx_valid) {
{
if(*s->tx == '\n') if(*s->tx == '\n')
{
printf("\r"); printf("\r");
}
printf("%c", *s->tx); printf("%c", *s->tx);
fflush(stdout); fflush(stdout);
} }
*s->rx_valid = 0;
if(s->datalen) {
*s->rx_valid=0;
if(s->datalen)
{
*s->rx=s->databuf[s->data_start]; *s->rx=s->databuf[s->data_start];
s->data_start = (s->data_start + 1) % 2048; s->data_start = (s->data_start + 1) % 2048;
s->datalen--; s->datalen--;
*s->rx_valid=1; *s->rx_valid = 1;
} }
return RC_OK; return RC_OK;
@ -195,9 +174,9 @@ static struct ext_module_s ext_mod = {
serial2console_tick 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); ret = register_module(&ext_mod);
return ret; return ret;
} }