Hardware Reference
In-Depth Information
map = (char
) mmap(
NULL, /
Any address
/
BLOCK_SIZE, /
# of bytes
/
PROT_READ|PROT_WRITE,
MAP_SHARED, /
Shared
/
fd, /
/dev/mem
/
GPIO_BASE /
Offset to GPIO
/
) ;
if ( (long)map == −1L ) {
perror("mmap(/dev/mem)");
exit(1) ;
}
close(fd);
ugpio = (volatile unsigned
)map;
}
The first thing performed in this code is to open the device driver node /dev/mem .
It is opened for reading and writing ( O_RDWR ), and the option flag O_SYNC requests that any
write(2) call to this file descriptor result in blocking the execution of the caller until it
has completed.
Address
Next, the mmap(2) call is invoked. The address argument is provided with NULL (zero)
so that the kernel can choose where to map it into the caller's address space. If the
application were to specify a starting address to use and the kernel was not able use it,
the system call would fail. The starting address is returned and assigned to the character
pointer map in the preceding listing.
Length
Argument 2 is supplied with the macro BLOCK_SIZE in this example. This is the number
of bytes you would like to map into your address space. This was defined earlier in the
program as 4 KB:
#define BLOCK_SIZE (4
1024)
While the application may not need the full 4 KB of physical memory mapped,
mmap(2) may insist on using a multiple of the page size. This can be verified on the
command line as follows:
$ getconf PAGE_SIZE
4096
 
Search WWH ::




Custom Search