I tried hard to understand the code examples of driving the gpio pins at http://elinux.org/RPi_Low-level_periphe ... e_examples
. I must admit that I only have a vague idea of what (e.g.) a driver is. I would really appreciate it if someone could clarify and correct some things:
I think I understood the shell example: essentially there is a driver that makes the gpio pins accessible through a pseudo file system /sys/class/gpio/. Moreover this is a standard linux driver, not specific to Raspberry Pi.
The difficult part is the C example. Combining information from the datasheet, this code, Gordon Henderson's wiringPi code and some man pages, I ended up with some superficial understanding.
The SoC contains several control and access registers to the peripherals, that can be accessed as if they were in physical memory starting at address 0x20000000 (BCM2708_PERI_BASE), virtual memory address 0xF2000000, or bus address 0x7E000000. Starting from this base address, the GPIO registers start at location 0x2000000. The datasheet lists bus addresses, but the code example uses physical memory addresses. Physical memory is accessed through the file /dev/mem, but this is accessed through an mmap-ed piece of heap memory.
Why is that? Is that the only way, or is it for performance or convenience? Also it looks like what is mapped is a chunk of size BLOCK_SIZE that starts at an address that is a multiple of PAGE_SIZE. Where do the values of BLOCK_SIZE and PAGE_SIZE come from? Why is a chunk of size BLOCK_SIZE mapped? It seems to be much more than needed. Why does the pointer have to be a multiple of PAGE_SIZE? For performance?
Many thanks in advance!