Debian Codegraph @

source/c_gpio.c

raw symbols SCIP
1 /*
2 Copyright (c) 2012-2021 Ben Croston
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <fcntl.h>
27 #include <sys/mman.h>
28 #include <string.h>
29 #include "c_gpio.h"
30
31 #define BCM2708_PERI_BASE_DEFAULT 0x20000000
32 #define BCM2709_PERI_BASE_DEFAULT 0x3f000000
33 #define BCM2710_PERI_BASE_DEFAULT 0x3f000000
34 #define BCM2711_PERI_BASE_DEFAULT 0xfe000000
35 #define GPIO_BASE_OFFSET 0x200000
36 #define FSEL_OFFSET 0 // 0x0000
37 #define SET_OFFSET 7 // 0x001c / 4
38 #define CLR_OFFSET 10 // 0x0028 / 4
39 #define PINLEVEL_OFFSET 13 // 0x0034 / 4
40 #define EVENT_DETECT_OFFSET 16 // 0x0040 / 4
41 #define RISING_ED_OFFSET 19 // 0x004c / 4
42 #define FALLING_ED_OFFSET 22 // 0x0058 / 4
43 #define HIGH_DETECT_OFFSET 25 // 0x0064 / 4
44 #define LOW_DETECT_OFFSET 28 // 0x0070 / 4
45 #define PULLUPDN_OFFSET 37 // 0x0094 / 4
46 #define PULLUPDNCLK_OFFSET 38 // 0x0098 / 4
47
48 #define PULLUPDN_OFFSET_2711_0 57
49 #define PULLUPDN_OFFSET_2711_1 58
50 #define PULLUPDN_OFFSET_2711_2 59
51 #define PULLUPDN_OFFSET_2711_3 60
52
53 #define PAGE_SIZE (4*1024)
54 #define BLOCK_SIZE (4*1024)
55
56 static volatile uint32_t *gpio_map;
57
58 void short_wait(void)
59 {
60 int i;
61
62 for (i=0; i<150; i++) { // wait 150 cycles
63 asm volatile("nop");
64 }
65 }
66
67 int setup(void)
68 {
69 int mem_fd;
70 uint8_t *gpio_mem;
71 uint32_t peri_base = 0;
72 uint32_t gpio_base;
73 uint8_t ranges[12] = { 0 };
74 uint8_t rev[4] = { 0 };
75 uint32_t cpu = 0;
76 FILE *fp;
77 char buffer[1024];
78 char hardware[1024];
79 int found = 0;
80
81 // try /dev/gpiomem first - this does not require root privs
82 if ((mem_fd = open("/dev/gpiomem", O_RDWR|O_SYNC)) > 0)
83 {
84 if ((gpio_map = (uint32_t *)mmap(NULL, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, 0)) == MAP_FAILED) {
85 return SETUP_MMAP_FAIL;
86 } else {
87 return SETUP_OK;
88 }
89 }
90
91 // revert to /dev/mem method - requires root privileges
92
93 if ((fp = fopen("/proc/device-tree/soc/ranges", "rb")) != NULL)
94 {
95 // get peri base from device tree
96 if (fread(ranges, 1, sizeof(ranges), fp) >= 8) {
97 peri_base = ranges[4] << 24 | ranges[5] << 16 | ranges[6] << 8 | ranges[7] << 0;
98 if (!peri_base) {
99 peri_base = ranges[8] << 24 | ranges[9] << 16 | ranges[10] << 8 | ranges[11] << 0;
100 }
101 }
102 if ((ranges[0] != 0x7e) ||
103 (ranges[1] != 0x00) ||
104 (ranges[2] != 0x00) ||
105 (ranges[3] != 0x00) ||
106 ((peri_base != BCM2708_PERI_BASE_DEFAULT) &&
107 (peri_base != BCM2709_PERI_BASE_DEFAULT) &&
108 (peri_base != BCM2711_PERI_BASE_DEFAULT))) {
109 // invalid ranges file
110 peri_base = 0;
111 }
112 fclose(fp);
113 }
114
115 // guess peri_base based on /proc/device-tree/system/linux,revision
116 if (!peri_base) {
117 if ((fp = fopen("/proc/device-tree/system/linux,revision", "rb")) != NULL) {
118 if (fread(rev, 1, sizeof(rev), fp) == 4) {
119 cpu = (rev[2] >> 4) & 0xf;
120 switch (cpu) {
121 case 0 : peri_base = BCM2708_PERI_BASE_DEFAULT;
122 break;
123 case 1 :
124 case 2 : peri_base = BCM2709_PERI_BASE_DEFAULT;
125 break;
126 case 3 : peri_base = BCM2711_PERI_BASE_DEFAULT;
127 break;
128 }
129 }
130 fclose(fp);
131 }
132 }
133
134 // guess peri_base based on /proc/cpuinfo hardware field
135 if (!peri_base) {
136 if ((fp = fopen("/proc/cpuinfo", "r")) == NULL)
137 return SETUP_CPUINFO_FAIL;
138
139 while(!feof(fp) && !found && fgets(buffer, sizeof(buffer), fp)) {
140 sscanf(buffer, "Hardware : %s", hardware);
141 if (strcmp(hardware, "BCM2708") == 0 || strcmp(hardware, "BCM2835") == 0) {
142 // pi 1 hardware
143 peri_base = BCM2708_PERI_BASE_DEFAULT;
144 } else if (strcmp(hardware, "BCM2709") == 0 || strcmp(hardware, "BCM2836") == 0) {
145 // pi 2 hardware
146 peri_base = BCM2709_PERI_BASE_DEFAULT;
147 } else if (strcmp(hardware, "BCM2710") == 0 || strcmp(hardware, "BCM2837") == 0) {
148 // pi 3 hardware
149 peri_base = BCM2710_PERI_BASE_DEFAULT;
150 } else if (strcmp(hardware, "BCM2711") == 0) {
151 // pi 4 hardware
152 peri_base = BCM2711_PERI_BASE_DEFAULT;
153 }
154 }
155 fclose(fp);
156 }
157
158 if (!peri_base)
159 return SETUP_NO_PERI_ADDR;
160
161 gpio_base = peri_base + GPIO_BASE_OFFSET;
162
163 // mmap the GPIO memory registers
164 if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0)
165 return SETUP_DEVMEM_FAIL;
166
167 if ((gpio_mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
168 return SETUP_MALLOC_FAIL;
169
170 if ((uint32_t)gpio_mem % PAGE_SIZE)
171 gpio_mem += PAGE_SIZE - ((uint32_t)gpio_mem % PAGE_SIZE);
172
173 if ((gpio_map = (uint32_t *)mmap( (void *)gpio_mem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, mem_fd, gpio_base)) == MAP_FAILED)
174 return SETUP_MMAP_FAIL;
175
176 return SETUP_OK;
177 }
178
179 void clear_event_detect(int gpio)
180 {
181 int offset = EVENT_DETECT_OFFSET + (gpio/32);
182 int shift = (gpio%32);
183
184 *(gpio_map+offset) |= (1 << shift);
185 short_wait();
186 *(gpio_map+offset) = 0;
187 }
188
189 int eventdetected(int gpio)
190 {
191 int offset, value, bit;
192
193 offset = EVENT_DETECT_OFFSET + (gpio/32);
194 bit = (1 << (gpio%32));
195 value = *(gpio_map+offset) & bit;
196 if (value)
197 clear_event_detect(gpio);
198 return value;
199 }
200
201 void set_rising_event(int gpio, int enable)
202 {
203 int offset = RISING_ED_OFFSET + (gpio/32);
204 int shift = (gpio%32);
205
206 if (enable)
207 *(gpio_map+offset) |= 1 << shift;
208 else
209 *(gpio_map+offset) &= ~(1 << shift);
210 clear_event_detect(gpio);
211 }
212
213 void set_falling_event(int gpio, int enable)
214 {
215 int offset = FALLING_ED_OFFSET + (gpio/32);
216 int shift = (gpio%32);
217
218 if (enable) {
219 *(gpio_map+offset) |= (1 << shift);
220 *(gpio_map+offset) = (1 << shift);
221 } else {
222 *(gpio_map+offset) &= ~(1 << shift);
223 }
224 clear_event_detect(gpio);
225 }
226
227 void set_high_event(int gpio, int enable)
228 {
229 int offset = HIGH_DETECT_OFFSET + (gpio/32);
230 int shift = (gpio%32);
231
232 if (enable)
233 *(gpio_map+offset) |= (1 << shift);
234 else
235 *(gpio_map+offset) &= ~(1 << shift);
236 clear_event_detect(gpio);
237 }
238
239 void set_low_event(int gpio, int enable)
240 {
241 int offset = LOW_DETECT_OFFSET + (gpio/32);
242 int shift = (gpio%32);
243
244 if (enable)
245 *(gpio_map+offset) |= 1 << shift;
246 else
247 *(gpio_map+offset) &= ~(1 << shift);
248 clear_event_detect(gpio);
249 }
250
251 void set_pullupdn(int gpio, int pud)
252 {
253 // Check GPIO register
254 int is2711 = *(gpio_map+PULLUPDN_OFFSET_2711_3) != 0x6770696f;
255 if (is2711) {
256 // Pi 4 Pull-up/down method
257 int pullreg = PULLUPDN_OFFSET_2711_0 + (gpio >> 4);
258 int pullshift = (gpio & 0xf) << 1;
259 unsigned int pullbits;
260 unsigned int pull = 0;
261 switch (pud) {
262 case PUD_OFF: pull = 0; break;
263 case PUD_UP: pull = 1; break;
264 case PUD_DOWN: pull = 2; break;
265 default: pull = 0; // switch PUD to OFF for other values
266 }
267 pullbits = *(gpio_map + pullreg);
268 pullbits &= ~(3 << pullshift);
269 pullbits |= (pull << pullshift);
270 *(gpio_map + pullreg) = pullbits;
271 } else {
272 // Legacy Pull-up/down method
273 int clk_offset = PULLUPDNCLK_OFFSET + (gpio/32);
274 int shift = (gpio%32);
275
276 if (pud == PUD_DOWN) {
277 *(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_DOWN;
278 } else if (pud == PUD_UP) {
279 *(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_UP;
280 } else { // pud == PUD_OFF
281 *(gpio_map+PULLUPDN_OFFSET) &= ~3;
282 }
283 short_wait();
284 *(gpio_map+clk_offset) = 1 << shift;
285 short_wait();
286 *(gpio_map+PULLUPDN_OFFSET) &= ~3;
287 *(gpio_map+clk_offset) = 0;
288 }
289 }
290
291 void setup_gpio(int gpio, int direction, int pud)
292 {
293 int offset = FSEL_OFFSET + (gpio/10);
294 int shift = (gpio%10)*3;
295
296 set_pullupdn(gpio, pud);
297 if (direction == OUTPUT)
298 *(gpio_map+offset) = (*(gpio_map+offset) & ~(7<<shift)) | (1<<shift);
299 else // direction == INPUT
300 *(gpio_map+offset) = (*(gpio_map+offset) & ~(7<<shift));
301 }
302
303 // Contribution by Eric Ptak <[email protected]>
304 int gpio_function(int gpio)
305 {
306 int offset = FSEL_OFFSET + (gpio/10);
307 int shift = (gpio%10)*3;
308 int value = *(gpio_map+offset);
309 value >>= shift;
310 value &= 7;
311 return value; // 0=input, 1=output, 4=alt0
312 }
313
314 void output_gpio(int gpio, int value)
315 {
316 int offset, shift;
317
318 if (value) // value == HIGH
319 offset = SET_OFFSET + (gpio/32);
320 else // value == LOW
321 offset = CLR_OFFSET + (gpio/32);
322
323 shift = (gpio%32);
324
325 *(gpio_map+offset) = 1 << shift;
326 }
327
328 int input_gpio(int gpio)
329 {
330 int offset, value, mask;
331
332 offset = PINLEVEL_OFFSET + (gpio/32);
333 mask = (1 << gpio%32);
334 value = *(gpio_map+offset) & mask;
335 return value;
336 }
337
338 void cleanup(void)
339 {
340 munmap((void *)gpio_map, BLOCK_SIZE);
341 }