-
Notifications
You must be signed in to change notification settings - Fork 3
/
gpio-s905.c
475 lines (411 loc) · 10.1 KB
/
gpio-s905.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
* simple mmapped gpio bit-banging
*
* - implementation for Amlogic S905
* - tested on Odroid C2, Linux 5.10.102-meson64
*
* (C) Copyright 2022 Heiko Jehmlich <hje@jecons.de>
*
* 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 PIO_REG_CFG(B, G) (uint32_t*)(B) + (G/10)
#define PIO_REG_SET(B, G) (uint32_t*)(B) + (0x1C/4)
#define PIO_REG_CLR(B, G) (uint32_t*)(B) + (0x28/4)
#define PIO_REG_GET(B, G) (uint32_t*)(B) + (0x34/4)
typedef struct {
int pin;
int func;
int data;
} gpio_status_t;
static volatile void *gpio;
static volatile uint32_t *timer;
static void test_lirc() {
const char *pio = "GPIO22";
gpio_configure(pio, 1, 0, 1);
gpio_print(pio);
xlog("3x lirc VOLUP/VOLDOWN\n");
gpio_lirc(pio, 0x00FD20DF);
gpio_lirc(pio, 0x00FD20DF);
gpio_lirc(pio, 0x00FD20DF);
sleep(1);
gpio_lirc(pio, 0x00FD10EF);
gpio_lirc(pio, 0x00FD10EF);
gpio_lirc(pio, 0x00FD10EF);
gpio_print(pio);
}
static void test_flamingo() {
const char *pio = "GPIO17";
gpio_configure(pio, 1, 0, 0);
xlog("flamingo v1 ON\n");
gpio_flamingo_v1(pio, 0x02796056, 28, 4, 330);
sleep(1);
xlog("flamingo v2 OFF\n");
gpio_flamingo_v1(pio, 0x0253174e, 28, 4, 330);
}
static void test_blink() {
const char *pio = "GPIO17";
xlog("configure with initial 0\n");
gpio_configure(pio, 1, 0, 0);
sleep(1);
xlog("configure with initial 1\n");
gpio_configure(pio, 1, 0, 1);
sleep(1);
xlog("configure with initial not set\n");
gpio_configure(pio, 1, 0, -1);
sleep(1);
xlog("blink test\n");
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\n");
gpio_toggle(pio);
gpio_print(pio);
sleep(1);
gpio_toggle(pio);
gpio_print(pio);
}
static void test_timers() {
uint32_t t;
xlog("T0 %08u T1 %08u\n", timer[0], timer[1]);
usleep(1000);
xlog("T0 %08u T1 %08u\n", timer[0], timer[1]);
usleep(1000);
xlog("T0 %08u T1 %08u\n", timer[0], timer[1]);
t = timer[1];
gpio_delay_micros(330);
xlog("T1 330 %08u\n", timer[1] - t);
t = timer[1];
gpio_delay_micros(188);
xlog("T1 188 %08u\n", timer[1] - t);
t = timer[1];
gpio_delay_micros(88);
xlog("T1 088 %08u\n", timer[1] - t);
t = timer[1];
gpio_delay_micros(22);
xlog("T1 022 %08u\n", timer[1] - t);
uint32_t delay = 1813594;
uint32_t begin = gpio_micros();
usleep(delay);
uint32_t elapsed = gpio_micros_since(&begin);
xlog("usleep %u T1 elapsed = %u\n", delay, elapsed);
}
static void mem_read(gpio_status_t *pio) {
uint32_t *addr;
int offset_func = (pio->pin % 10) * 3;
/* function */
addr = PIO_REG_CFG(gpio, pio->pin);
pio->func = (*addr >> offset_func) & 0x07;
/* data */
addr = PIO_REG_GET(gpio, pio->pin);
pio->data = (*addr >> pio->pin) & 0x01;
}
static void mem_write(gpio_status_t *pio) {
uint32_t *addr, val;
int offset_func = (pio->pin % 10) * 3;
uint32_t bit = 1 << pio->pin;
/* function */
addr = PIO_REG_CFG(gpio, pio->pin);
val = *addr;
val &= ~(0x07 << offset_func);
val |= (pio->func & 0x07) << offset_func;
*addr = val;
/* initial value - only if 0/1 - otherwise leave unchanged */
if (pio->data == 0) {
addr = PIO_REG_CLR(gpio, pin);
val = *addr;
val |= bit;
*addr = val;
} else if (pio->data == 1) {
addr = PIO_REG_SET(gpio, pin);
val = *addr;
val |= bit;
*addr = val;
} else {
addr = PIO_REG_GET(gpio, pio->pin);
val = *addr;
pio->data = (val >> pio->pin) & 0x01;
}
}
void gpio_print(const char *name) {
while (*name >= 'A')
name++;
gpio_status_t pio;
pio.pin = atoi(name);
mem_read(&pio);
xlog("GPIO%d", pio.pin);
xlog("<%x>", pio.func);
xlog("<%x>", pio.data);
xlog("\n");
}
int gpio_configure(const char *name, int function, int trigger, int initial) {
while (*name >= 'A')
name++;
gpio_status_t pio;
pio.pin = atoi(name);
pio.func = function;
pio.data = initial;
mem_write(&pio);
return pio.data;
}
int gpio_get(const char *name) {
while (*name >= 'A')
name++;
int pin = atoi(name);
uint32_t *addr = PIO_REG_GET(gpio, pio->pin);
return (*addr >> pin) & 0x01;
}
void gpio_set(const char *name, int value) {
while (*name >= 'A')
name++;
uint32_t bit = 1 << atoi(name);
uint32_t *addr, val;
if (value) {
addr = PIO_REG_SET(gpio, pin);
val = *addr;
val |= bit;
*addr = val;
} else {
addr = PIO_REG_CLR(gpio, pin);
val = *addr;
val |= bit;
*addr = val;
}
}
int gpio_toggle(const char *name) {
while (*name >= 'A')
name++;
uint32_t bit = 1 << atoi(name);
uint32_t *addr, val;
addr = PIO_REG_GET(gpio, pin);
if (*addr & bit) {
addr = PIO_REG_CLR(gpio, pin);
val = *addr;
val |= bit;
*addr = val;
return 0;
} else {
addr = PIO_REG_SET(gpio, pin);
val = *addr;
val |= bit;
*addr = val;
return 1;
}
}
//
// rc1 pattern
//
// short high pulse followed by long low pulse or long high + short low pulse, no clock
// _ ___
// 0 = _| |___ 1 = _| |_
//
// https://forum.arduino.cc/index.php?topic=201771.0
//
void gpio_flamingo_v1(const char *name, uint32_t message, int bits, int repeat, int pulse) {
while (*name >= 'A')
name++;
int pin = atoi(name);
uint32_t bit = 1 << pin;
uint32_t *set = PIO_REG_SET(gpio, pin);
uint32_t *clr = PIO_REG_CLR(gpio, pin);
while (repeat--) {
uint32_t mask = 1 << (bits - 1);
// sync
*set |= bit;
gpio_delay_micros(pulse);
*clr |= bit;
gpio_delay_micros(pulse * 15);
while (mask) {
if (message & mask) {
// 1
*set |= bit;
gpio_delay_micros(pulse * 3);
*clr |= bit;
gpio_delay_micros(pulse);
} else {
// 0
*set |= bit;
gpio_delay_micros(pulse);
*clr |= bit;
gpio_delay_micros(pulse * 3);
}
mask = mask >> 1;
}
}
usleep(pulse * 50);
}
//
// rc2 pattern
//
// clock pulse + data pulse, either short or long distance from clock to data pulse
// _ _ _ _
// 0 = _| |_| |____ 1 = _| |____| |_
//
// 32 bit pattern: 00000000 1000001101011010 0001 0001
// 1 2 3
// 1=Transmitter ID, 16 bit
// 2=Command, 4 bit, 0=OFF, 1=ON
// 3=Channel, 4 bit
//
// https://forum.pilight.org/showthread.php?tid=1110&page=12
void gpio_flamingo_v2(const char *name, uint32_t message, int bits, int repeat, int phi, int plo) {
while (*name >= 'A')
name++;
int px = (phi + plo) * 2 + plo;
int sync = px * 2;
int pin = atoi(name);
uint32_t bit = 1 << pin;
uint32_t *set = PIO_REG_SET(gpio, pin);
uint32_t *clr = PIO_REG_CLR(gpio, pin);
while (repeat--) {
uint32_t mask = 1 << (bits - 1);
// sync
*set |= bit;
gpio_delay_micros(phi);
*clr |= bit;
gpio_delay_micros(sync);
while (mask) {
if (message & mask) {
// 1
*set |= bit;
gpio_delay_micros(phi);
*clr |= bit;
gpio_delay_micros(px);
*set |= bit;
gpio_delay_micros(phi);
*clr |= bit;
gpio_delay_micros(plo);
} else {
// 0
*set |= bit;
gpio_delay_micros(phi);
*clr |= bit;
gpio_delay_micros(plo);
*set |= bit;
gpio_delay_micros(phi);
*clr |= bit;
gpio_delay_micros(px);
}
mask = mask >> 1;
}
// a clock or parity (?) bit terminates the message
*set |= bit;
gpio_delay_micros(phi);
*clr |= bit;
gpio_delay_micros(plo);
// wait before sending next sync
gpio_delay_micros(sync * 4);
}
usleep(sync * 50);
}
void gpio_lirc(const char *name, uint32_t message) {
}
void gpio_delay_micros(uint32_t us) {
// usleep() on its own gives latencies 20-40 us; this combination gives < 25 us.
uint32_t start = timer[1];
if (us >= 100)
usleep(us - 80);
while ((timer[1] - start) < us)
;
}
uint32_t gpio_micros() {
return timer[1];
}
uint32_t gpio_micros_since(uint32_t *when) {
uint32_t now = timer[1];
uint32_t elapsed;
if (now > *when)
elapsed = now - *when;
else
elapsed = UINT32_MAX - *when + now;
*when = now;
return elapsed;
}
static int init() {
int pagesize = sysconf(_SC_PAGESIZE);
if (timer != 0 && gpio != 0)
return 0; // already initalized
// figure out the IO Base Address
FILE *f = fopen("/proc/cpuinfo", "r");
char buf[1024];
fgets(buf, sizeof(buf), f); // skip first line
fgets(buf, sizeof(buf), f); // model name
uint32_t base = 0;
if (strstr(buf, "ARMv6"))
base = 0x20000000;
else if (strstr(buf, "ARMv7"))
base = 0x3f000000;
else if (strstr(buf, "ARMv8"))
base = 0x3f000000;
else
return xerr("Unknown CPU type\n");
fclose(f);
// access memory
int fd = open("/dev/mem", O_RDWR | O_SYNC);
if (fd == -1)
return xerr("/dev/mem failed: %s\n", strerror(errno));
// mmap timer
timer = (uint32_t*) mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, fd, base + 0x3000);
if (timer == MAP_FAILED)
return xerr("mmap timer failed: %s\n", strerror(errno));
// mmap gpio
gpio = mmap(NULL, pagesize, (PROT_READ | PROT_WRITE), MAP_SHARED, fd, base + 0x200000);
if (gpio == MAP_FAILED)
return xerr("mmap gpio failed: %s\n", strerror(errno));
close(fd);
return 0;
}
static void stop() {
int pagesize = sysconf(_SC_PAGESIZE);
munmap((void*) gpio, pagesize);
munmap((void*) timer, pagesize);
}
int gpio_main(int argc, char **argv) {
set_xlog(XLOG_STDOUT);
set_debug(1);
init();
xlog("mmap OK\n");
test_timers();
test_lirc();
test_flamingo();
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