Hardware Reference
In-Depth Information
Throughout this topic, you'll see the macros BCM2708_PERI_BASE and GPIO_BASE , for
example, used in programs that access the peripherals directly.
Memory Mapping
To gain access to physical memory under Linux, we make use of the /dev/mem character
device and the mmap(2) system call. The /dev/mem node is shown here:
$ ls −l /dev/mem
crw−r−−−−T 1 root kmem 1, 1 Dec 31 1969 /dev/mem
From the ownership information shown, it is immediately obvious that you'll need
root privileges to access it. This is sensible given that a process can cause havoc with
direct access to the physical memory. Clearly, the Pi developer should exercise caution in
what the applications do with it.
The mmap(2) system call API is shown here:
#include <sys/mman.h>
void
mmap(
void
addr, /
Address to use
/
size_t length, /
Number of bytes to access
/
int prot, /
Memory protection
/
int flags, /
Option flags
/
int fd, /
Opened file descriptor
/
off_t offset /
Starting off set
/
) ;
Rather than look at all the options and flags available to this somewhat complicated
system call, let's look at the ones that we use in the following code:
static char
map = 0;
static void
gpio_init() {
int fd;
char
map;
fd = open("/dev/mem",O_RDWR|O_SYNC) ; /
Needs root access
/
if ( fd < 0 ) {
perror("Opening /dev/mem") ;
exit(1) ;
}
 
Search WWH ::




Custom Search