-
Notifications
You must be signed in to change notification settings - Fork 412
/
pigpio.3
11126 lines (7974 loc) · 160 KB
/
pigpio.3
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
." Process this file with
." groff -man -Tascii pigpio.3
."
.TH pigpio 3 2012-2020 Linux "pigpio archive"
.SH NAME
pigpio - A C library to manipulate the Pi's GPIO.
.SH SYNOPSIS
#include <pigpio.h>
gcc -Wall -pthread -o prog prog.c -lpigpio -lrt
sudo ./prog
.SH DESCRIPTION
.ad l
.nh
.br
.br
pigpio is a C library for the Raspberry which allows control of the GPIO.
.br
.br
.SS Features
.br
.br
o hardware timed PWM on any of GPIO 0-31
.br
.br
o hardware timed servo pulses on any of GPIO 0-31
.br
.br
o callbacks when any of GPIO 0-31 change state
.br
.br
o callbacks at timed intervals
.br
.br
o reading/writing all of the GPIO in a bank as one operation
.br
.br
o individually setting GPIO modes, reading and writing
.br
.br
o notifications when any of GPIO 0-31 change state
.br
.br
o the construction of output waveforms with microsecond timing
.br
.br
o rudimentary permission control over GPIO
.br
.br
o a simple interface to start and stop new threads
.br
.br
o I2C, SPI, and serial link wrappers
.br
.br
o creating and running scripts
.br
.br
.SS GPIO
.br
.br
ALL GPIO are identified by their Broadcom number.
.br
.br
.SS Credits
.br
.br
The PWM and servo pulses are timed using the DMA and PWM peripherals.
.br
.br
This use was inspired by Richard Hirst's servoblaster kernel module.
.br
.br
.SS Usage
.br
.br
Include <pigpio.h> in your source files.
.br
.br
Assuming your source is in prog.c use the following command to build and
run the executable.
.br
.br
.EX
gcc -Wall -pthread -o prog prog.c -lpigpio -lrt
.br
sudo ./prog
.br
.EE
.br
.br
For examples of usage see the C programs within the pigpio archive file.
.br
.br
.SS Notes
.br
.br
All the functions which return an int return < 0 on error.
.br
.br
\fBgpioInitialise\fP must be called before all other library functions
with the following exceptions:
.br
.br
.EX
\fBgpioCfg*\fP
.br
\fBgpioVersion\fP
.br
\fBgpioHardwareRevision\fP
.br
.EE
.br
.br
If the library is not initialised all but the \fBgpioCfg*\fP,
\fBgpioVersion\fP, and \fBgpioHardwareRevision\fP functions will
return error PI_NOT_INITIALISED.
.br
.br
If the library is initialised the \fBgpioCfg*\fP functions will return
error PI_INITIALISED.
.br
.br
If you intend to rely on signals sent to your application, you should
turn off the internal signal handling as shown in this example:
.br
.br
.EX
int cfg = gpioCfgGetInternals();
.br
cfg |= PI_CFG_NOSIGHANDLER; // (1<<10)
.br
gpioCfgSetInternals(cfg);
.br
int status = gpioInitialise();
.br
.EE
.br
.br
.SH OVERVIEW
.br
.SS ESSENTIAL
.br
.br
gpioInitialise Initialise library
.br
gpioTerminate Stop library
.br
.SS BASIC
.br
.br
gpioSetMode Set a GPIO mode
.br
gpioGetMode Get a GPIO mode
.br
.br
gpioSetPullUpDown Set/clear GPIO pull up/down resistor
.br
.br
gpioRead Read a GPIO
.br
gpioWrite Write a GPIO
.br
.SS PWM (overrides servo commands on same GPIO)
.br
.br
gpioPWM Start/stop PWM pulses on a GPIO
.br
gpioSetPWMfrequency Configure PWM frequency for a GPIO
.br
gpioSetPWMrange Configure PWM range for a GPIO
.br
.br
gpioGetPWMdutycycle Get dutycycle setting on a GPIO
.br
gpioGetPWMfrequency Get configured PWM frequency for a GPIO
.br
gpioGetPWMrange Get configured PWM range for a GPIO
.br
.br
gpioGetPWMrealRange Get underlying PWM range for a GPIO
.br
.SS Servo (overrides PWM commands on same GPIO)
.br
.br
gpioServo Start/stop servo pulses on a GPIO
.br
.br
gpioGetServoPulsewidth Get pulsewidth setting on a GPIO
.br
.SS INTERMEDIATE
.br
.br
gpioTrigger Send a trigger pulse to a GPIO
.br
.br
gpioSetWatchdog Set a watchdog on a GPIO
.br
.br
gpioRead_Bits_0_31 Read all GPIO in bank 1
.br
gpioRead_Bits_32_53 Read all GPIO in bank 2
.br
.br
gpioWrite_Bits_0_31_Clear Clear selected GPIO in bank 1
.br
gpioWrite_Bits_32_53_Clear Clear selected GPIO in bank 2
.br
.br
gpioWrite_Bits_0_31_Set Set selected GPIO in bank 1
.br
gpioWrite_Bits_32_53_Set Set selected GPIO in bank 2
.br
.br
gpioSetAlertFunc Request a GPIO level change callback
.br
gpioSetAlertFuncEx Request a GPIO change callback, extended
.br
.br
gpioSetTimerFunc Request a regular timed callback
.br
gpioSetTimerFuncEx Request a regular timed callback, extended
.br
.br
gpioStartThread Start a new thread
.br
gpioStopThread Stop a previously started thread
.br
.SS ADVANCED
.br
.br
gpioNotifyOpen Request a notification handle
.br
gpioNotifyClose Close a notification
.br
gpioNotifyOpenWithSize Request a notification with sized pipe
.br
gpioNotifyBegin Start notifications for selected GPIO
.br
gpioNotifyPause Pause notifications
.br
.br
gpioHardwareClock Start hardware clock on supported GPIO
.br
.br
gpioHardwarePWM Start hardware PWM on supported GPIO
.br
.br
gpioGlitchFilter Set a glitch filter on a GPIO
.br
gpioNoiseFilter Set a noise filter on a GPIO
.br
.br
gpioSetPad Sets a pads drive strength
.br
gpioGetPad Gets a pads drive strength
.br
.br
shell Executes a shell command
.br
.br
gpioSetISRFunc Request a GPIO interrupt callback
.br
gpioSetISRFuncEx Request a GPIO interrupt callback, extended
.br
.br
gpioSetSignalFunc Request a signal callback
.br
gpioSetSignalFuncEx Request a signal callback, extended
.br
.br
gpioSetGetSamplesFunc Requests a GPIO samples callback
.br
gpioSetGetSamplesFuncEx Requests a GPIO samples callback, extended
.br
.SS Custom
.br
.br
gpioCustom1 User custom function 1
.br
gpioCustom2 User custom function 2
.br
.SS Events
.br
.br
eventMonitor Sets the events to monitor
.br
eventSetFunc Request an event callback
.br
eventSetFuncEx Request an event callback, extended
.br
.br
eventTrigger Trigger an event
.br
.SS Scripts
.br
.br
gpioStoreScript Store a script
.br
gpioRunScript Run a stored script
.br
gpioUpdateScript Set a scripts parameters
.br
gpioScriptStatus Get script status and parameters
.br
gpioStopScript Stop a running script
.br
gpioDeleteScript Delete a stored script
.br
.SS I2C
.br
.br
i2cOpen Opens an I2C device
.br
i2cClose Closes an I2C device
.br
.br
i2cWriteQuick SMBus write quick
.br
.br
i2cReadByte SMBus read byte
.br
i2cWriteByte SMBus write byte
.br
.br
i2cReadByteData SMBus read byte data
.br
i2cWriteByteData SMBus write byte data
.br
.br
i2cReadWordData SMBus read word data
.br
i2cWriteWordData SMBus write word data
.br
.br
i2cReadBlockData SMBus read block data
.br
i2cWriteBlockData SMBus write block data
.br
.br
i2cReadI2CBlockData SMBus read I2C block data
.br
i2cWriteI2CBlockData SMBus write I2C block data
.br
.br
i2cReadDevice Reads the raw I2C device
.br
i2cWriteDevice Writes the raw I2C device
.br
.br
i2cProcessCall SMBus process call
.br
i2cBlockProcessCall SMBus block process call
.br
.br
i2cSwitchCombined Sets or clears the combined flag
.br
.br
i2cSegments Performs multiple I2C transactions
.br
.br
i2cZip Performs multiple I2C transactions
.br
.SS I2C BIT BANG
.br
.br
bbI2COpen Opens GPIO for bit banging I2C
.br
bbI2CClose Closes GPIO for bit banging I2C
.br
.br
bbI2CZip Performs bit banged I2C transactions
.br
.SS I2C/SPI SLAVE
.br
.br
bscXfer I2C/SPI as slave transfer
.br
.SS SERIAL
.br
.br
serOpen Opens a serial device
.br
serClose Closes a serial device
.br
.br
serReadByte Reads a byte from a serial device
.br
serWriteByte Writes a byte to a serial device
.br
.br
serRead Reads bytes from a serial device
.br
serWrite Writes bytes to a serial device
.br
.br
serDataAvailable Returns number of bytes ready to be read
.br
.SS SERIAL BIT BANG (read only)
.br
.br
gpioSerialReadOpen Opens a GPIO for bit bang serial reads
.br
gpioSerialReadClose Closes a GPIO for bit bang serial reads
.br
.br
gpioSerialReadInvert Configures normal/inverted for serial reads
.br
.br
gpioSerialRead Reads bit bang serial data from a GPIO
.br
.SS SPI
.br
.br
spiOpen Opens a SPI device
.br
spiClose Closes a SPI device
.br
.br
spiRead Reads bytes from a SPI device
.br
spiWrite Writes bytes to a SPI device
.br
spiXfer Transfers bytes with a SPI device
.br
.SS SPI BIT BANG
.br
.br
bbSPIOpen Opens GPIO for bit banging SPI
.br
bbSPIClose Closes GPIO for bit banging SPI
.br
.br
bbSPIXfer Performs bit banged SPI transactions
.br
.SS FILES
.br
.br
fileOpen Opens a file
.br
fileClose Closes a file
.br
.br
fileRead Reads bytes from a file
.br
fileWrite Writes bytes to a file
.br
.br
fileSeek Seeks to a position within a file
.br
.br
fileList List files which match a pattern
.br
.SS WAVES
.br
.br
gpioWaveClear Deletes all waveforms
.br
.br
gpioWaveAddNew Starts a new waveform
.br
gpioWaveAddGeneric Adds a series of pulses to the waveform
.br
gpioWaveAddSerial Adds serial data to the waveform
.br
.br
gpioWaveCreate Creates a waveform from added data
.br
gpioWaveCreatePad Creates a waveform of fixed size from added data
.br
gpioWaveDelete Deletes a waveform
.br
.br
gpioWaveTxSend Transmits a waveform
.br
.br
gpioWaveChain Transmits a chain of waveforms
.br
.br
gpioWaveTxAt Returns the current transmitting waveform
.br
.br
gpioWaveTxBusy Checks to see if the waveform has ended
.br
.br
gpioWaveTxStop Aborts the current waveform
.br
.br
gpioWaveGetCbs Length in CBs of the current waveform
.br
gpioWaveGetHighCbs Length of longest waveform so far
.br
gpioWaveGetMaxCbs Absolute maximum allowed CBs
.br
.br
gpioWaveGetMicros Length in micros of the current waveform
.br
gpioWaveGetHighMicros Length of longest waveform so far
.br
gpioWaveGetMaxMicros Absolute maximum allowed micros
.br
.br
gpioWaveGetPulses Length in pulses of the current waveform
.br
gpioWaveGetHighPulses Length of longest waveform so far
.br
gpioWaveGetMaxPulses Absolute maximum allowed pulses
.br
.SS UTILITIES
.br
.br
gpioDelay Delay for a number of microseconds
.br
.br
gpioTick Get current tick (microseconds)
.br
.br
gpioHardwareRevision Get hardware revision
.br
gpioVersion Get the pigpio version
.br
.br
getBitInBytes Get the value of a bit
.br
putBitInBytes Set the value of a bit
.br
.br
gpioTime Get current time
.br
gpioSleep Sleep for specified time
.br
.br
time_sleep Sleeps for a float number of seconds
.br
time_time Float number of seconds since the epoch
.br
.SS CONFIGURATION
.br
.br
gpioCfgBufferSize Configure the GPIO sample buffer size
.br
gpioCfgClock Configure the GPIO sample rate
.br
gpioCfgDMAchannel Configure the DMA channel (DEPRECATED)
.br
gpioCfgDMAchannels Configure the DMA channels
.br
gpioCfgPermissions Configure the GPIO access permissions
.br
gpioCfgInterfaces Configure user interfaces
.br
gpioCfgSocketPort Configure socket port
.br
gpioCfgMemAlloc Configure DMA memory allocation mode
.br
gpioCfgNetAddr Configure allowed network addresses
.br
.br
gpioCfgGetInternals Get internal configuration settings
.br
gpioCfgSetInternals Set internal configuration settings
.br
.SS EXPERT
.br
.br
rawWaveAddSPI Not intended for general use
.br
rawWaveAddGeneric Not intended for general use
.br
rawWaveCB Not intended for general use
.br
rawWaveCBAdr Not intended for general use
.br
rawWaveGetOOL Not intended for general use
.br
rawWaveSetOOL Not intended for general use
.br
rawWaveGetOut Not intended for general use
.br
rawWaveSetOut Not intended for general use
.br
rawWaveGetIn Not intended for general use
.br
rawWaveSetIn Not intended for general use
.br
rawWaveInfo Not intended for general use
.br
rawDumpWave Not intended for general use
.br
rawDumpScript Not intended for general use
.br
.SH FUNCTIONS
.IP "\fBint gpioInitialise(void)\fP"
.IP "" 4
Initialises the library.
.br
.br
Returns the pigpio version number if OK, otherwise PI_INIT_FAILED.
.br
.br
gpioInitialise must be called before using the other library functions
with the following exceptions:
.br
.br
.EX
\fBgpioCfg*\fP
.br
\fBgpioVersion\fP
.br
\fBgpioHardwareRevision\fP
.br
.EE
.br
.br
\fBExample\fP
.br
.EX
if (gpioInitialise() < 0)
.br
{
.br
// pigpio initialisation failed.
.br
}
.br
else
.br
{
.br
// pigpio initialised okay.
.br
}
.br
.EE
.IP "\fBvoid gpioTerminate(void)\fP"
.IP "" 4
Terminates the library.
.br
.br
Returns nothing.
.br
.br
Call before program exit.
.br
.br
This function resets the used DMA channels, releases memory, and
terminates any running threads.
.br
.br
\fBExample\fP
.br
.EX
gpioTerminate();
.br
.EE
.IP "\fBint gpioSetMode(unsigned gpio, unsigned mode)\fP"
.IP "" 4
Sets the GPIO mode, typically input or output.
.br
.br
.EX
gpio: 0-53
.br
mode: 0-7
.br
.EE
.br
.br
Returns 0 if OK, otherwise PI_BAD_GPIO or PI_BAD_MODE.
.br
.br
Arduino style: pinMode.
.br
.br
\fBExample\fP
.br
.EX
gpioSetMode(17, PI_INPUT); // Set GPIO17 as input.
.br
.br
gpioSetMode(18, PI_OUTPUT); // Set GPIO18 as output.
.br
.br
gpioSetMode(22,PI_ALT0); // Set GPIO22 to alternative mode 0.
.br
.EE
.br
.br
See \fBhttp://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/BCM2835-ARM-Peripherals.pdf\fP page 102 for an overview of the modes.
.IP "\fBint gpioGetMode(unsigned gpio)\fP"
.IP "" 4
Gets the GPIO mode.
.br
.br
.EX
gpio: 0-53
.br
.EE
.br
.br
Returns the GPIO mode if OK, otherwise PI_BAD_GPIO.
.br
.br
\fBExample\fP
.br
.EX
if (gpioGetMode(17) != PI_ALT0)
.br
{
.br
gpioSetMode(17, PI_ALT0); // set GPIO17 to ALT0
.br
}
.br
.EE
.IP "\fBint gpioSetPullUpDown(unsigned gpio, unsigned pud)\fP"
.IP "" 4
Sets or clears resistor pull ups or downs on the GPIO.
.br
.br
.EX
gpio: 0-53
.br
pud: 0-2
.br
.EE
.br
.br
Returns 0 if OK, otherwise PI_BAD_GPIO or PI_BAD_PUD.
.br
.br
\fBExample\fP
.br
.EX
gpioSetPullUpDown(17, PI_PUD_UP); // Sets a pull-up.
.br
.br
gpioSetPullUpDown(18, PI_PUD_DOWN); // Sets a pull-down.
.br
.br
gpioSetPullUpDown(23, PI_PUD_OFF); // Clear any pull-ups/downs.
.br
.EE
.IP "\fBint gpioRead(unsigned gpio)\fP"
.IP "" 4
Reads the GPIO level, on or off.
.br
.br
.EX
gpio: 0-53
.br
.EE
.br
.br
Returns the GPIO level if OK, otherwise PI_BAD_GPIO.
.br
.br
Arduino style: digitalRead.
.br
.br
\fBExample\fP
.br
.EX
printf("GPIO24 is level %d", gpioRead(24));
.br