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) {
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;
#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;