-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgpio-sunxi.c
385 lines (322 loc) · 8.58 KB
/
gpio-sunxi.c
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* simple mmapped gpio bit-banging
*
* based on pio.c from
* https://github.com/linux-sunxi/sunxi-tools
*
* (C) Copyright 2022 Heiko Jehmlich <[email protected]>
* (C) Copyright 2011 Henrik Nordstrom <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include "utils.h"
#include "gpio.h"
#include "mcp.h"
#define MEM_BASE 0x01c20000
#define PIO_NR_PORTS 9 /* A-I */
#define PIO_OFFSET 0x0800
#define PIO_REG_CFG(B, N, I) (uint32_t*)(B + PIO_OFFSET + N*0x24 + (I<<2) + 0x00)
#define PIO_REG_DLEVEL(B, N, I) (uint32_t*)(B + PIO_OFFSET + N*0x24 + (I<<2) + 0x14)
#define PIO_REG_PULL(B, N, I) (uint32_t*)(B + PIO_OFFSET + N*0x24 + (I<<2) + 0x1C)
#define PIO_REG_DATA(B, N) (uint32_t*)(B + PIO_OFFSET + N*0x24 + 0x10)
#define TMR_OFFSET 0x0C00
#define TMR_AVSCLK(B) (uint32_t*)(B + 0x0144)
#define TMR_AVSCTRL(B) (uint32_t*)(B + TMR_OFFSET + 0x80)
#define TMR_AVS0(B) (uint32_t*)(B + TMR_OFFSET + 0x84)
#define TMR_AVS1(B) (uint32_t*)(B + TMR_OFFSET + 0x88)
#define TMR_AVSDIV(B) (uint32_t*)(B + TMR_OFFSET + 0x8c)
#define I2S0_OFFSET 0x2000
#define I2S0_CLKDIV(B) (uint32_t*)(B + I2S0_OFFSET + 0x24)
typedef struct {
int port;
int pin;
int func;
int pull;
int trig;
int data;
} gpio_status_t;
static volatile void *mem;
static void test_blink() {
const char *pio = "PA3";
xlog("configure with initial 0");
gpio_configure(pio, 1, 0, 0);
sleep(1);
xlog("configure with initial 1");
gpio_configure(pio, 1, 0, 1);
sleep(1);
xlog("configure with initial not set");
gpio_configure(pio, 1, 0, -1);
sleep(1);
xlog("blink test");
for (int i = 0; i < 3; i++) {
gpio_set(pio, 1);
gpio_print(pio);
sleep(1);
gpio_set(pio, 0);
gpio_print(pio);
sleep(1);
}
sleep(1);
xlog("toggle test");
gpio_toggle(pio);
gpio_print(pio);
sleep(1);
gpio_toggle(pio);
gpio_print(pio);
}
static void test_timers() {
uint32_t *avs0 = TMR_AVS0(mem);
uint32_t *avs1 = TMR_AVS1(mem);
*avs1 = *avs0 = 0;
usleep(1000);
xlog("AVS0 %08u AVS1 %08u", *avs0, *avs1);
usleep(1000);
xlog("AVS0 %08u AVS1 %08u", *avs0, *avs1);
usleep(1000);
xlog("AVS0 %08u AVS1 %08u", *avs0, *avs1);
gpio_delay_micros(330);
xlog("AVS1 330 %08u", *avs1);
gpio_delay_micros(188);
xlog("AVS1 188 %08u", *avs1);
gpio_delay_micros(88);
xlog("AVS1 088 %08u", *avs1);
gpio_delay_micros(22);
xlog("AVS1 022 %08u", *avs1);
xlog("AVS0 %08u", *avs0);
uint32_t delay = 1813594;
uint32_t begin = gpio_micros();
usleep(delay);
uint32_t elapsed = gpio_micros_since(&begin);
xlog("AVS0 %08u", *avs0);
xlog("usleep %u AVS0 elapsed = %u", delay, elapsed);
}
static void mem_read(gpio_status_t *pio) {
uint32_t val;
uint32_t port_num_func, port_num_pull;
uint32_t offset_func, offset_pull;
port_num_func = pio->pin >> 3;
offset_func = (pio->pin & 0x07) << 2;
port_num_pull = pio->pin >> 4;
offset_pull = (pio->pin & 0x0f) << 1;
/* function */
val = *PIO_REG_CFG(mem, pio->port, port_num_func);
pio->func = (val >> offset_func) & 0x07;
/* pull */
val = *PIO_REG_PULL(mem, pio->port, port_num_pull);
pio->pull = (val >> offset_pull) & 0x03;
/* trigger */
val = *PIO_REG_DLEVEL(mem, pio->port, port_num_pull);
pio->trig = (val >> offset_pull) & 0x03;
/* data */
val = *PIO_REG_DATA(mem, pio->port);
pio->data = (val >> pio->pin) & 0x01;
}
static void mem_write(gpio_status_t *pio) {
uint32_t *addr, val;
uint32_t port_num_func, port_num_pull;
uint32_t offset_func, offset_pull;
port_num_func = pio->pin >> 3;
offset_func = (pio->pin & 0x07) << 2;
port_num_pull = pio->pin >> 4;
offset_pull = (pio->pin & 0x0f) << 1;
/* function */
addr = PIO_REG_CFG(mem, pio->port, port_num_func);
val = *addr;
val &= ~(0x07 << offset_func);
val |= (pio->func & 0x07) << offset_func;
*addr = val;
/* pull */
addr = PIO_REG_PULL(mem, pio->port, port_num_pull);
val = *addr;
val &= ~(0x03 << offset_pull);
val |= (pio->pull & 0x03) << offset_pull;
*addr = val;
/* trigger */
addr = PIO_REG_DLEVEL(mem, pio->port, port_num_pull);
val = *addr;
val &= ~(0x03 << offset_pull);
val |= (pio->trig & 0x03) << offset_pull;
*addr = val;
/* initial value - only if 0/1 - otherwise leave unchanged */
addr = PIO_REG_DATA(mem, pio->port);
val = *addr;
if (pio->data == 0) {
val &= ~(0x01 << pio->pin);
*addr = val;
} else if (pio->data == 1) {
val |= (0x01 << pio->pin);
*addr = val;
} else
pio->data = (val >> pio->pin) & 0x01;
}
void gpio_x() {
// uint32_t *addr, val;
//
// addr = I2S0_CLKDIV(mem);
// val = *addr;
// xlog("I2S0_CLKDIV 0x%x %s", val, printbits(val, SPACEMASK));
//
// val |= 1 << 9;
// val = *addr;
// xlog("I2S0_CLKDIV 0x%x %s", val, printbits(val, SPACEMASK));
}
void gpio_print(const char *name) {
if (*name == 'P')
name++;
gpio_status_t pio;
pio.port = *name++ - 'A';
pio.pin = atoi(name);
mem_read(&pio);
xlog("P%c%d", 'A' + pio.port, pio.pin);
xlog("<%x>", pio.func);
xlog("<%x>", pio.pull);
xlog("<%x>", pio.trig);
xlog("<%x>", pio.data);
xlog("");
}
int gpio_configure(const char *name, int function, int trigger, int initial) {
if (*name == 'P')
name++;
gpio_status_t pio;
pio.port = *name++ - 'A';
pio.pin = atoi(name);
pio.func = function;
pio.trig = trigger;
pio.data = initial;
mem_write(&pio);
return pio.data;
}
int gpio_get(const char *name) {
if (*name == 'P')
name++;
int port = *name++ - 'A';
int pin = atoi(name);
uint32_t val = *PIO_REG_DATA(mem, port);
return (val >> pin) & 0x01;
}
void gpio_set(const char *name, int value) {
if (*name == 'P')
name++;
int port = *name++ - 'A';
int pin = atoi(name);
uint32_t *addr = PIO_REG_DATA(mem, port);
uint32_t val = *addr;
if (value)
val |= (1 << pin);
else
val &= ~(1 << pin);
*addr = val;
}
int gpio_toggle(const char *name) {
if (*name == 'P')
name++;
int port = *name++ - 'A';
int pin = atoi(name);
uint32_t *addr = PIO_REG_DATA(mem, port);
uint32_t val = *addr;
if ((1 << pin) & val) {
val &= ~(1 << pin);
*addr = val;
return 0;
} else {
val |= (1 << pin);
*addr = val;
return 1;
}
}
// usleep() on its own gives latencies 20-40 us; this combination gives < 25 us.
void gpio_delay_micros(uint32_t us) {
// we use AVS1 Timer
uint32_t *ctrl = TMR_AVSCTRL(mem);
uint32_t *avs1 = TMR_AVS1(mem);
*ctrl &= ~0b10; // stop
*avs1 = 0; // clear
*ctrl |= 0b10; // run
if (us > 100)
usleep(us - 90);
while (*avs1 < us)
;
}
uint32_t gpio_micros() {
uint32_t *avs0 = TMR_AVS0(mem);
return *avs0;
}
uint32_t gpio_micros_since(uint32_t *when) {
uint32_t *avs0 = TMR_AVS0(mem);
uint32_t now = *avs0;
uint32_t elapsed;
if (now > *when)
elapsed = now - *when;
else
elapsed = 0xffffffff - *when + now;
*when = now;
return elapsed;
}
static int init() {
int pagesize = sysconf(_SC_PAGESIZE) * 4;
// access memory
int fd = open("/dev/mem", O_RDWR | O_SYNC);
if (fd == -1)
return xerr("/dev/mem failed: %s", strerror(errno));
mem = mmap(NULL, pagesize, PROT_WRITE | PROT_READ, MAP_SHARED, fd, MEM_BASE);
if (mem == MAP_FAILED)
return xerr("mmap gpio failed: %s", strerror(errno));
// enable AVS timers clock, set divisor to 12 --> gives 1us
uint32_t *avsclk = TMR_AVSCLK(mem);
uint32_t *avsdiv = TMR_AVSDIV(mem);
uint32_t *ctrl = TMR_AVSCTRL(mem);
uint32_t *avs0 = TMR_AVS0(mem);
uint32_t *avs1 = TMR_AVS1(mem);
*ctrl &= ~0b11; // stop
*avs1 = *avs0 = 0;
*avsdiv = 0x000C000C;
*avsclk |= 1 << 31;
*ctrl |= 0b11; // run Forest, run...
close(fd);
return 0;
}
static void stop() {
int pagesize = sysconf(_SC_PAGESIZE) * 4;
uint32_t *ctrl = TMR_AVSCTRL(mem);
*ctrl &= ~0b11; // stop AVS timers
munmap((void*) mem, pagesize);
}
int gpio_main(int argc, char **argv) {
set_xlog(XLOG_STDOUT);
set_debug(1);
init();
xlog("mmap OK");
test_timers();
test_blink();
stop();
return 0;
}
#ifdef GPIO_MAIN
int main(int argc, char **argv) {
return gpio_main(argc, argv);
}
#else
MCP_REGISTER(gpio, 1, &init, &stop, NULL);
#endif