Hardware Reference
In-Depth Information
Solution
In smaller processors that aren't running an operating system, you can read and write a giv-
en memory address directly from C. With Linux running on Bone, many of the memory
locations are hardware protected, so you can't accidentally access them directly.
This recipe shows how to use mmap() (memory map) to map the GPIO registers to an ar-
ray in C. Then all you need to do is access the array to read and write the registers.
WARNING
This solution is much more involved than the previous ones. You need to understand binary
and hex numbers and be able to read the AM335x Technical Reference Manual.
This solution will read a pushbutton attached to P9_42 and flash an LED attached to
P9_13 . Note that this is a change from the previous solutions that makes the code used
here much simpler.
TIP
See Recipe 8.3 for details on mapping the GPIO numbers to memory addresses.
Add the code in Example 8-3 to a file named pushLEDmmap.h .
Example 8-3. Memory address definitions (pushLEDmmap.h)
// From: http://stackoverflow.com/questions/13124271/
driving-beaglebone-gpio
// -through-dev-mem
// user contributions licensed under cc by-sa 3.0 with attribution
required
// http://creativecommons.org/licenses/by-sa/3.0/
// http://blog.stackoverflow.com/2009/06/attribution-required/
// Author: madscientist159 (http://stackoverflow.com/users/3000377/
madscientist159)
Search WWH ::




Custom Search