EBC Exercise 11b gpio via mmap

In previous exercises (EBC Exercise 10 Flashing an LED and EBC Exercise 11 gpio Polling and Interrupts) we saw how to interact with the general purpose I/O pins via sysfs files. These are rather easy ways to work with gpio; however they tend to be slow and require a lot of the CPU. In this exercise we explore accessing gpio directly via mmap. First we'll do it via  and the command line and later via a C program.

Memory Map
One of the nice things about using gpio via sysfs is you don't need to know little details like what gpio pin is mapped to what memory address. Now you need to know those details. Let's flash the USR3 LED by directly writing to memory. First turn off the trigger.

beagle$ cd /sys/class/leds/beaglebone\:green\:usr3 beagle$ echo none > trigger beagle$ echo 1 > brightness The USR3 LED should be on. Now find which gpio it is attached to. beagle$ cd ~/exercises/mmap beagle$ ../gpio/findGPIO.js USR3 { name: 'USR3', gpio: 56, led: 'usr3', mux: 'gpmc_a8', key: 'USR3', muxRegOffset: '0x060', options: [ 'gpmc_a8', 'gmii2_rxd3', 'rgmii2_rd3', 'mmc2_dat6', 'gpmc_a24', 'pr1_mii1_rxd0', 'mcasp0_aclkx', 'gpio1_24' ] } USR3 (gpio 56) mode: 7 (gpio1_24) 0x060 pullup pin 24 (44e10860): (MUX UNCLAIMED) (GPIO UNCLAIMED) It's attached to gpio1_24, that is, gpio port 1, bit 24. To find the address of this register, look up the am335x Technical Reference Manual (Google it). Look for GPIO1 in the Memory Map table. You'll see its base address is 0x4804_C000. Click on the GPIO1 link and you'll see '''Table 25-5. GPIO REGISTERS'''. This shows you what to add to the base address to get the various registers for GPIO1. For example, to see how the 32 GPIO1 pins are set you read the GPIO_DATAOUT register who's offset is 0x13Ch. Therefore you want to access 0x4804c000 + 0x13c = 0x4804c13c.

devmem2
An easy way to read the contents of a memory location is with devmem2 beagle$ devmem2 0x4804c13c /dev/mem opened. Memory mapped at address 0xb6f99000. Read at address 0x4804C13C (0xb6f9913c): 0x09800000 The address passed is the physical address. devmem2 returns the virtual address along with the contents of the memory location. USR3 is gpio1_24, which is the 24th bit of this value, which is 1 if the LED is on.

The GPIO_CLEARDATAOUT (0x190) register is used to clear given bits in a register. beagle$ devmem2 0x4804c190 w 0x01000000 /dev/mem opened. Memory mapped at address 0xb6f53000. Read at address 0x4804C190 (0xb6f53190): 0x09800000 Write at address 0x4804C190 (0xb6f53190): 0x01000000, readback 0x01000000 The LED should be off now. Turn it on using the GPIO_SETDATAOUT (0x194) register. beagle$ devmem2 0x4804c194 w 0x01000000 /dev/mem opened. Memory mapped at address 0xb6f9f000. Read at address 0x4804C194 (0xb6f9f194): 0x00800000 Write at address 0x4804C194 (0xb6f9f194): 0x01800000, readback 0x01800000 The USR3 LED should be back on again.