ioport: Add an ioctl to get the port state
authorMichel Pollet <buserror@gmail.com>
Tue, 31 Aug 2010 10:48:44 +0000 (11:48 +0100)
committerMichel Pollet <buserror@gmail.com>
Tue, 31 Aug 2010 10:48:44 +0000 (11:48 +0100)
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 <buserror@gmail.com>
simavr/sim/avr_ioport.c
simavr/sim/avr_ioport.h

index 555de85..be30652 100644 (file)
@@ -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;
index af20083..1fc2ba4 100644 (file)
@@ -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;