From: Michel Pollet Date: Tue, 31 Aug 2010 10:48:44 +0000 (+0100) Subject: ioport: Add an ioctl to get the port state X-Git-Tag: v1.0a5~2 X-Git-Url: https://git.htl-mechatronik.at/public/?a=commitdiff_plain;h=ad7ea664f377587f9e91b6c685e8bbedd8f489d4;p=sx%2Fsimavr.git ioport: Add an ioctl to get the port state Example: for (int i = 'A'; i <= 'F'; i++) { avr_ioport_state_t state; if (avr_ioctl(AVR_IOCTL_IOPORT_GETSTATE(i), &state) == 0) printf("PORT%c %02x DDR %02x PIN %02x\n", state.name, state.port, state.ddr, state.pin); } Signed-off-by: Michel Pollet --- diff --git a/simavr/sim/avr_ioport.c b/simavr/sim/avr_ioport.c index 555de85..be30652 100644 --- a/simavr/sim/avr_ioport.c +++ b/simavr/sim/avr_ioport.c @@ -103,6 +103,7 @@ static void avr_ioport_reset(avr_io_t * port) static int avr_ioport_ioctl(struct avr_io_t * port, uint32_t ctl, void * io_param) { avr_ioport_t * p = (avr_ioport_t *)port; + avr_t * avr = p->io.avr; int res = -1; switch(ctl) { @@ -125,6 +126,22 @@ static int avr_ioport_ioctl(struct avr_io_t * port, uint32_t ctl, void * io_para return o; } } break; + default: { + /* + * Return the port state if the IOCTL matches us. + */ + if (ctl == AVR_IOCTL_IOPORT_GETSTATE(p->name)) { + avr_ioport_state_t state = { + .name = p->name, + .port = avr->data[p->r_port], + .ddr = avr->data[p->r_ddr], + .pin = avr->data[p->r_pin], + }; + if (io_param) + *((avr_ioport_state_t*)io_param) = state; + res = 0; + } + } } return res; diff --git a/simavr/sim/avr_ioport.h b/simavr/sim/avr_ioport.h index af20083..1fc2ba4 100644 --- a/simavr/sim/avr_ioport.h +++ b/simavr/sim/avr_ioport.h @@ -45,6 +45,27 @@ typedef struct avr_ioport_getirq_t { #define AVR_IOCTL_IOPORT_GETIRQ_REGBIT AVR_IOCTL_DEF('i','o','g','r') +/* + * ioctl used to get a port state. + * + * for (int i = 'A'; i <= 'F'; i++) { + * avr_ioport_state_t state; + * if (avr_ioctl(AVR_IOCTL_IOPORT_GETSTATE(i), &state) == 0) + * printf("PORT%c %02x DDR %02x PIN %02x\n", + * state.name, state.port, state.ddr, state.pin); + * } + */ +typedef struct avr_ioport_state_t { + unsigned long name : 7, + port : 8, ddr : 8, pin : 8; +} avr_ioport_state_t; + +// add port name (uppercase) to get the port state +#define AVR_IOCTL_IOPORT_GETSTATE(_name) AVR_IOCTL_DEF('i','o','s',(_name)) + +/* + * Definition for an IO port + */ typedef struct avr_ioport_t { avr_io_t io; char name;