Hardware Reference
In-Depth Information
The program
pullup.c
, shown next, provides a simple utility to change the pull-up
resistor settings. The program listing for
gpio_io.c
is provided in the “Direct Register
Access” section. The source for
timed_wait.c
is found in the “Source Code” section in
Chapter 1 of
Experimenting with Raspberry Pi
(Apress, 2014).
After compiling, the following example changes the GPIO 7 pull-up to high and GPIO
8 to low:
$ ./pullup 7=low 8=high
1 /
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
2
pullup.c : Change the pull−up resistor setting for GPIO pin
∗
3
/
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <setjmp.h>
11 #include <sys/mman.h>
12 #include <signal.h>
13
14 #include "gpio_io.c" /
GPIO routines
/
∗
∗
15 #include "timed_wait.c" /
Delay
/
∗
∗
16
17 /
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
18
0x7E200094 GPPUD GPIO Pin Pull−up/down Enable
∗
19
0x7E200098 GPPUDCLK0 GPIO Pin Pull−up/down Enable Clock 0
∗
20
/
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
21
22 #define GPIO_GPPUD
(ugpio+37)
∗
23 #define GPIO_GPPUDCLK0
(ugpio+38)
∗
24
25 static inline void
26 gpio_setpullup(int gpio, int pull) {
27 unsigned mask = 1 << gpio; /
GPIOs 0 to 31 only
/
∗
∗
28 unsigned pmask = pull >= 0 ? ( 1 << !! pull) : 0;
29
30 GPIO_GPPUD = pmask; /
Select pull−up setting
/
∗
∗
31 timed_wait (0, 500, 0);
32 GPIO_GPPUDCLK0 = mask; /
Set the GPIO of interest
/
∗
∗
33 timed_wait (0, 500, 0);
34 GPIO_GPPUD = 0; /
Reset pmask
/
∗
∗
35 timed_wait (0, 500, 0);
36 GPIO_GPPUDCLK0 = 0; /
Set the GPIO of interest
/
∗
∗
37 timed_wait (0, 500, 0);
38 }
39