Difference between revisions of "Userspace I/O"

From krtkl wiki
Jump to: navigation, search
(Created page with "==== Boot Arguments ==== <pre>uio_pdrv_genirq.of_id=krtkl,generic-uio,ui_pdrv</pre> ==== Device Tree Bindings ==== <pre> axi_gpio0: gpio@41200000 { reg = <0x4120000...")
 
m (Application Interface)
Line 16: Line 16:
 
==== Application Interface ====
 
==== Application Interface ====
  
<pre>
+
<syntaxhighlight lang="c">
 
/* Open the UIO device */
 
/* Open the UIO device */
 
fd = open("/dev/uio0", O_RDWR | O_SYNC);
 
fd = open("/dev/uio0", O_RDWR | O_SYNC);
Line 65: Line 65:
  
 
munmap(iomem, size);
 
munmap(iomem, size);
</pre>
+
</syntaxhighlight>

Revision as of 04:10, 26 January 2018

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

/* Apply the offset to the memory */
iomem += offset / sizeof(void *);

/* 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);