forked from joan2937/lg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lgpio.h
2947 lines (2169 loc) · 66.4 KB
/
lgpio.h
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
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
#ifndef LGPIO_H
#define LGPIO_H
#include <stdint.h>
#include <inttypes.h>
#include <pthread.h>
#include <linux/gpio.h>
#define LGPIO_VERSION 0x00010700
#define LG_CD "LG_CD" /* configuration directory */
#define LG_WD "LG_WD" /* working directory */
/*TEXT
lgpio is a C library for Linux Single Board Computers which
allows control of the General Purpose Input Output pins.
*Features*
o reading and writing GPIO singly and in groups
o software timed PWM and waves
o GPIO callbacks
o pipe notification of GPIO alerts
o I2C wrapper
o SPI wrapper
o serial link wrapper
o a simple interface to start and stop new threads
*Usage*
Include <lgpio.h> in your source files.
Assuming your source is in a single file called prog.c use the following
command to build and run the executable.
. .
gcc -Wall -o prog prog.c -llgpio
./prog
. .
For examples of usage see the C programs within the lg archive file.
*Notes*
All the functions which return an int return < 0 on error.
TEXT*/
/*OVERVIEW
GPIO
lgGpiochipOpen Opens a gpiochip device
lgGpiochipClose Closes a gpiochip device
lgGpioGetChipInfo Gets gpiochip information
lgGpioGetLineInfo Gets gpiochip line information
lgGpioGetMode Gets the mode of a GPIO
lgGpioSetUser Notifies Linux of the GPIO user
lgGpioClaimInput Claims a GPIO for input
lgGpioClaimOutput Claims a GPIO for output
lgGpioClaimAlert Claims a GPIO for alerts
lgGpioFree Frees a GPIO
lgGroupClaimInput Claims a group of GPIO for inputs
lgGroupClaimOutput Claims a group of GPIO for outputs
lgGroupFree Frees a group of GPIO
lgGpioRead Reads a GPIO
lgGpioWrite Writes a GPIO
lgGroupRead Reads a group of GPIO
lgGroupWrite Writes a group of GPIO
lgTxPulse Starts pulses on a GPIO
lgTxPwm Starts PWM pulses on a GPIO
lgTxServo Starts Servo pulses on a GPIO
lgTxWave Starts a wave on a group of GPIO
lgTxBusy See if tx is active on a GPIO or group
lgTxRoom See if more room for tx on a GPIO or group
lgGpioSetDebounce Sets the debounce time for a GPIO
lgGpioSetWatchdog Sets the watchdog time for a GPIO
lgGpioSetAlertsFunc Starts a GPIO callback
lgGpioSetSamplesFunc Starts a GPIO callback for all GPIO
I2C
lgI2cOpen Opens an I2C device
lgI2cClose Closes an I2C device
lgI2cWriteQuick SMBus write quick
lgI2cReadByte SMBus read byte
lgI2cWriteByte SMBus write byte
lgI2cReadByteData SMBus read byte data
lgI2cWriteByteData SMBus write byte data
lgI2cReadWordData SMBus read word data
lgI2cWriteWordData SMBus write word data
lgI2cReadBlockData SMBus read block data
lgI2cWriteBlockData SMBus write block data
lgI2cReadI2CBlockData SMBus read I2C block data
lgI2cWriteI2CBlockData SMBus write I2C block data
lgI2cReadDevice Reads the raw I2C device
lgI2cWriteDevice Writes the raw I2C device
lgI2cProcessCall SMBus process call
lgI2cBlockProcessCall SMBus block process call
lgI2cSegments Performs multiple I2C transactions
lgI2cZip Performs multiple I2C transactions
NOTIFICATIONS
lgNotifyOpen Request a notification
lgNotifyClose Close a notification
lgNotifyPause Pause notifications
lgNotifyResume Start notifications
SERIAL
lgSerialOpen Opens a serial device
lgSerialClose Closes a serial device
lgSerialReadByte Reads a byte from a serial device
lgSerialWriteByte Writes a byte to a serial device
lgSerialRead Reads bytes from a serial device
lgSerialWrite Writes bytes to a serial device
lgSerialDataAvailable Returns number of bytes ready to be read
SPI
lgSpiOpen Opens a SPI device
lgSpiClose Closes a SPI device
lgSpiRead Reads bytes from a SPI device
lgSpiWrite Writes bytes to a SPI device
lgSpiXfer Transfers bytes with a SPI device
THREADS
lgThreadStart Start a new thread
lgThreadStop Stop a previously started thread
UTILITIES
lguVersion Gets the library version
lguSbcName Gets the host name of the SBC
lguGetInternal Get an internal configuration value
lguSetInternal Set an internal configuration value
lguSleep Sleeps for a given time
lguTimestamp Gets the current timestamp
lguTime Gets the current time
lguErrorText Gets a text description of an error code
lguSetWorkDir Set the working directory
lguGetWorkDir Get the working directory
OVERVIEW*/
#ifdef __cplusplus
extern "C" {
#endif
#if __BIG_ENDIAN__
#define htonll(x) (x)
#define ntohll(x) (x)
#else
#define htonll(x) (((uint64_t)htonl((x)&0xffffffff)<<32)|htonl((x)>>32))
#define ntohll(x) (((uint64_t)ntohl((x)&0xffffffff)<<32)|ntohl((x)>>32))
#endif
#define LG_CFG_ID_DEBUG_LEVEL 0
#define LG_CFG_ID_MIN_DELAY 1
#define LG_MAX_PATH 1024
#define LG_THREAD_NONE 0
#define LG_THREAD_STARTED 1
#define LG_THREAD_RUNNING 2
#define LG_NOTIFY_CLOSED 0
#define LG_NOTIFY_CLOSING 1
#define LG_NOTIFY_RUNNING 2
#define LG_NOTIFY_PAUSED 3
#define MAX_REPORT 250
#define MAX_SAMPLE 4000
#define MAX_EMITS (PIPE_BUF / sizeof(lgGpioReport_t))
#define STACK_SIZE (256*1024)
#define LG_USER_LEN 16
#define LG_SALT_LEN 16
/* File constants
*/
#define LG_FILE_NONE 0
#define LG_FILE_MIN 1
#define LG_FILE_READ 1
#define LG_FILE_WRITE 2
#define LG_FILE_RW 3
#define LG_FILE_APPEND 4
#define LG_FILE_CREATE 8
#define LG_FILE_TRUNC 16
#define LG_FILE_MAX 31
#define LG_FROM_START 0
#define LG_FROM_CURRENT 1
#define LG_FROM_END 2
/* GPIO constants
*/
#define LG_GPIO_LABEL_LEN 32
#define LG_GPIO_NAME_LEN 32
#define LG_GPIO_USER_LEN 32
#define LG_GPIO_IS_KERNEL GPIOLINE_FLAG_KERNEL
#define LG_GPIO_IS_OUT GPIOLINE_FLAG_IS_OUT
#define LG_GPIO_IS_ACTIVE_LOW GPIOLINE_FLAG_ACTIVE_LOW
#define LG_GPIO_IS_OPEN_DRAIN GPIOLINE_FLAG_OPEN_DRAIN
#define LG_GPIO_IS_OPEN_SOURCE GPIOLINE_FLAG_OPEN_SOURCE
#define LG_SET_INPUT GPIOHANDLE_REQUEST_INPUT
#define LG_SET_OUTPUT GPIOHANDLE_REQUEST_OUTPUT
#define LG_SET_ACTIVE_LOW GPIOHANDLE_REQUEST_ACTIVE_LOW
#define LG_SET_OPEN_DRAIN GPIOHANDLE_REQUEST_OPEN_DRAIN
#define LG_SET_OPEN_SOURCE GPIOHANDLE_REQUEST_OPEN_SOURCE
#define LG_RISING_EDGE GPIOEVENT_REQUEST_RISING_EDGE
#define LG_FALLING_EDGE GPIOEVENT_REQUEST_FALLING_EDGE
#define LG_BOTH_EDGES GPIOEVENT_REQUEST_BOTH_EDGES
#define LG_LOW 0
#define LG_HIGH 1
#define LG_TIMEOUT 2
#define LG_TX_PWM 0
#define LG_TX_WAVE 1
#define LG_MAX_MICS_DEBOUNCE 5000000 /* 5 seconds */
#define LG_MAX_MICS_WATCHDOG 300000000 /* 5 minutes */
/* Script constants
*/
#define LG_MAX_MICS_DELAY 5e6 /* 5 seconds */
#define LG_MAX_MILS_DELAY 300e6 /* 5 minutes */
/* script status
*/
#define LG_SCRIPT_INITING 0
#define LG_SCRIPT_READY 1
#define LG_SCRIPT_RUNNING 2
#define LG_SCRIPT_WAITING 3
#define LG_SCRIPT_EXITED 4
#define LG_SCRIPT_ENDED 5
#define LG_SCRIPT_HALTED 6
#define LG_SCRIPT_FAILED 7
/* SPI constants
*/
#define LG_MAX_SPI_DEVICE_COUNT (1<<16)
/* I2C constants
*/
/* max lgI2cMsg_t per transaction */
#define LG_I2C_RDRW_IOCTL_MAX_MSGS 42
#define LG_MAX_I2C_DEVICE_COUNT (1<<16)
#define LG_MAX_I2C_ADDR 0x7F
/* i2cZip commands */
#define LG_I2C_END 0
#define LG_I2C_ESC 1
#define LG_I2C_ADDR 2
#define LG_I2C_FLAGS 3
#define LG_I2C_READ 4
#define LG_I2C_WRITE 5
/* types
*/
typedef struct lgChipInfo_s
{
uint32_t lines;
char name[LG_GPIO_NAME_LEN]; /* Linux name */
char label[LG_GPIO_LABEL_LEN]; /* functional name */
} lgChipInfo_t, *lgChipInfo_p;
typedef struct
{
uint16_t state;
int fd;
int pipe_number;
int max_emits;
} lgNotify_t;
typedef void (*callbk_t) ();
typedef struct
{
uint64_t timestamp; /* alert time in nanoseconds*/
uint8_t chip; /* gpiochip device number */
uint8_t gpio; /* offset into gpio device */
uint8_t level; /* 0=low, 1=high, 2=watchdog */
uint8_t flags; /* none defined, ignore report if non-zero */
} lgGpioReport_t;
typedef struct lgGpioAlert_s
{
lgGpioReport_t report;
int nfyHandle;
} lgGpioAlert_t, *lgGpioAlert_p;
typedef struct lgLineInfo_s
{
uint32_t offset; /* GPIO number */
uint32_t lFlags;
char name[LG_GPIO_NAME_LEN]; /* GPIO name */
char user[LG_GPIO_USER_LEN]; /* user */
} lgLineInfo_t, *lgLineInfo_p;
typedef struct lgPulse_s
{
uint64_t bits;
uint64_t mask;
int64_t delay;
} lgPulse_t, *lgPulse_p;
typedef struct
{
uint16_t addr; /* slave address */
uint16_t flags;
uint16_t len; /* msg length */
uint8_t *buf; /* pointer to msg data */
} lgI2cMsg_t;
typedef void (*lgGpioAlertsFunc_t) (int num_alerts,
lgGpioAlert_p alerts,
void *userdata);
typedef void *(lgThreadFunc_t) (void *);
/* semi-private prototypes
*/
const char *lguGetConfigDir(void);
void lguSetConfigDir(const char *dirPath);
int lgGpioSetBannedState(int handle, int gpio, int banned);
/* GPIO chip API
*/
/*F*/
int lgGpiochipOpen(int gpioDev);
/*D
This returns a handle to a gpiochip device.
. .
gpioDev: >= 0
. .
If OK returns a handle (>= 0).
On failure returns a negative error code.
...
h = lgGpiochipOpen(0); // open /dev/gpiochip0
if (h >= 0)
{
// open ok
}
else
{
// open error
}
...
D*/
/*F*/
int lgGpiochipClose(int handle);
/*D
This closes an opened gpiochip device.
. .
handle: >= 0 (as returned by [*lgGpiochipOpen*])
. .
If OK returns 0.
On failure returns a negative error code.
...
status = lgGpiochipClose(h); // close gpiochip
if (status < 0)
{
// close failed
}
...
D*/
/*F*/
int lgGpioGetChipInfo(int handle, lgChipInfo_p chipInfo);
/*D
This returns information about a gpiochip.
. .
handle: >= 0 (as returned by [*lgGpiochipOpen*])
chipInfo: A pointer to space for a lgChipInfo_t object
. .
If OK returns 0 and updates chipInfo.
On failure returns a negative error code.
This command gets the number of GPIO on the gpiochip,
its name, and its usage.
...
lgChipInfo_t cInfo;
status = lgGpioGetChipInfo(h, &cInfo);
if (status == LG_OKAY)
{
printf("lines=%d name=%s label=%s\n",
cInfo.lines, cInfo.name, cInfo.label))
}
...
D*/
/*F*/
int lgGpioGetLineInfo(int handle, int gpio, lgLineInfo_p lineInfo);
/*D
Returns information about a GPIO.
. .
handle: >= 0 (as returned by [*lgGpiochipOpen*])
gpio: >= 0, as legal for the gpiochip
lineInfo: A pointer to space for a lgLineInfo_t object
. .
If OK returns 0 and updates lineInfo.
On failure returns a negative error code.
This command gets information for a GPIO of a gpiochip.
In particular it gets the GPIO number, kernel usage flags,
its user, and its purpose.
The usage flags are bits.
Bit @ value @ Bit meaning
0 @ 1 @ GPIO in use by the kernel
1 @ 2 @ GPIO is an output
2 @ 4 @ GPIO is active low
3 @ 8 @ GPIO is open drain
4 @ 16 @ GPIO is open source
The user and purpose fields are filled in by the software which has
claimed the GPIO and may be blank.
...
lgLineInfo_t lInfo;
status = lgGpioGetLineInfo(h, gpio, &lInfo);
if (status == LG_OKAY)
{
printf("lFlags=%d name=%s user=%s\n",
lInfo.lines, lInfo.name, lInfo.user))
}
...
D*/
/*F*/
int lgGpioGetMode(int handle, int gpio);
/*D
Returns the GPIO mode.
. .
handle: >= 0 (as returned by [*lgGpiochipOpen*])
gpio: >= 0, as legal for the gpiochip
. .
If OK returns the GPIO mode.
On failure returns a negative error code.
Mode bit @ Value @ Meaning
0 @ 1 @ Kernel: In use by the kernel
1 @ 2 @ Kernel: Output
2 @ 4 @ Kernel: Active low
3 @ 8 @ Kernel: Open drain
4 @ 16 @ Kernel: Open source
5 @ 32 @ Kernel: ---
6 @ 64 @ Kernel: ---
7 @ 128 @ Kernel: ---
8 @ 256 @ LG: Input
9 @ 512 @ LG: Output
10 @ 1024 @ LG: Alert
11 @ 2048 @ LG: Group
12 @ 4096 @ LG: ---
13 @ 8192 @ LG: ---
14 @ 16384 @ LG: ---
15 @ 32768 @ LG: ---
D*/
/*F*/
int lgGpioSetUser(int handle, const char *gpiouser);
/*D
This sets the user string to be associated with each claimed GPIO.
. .
handle: >= 0 (as returned by [*lgGpiochipOpen*])
gpiouser: a string up to 32 characters long
. .
If OK returns 0.
On failure returns a negative error code.
...
status = lgGpioSetUser(h, "my_title");
...
D*/
/*F*/
int lgGpioClaimInput(int handle, int lFlags, int gpio);
/*D
This claims a GPIO for input.
. .
handle: >= 0 (as returned by [*lgGpiochipOpen*])
lFlags: line flags for the GPIO
gpio: the GPIO to be claimed
. .
If OK returns 0.
On failure returns a negative error code.
The line flags may be used to set the GPIO
as active low, open drain, or open source.
...
// open GPIO 23 for input
status = lgGpioClaimInput(h, 0, 23);
...
D*/
/*F*/
int lgGpioClaimOutput(int handle, int lFlags, int gpio, int level);
/*D
This claims a GPIO for output.
. .
handle: >= 0 (as returned by [*lgGpiochipOpen*])
lFlags: line flags for the GPIO
gpio: the GPIO to be claimed
level: the initial level to set for the GPIO
. .
If OK returns 0.
On failure returns a negative error code.
The line flags may be used to set the GPIO
as active low, open drain, or open source.
If level is zero the GPIO will be initialised low. If any other
value is used the GPIO will be initialised high.
...
// open GPIO 31 for high output
status = lgGpioClaimOutput(h, 0, 31, 1);
...
D*/
/*F*/
int lgGpioClaimAlert(
int handle, int lFlags, int eFlags, int gpio, int nfyHandle);
/*D
This claims a GPIO for alerts on level changes.
. .
handle: >= 0 (as returned by [*lgGpiochipOpen*])
lFlags: line flags for the GPIO
eFlags: event flags for the GPIO
gpio: >= 0, as legal for the gpiochip
nfyHandle: >= 0 (as returned by [*lgNotifyOpen*])
. .
If OK returns 0.
On failure returns a negative error code.
The line flags may be used to set the GPIO
as active low, open drain, or open source.
The event flags are used to specify alerts for a rising edge,
falling edge, or both edges.
The alerts will be sent to a previously opened notification. If
you don't want them sent to a notification set nfyHandle to -1.
The alerts will also be sent to any callback registered for the
GPIO by [*lgGpioSetAlertsFunc*].
All GPIO alerts are also sent to a callback registered by
[*lgGpioSetSamplesFunc*].
...
status = lgGpioClaimAlert(h, 0, LG_BOTH_EDGES, 16, -1);
...
D*/
/*F*/
int lgGpioFree(int handle, int gpio);
/*D
This frees a GPIO.
. .
handle: >= 0 (as returned by [*lgGpiochipOpen*])
gpio: the GPIO to be freed
. .
If OK returns 0.
On failure returns a negative error code.
The GPIO may now be claimed by another user or for a different purpose.
...
status = lgGpioFree(h, 16);
...
D*/
/*F*/
int lgGroupClaimInput(
int handle, int lFlags, int count, const int *gpios);
/*D
This claims a group of GPIO for inputs.
. .
handle: >= 0 (as returned by [*lgGpiochipOpen*])
lFlags: line flags for the GPIO group
count: the number of GPIO to claim
gpios: the group GPIO
. .
If OK returns 0.
On failure returns a negative error code.
The line flags may be used to set the group
as active low, open drain, or open source.
gpios is an array of one or more GPIO. The first GPIO is
called the group leader and is used to reference the group as a whole.
...
int buttons[4] = {9, 7, 2, 6};
status = lgGroupClaimInput(h, 0, 4, buttons);
if (status == LG_OKAY)
{
// OK
}
else
{
// Error
}
...
D*/
/*F*/
int lgGroupClaimOutput(
int handle, int lFlags, int count, const int *gpios, const int *levels);
/*D
This claims a group of GPIO for outputs.
. .
handle: >= 0 (as returned by [*lgGpiochipOpen*])
lFlags: line flags for the GPIO group
count: the number of GPIO to claim
gpios: the group GPIO
levels: the initial level for each GPIO
. .
If OK returns 0.
On failure returns a negative error code.
The line flags may be used to set the group
as active low, open drain, or open source.
gpios is an array of one or more GPIO. The first GPIO is
called the group leader and is used to reference the group as a whole.
levels is an array of initialisation values for the GPIO. If a value is
zero the corresponding GPIO will be initialised low. If any other
value is used the corresponding GPIO will be initialised high.
...
int leds[7] = {15, 16, 17, 8, 12, 13, 14};
int levels[7] = { 1, 0, 1, 1, 1, 0, 0};
status = lgGroupClaimInput(h, 0, 7, leds, levels);
if (status == LG_OKAY)
{
// OK
}
else
{
// Error
}
...
D*/
/*F*/
int lgGroupFree(int handle, int gpio);
/*D
This frees all the GPIO associated with a group.
. .
handle: >= 0 (as returned by [*lgGpiochipOpen*])
gpio: the group to be freed
. .
If OK returns 0.
On failure returns a negative error code.
The GPIO may now be claimed by another user or for a different purpose.
...
status = lgGroupFree(9); // free buttons
...
D*/
/*F*/
int lgGpioRead(int handle, int gpio);
/*D
This returns the level of a GPIO.
. .
handle: >= 0 (as returned by [*lgGpiochipOpen*])
gpio: the GPIO to be read
. .
If OK returns 0 (low) or 1 (high).
On failure returns a negative error code.
This command will work for any claimed GPIO (even if a member
of a group). For an output GPIO the value returned
will be that last written to the GPIO.
...
level = lgGpioRead(h, 15); // get level of GPIO 15
...
D*/
/*F*/
int lgGpioWrite(int handle, int gpio, int level);
/*D
This sets the level of an output GPIO.
. .
handle: >= 0 (as returned by [*lgGpiochipOpen*])
gpio: the GPIO to be written
level: the level to set
. .
If OK returns 0.
On failure returns a negative error code.
This command will work for any GPIO claimed as an output
(even if a member of a group).
If level is zero the GPIO will be set low (0).
If any other value is used the GPIO will be set high (1).
...
status = lgGpioWrite(h, 23, 1); // set GPIO 23 high
...
D*/
/*F*/
int lgGroupRead(int handle, int gpio, uint64_t *groupBits);
/*D
This returns the levels read from a group.
. .
handle: >= 0 (as returned by [*lgGpiochipOpen*])
gpio: the group to be read
groupBits: a pointer to a 64-bit memory area for the returned levels
. .
If OK returns the group size and updates groupBits.
On failure returns a negative error code.
This command will work for an output group as well as an input
group. For an output group the value returned
will be that last written to the group GPIO.
Note that this command will also work on an individual GPIO claimed
as an input or output as that is treated as a group with one member.
After a successful read groupBits is set as follows.
Bit 0 is the level of the group leader.
Bit 1 is the level of the second GPIO in the group.
Bit x is the level of GPIO x+1 of the group.
...
// assuming a read group of 4 buttons: 9, 7, 2, 6.
uint64_t bits;
size = lgGroupRead(h, 9, &bits); // 9 is buttons group leader
if (size >= 0) // size of group is returned so size will be 4
{
level_9 = (bits >> 0) & 1;
level_7 = (bits >> 1) & 1;
level_2 = (bits >> 2) & 1;
level_6 = (bits >> 3) & 1;
}
else
{
// error
}
...
D*/
/*F*/
int lgGroupWrite(
int handle, int gpio, uint64_t groupBits, uint64_t groupMask);
/*D
This sets the levels of an output group.
. .
handle: >= 0 (as returned by [*lgGpiochipOpen*])
gpio: the group to be written
groupBits: the level to set if the corresponding bit in groupMask is set
groupMask: a mask indicating the group GPIO to be updated
. .
If OK returns 0.
On failure returns a negative error code.
The values of each GPIO of the group are set according to the bits
of groupBits.
Bit 0 sets the level of the group leader.
Bit 1 sets the level of the second GPIO in the group.
Bit x sets the level of GPIO x+1 in the group.
However this may be modified by the groupMask. A GPIO is only
updated if the corresponding bit in the mask is 1.
...
// assuming an output group of 7 LEDs: 15, 16, 17, 8, 12, 13, 14.
// switch on all LEDs
status = lgGroupWrite(h, 15, 0x7f, 0x7f);
// switch off all LEDs
status = lgGroupWrite(h, 15, 0x00, 0x7f);
// switch on first 4 LEDs, leave others unaltered
status = lgGroupWrite(h, 15, 0x0f, 0x0f);
// switch on LED attached to GPIO 13, leave others unaltered
status = lgGroupWrite(h, 15, 32, 32);
...
D*/
/*F*/
int lgTxPulse(
int handle,
int gpio,
int pulseOn,
int pulseOff,
int pulseOffset,
int pulseCycles);
/*D
This starts software timed pulses on an output GPIO.
. .
handle: >= 0 (as returned by [*lgGpiochipOpen*])
gpio: the GPIO to be written
pulseOn: pulse high time in microseconds
pulseOff: pulse low time in microseconds
pulseOffset: offset from nominal pulse start position
pulseCycles: the number of pulses to be sent, 0 for infinite
. .
If OK returns the number of entries left in the PWM queue for the GPIO.
On failure returns a negative error code.
If both pulseOn and pulseOff are zero pulses will be switched off
for that GPIO. The active pulse, if any, will be stopped and any
queued pulses will be deleted.
Each successful call to this function consumes one PWM queue entry.
pulseCycles cycles are transmitted (0 means infinite). Each cycle
consists of pulseOn microseconds of GPIO high followed by pulseOff
microseconds of GPIO low.
PWM is characterised by two values, its frequency (number of cycles
per second) and its duty cycle (percentage of high time per cycle).
The set frequency will be 1000000 / (pulseOn + pulseOff) Hz.
The set duty cycle will be pulseOn / (pulseOn + pulseOff) * 100 %.
E.g. if pulseOn is 50 and pulseOff is 100 the frequency will be 6666.67 Hz