Userspace I/O

From krtkl wiki
Jump to: navigation, search

Boot Arguments

uio_pdrv_genirq.of_id=krtkl,generic-uio,ui_pdrv

Device Tree Bindings

axi_gpio0: gpio@41200000 {
        reg = <0x41200000 0x10000>;
        compatible = "krtkl,generic-uio,ui_pdrv";
        interrupt-parent = <&intc>;
        interrupts = <0 29 4>; 
};

Application Interface

/* Open the UIO device */
fd = open("/dev/uio0", O_RDWR | O_SYNC);

/* Map the memory */
iomem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

/* Close the UIO device */
close(fd);

/* Point structure to memory */
dev = (struct peripheral *)iomem;

/* Perform read/write operations using structure pointer */
dev->ier = (1 << 0);

/* Unmask the interrupt */
en = 1;
write(fd, &en, sizeof(en));

/* Wait for an interrupt with blocking read */
read(fd, &nirq, sizeof(nirq));

/* Handle the interrupt */
if (nirq) {
        stat = dev->isr;           /* Read interrupt  status */
        if (stat)
                dev->isr = stat;   /* Clear interrupt on device */

        data = dev->data;
}

/* Unmap the memory when finished */
munmap(iomem, size);

fd = open("/dev/uio0", O_RDWR | O_SYNC);

/* Map the Nth memory map by specifying offset of N * PAGE_SIZE */
iomem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, PAGE_SIZE);

close(fd);

/* Perform memory operations */
msync(iomem, size, MS_SYNC);

munmap(iomem, size);