Hardware Reference
In-Depth Information
gpio_io.c
The following pages show the program listing for gpio_io.c :
1 /
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
2
gpio_io.c : GPIO Access Code
3
/
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
4
5 #define BCM2708_PERI_BASE 0x20000000
6 #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000)
7 #define BLOCK_SIZE (4
1024)
8
9 /
GPIO setup macros. Always use INP_GPIO (x) before using OUT_GPIO(x)
10 or SET_GPIO_ALT(x, y )
/
11 #define INP_GPIO(g) \
(ugpio + ((g)/10)) &= ~(7 <<(((g) % 10)
3))
12 #define OUT_GPIO(g)
(ugpio + ((g)/10)) |= (1 <<(((g) % 10)
3))
13 #define SET_GPIO_ALT(g,a) \
14
(ugpio + (((g)/10))) |= (((a) <=3?(a) + 4 : \
(a)==4?3:2)<<(((g)%10)
3))
15
16 #define GPIO_SET
(ugpio+7) /
sets bits
/
17 #define GPIO_CLR
(ugpio+10) /
clears bits
/
18 #define GPIO_GET
(ugpio+13) /
gets all GPIO input levels
/
19
20 typedef enum {
21 Input = 0, /
GPIO is an Input
/
22 Output /
GPIO is an Output
/
23 } direction_t;
24
25 static volatile unsigned
ugpio;
26
27 /
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
28
Perform initialization to access GPIO registers:
29
Sets up pointer ugpio.
30
/
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
31 static void
32 gpio_init() {
33 int fd;
34 char
map;
35 /
/
36 fd = open("/dev/mem",O_RDWR|O_SYNC);
37 if ( fd < 0 ) {
38 perror("Opening/dev/mem");
39 exit(1);
40 }
41
Needs root access
 
Search WWH ::




Custom Search