-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
gpio.c
1538 lines (1205 loc) · 36.5 KB
/
gpio.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
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* gpio.c:
* Swiss-Army-Knife, Set-UID command-line interface to the Raspberry
* Pi's GPIO.
* Copyright (c) 2012-2018 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <wiringPi.h>
#include <wpiExtensions.h>
#include <gertboard.h>
#include <piFace.h>
#include "../version.h"
extern int wiringPiDebug ;
// External functions I can't be bothered creating a separate .h file for:
extern void doReadall (void) ;
extern void doAllReadall (void) ;
extern void doQmode (int argc, char *argv []) ;
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (1==2)
#endif
#define PI_USB_POWER_CONTROL 38
#define I2CDETECT "i2cdetect"
#define MODPROBE "modprobe"
#define RMMOD "rmmod"
int wpMode ;
char *usage = "Usage: gpio -v\n"
" gpio -h\n"
" gpio [-g|-1] ...\n"
" gpio [-d] ...\n"
" [-x extension:params] [[ -x ...]] ...\n"
" gpio [-p] <read/write/wb> ...\n"
" gpio <mode/read/write/aread/awritewb/pwm/pwmTone/clock> ...\n"
" gpio <toggle/blink> <pin>\n"
" gpio readall\n"
" gpio unexportall/exports\n"
" gpio export/edge/unexport ...\n"
" gpio wfi <pin> <mode>\n"
" gpio drive <group> <value>\n"
" gpio pwm-bal/pwm-ms \n"
" gpio pwmr <range> \n"
" gpio pwmc <divider> \n"
" gpio load spi/i2c\n"
" gpio unload spi/i2c\n"
" gpio i2cd/i2cdetect\n"
" gpio rbx/rbd\n"
" gpio wb <value>\n"
" gpio usbp high/low\n"
" gpio gbr <channel>\n"
" gpio gbw <channel> <value>" ; // No trailing newline needed here.
#ifdef NOT_FOR_NOW
/*
* decodePin:
* Decode a pin "number" which can actually be a pin name to represent
* one of the Pi's on-board pins.
*********************************************************************************
*/
static int decodePin (const char *str)
{
// The first case - see if it's a number:
if (isdigit (str [0]))
return atoi (str) ;
return 0 ;
}
#endif
/*
* findExecutable:
* Code to locate the path to the given executable. We have a fixed list
* of locations to try which completely overrides any $PATH environment.
* This may be detrimental, however it avoids the reliance on $PATH
* which may be a security issue when this program is run a set-uid-root.
*********************************************************************************
*/
static const char *searchPath [] =
{
"/sbin",
"/usr/sbin",
"/bin",
"/usr/bin",
NULL,
} ;
static char *findExecutable (const char *progName)
{
static char *path = NULL ;
int len = strlen (progName) ;
int i = 0 ;
struct stat statBuf ;
for (i = 0 ; searchPath [i] != NULL ; ++i)
{
path = malloc (strlen (searchPath [i]) + len + 2) ;
sprintf (path, "%s/%s", searchPath [i], progName) ;
if (stat (path, &statBuf) == 0)
return path ;
free (path) ;
}
return NULL ;
}
/*
* changeOwner:
* Change the ownership of the file to the real userId of the calling
* program so we can access it.
*********************************************************************************
*/
static void changeOwner (char *cmd, char *file)
{
uid_t uid = getuid () ;
uid_t gid = getgid () ;
if (chown (file, uid, gid) != 0)
{
// Removed (ignoring) the check for not existing as I'm fed-up with morons telling me that
// the warning message is an error.
if (errno != ENOENT)
fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
}
}
/*
* moduleLoaded:
* Return true/false if the supplied module is loaded
*********************************************************************************
*/
static int moduleLoaded (char *modName)
{
int len = strlen (modName) ;
int found = FALSE ;
FILE *fd = fopen ("/proc/modules", "r") ;
char line [80] ;
if (fd == NULL)
{
fprintf (stderr, "gpio: Unable to check /proc/modules: %s\n", strerror (errno)) ;
exit (1) ;
}
while (fgets (line, 80, fd) != NULL)
{
if (strncmp (line, modName, len) != 0)
continue ;
found = TRUE ;
break ;
}
fclose (fd) ;
return found ;
}
/*
* doLoad:
* Load either the spi or i2c modules and change device ownerships, etc.
*********************************************************************************
*/
static void checkDevTree (char *argv [])
{
struct stat statBuf ;
if (stat ("/proc/device-tree", &statBuf) == 0) // We're on a devtree system ...
{
fprintf (stderr,
"%s: Unable to load/unload modules as this Pi has the device tree enabled.\n"
" You need to run the raspi-config program (as root) and select the\n"
" modules (SPI or I2C) that you wish to load/unload there and reboot.\n", argv [0]) ;
exit (1) ;
}
}
static void _doLoadUsage (char *argv [])
{
fprintf (stderr, "Usage: %s load <spi/i2c> [I2C baudrate in Kb/sec]\n", argv [0]) ;
exit (1) ;
}
static void doLoad (int argc, char *argv [])
{
char *module1, *module2 ;
char cmd [80] ;
char *file1, *file2 ;
char args1 [32], args2 [32] ;
checkDevTree (argv) ;
if (argc < 3)
_doLoadUsage (argv) ;
args1 [0] = args2 [0] = 0 ;
/**/ if (strcasecmp (argv [2], "spi") == 0)
{
module1 = "spidev" ;
module2 = "spi_bcm2708" ;
file1 = "/dev/spidev0.0" ;
file2 = "/dev/spidev0.1" ;
if (argc == 4)
{
fprintf (stderr, "%s: Unable to set the buffer size now. Load aborted. Please see the man page.\n", argv [0]) ;
exit (1) ;
}
else if (argc > 4)
_doLoadUsage (argv) ;
}
else if (strcasecmp (argv [2], "i2c") == 0)
{
module1 = "i2c_dev" ;
module2 = "i2c_bcm2708" ;
file1 = "/dev/i2c-0" ;
file2 = "/dev/i2c-1" ;
if (argc == 4)
sprintf (args2, " baudrate=%d", atoi (argv [3]) * 1000) ;
else if (argc > 4)
_doLoadUsage (argv) ;
}
else
_doLoadUsage (argv) ;
if (findExecutable ("modprobe") == NULL)
printf ("No found\n") ;
if (!moduleLoaded (module1))
{
sprintf (cmd, "%s %s%s", findExecutable (MODPROBE), module1, args1) ;
system (cmd) ;
}
if (!moduleLoaded (module2))
{
sprintf (cmd, "%s %s%s", findExecutable (MODPROBE), module2, args2) ;
system (cmd) ;
}
if (!moduleLoaded (module2))
{
fprintf (stderr, "%s: Unable to load %s\n", argv [0], module2) ;
exit (1) ;
}
sleep (1) ; // To let things get settled
changeOwner (argv [0], file1) ;
changeOwner (argv [0], file2) ;
}
/*
* doUnLoad:
* Un-Load either the spi or i2c modules and change device ownerships, etc.
*********************************************************************************
*/
static void _doUnLoadUsage (char *argv [])
{
fprintf (stderr, "Usage: %s unload <spi/i2c>\n", argv [0]) ;
exit (1) ;
}
static void doUnLoad (int argc, char *argv [])
{
char *module1, *module2 ;
char cmd [80] ;
checkDevTree (argv) ;
if (argc != 3)
_doUnLoadUsage (argv) ;
/**/ if (strcasecmp (argv [2], "spi") == 0)
{
module1 = "spidev" ;
module2 = "spi_bcm2708" ;
}
else if (strcasecmp (argv [2], "i2c") == 0)
{
module1 = "i2c_dev" ;
module2 = "i2c_bcm2708" ;
}
else
_doUnLoadUsage (argv) ;
if (moduleLoaded (module1))
{
sprintf (cmd, "%s %s", findExecutable (RMMOD), module1) ;
system (cmd) ;
}
if (moduleLoaded (module2))
{
sprintf (cmd, "%s %s", findExecutable (RMMOD), module2) ;
system (cmd) ;
}
}
/*
* doI2Cdetect:
* Run the i2cdetect command with the right runes for this Pi revision
*********************************************************************************
*/
static void doI2Cdetect (UNU int argc, char *argv [])
{
int port = piGpioLayout () == 1 ? 0 : 1 ;
char *c, *command ;
if ((c = findExecutable (I2CDETECT)) == NULL)
{
fprintf (stderr, "%s: Unable to find i2cdetect command: %s\n", argv [0], strerror (errno)) ;
return ;
}
if (!moduleLoaded ("i2c_dev"))
{
fprintf (stderr, "%s: The I2C kernel module(s) are not loaded.\n", argv [0]) ;
return ;
}
command = malloc (strlen (c) + 16) ;
sprintf (command, "%s -y %d", c, port) ;
if (system (command) < 0)
fprintf (stderr, "%s: Unable to run i2cdetect: %s\n", argv [0], strerror (errno)) ;
}
/*
* doExports:
* List all GPIO exports
*********************************************************************************
*/
static void doExports (UNU int argc, UNU char *argv [])
{
int fd ;
int i, l, first ;
char fName [128] ;
char buf [16] ;
for (first = 0, i = 0 ; i < 64 ; ++i) // Crude, but effective
{
// Try to read the direction
sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ;
if ((fd = open (fName, O_RDONLY)) == -1)
continue ;
if (first == 0)
{
++first ;
printf ("GPIO Pins exported:\n") ;
}
printf ("%4d: ", i) ;
if ((l = read (fd, buf, 16)) == 0)
sprintf (buf, "%s", "?") ;
buf [l] = 0 ;
if ((buf [strlen (buf) - 1]) == '\n')
buf [strlen (buf) - 1] = 0 ;
printf ("%-3s", buf) ;
close (fd) ;
// Try to Read the value
sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ;
if ((fd = open (fName, O_RDONLY)) == -1)
{
printf ("No Value file (huh?)\n") ;
continue ;
}
if ((l = read (fd, buf, 16)) == 0)
sprintf (buf, "%s", "?") ;
buf [l] = 0 ;
if ((buf [strlen (buf) - 1]) == '\n')
buf [strlen (buf) - 1] = 0 ;
printf (" %s", buf) ;
// Read any edge trigger file
sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
if ((fd = open (fName, O_RDONLY)) == -1)
{
printf ("\n") ;
continue ;
}
if ((l = read (fd, buf, 16)) == 0)
sprintf (buf, "%s", "?") ;
buf [l] = 0 ;
if ((buf [strlen (buf) - 1]) == '\n')
buf [strlen (buf) - 1] = 0 ;
printf (" %-8s\n", buf) ;
close (fd) ;
}
}
/*
* doExport:
* gpio export pin mode
* This uses the /sys/class/gpio device interface.
*********************************************************************************
*/
void doExport (int argc, char *argv [])
{
FILE *fd ;
int pin ;
char *mode ;
char fName [128] ;
if (argc != 4)
{
fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
exit (1) ;
}
pin = atoi (argv [2]) ;
mode = argv [3] ;
if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
{
fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
exit (1) ;
}
fprintf (fd, "%d\n", pin) ;
fclose (fd) ;
sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
if ((fd = fopen (fName, "w")) == NULL)
{
fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
exit (1) ;
}
/**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
fprintf (fd, "in\n") ;
else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
fprintf (fd, "out\n") ;
else if ((strcasecmp (mode, "high") == 0) || (strcasecmp (mode, "up") == 0))
fprintf (fd, "high\n") ;
else if ((strcasecmp (mode, "low") == 0) || (strcasecmp (mode, "down") == 0))
fprintf (fd, "low\n") ;
else
{
fprintf (stderr, "%s: Invalid mode: %s. Should be in, out, high or low\n", argv [1], mode) ;
exit (1) ;
}
fclose (fd) ;
// Change ownership so the current user can actually use it
sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
changeOwner (argv [0], fName) ;
sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
changeOwner (argv [0], fName) ;
}
/*
* doWfi:
* gpio wfi pin mode
* Wait for Interrupt on a given pin.
* Slight cheat here - it's easier to actually use ISR now (which calls
* gpio to set the pin modes!) then we simply sleep, and expect the thread
* to exit the program. Crude but effective.
*********************************************************************************
*/
static void wfi (void)
{ exit (0) ; }
void doWfi (int argc, char *argv [])
{
int pin, mode ;
if (argc != 4)
{
fprintf (stderr, "Usage: %s wfi pin mode\n", argv [0]) ;
exit (1) ;
}
pin = atoi (argv [2]) ;
/**/ if (strcasecmp (argv [3], "rising") == 0) mode = INT_EDGE_RISING ;
else if (strcasecmp (argv [3], "falling") == 0) mode = INT_EDGE_FALLING ;
else if (strcasecmp (argv [3], "both") == 0) mode = INT_EDGE_BOTH ;
else
{
fprintf (stderr, "%s: wfi: Invalid mode: %s. Should be rising, falling or both\n", argv [1], argv [3]) ;
exit (1) ;
}
if (wiringPiISR (pin, mode, &wfi) < 0)
{
fprintf (stderr, "%s: wfi: Unable to setup ISR: %s\n", argv [1], strerror (errno)) ;
exit (1) ;
}
for (;;)
delay (9999) ;
}
/*
* doEdge:
* gpio edge pin mode
* Easy access to changing the edge trigger on a GPIO pin
* This uses the /sys/class/gpio device interface.
*********************************************************************************
*/
void doEdge (int argc, char *argv [])
{
FILE *fd ;
int pin ;
char *mode ;
char fName [128] ;
if (argc != 4)
{
fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
exit (1) ;
}
pin = atoi (argv [2]) ;
mode = argv [3] ;
// Export the pin and set direction to input
if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
{
fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
exit (1) ;
}
fprintf (fd, "%d\n", pin) ;
fclose (fd) ;
sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
if ((fd = fopen (fName, "w")) == NULL)
{
fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
exit (1) ;
}
fprintf (fd, "in\n") ;
fclose (fd) ;
sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
if ((fd = fopen (fName, "w")) == NULL)
{
fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
exit (1) ;
}
/**/ if (strcasecmp (mode, "none") == 0) fprintf (fd, "none\n") ;
else if (strcasecmp (mode, "rising") == 0) fprintf (fd, "rising\n") ;
else if (strcasecmp (mode, "falling") == 0) fprintf (fd, "falling\n") ;
else if (strcasecmp (mode, "both") == 0) fprintf (fd, "both\n") ;
else
{
fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
exit (1) ;
}
// Change ownership of the value and edge files, so the current user can actually use it!
sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
changeOwner (argv [0], fName) ;
sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
changeOwner (argv [0], fName) ;
fclose (fd) ;
}
/*
* doUnexport:
* gpio unexport pin
* This uses the /sys/class/gpio device interface.
*********************************************************************************
*/
void doUnexport (int argc, char *argv [])
{
FILE *fd ;
int pin ;
if (argc != 3)
{
fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
exit (1) ;
}
pin = atoi (argv [2]) ;
if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
{
fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
exit (1) ;
}
fprintf (fd, "%d\n", pin) ;
fclose (fd) ;
}
/*
* doUnexportAll:
* gpio unexportall
* Un-Export all the GPIO pins.
* This uses the /sys/class/gpio device interface.
*********************************************************************************
*/
void doUnexportall (char *progName)
{
FILE *fd ;
int pin ;
for (pin = 0 ; pin < 63 ; ++pin)
{
if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
{
fprintf (stderr, "%s: Unable to open GPIO export interface\n", progName) ;
exit (1) ;
}
fprintf (fd, "%d\n", pin) ;
fclose (fd) ;
}
}
/*
* doReset:
* Reset the GPIO pins - as much as we can do
*********************************************************************************
*/
static void doReset (UNU char *progName)
{
printf ("GPIO Reset is dangerous and has been removed from the gpio command.\n") ;
printf (" - Please write a shell-script to reset the GPIO pins into the state\n") ;
printf (" that you need them in for your applications.\n") ;
}
/*
* doMode:
* gpio mode pin mode ...
*********************************************************************************
*/
void doMode (int argc, char *argv [])
{
int pin ;
char *mode ;
if (argc != 4)
{
fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
exit (1) ;
}
pin = atoi (argv [2]) ;
mode = argv [3] ;
/**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
else if (strcasecmp (mode, "input") == 0) pinMode (pin, INPUT) ;
else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
else if (strcasecmp (mode, "output") == 0) pinMode (pin, OUTPUT) ;
else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
else if (strcasecmp (mode, "pwmTone") == 0) pinMode (pin, PWM_TONE_OUTPUT) ;
else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ;
else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ;
else if (strcasecmp (mode, "alt0") == 0) pinModeAlt (pin, 0b100) ;
else if (strcasecmp (mode, "alt1") == 0) pinModeAlt (pin, 0b101) ;
else if (strcasecmp (mode, "alt2") == 0) pinModeAlt (pin, 0b110) ;
else if (strcasecmp (mode, "alt3") == 0) pinModeAlt (pin, 0b111) ;
else if (strcasecmp (mode, "alt4") == 0) pinModeAlt (pin, 0b011) ;
else if (strcasecmp (mode, "alt5") == 0) pinModeAlt (pin, 0b010) ;
else
{
fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ;
exit (1) ;
}
}
/*
* doPadDrive:
* gpio drive group value
*********************************************************************************
*/
static void doPadDrive (int argc, char *argv [])
{
int group, val ;
if (argc != 4)
{
fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
exit (1) ;
}
group = atoi (argv [2]) ;
val = atoi (argv [3]) ;
if ((group < 0) || (group > 2))
{
fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
exit (1) ;
}
if ((val < 0) || (val > 7))
{
fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
exit (1) ;
}
setPadDrive (group, val) ;
}
/*
* doUsbP:
* Control USB Power - High (1.2A) or Low (600mA)
* gpio usbp high/low
*********************************************************************************
*/
static void doUsbP (int argc, char *argv [])
{
int model, rev, mem, maker, overVolted ;
if (argc != 3)
{
fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ;
exit (1) ;
}
// Make sure we're on a B+
piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
if (!((model == PI_MODEL_BP) || (model == PI_MODEL_2)))
{
fprintf (stderr, "USB power contol is applicable to B+ and v2 boards only.\n") ;
exit (1) ;
}
// Make sure we start in BCM_GPIO mode
wiringPiSetupGpio () ;
if ((strcasecmp (argv [2], "high") == 0) || (strcasecmp (argv [2], "hi") == 0))
{
digitalWrite (PI_USB_POWER_CONTROL, 1) ;
pinMode (PI_USB_POWER_CONTROL, OUTPUT) ;
printf ("Switched to HIGH current USB (1.2A)\n") ;
return ;
}
if ((strcasecmp (argv [2], "low") == 0) || (strcasecmp (argv [2], "lo") == 0))
{
digitalWrite (PI_USB_POWER_CONTROL, 0) ;
pinMode (PI_USB_POWER_CONTROL, OUTPUT) ;
printf ("Switched to LOW current USB (600mA)\n") ;
return ;
}
fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ;
exit (1) ;
}
/*
* doGbw:
* gpio gbw channel value
* Gertboard Write - To the Analog output
*********************************************************************************
*/
static void doGbw (int argc, char *argv [])
{
int channel, value ;
if (argc != 4)
{
fprintf (stderr, "Usage: %s gbw <channel> <value>\n", argv [0]) ;
exit (1) ;
}
channel = atoi (argv [2]) ;
value = atoi (argv [3]) ;
if ((channel < 0) || (channel > 1))
{
fprintf (stderr, "%s: gbw: Channel number must be 0 or 1\n", argv [0]) ;
exit (1) ;
}
if ((value < 0) || (value > 255))
{
fprintf (stderr, "%s: gbw: Value must be from 0 to 255\n", argv [0]) ;
exit (1) ;
}
if (gertboardAnalogSetup (64) < 0)
{
fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
exit (1) ;
}
analogWrite (64 + channel, value) ;
}
/*
* doGbr:
* gpio gbr channel
* From the analog input
*********************************************************************************
*/
static void doGbr (int argc, char *argv [])
{
int channel ;
if (argc != 3)
{
fprintf (stderr, "Usage: %s gbr <channel>\n", argv [0]) ;
exit (1) ;
}
channel = atoi (argv [2]) ;
if ((channel < 0) || (channel > 1))
{
fprintf (stderr, "%s: gbr: Channel number must be 0 or 1\n", argv [0]) ;
exit (1) ;
}
if (gertboardAnalogSetup (64) < 0)
{
fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
exit (1) ;
}
printf ("%d\n", analogRead (64 + channel)) ;
}
/*
* doWrite:
* gpio write pin value
*********************************************************************************
*/
static void doWrite (int argc, char *argv [])
{
int pin, val ;
if (argc != 4)
{
fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
exit (1) ;
}
pin = atoi (argv [2]) ;
/**/ if ((strcasecmp (argv [3], "up") == 0) || (strcasecmp (argv [3], "on") == 0))
val = 1 ;
else if ((strcasecmp (argv [3], "down") == 0) || (strcasecmp (argv [3], "off") == 0))
val = 0 ;
else
val = atoi (argv [3]) ;
/**/ if (val == 0)
digitalWrite (pin, LOW) ;
else
digitalWrite (pin, HIGH) ;
}
/*
* doAwriterite:
* gpio awrite pin value
*********************************************************************************
*/
static void doAwrite (int argc, char *argv [])
{
int pin, val ;
if (argc != 4)
{
fprintf (stderr, "Usage: %s awrite pin value\n", argv [0]) ;
exit (1) ;
}
pin = atoi (argv [2]) ;
val = atoi (argv [3]) ;
analogWrite (pin, val) ;
}
/*
* doWriteByte:
* gpio wb value
*********************************************************************************
*/
static void doWriteByte (int argc, char *argv [])
{
int val ;