-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiringPi.c
444 lines (355 loc) · 11.1 KB
/
wiringPi.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
/*
* SPDX-License-Identifier: MIT
* Copyright © 2023 Lesosoftware https://github.com/leso-kn.
*
* wiringpi-gpiod - Main library implementation.
*/
#include "wiringPi.h"
#include <malloc.h>
#include <memory.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <gpiod.h>
#include <pthread.h>
#include <sched.h>
#include <sys/time.h>
#include <unistd.h>
//
#define LOG_WARN "[\033[33mwarn\033[0m] "
struct gpiod_chip *chip = NULL;
static uint64_t epochMilli, epochMicro;
//
const char *piModelNames[] = {"generic"};
const char *piRevisionNames[] = {"generic"};
const char *piMakerNames[] = {"generic"};
const int piMemorySize[] = {0};
struct wiringPiNodeStruct *wiringPiNodes = NULL;
/*
* Export variables for the hardware pointers
*/
volatile unsigned int *_wiringPiGpio = 0;
volatile unsigned int *_wiringPiPwm = 0;
volatile unsigned int *_wiringPiClk = 0;
volatile unsigned int *_wiringPiPads = 0;
volatile unsigned int *_wiringPiTimer = 0;
volatile unsigned int *_wiringPiTimerIrqRaw = 0;
int wiringPiFailure(int fatal, const char *message, ...) {
va_list argp;
va_start(argp, message);
vfprintf(stderr, message, argp);
va_end(argp);
exit(fatal);
}
/*
* Core wiringPi functions
*/
struct wiringPiNodeStruct *wiringPiFindNode(int pin) {
fprintf(stderr, LOG_WARN
"wiringPiFindNode() called\n "
"wiring-pi-nodes are currently not supported by wiringpi-gpiod\n");
return malloc(sizeof(struct wiringPiNodeStruct));
};
struct wiringPiNodeStruct *wiringPiNewNode(int pinBase, int numPins) {
fprintf(stderr, LOG_WARN
"wiringPiNewNode() called\n "
"wiring-pi-nodes are currently not supported by wiringpi-gpiod\n");
return malloc(sizeof(struct wiringPiNodeStruct));
}
void wiringPiVersion(int *major, int *minor) {
*major = WIRINGPIGEN_VERSION_MAJOR;
*minor = WIRINGPIGEN_VERSION_MINOR;
}
int wiringPiSetup(void) {
chip = gpiod_chip_open("/dev/gpiochip0");
// Initialize Epoch
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
epochMilli = (uint64_t)ts.tv_sec * (uint64_t)1000 +
(uint64_t)(ts.tv_nsec / 1000000L);
epochMicro = (uint64_t)ts.tv_sec * (uint64_t)1000000 +
(uint64_t)(ts.tv_nsec / 1000L);
return 0;
};
int wiringPiSetupSys(void) { return wiringPiSetup(); };
int wiringPiSetupGpio(void) { return wiringPiSetup(); };
int wiringPiSetupPhys(void) { return wiringPiSetup(); };
void pinModeAlt(int pin, int mode) {
fprintf(stderr,
LOG_WARN "pinModeAlt() called\n "
"alternative pin modes are currently not supported by "
"wiringpi-gpiod\n");
};
void pinMode(int pin, int mode) {
struct gpiod_line *line = gpiod_chip_get_line(chip, pin);
switch (mode) {
case OUTPUT:
if (gpiod_line_set_direction_output(line, 1) != 1)
gpiod_line_request_output(line, "wiringpi-gpiod", 1);
break;
case INPUT:
if (gpiod_line_set_direction_input(line) != 0)
gpiod_line_request_input(line, "wiringpi-gpiod");
break;
}
};
void pullUpDnControl(int pin, int pud) {
struct gpiod_line *line = gpiod_chip_get_line(chip, pin);
switch (pud) {
case PUD_OFF:
gpiod_line_set_flags(line, GPIOD_LINE_REQUEST_FLAG_BIAS_DISABLE);
break;
case PUD_DOWN:
gpiod_line_set_flags(line, GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_DOWN);
break;
case PUD_UP:
gpiod_line_set_flags(line, GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP);
break;
}
};
int digitalRead(int pin) {
struct gpiod_line *line = gpiod_chip_get_line(chip, pin);
return gpiod_line_get_value(line);
};
void digitalWrite(int pin, int value) {
struct gpiod_line *line = gpiod_chip_get_line(chip, pin);
if (gpiod_line_direction(line) == GPIOD_LINE_DIRECTION_INPUT)
gpiod_line_set_flags(line, value
? GPIOD_LINE_REQUEST_FLAG_BIAS_PULL_UP
: GPIOD_LINE_REQUEST_FLAG_BIAS_DISABLE);
else
gpiod_line_set_value(line, value);
};
void pwmWrite(int pin, int value) {
fprintf(stderr,
LOG_WARN "pwmWrite() called\n "
"pwm is currently not supported by wiringpi-gpiod\n");
}
int analogRead(int pin) {
fprintf(stderr,
LOG_WARN "analogRead() called\n "
"analog is currently not supported by wiringpi-gpiod\n");
};
void analogWrite(int pin, int value) {
fprintf(stderr,
LOG_WARN "analogWrite() called\n "
"analog is currently not supported by wiringpi-gpiod\n");
};
/*
* PiFace specifics
* (Deprecated)
*/
// @deprecated PiFace functionality is deprecated
int wiringPiSetupPiFace(void) {
fprintf(stderr, LOG_WARN
"wiringPiSetupPiFace() called\n "
"Pi-faces are currently not supported by wiringpi-gpiod\n");
};
/* Don't use this - for gpio program only
* @deprecated PiFace functionality is deprecated
*/
int wiringPiSetupPiFaceForGpioProg(void) {
fprintf(stderr, LOG_WARN
"wiringPiSetupPiFaceForGpioProg() called\n "
"Pi-faces are currently not supported by wiringpi-gpiod\n");
}
/*
* On-Board Raspberry Pi hardware specific stuff
*/
int piGpioLayout(void) { return gpiod_chip_num_lines(chip) > 26 ? 2 : 1; }
// @deprecated Use piBoardId() instead
int piBoardRev(void) {
int rev = 0;
piBoardId(NULL, &rev, NULL, NULL, NULL);
return rev;
}
void piBoardId(int *model, int *rev, int *mem, int *maker, int *overVolted) {
switch (piGpioLayout()) {
case 1:
*model = PI_MODEL_A;
*rev = PI_VERSION_1;
*mem = 0;
*maker = PI_MAKER_UNKNOWN;
*overVolted = 0;
break;
case 2:
*model = PI_MODEL_3BP;
*rev = PI_VERSION_2;
*mem = 0;
*maker = PI_MAKER_UNKNOWN;
*overVolted = 0;
break;
}
}
int wpiPinToGpio(int wpiPin) {
fprintf(stderr, LOG_WARN
"wpiPinToGpio() called\n "
"not converting value, wiringpi-gpiod uses physical pin numbers\n");
return wpiPin;
}
int physPinToGpio(int physPin) {
fprintf(stderr, LOG_WARN
"physPinToGpio() called\n "
"not converting value, wiringpi-gpiod uses physical pin numbers\n");
return physPin;
}
void setPadDrive(int group, int value) {}
int getAlt(int pin) {
fprintf(stderr,
LOG_WARN "getAlt() called\n "
"alternative pin modes are currently not supported by "
"wiringpi-gpiod\n");
return 0;
}
void pwmToneWrite(int pin, int freq) {
fprintf(stderr,
LOG_WARN "pwmSetClock() called\n "
"pwm is currently not supported by wiringpi-gpiod\n");
}
void pwmSetMode(int mode) {
fprintf(stderr,
LOG_WARN "pwmSetClock() called\n "
"pwm is currently not supported by wiringpi-gpiod\n");
}
void pwmSetRange(unsigned int range) {
fprintf(stderr,
LOG_WARN "pwmSetClock() called\n "
"pwm is currently not supported by wiringpi-gpiod\n");
}
void pwmSetClock(int divisor) {
fprintf(stderr,
LOG_WARN "pwmSetClock() called\n "
"pwm is currently not supported by wiringpi-gpiod\n");
}
void gpioClockSet(int pin, int freq) {
fprintf(stderr,
LOG_WARN "gpioClockSet() called\n "
"Clocks are currently not supported by wiringpi-gpiod\n");
}
unsigned int digitalReadByte(void) {
int pin, x;
uint32_t raw;
uint32_t data = 0;
for (pin = 0; pin < 8; ++pin) {
x = digitalRead(pin);
data = (data << 1) | x;
}
return data;
}
unsigned int digitalReadByte2(void) {
int pin, x;
uint32_t data = 0;
for (pin = 20; pin < 28; ++pin) {
x = digitalRead(pin);
data = (data << 1) | x;
}
return data;
}
void digitalWriteByte(const int value) {
uint32_t pinSet = 0;
uint32_t pinClr = 0;
int mask = 1;
int pin;
for (pin = 0; pin < 8; ++pin) {
digitalWrite(pin, value & mask);
mask <<= 1;
}
return;
}
void digitalWriteByte2(const int value) {
register int mask = 1;
register int pin;
for (pin = 20; pin < 28; ++pin) {
digitalWrite(pin, value & mask);
mask <<= 1;
}
return;
}
/*
* Interrupts (Also Pi hardware specific)
*/
int waitForInterrupt(int pin, int mS) {
fprintf(stderr, LOG_WARN
"waitForInterrupt() called\n "
"Interrupts are currently not supported by wiringpi-gpiod\n");
return 0;
};
int wiringPiISR(int pin, int mode, void (*function)(void)) {
fprintf(stderr, LOG_WARN
"wiringPiISR() called\n "
"Interrupts are currently not supported by wiringpi-gpiod\n");
return 0;
};
/*
* Threads
*/
static pthread_mutex_t piMutexes[4];
int piThreadCreate(void *(*fn)(void *)) {
pthread_t myThread;
return pthread_create(&myThread, NULL, fn, NULL);
}
void piLock(int key) { pthread_mutex_lock(&piMutexes[key]); }
void piUnlock(int key) { pthread_mutex_unlock(&piMutexes[key]); }
/*
* Schedulling priority
*/
int piHiPri(const int pri) {
struct sched_param sched;
memset(&sched, 0, sizeof(sched));
if (pri > sched_get_priority_max(SCHED_RR))
sched.sched_priority = sched_get_priority_max(SCHED_RR);
else
sched.sched_priority = pri;
return sched_setscheduler(0, SCHED_RR, &sched);
}
/*
* Extras from arduino land
*/
// wait for n milliseconds
void delay(unsigned int howLong) {
struct timespec sleeper, dummy;
sleeper.tv_sec = (time_t)(howLong / 1000);
sleeper.tv_nsec = (long)(howLong % 1000) * 1000000;
nanosleep(&sleeper, &dummy);
}
void delayMicrosecondsHard(unsigned int howLong) {
struct timeval tNow, tLong, tEnd;
gettimeofday(&tNow, NULL);
tLong.tv_sec = howLong / 1000000;
tLong.tv_usec = howLong % 1000000;
timeradd(&tNow, &tLong, &tEnd);
while (timercmp(&tNow, &tEnd, <))
gettimeofday(&tNow, NULL);
}
// wait for n microseconds
void delayMicroseconds(unsigned int howLong) {
struct timespec sleeper;
unsigned int uSecs = howLong % 1000000;
unsigned int wSecs = howLong / 1000000;
/**/ if (howLong == 0)
return;
else if (howLong < 100)
delayMicrosecondsHard(howLong);
else {
sleeper.tv_sec = wSecs;
sleeper.tv_nsec = (long)(uSecs * 1000L);
nanosleep(&sleeper, NULL);
}
}
unsigned int millis(void) {
uint64_t now;
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
now = (uint64_t)ts.tv_sec * (uint64_t)1000 +
(uint64_t)(ts.tv_nsec / 1000000L);
return (uint32_t)(now - epochMilli);
}
unsigned int micros(void) {
uint64_t now;
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
now =
(uint64_t)ts.tv_sec * (uint64_t)1000000 + (uint64_t)(ts.tv_nsec / 1000);
return (uint32_t)(now - epochMicro);
}