forked from wiringX/wiringX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raspberrypi.c
941 lines (794 loc) · 23.1 KB
/
raspberrypi.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
/*
Copyright (c) 2014 CurlyMo <[email protected]>
2012 Gordon Henderson
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
#include <poll.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include "wiringX.h"
#ifndef __FreeBSD__
#include "i2c-dev.h"
#endif
#include "raspberrypi.h"
#define WPI_MODE_PINS 0
#define WPI_MODE_GPIO 1
#define WPI_MODE_GPIO_SYS 2
#define WPI_MODE_PHYS 3
#define WPI_MODE_PIFACE 4
#define WPI_MODE_UNINITIALISED -1
#define PI_GPIO_MASK (0xFFFFFFC0)
#define NUM_PINS 32
#define PI_MODEL_UNKNOWN 0
#define PI_MODEL_A 1
#define PI_MODEL_B 2
#define PI_MODEL_BP 3
#define PI_MODEL_CM 4
#define PI_MODEL_AP 5
#define PI_MODEL_2 6
#define PI_VERSION_UNKNOWN 0
#define PI_VERSION_1 1
#define PI_VERSION_1_1 2
#define PI_VERSION_1_2 3
#define PI_VERSION_2 4
#define PI_MAKER_UNKNOWN 0
#define PI_MAKER_EGOMAN 1
#define PI_MAKER_SONY 2
#define PI_MAKER_QISDA 3
#define PI_MAKER_MBEST 4
static volatile unsigned int BCM2708_PERI_BASE = 0x20000000;
#define GPIO_PADS (BCM2708_PERI_BASE + 0x00100000)
#define CLOCK_BASE (BCM2708_PERI_BASE + 0x00101000)
#define GPIO_BASE (BCM2708_PERI_BASE + 0x00200000)
#define PAGE_SIZE (4*1024)
#define BLOCK_SIZE (4*1024)
static int wiringPiMode = WPI_MODE_UNINITIALISED;
static int piModel2 = 0;
static volatile uint32_t *gpio;
static int pinModes[NUM_PINS];
static uint8_t gpioToShift[] = {
0, 3, 6, 9, 12, 15, 18, 21, 24, 27,
0, 3, 6, 9, 12, 15, 18, 21, 24, 27,
0, 3, 6, 9, 12, 15, 18, 21, 24, 27,
0, 3, 6, 9, 12, 15, 18, 21, 24, 27,
0, 3, 6, 9, 12, 15, 18, 21, 24, 27,
};
static uint8_t gpioToGPFSEL[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
};
static int *pinToGpio;
static int pinToGpioR1[64] = {
17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
0, 1, // I2C - SDA1, SCL1 wpi 8 - 9
8, 7, // SPI - CE1, CE0 wpi 10 - 11
10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
14, 15, // UART - Tx, Rx wpi 15 - 16
// Padding:
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
};
static int pinToGpioR2[64] = {
17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7: wpi 0 - 7
2, 3, // I2C - SDA0, SCL0 wpi 8 - 9
8, 7, // SPI - CE1, CE0 wpi 10 - 11
10, 9, 11, // SPI - MOSI, MISO, SCLK wpi 12 - 14
14, 15, // UART - Tx, Rx wpi 15 - 16
28, 29, 30, 31, // Rev 2: New GPIOs 8 though 11 wpi 17 - 20
5, 6, 13, 19, 26, // B+ wpi 21, 22, 23, 24, 25
12, 16, 20, 21, // B+ wpi 26, 27, 28, 29
0, 1, // B+ wpi 30, 31
// Padding:
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
};
static int *physToGpio;
static int physToGpioR1[64] = {
-1, // 0
-1, -1, // 1, 2
0, -1,
1, -1,
4, 14,
-1, 15,
17, 18,
21, -1,
22, 23,
-1, 24,
10, -1,
9, 25,
11, 8,
-1, 7, // 25, 26
-1, -1, -1, -1, -1, // ... 31
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
} ;
static int physToGpioR2[64] = {
-1, // 0
-1, -1, // 1, 2
2, -1,
3, -1,
4, 14,
-1, 15,
17, 18,
27, -1,
22, 23,
-1, 24,
10, -1,
9, 25,
11, 8,
-1, 7, // 25, 26
// B+
0, 1,
5, -1,
6, 12,
13, -1,
19, 16,
26, 20,
-1, 21,
// the P5 connector on the Rev 2 boards:
-1, -1,
-1, -1,
-1, -1,
-1, -1,
-1, -1,
28, 29,
30, 31,
-1, -1,
-1, -1,
-1, -1,
-1, -1,
};
static uint8_t gpioToGPSET[] = {
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
} ;
static uint8_t gpioToGPCLR[] = {
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
};
static uint8_t gpioToGPLEV[] = {
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
};
static int sysFds[64] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
/* SPI Bus Parameters */
static uint8_t spiMode = 0;
static uint8_t spiBPW = 8;
static uint16_t spiDelay = 0;
static uint32_t spiSpeeds[2];
static int spiFds[2];
int raspberrypiValidGPIO(int pin) {
if(pinToGpio[pin] != -1) {
return 0;
}
return -1;
}
static int changeOwner(char *file) {
uid_t uid = getuid();
uid_t gid = getgid();
if(chown(file, uid, gid) != 0) {
if(errno == ENOENT) {
wiringXLog(LOG_ERR, "raspberrypi->changeOwner: File not present: %s", file);
return -1;
} else {
wiringXLog(LOG_ERR, "raspberrypi->changeOwner: Unable to change ownership of %s: %s", file, strerror (errno));
return -1;
}
}
return 0;
}
static int piBoardRev(void) {
FILE *cpuFd;
char line[120], revision[120], hardware[120], name[120];
char *c;
static int boardRev = -1;
if((cpuFd = fopen("/proc/cpuinfo", "r")) == NULL) {
wiringXLog(LOG_ERR, "raspberrypi->identify: Unable open /proc/cpuinfo");
return -1;
}
while(fgets(line, 120, cpuFd) != NULL) {
if(strncmp(line, "Revision", 8) == 0) {
strcpy(revision, line);
}
if(strncmp(line, "Hardware", 8) == 0) {
strcpy(hardware, line);
}
}
fclose(cpuFd);
sscanf(hardware, "Hardware%*[ \t]:%*[ ]%[a-zA-Z0-9 ./()]%*[\n]", name);
if(strstr(name, "BCM2708") != NULL) {
if(boardRev != -1) {
return boardRev;
}
if((cpuFd = fopen("/proc/cpuinfo", "r")) == NULL) {
wiringXLog(LOG_ERR, "raspberrypi->identify: Unable to open /proc/cpuinfo");
return -1;
}
while(fgets(line, 120, cpuFd) != NULL) {
if(strncmp(line, "Revision", 8) == 0) {
break;
}
}
fclose(cpuFd);
if(strncmp(line, "Revision", 8) != 0) {
wiringXLog(LOG_ERR, "raspberrypi->identify: No \"Revision\" line");
return -1;
}
for(c = &line[strlen(line) - 1] ; (*c == '\n') || (*c == '\r'); --c) {
*c = 0;
}
for(c = line; *c; ++c) {
if(isdigit(*c)) {
break;
}
}
if(!isdigit(*c)) {
wiringXLog(LOG_ERR, "raspberrypi->identify: No numeric revision string");
return -1;
}
if(strlen(c) < 4) {
wiringXLog(LOG_ERR, "raspberrypi->identify: Bogus \"Revision\" line (too small)");
return -1;
}
c = c + strlen(c) - 4;
if((strcmp(c, "0002") == 0) || (strcmp(c, "0003") == 0)) {
boardRev = 1;
} else {
boardRev = 2;
}
return boardRev;
} else if(strstr(name, "BCM2709") != NULL) {
piModel2 = 1;
boardRev = 2;
return boardRev;
} else {
return -1;
}
}
static int piBoardId(int *model, int *rev, int *mem, int *maker, int *overVolted) {
FILE *cpuFd ;
char line [120] ;
char *c ;
(void)piBoardRev(); // Call this first to make sure all's OK. Don't care about the result.
if((cpuFd = fopen("/proc/cpuinfo", "r")) == NULL) {
wiringXLog(LOG_ERR, "raspberrypi->piBoardId: Unable to open /proc/cpuinfo");
return -1;
}
while(fgets (line, 120, cpuFd) != NULL) {
if(strncmp (line, "Revision", 8) == 0) {
break;
}
}
fclose(cpuFd);
if(strncmp(line, "Revision", 8) != 0) {
wiringXLog(LOG_ERR, "raspberrypi->piBoardId: No \"Revision\" line");
return -1;
}
// Chomp trailing CR/NL
for(c = &line[strlen(line) - 1]; (*c == '\n') || (*c == '\r'); --c) {
*c = 0;
}
// Scan to first digit
for(c = line; *c; ++c) {
if(isdigit(*c)) {
break;
}
}
// Make sure its long enough
if(strlen(c) < 4) {
wiringXLog(LOG_ERR, "raspberrypi->piBoardId: Bogus \"Revision\" line");
return -1;
}
// If longer than 4, we'll assume it's been overvolted
*overVolted = strlen(c) > 4;
// Extract last 4 characters:
c = c + strlen(c) - 4;
// Fill out the replys as appropriate
if(piModel2 == 1) {
if(strcmp (c, "0010") == 0) {
*model = PI_MODEL_2;
*rev = PI_VERSION_1_1;
*mem = 1024;
*maker = PI_MAKER_SONY;
} else {
*model = 0;
*rev = 0;
*mem = 0;
*maker = 0;
}
} else if(strcmp(c, "0002") == 0) {
*model = PI_MODEL_B;
*rev = PI_VERSION_1;
*mem = 256;
*maker = PI_MAKER_EGOMAN;
} else if(strcmp(c, "0003") == 0) {
*model = PI_MODEL_B;
*rev = PI_VERSION_1_1;
*mem = 256;
*maker = PI_MAKER_EGOMAN;
} else if(strcmp(c, "0004") == 0) {
*model = PI_MODEL_B;
*rev = PI_VERSION_2;
*mem = 256;
*maker = PI_MAKER_SONY;
} else if(strcmp(c, "0005") == 0) {
*model = PI_MODEL_B;
*rev = PI_VERSION_2;
*mem = 256;
*maker = PI_MAKER_QISDA;
} else if(strcmp(c, "0006") == 0) {
*model = PI_MODEL_B;
*rev = PI_VERSION_2;
*mem = 256;
*maker = PI_MAKER_EGOMAN;
} else if(strcmp(c, "0007") == 0) {
*model = PI_MODEL_A;
*rev = PI_VERSION_2;
*mem = 256;
*maker = PI_MAKER_EGOMAN;
} else if(strcmp(c, "0008") == 0) {
*model = PI_MODEL_A;
*rev = PI_VERSION_2;
*mem = 256;
*maker = PI_MAKER_SONY;
} else if(strcmp(c, "0009") == 0) {
*model = PI_MODEL_B;
*rev = PI_VERSION_2;
*mem = 256;
*maker = PI_MAKER_QISDA;
} else if(strcmp(c, "000d") == 0) {
*model = PI_MODEL_B;
*rev = PI_VERSION_2;
*mem = 512;
*maker = PI_MAKER_EGOMAN;
} else if(strcmp(c, "000e") == 0) {
*model = PI_MODEL_B;
*rev = PI_VERSION_2;
*mem = 512;
*maker = PI_MAKER_SONY;
} else if(strcmp(c, "000f") == 0) {
*model = PI_MODEL_B;
*rev = PI_VERSION_2;
*mem = 512;
*maker = PI_MAKER_EGOMAN;
} else if(strcmp(c, "0010") == 0) {
*model = PI_MODEL_BP;
*rev = PI_VERSION_1_2;
*mem = 512;
*maker = PI_MAKER_SONY;
} else if(strcmp(c, "0011") == 0) {
*model = PI_MODEL_CM;
*rev = PI_VERSION_1_2;
*mem = 512;
*maker = PI_MAKER_SONY;
} else if(strcmp(c, "0012") == 0) {
*model = PI_MODEL_AP;
*rev = PI_VERSION_1_2;
*mem = 256;
*maker = PI_MAKER_SONY;
} else if(strcmp(c, "0013") == 0) {
*model = PI_MODEL_BP;
*rev = PI_VERSION_1_2;
*mem = 512;
*maker = PI_MAKER_MBEST;
} else {
*model = 0;
*rev = 0;
*mem = 0;
*maker = 0;
}
return 0;
}
static int setup(void) {
int fd;
int boardRev;
int model, rev, mem, maker, overVolted;
boardRev = piBoardRev();
if(boardRev == 1) {
pinToGpio = pinToGpioR1;
physToGpio = physToGpioR1;
} else {
if(piModel2 == 1) {
BCM2708_PERI_BASE = 0x3F000000;
}
pinToGpio = pinToGpioR2;
physToGpio = physToGpioR2;
}
if((fd = open("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0) {
wiringXLog(LOG_ERR, "raspberrypi->setup: Unable to open /dev/mem: %s", strerror(errno));
return -1;
}
gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE);
if((int32_t)gpio == -1) {
wiringXLog(LOG_ERR, "raspberrypi->setup: mmap (GPIO) failed: %s", strerror(errno));
return -1;
}
if(piBoardId(&model, &rev, &mem, &maker, &overVolted) == -1) {
return -1;
}
if(model == PI_MODEL_CM) {
wiringPiMode = WPI_MODE_GPIO;
} else {
wiringPiMode = WPI_MODE_PINS;
}
return 0;
}
static int raspberrypiDigitalRead(int pin) {
if(pinModes[pin] != INPUT && pinModes[pin] != SYS) {
wiringXLog(LOG_ERR, "raspberrypi->digitalRead: Trying to write to pin %d, but it's not configured as input", pin);
return -1;
}
if(raspberrypiValidGPIO(pin) != 0) {
wiringXLog(LOG_ERR, "raspberrypi->digitalRead: Invalid pin number %d (0 >= pin <= 31)", pin);
return -1;
}
if((pin & PI_GPIO_MASK) == 0) {
if(wiringPiMode == WPI_MODE_PINS) {
pin = pinToGpio[pin] ;
} else if (wiringPiMode == WPI_MODE_PHYS) {
pin = physToGpio[pin] ;
} else if (wiringPiMode != WPI_MODE_GPIO) {
return -1;
}
if((*(gpio + gpioToGPLEV[pin]) & (1 << (pin & 31))) != 0) {
return HIGH;
} else {
return LOW;
}
}
return 0;
}
static int raspberrypiDigitalWrite(int pin, int value) {
if(pinModes[pin] != OUTPUT) {
wiringXLog(LOG_ERR, "raspberrypi->digitalWrite: Trying to write to pin %d, but it's not configured as output", pin);
return -1;
}
if(raspberrypiValidGPIO(pin) != 0) {
wiringXLog(LOG_ERR, "raspberrypi->digitalWrite: Invalid pin number %d (0 >= pin <= 31)", pin);
return -1;
}
if((pin & PI_GPIO_MASK) == 0) {
if(wiringPiMode == WPI_MODE_PINS) {
pin = pinToGpio[pin] ;
} else if(wiringPiMode == WPI_MODE_PHYS) {
pin = physToGpio[pin] ;
} else if(wiringPiMode != WPI_MODE_GPIO) {
return -1;
}
if(value == LOW)
*(gpio + gpioToGPCLR [pin]) = 1 << (pin & 31);
else
*(gpio + gpioToGPSET [pin]) = 1 << (pin & 31);
}
return 0;
}
static int raspberrypiPinMode(int pin, int mode) {
int fSel, shift;
if(raspberrypiValidGPIO(pin) != 0) {
wiringXLog(LOG_ERR, "raspberrypi->pinMode: Invalid pin number %d (0 >= pin <= 31)", pin);
return -1;
}
if((pin & PI_GPIO_MASK) == 0) {
pinModes[pin] = mode;
if(wiringPiMode == WPI_MODE_PINS) {
pin = pinToGpio[pin];
} else if(wiringPiMode == WPI_MODE_PHYS)
pin = physToGpio[pin];
else if(wiringPiMode != WPI_MODE_GPIO)
return -1;
fSel = gpioToGPFSEL[pin];
shift = gpioToShift[pin];
if(mode == INPUT) {
*(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift));
} else if(mode == OUTPUT) {
*(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | (1 << shift);
}
}
return 0;
}
static int raspberrypiISR(int pin, int mode) {
int i = 0, fd = 0, match = 0, count = 0;
const char *sMode = NULL;
char path[35], c, line[120];
FILE *f = NULL;
if(raspberrypiValidGPIO(pin) != 0) {
wiringXLog(LOG_ERR, "raspberrypi->isr: Invalid pin number %d (0 >= pin <= 31)", pin);
return -1;
}
pinModes[pin] = SYS;
if(mode == INT_EDGE_FALLING) {
sMode = "falling" ;
} else if(mode == INT_EDGE_RISING) {
sMode = "rising" ;
} else if(mode == INT_EDGE_BOTH) {
sMode = "both";
} else {
wiringXLog(LOG_ERR, "raspberrypi->isr: Invalid mode. Should be INT_EDGE_BOTH, INT_EDGE_RISING, or INT_EDGE_FALLING");
return -1;
}
sprintf(path, "/sys/class/gpio/gpio%d/value", pinToGpio[pin]);
fd = open(path, O_RDWR);
if(fd < 0) {
if((f = fopen("/sys/class/gpio/export", "w")) == NULL) {
wiringXLog(LOG_ERR, "raspberrypi->isr: Unable to open GPIO export interface: %s", strerror(errno));
return -1;
}
fprintf(f, "%d\n", pinToGpio[pin]);
fclose(f);
}
sprintf(path, "/sys/class/gpio/gpio%d/direction", pinToGpio[pin]);
if((f = fopen(path, "w")) == NULL) {
wiringXLog(LOG_ERR, "raspberrypi->isr: Unable to open GPIO direction interface for pin %d: %s", pin, strerror(errno));
return -1;
}
fprintf(f, "in\n");
fclose(f);
sprintf(path, "/sys/class/gpio/gpio%d/edge", pinToGpio[pin]);
if((f = fopen(path, "w")) == NULL) {
wiringXLog(LOG_ERR, "raspberrypi->isr: Unable to open GPIO edge interface for pin %d: %s", pin, strerror(errno));
return -1;
}
if(strcasecmp(sMode, "none") == 0) {
fprintf(f, "none\n");
} else if(strcasecmp(sMode, "rising") == 0) {
fprintf(f, "rising\n");
} else if(strcasecmp(sMode, "falling") == 0) {
fprintf(f, "falling\n");
} else if(strcasecmp (sMode, "both") == 0) {
fprintf(f, "both\n");
} else {
wiringXLog(LOG_ERR, "raspberrypi->isr: Invalid mode: %s. Should be rising, falling or both", sMode);
return -1;
}
fclose(f);
if((f = fopen(path, "r")) == NULL) {
wiringXLog(LOG_ERR, "raspberrypi->isr: Unable to open GPIO edge interface for pin %d: %s", pin, strerror(errno));
return -1;
}
match = 0;
while(fgets(line, 120, f) != NULL) {
if(strstr(line, sMode) != NULL) {
match = 1;
break;
}
}
fclose(f);
if(match == 0) {
wiringXLog(LOG_ERR, "raspberrypi->isr: Failed to set interrupt edge to %s", sMode);
return -1;
}
sprintf(path, "/sys/class/gpio/gpio%d/value", pinToGpio[pin]);
if((sysFds[pin] = open(path, O_RDONLY)) < 0) {
wiringXLog(LOG_ERR, "raspberrypi->isr: Unable to open GPIO value interface: %s", strerror(errno));
return -1;
}
changeOwner(path);
sprintf(path, "/sys/class/gpio/gpio%d/edge", pinToGpio[pin]);
changeOwner(path);
ioctl(fd, FIONREAD, &count);
for(i=0; i<count; ++i) {
read(fd, &c, 1);
}
close(fd);
return 0;
}
static int raspberrypiWaitForInterrupt(int pin, int ms) {
int x = 0;
uint8_t c = 0;
struct pollfd polls;
if(raspberrypiValidGPIO(pin) != 0) {
wiringXLog(LOG_ERR, "raspberrypi->waitForInterrupt: Invalid pin number %d (0 >= pin <= 31)", pin);
return -1;
}
if(pinModes[pin] != SYS) {
wiringXLog(LOG_ERR, "raspberrypi->waitForInterrupt: Trying to read from pin %d, but it's not configured as interrupt", pin);
return -1;
}
if(sysFds[pin] == -1) {
wiringXLog(LOG_ERR, "raspberrypi->waitForInterrupt: GPIO %d not set as interrupt", pin);
return -1;
}
polls.fd = sysFds[pin];
polls.events = POLLPRI;
x = poll(&polls, 1, ms);
/* Don't react to signals */
if(x == -1 && errno == EINTR) {
x = 0;
}
(void)read(sysFds[pin], &c, 1);
lseek(sysFds[pin], 0, SEEK_SET);
return x;
}
static int raspberrypiGC(void) {
int i = 0, fd = 0;
char path[35];
FILE *f = NULL;
for(i=0;i<NUM_PINS;i++) {
if(pinModes[i] == OUTPUT) {
pinMode(i, INPUT);
} else if(pinModes[i] == SYS) {
sprintf(path, "/sys/class/gpio/gpio%d/value", pinToGpio[i]);
if((fd = open(path, O_RDWR)) > 0) {
if((f = fopen("/sys/class/gpio/unexport", "w")) == NULL) {
wiringXLog(LOG_ERR, "raspberrypi->gc: Unable to open GPIO unexport interface: %s", strerror(errno));
}
fprintf(f, "%d\n", pinToGpio[i]);
fclose(f);
close(fd);
}
}
if(sysFds[i] > 0) {
close(sysFds[i]);
}
}
if(gpio) {
munmap((void *)gpio, BLOCK_SIZE);
}
return 0;
}
#ifndef __FreeBSD__
static int raspberrypiI2CRead(int fd) {
return i2c_smbus_read_byte(fd);
}
static int raspberrypiI2CReadReg8(int fd, int reg) {
return i2c_smbus_read_byte_data(fd, reg);
}
static int raspberrypiI2CReadReg16(int fd, int reg) {
return i2c_smbus_read_word_data(fd, reg);
}
static int raspberrypiI2CWrite(int fd, int data) {
return i2c_smbus_write_byte(fd, data);
}
static int raspberrypiI2CWriteReg8(int fd, int reg, int data) {
return i2c_smbus_write_byte_data(fd, reg, data);
}
static int raspberrypiI2CWriteReg16(int fd, int reg, int data) {
return i2c_smbus_write_word_data(fd, reg, data);
}
static int raspberrypiI2CSetup(int devId) {
int rev = 0, fd = 0;
const char *device = NULL;
if((rev = piBoardRev()) < 0) {
wiringXLog(LOG_ERR, "raspberrypi->I2CSetup: Unable to determine Pi board revision");
return -1;
}
if(rev == 1)
device = "/dev/i2c-0";
else
device = "/dev/i2c-1";
if((fd = open(device, O_RDWR)) < 0) {
wiringXLog(LOG_ERR, "raspberrypi->I2CSetup: Unable to open %s: %s", device, strerror(errno));
return -1;
}
if(ioctl(fd, I2C_SLAVE, devId) < 0) {
wiringXLog(LOG_ERR, "raspberrypi->I2CSetup: Unable to set %s to slave mode: %s", device, strerror(errno));
return -1;
}
return fd;
}
int raspberrypiSPIGetFd(int channel) {
return spiFds[channel & 1];
}
int raspberrypiSPIDataRW(int channel, unsigned char *data, int len) {
struct spi_ioc_transfer spi;
memset(&spi, 0, sizeof(spi)); // found at http://www.raspberrypi.org/forums/viewtopic.php?p=680665#p680665
channel &= 1;
spi.tx_buf = (unsigned long)data;
spi.rx_buf = (unsigned long)data;
spi.len = len;
spi.delay_usecs = spiDelay;
spi.speed_hz = spiSpeeds[channel];
spi.bits_per_word = spiBPW;
#ifdef SPI_IOC_WR_MODE32
spi.tx_nbits = 0;
#endif
#ifdef SPI_IOC_RD_MODE32
spi.rx_nbits = 0;
#endif
if(ioctl(spiFds[channel], SPI_IOC_MESSAGE(1), &spi) < 0) {
wiringXLog(LOG_ERR, "raspberrypi->SPIDataRW: Unable to read/write from channel %d: %s", channel, strerror(errno));
return -1;
}
return 0;
}
int raspberrypiSPISetup(int channel, int speed) {
int fd;
const char *device = NULL;
channel &= 1;
if(channel == 0) {
device = "/dev/spidev0.0";
} else {
device = "/dev/spidev0.1";
}
if((fd = open(device, O_RDWR)) < 0) {
wiringXLog(LOG_ERR, "raspberrypi->SPISetup: Unable to open device %s: %s", device, strerror(errno));
return -1;
}
spiSpeeds[channel] = speed;
spiFds[channel] = fd;
if(ioctl(fd, SPI_IOC_WR_MODE, &spiMode) < 0) {
wiringXLog(LOG_ERR, "raspberrypi->SPISetup: Unable to set write mode for device %s: %s", device, strerror(errno));
return -1;
}
if(ioctl(fd, SPI_IOC_RD_MODE, &spiMode) < 0) {
wiringXLog(LOG_ERR, "raspberrypi->SPISetup: Unable to set read mode for device %s: %s", device, strerror(errno));
return -1;
}
if(ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) {
wiringXLog(LOG_ERR, "raspberrypi->SPISetup: Unable to set write bits_per_word for device %s: %s", device, strerror(errno));
return -1;
}
if(ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &spiBPW) < 0) {
wiringXLog(LOG_ERR, "raspberrypi->SPISetup: Unable to set read bits_per_word for device %s: %s", device, strerror(errno));
return -1;
}
if(ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) {
wiringXLog(LOG_ERR, "raspberrypi->SPISetup: Unable to set write max_speed for device %s: %s", device, strerror(errno));
return -1;
}
if(ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) {
wiringXLog(LOG_ERR, "raspberrypi->SPISetup: Unable to set read max_speed for device %s: %s", device, strerror(errno));
return -1;
}
return fd;
}
#endif
void raspberrypiInit(void) {
memset(pinModes, -1, NUM_PINS);
platform_register(&raspberrypi, "raspberrypi");
raspberrypi->setup=&setup;
raspberrypi->pinMode=&raspberrypiPinMode;
raspberrypi->digitalWrite=&raspberrypiDigitalWrite;
raspberrypi->digitalRead=&raspberrypiDigitalRead;
raspberrypi->identify=&piBoardRev;
raspberrypi->isr=&raspberrypiISR;
raspberrypi->waitForInterrupt=&raspberrypiWaitForInterrupt;
#ifndef __FreeBSD__
raspberrypi->I2CRead=&raspberrypiI2CRead;
raspberrypi->I2CReadReg8=&raspberrypiI2CReadReg8;
raspberrypi->I2CReadReg16=&raspberrypiI2CReadReg16;
raspberrypi->I2CWrite=&raspberrypiI2CWrite;
raspberrypi->I2CWriteReg8=&raspberrypiI2CWriteReg8;
raspberrypi->I2CWriteReg16=&raspberrypiI2CWriteReg16;
raspberrypi->I2CSetup=&raspberrypiI2CSetup;
raspberrypi->SPIGetFd=&raspberrypiSPIGetFd;
raspberrypi->SPIDataRW=&raspberrypiSPIDataRW;
raspberrypi->SPISetup=&raspberrypiSPISetup;
#endif
raspberrypi->gc=&raspberrypiGC;
raspberrypi->validGPIO=&raspberrypiValidGPIO;
}