-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpiGPIOManipulation.h
281 lines (229 loc) · 6.85 KB
/
rpiGPIOManipulation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#ifndef _RPI_INCLUDED
#define _RPI_INCLUDED
#include <asm/io.h>
// NOTE: Check Broadcom BCM8325 datasheet, page 91+
/* GPIO registers base address. */
#define GPIO_BASE_ADDR (0x3F200000)
//--
//000 = GPIO Pin 'x' is an input
//001 = GPIO Pin 'x' is an output
// By default GPIO pin is being used as in input
#define GPIO_DIRECTION_IN (0)
#define GPIO_DIRECTION_OUT (1)
//--
//Handle GPIO: 0-9
/* GPIO Function Select 0. */
#define GPFSEL0_BASE_ADDR (GPIO_BASE_ADDR + 0x00000000)
//Handle GPIO: 10-19
/* GPIO Function Select 1. */
#define GPFSEL1_BASE_ADDR (GPIO_BASE_ADDR + 0x00000004)
//Handle GPIO: 20-29
/* GPIO Function Select 2. */
#define GPFSEL2_BASE_ADDR (GPIO_BASE_ADDR + 0x00000008)
//Handle GPIO: 30-39
/* GPIO Function Select 3. */
#define GPFSEL3_BASE_ADDR (GPIO_BASE_ADDR + 0x0000000C)
//Handle GPIO: 40-49
/* GPIO Function Select 4. */
#define GPFSEL4_BASE_ADDR (GPIO_BASE_ADDR + 0x00000010)
//Handle GPIO: 50-53
/* GPIO Function Select 5. */
#define GPFSEL5_BASE_ADDR (GPIO_BASE_ADDR + 0x00000014)
//--
//GPIO: 0-31
/* GPIO Pin Output Set 0. */
#define GPSET0_BASE_ADDR (GPIO_BASE_ADDR + 0x0000001C)
//GPIO: 32-53
/* GPIO Pin Output Set 1. */
#define GPSET1_BASE_ADDR (GPIO_BASE_ADDR + 0x00000020)
//--
//GPIO: 0-31
/* GPIO Pin Output Clear 0. */
#define GPCLR0_BASE_ADDR (GPIO_BASE_ADDR + 0x00000028)
//GPIO: 32-53
/* GPIO Pin Output Clear 1. */
#define GPCLR1_BASE_ADDR (GPIO_BASE_ADDR + 0x0000002C)
//--
//GPIO: 0-31
/* GPIO Pin Level 0. */
#define GPLEV0_BASE_ADDR (GPIO_BASE_ADDR + 0x00000034)
//GPIO: 32-53
/* GPIO Pin Level 1. */
#define GPLEV1_BASE_ADDR (GPIO_BASE_ADDR + 0x00000038)
//--
//GPIO: 0-53
/* GPIO Pin Pull-up/down Enable. */
#define GPPUD_BASE_ADDR (GPIO_BASE_ADDR + 0x00000094)
//GPIO: 0-31
/* GPIO Pull-up/down Clock Register 0. */
#define GPPUDCLK0_BASE_ADDR (GPIO_BASE_ADDR + 0x00000098)
//GPIO: 32-53
/* GPIO Pull-up/down Clock Register 1. */
#define GPPUDCLK1_BASE_ADDR (GPIO_BASE_ADDR + 0x0000009C)
/* PUD - GPIO Pin Pull-up/down */
#define PULL_NONE (0)
#define PULL_DOWN (1)
#define PULL_UP (2)
/* Avilable GPIO pins. */
#define GPIO_02 (2)
#define GPIO_03 (3)
#define GPIO_04 (4)
#define GPIO_05 (5)
#define GPIO_06 (6)
#define GPIO_07 (7)
#define GPIO_08 (8)
#define GPIO_09 (9)
#define GPIO_10 (10)
#define GPIO_11 (11)
#define GPIO_12 (12)
#define GPIO_13 (13)
#define GPIO_14 (14)
#define GPIO_15 (15)
#define GPIO_16 (16)
#define GPIO_17 (17)
#define GPIO_18 (18)
#define GPIO_19 (19)
#define GPIO_20 (20)
#define GPIO_21 (21)
#define GPIO_22 (22)
#define GPIO_23 (23)
#define GPIO_24 (24)
#define GPIO_25 (25)
#define GPIO_26 (26)
#define GPIO_27 (27)
/* Manipulation functions */
unsigned int GetGPFSELReg(char pin)
{
unsigned int addr;
if(pin >= 0 && pin <10)
addr = GPFSEL0_BASE_ADDR;
else if(pin >= 10 && pin <20)
addr = GPFSEL1_BASE_ADDR;
else if(pin >= 20 && pin <30)
addr = GPFSEL2_BASE_ADDR;
else if(pin >= 30 && pin <40)
addr = GPFSEL3_BASE_ADDR;
else if(pin >= 40 && pin <50)
addr = GPFSEL4_BASE_ADDR;
else /*if(pin >= 50 && pin <53) */
addr = GPFSEL5_BASE_ADDR;
return addr;
}
char GetGPIOPinOffset(char pin)
{
if(pin >= 0 && pin <10)
pin = pin;
else if(pin >= 10 && pin <20)
pin -= 10;
else if(pin >= 20 && pin <30)
pin -= 20;
else if(pin >= 30 && pin <40)
pin -= 30;
else if(pin >= 40 && pin <50)
pin -= 40;
else /*if(pin >= 50 && pin <53) */
pin -= 50;
return pin;
}
void SetInternalPullUpDown(char pin, char value)
{
unsigned int base_addr_gppud;
unsigned int base_addr_gppudclk;
void *addr = NULL;
unsigned int tmp;
unsigned int mask;
/* Get base address of GPIO Pull-up/down Register (GPPUD). */
base_addr_gppud = GPPUD_BASE_ADDR;
/* Get base address of GPIO Pull-up/down Clock Register (GPPUDCLK). */
base_addr_gppudclk = (pin < 32) ? GPPUDCLK0_BASE_ADDR : GPPUDCLK1_BASE_ADDR;
/* Get pin offset in register . */
pin = (pin < 32) ? pin : pin - 32;
/* Write to GPPUD to set the required control signal (i.e. Pull-up or Pull-Down or neither
to remove the current Pull-up/down). */
addr = ioremap(base_addr_gppud, 4);
iowrite32(value, addr);
/* Wait 150 cycles this provides the required set-up time for the control signal */
/* Write to GPPUDCLK0/1 to clock the control signal into the GPIO pads you wish to
modify NOTE only the pads which receive a clock will be modified, all others will
retain their previous state. */
addr = ioremap(base_addr_gppudclk, 4);
tmp = ioread32(addr);
mask = 0x1 << pin;
tmp |= mask;
iowrite32(tmp, addr);
/* Wait 150 cycles this provides the required hold time for the control signal */
/* Write to GPPUD to remove the control signal. */
addr = ioremap(base_addr_gppud, 4);
iowrite32(PULL_NONE, addr);
/* Write to GPPUDCLK0/1 to remove the clock. */
addr = ioremap(base_addr_gppudclk, 4);
tmp = ioread32(addr);
mask = 0x1 << pin;
tmp &= (~mask);
iowrite32(tmp, addr);
}
void SetGpioPinDirection(char pin, char direction)
{
unsigned int base_addr;
void *addr = NULL;
unsigned int tmp;
unsigned int mask;
/* Get base address of function selection register. */
base_addr = GetGPFSELReg(pin);
/* Calculate gpio pin offset. */
pin = GetGPIOPinOffset(pin);
/* Set gpio pin direction. */
addr = ioremap(base_addr, 4);
tmp = ioread32(addr);
if(direction)
{ //set as output: set 1
mask = 0x1 << (pin*3);
tmp |= mask;
}
else
{ //set as input: set 0
mask = ~(0x1 << (pin*3));
tmp &= mask;
}
iowrite32(tmp, addr);
}
void SetGpioPin(char pin)
{
void *addr = NULL;
unsigned int tmp;
/* Get base address of gpio set register. */
addr = (pin < 32) ? (void *) GPSET0_BASE_ADDR : (void *)GPSET1_BASE_ADDR;
pin = (pin < 32) ? pin : pin - 32;
/* Set gpio. */
addr = ioremap((unsigned long)addr, 4);
tmp = 0x1 << pin;
iowrite32(tmp, addr);
}
void ClearGpioPin(char pin)
{
void *addr = NULL;
unsigned int tmp;
/* Get base address of gpio clear register. */
addr = (pin < 32) ? (void *)GPCLR0_BASE_ADDR : (void *)GPCLR1_BASE_ADDR;
pin = (pin < 32) ? pin : pin - 32;
/* Clear gpio. */
addr = ioremap((unsigned long)addr, 4);
tmp = 0x1 << pin;
iowrite32(tmp, addr);
}
char GetGpioPinValue(char pin)
{
void *addr = NULL;
unsigned int tmp;
unsigned int mask;
/* Get base address of gpio level register. */
addr = (pin < 32) ? (void *) GPLEV0_BASE_ADDR : (void *)GPLEV1_BASE_ADDR;
pin = (pin < 32) ? pin : pin - 32;
/* Read gpio pin level. */
addr = ioremap((unsigned long)addr, 4);
tmp = ioread32(addr);
mask = 0x1 << pin;
tmp &= mask;
return (tmp >> pin);
}
#endif