EBC Exercise 11b gpio via mmap

From eLinux.org
Revision as of 14:40, 30 September 2013 by Yoder (talk | contribs) (Memory Map: Adding address details)
Jump to: navigation, search

thumb‎ Embedded Linux Class by Mark A. Yoder

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 devmem2, 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',
   [ 'gpmc_a8',
     '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 it's 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.

thumb‎ Embedded Linux Class by Mark A. Yoder