forked from joan2937/lg
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lgGpio.c
1661 lines (1269 loc) · 38.7 KB
/
lgGpio.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
/*
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/>
*/
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <poll.h>
#include <errno.h>
#include <string.h>
#include "lgpio.h"
#include "lgDbg.h"
#include "lgGpio.h"
#include "lgHdl.h"
#include "lgPthAlerts.h"
#include "lgPthTx.h"
#define LG_CHIP_MODE_UNKNOWN 0
#define LG_CHIP_BIT_INPUT (1<<0)
#define LG_CHIP_BIT_OUTPUT (1<<1)
#define LG_CHIP_BIT_ALERT (1<<2)
#define LG_CHIP_BIT_GROUP (1<<3)
void xWrite(lgChipObj_p chip, int gpio, int value);
callbk_t lgGpioSamplesFunc = NULL;
void *lgGpioSamplesUserdata = NULL;
static inline void xSetBit(uint64_t *b, int n)
{
*b |= ((uint64_t)1 << n);
}
static inline void xChangeBit(uint64_t *b, int n)
{
*b ^= ((uint64_t)1 << n);
}
static inline void xClearBit(uint64_t *b, int n)
{
*b &= ~((uint64_t)1 << n);
}
static inline int xTestBit(uint64_t b, int n)
{
return !!(b & ((uint64_t)1 << n));
}
static inline void xAssignBit(uint64_t *b, int n, int value)
{
if (value) xSetBit(b, n);
else xClearBit(b, n);
}
static void _lgGpiochipClose(void *objPtr)
{
int i;
lgChipObj_p chip;
chip = objPtr;
if (chip == NULL) return;
/* stop any PWM on chip */
lgPthTxStop(chip);
/* stop any event reads on chip */
lgPthAlertStop(chip);
usleep(100000); /* should be long enough for PWM/events to stop */
for (i=0; i<chip->lines; i++)
{
if (chip->LineInf[i].mode != LG_CHIP_MODE_UNKNOWN)
{
/* free GPIO */
LG_DBG(LG_DEBUG_ALLOC, "free GPIO: %d mode %d (%d of %d)",
i,
chip->LineInf[i].mode,
chip->LineInf[i].offset+1,
chip->LineInf[i].group_size);
if (chip->LineInf[i].offset == 0)
{
/* group leader - remove all group resources */
if (chip->LineInf[i].fd != -1)
{
LG_DBG(LG_DEBUG_ALLOC, "close fd: %d", chip->LineInf[i].fd);
close(chip->LineInf[i].fd);
chip->LineInf[i].fd = -1;
}
if (chip->LineInf[i].offsets_p)
{
LG_DBG(LG_DEBUG_ALLOC, "free offsets: *%p",
(void*)chip->LineInf[i].offsets_p);
free(chip->LineInf[i].offsets_p);
chip->LineInf[i].offsets_p = NULL;
}
if (chip->LineInf[i].values_p)
{
LG_DBG(LG_DEBUG_ALLOC, "free values: *%p",
(void*)chip->LineInf[i].values_p);
free(chip->LineInf[i].values_p);
chip->LineInf[i].values_p = NULL;
}
}
}
}
LG_DBG(LG_DEBUG_ALLOC, "free LineInf: *%p", (void*)chip->LineInf);
if (chip->LineInf) free(chip->LineInf);
LG_DBG(LG_DEBUG_ALLOC, "close chip fd: %d", chip->fd);
close(chip->fd);
}
static int xGpioHandleRequest(
lgChipObj_p chip, struct gpio_v2_line_request *req)
{
int i, gpio, mode;
int status;
uint32_t *offsets_p;
uint64_t *values_p;
LG_DBG(LG_DEBUG_USER, "chip=*%p req=*%p", (void*)chip, (void*)req);
LG_DBG(LG_DEBUG_ALLOC, "request %d with flags %llu GPIO=[%s]",
req->num_lines, req->config.flags, lgDbgInt2Str(req->num_lines,
(int *)req->offsets));
status = ioctl(chip->fd, GPIO_V2_GET_LINE_IOCTL, req);
if (status == 0)
{
offsets_p = calloc(req->num_lines, sizeof(uint32_t));
values_p = calloc(1, sizeof(uint64_t));
if ((offsets_p == NULL) || (values_p == NULL))
{
free(offsets_p); // passing NULL is legal
free(values_p); // passing NULL is legal
close(req->fd);
return LG_NOT_ENOUGH_MEMORY;
}
LG_DBG(LG_DEBUG_ALLOC, "alloc offsets: *%p, values: *%p",
(void*)offsets_p, (void*)values_p);
mode = 0;
if (req->config.flags & GPIO_V2_LINE_FLAG_INPUT)
mode |= LG_CHIP_BIT_INPUT;
if (req->config.flags & GPIO_V2_LINE_FLAG_OUTPUT)
mode |= LG_CHIP_BIT_OUTPUT;
if (req->num_lines > 1)
mode |= LG_CHIP_BIT_GROUP;
for (i=0; i<req->num_lines; i++)
{
gpio = req->offsets[i];
chip->LineInf[gpio].mode = mode;
chip->LineInf[gpio].group_size = req->num_lines;
chip->LineInf[gpio].fd = req->fd;
chip->LineInf[gpio].offset = i;
chip->LineInf[gpio].offsets_p = offsets_p;
chip->LineInf[gpio].values_p = values_p;
offsets_p[i] = gpio;
}
}
else
{
if (errno == EBUSY) status = LG_GPIO_BUSY;
else
{
LG_DBG(LG_DEBUG_ALWAYS, "%s", strerror(errno));
status = LG_UNEGPECTED_ERROR;
}
}
return status;
}
uint64_t xMakeFlags(uint64_t s)
{
uint64_t f = 0;
if (s & LG_RISING_EDGE) f |= GPIO_V2_LINE_FLAG_EDGE_RISING;
if (s & LG_FALLING_EDGE) f |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
if (s & LG_SET_ACTIVE_LOW) f |= GPIO_V2_LINE_FLAG_ACTIVE_LOW;
if (s & LG_SET_OPEN_DRAIN) f |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
if (s & LG_SET_OPEN_SOURCE) f |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
if (s & LG_SET_PULL_UP) f |= GPIO_V2_LINE_FLAG_BIAS_PULL_UP;
if (s & LG_SET_PULL_DOWN) f |= GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN;
if (s & LG_SET_PULL_NONE) f |= GPIO_V2_LINE_FLAG_BIAS_DISABLED;
if (s & LG_SET_INPUT) f |= GPIO_V2_LINE_FLAG_INPUT;
if (s & LG_SET_OUTPUT) f |= GPIO_V2_LINE_FLAG_OUTPUT;
/*
if (s & LG_SET_REALTIME_CLOCK) f |=
GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
*/
return f;
}
uint64_t xMakeStatus(uint64_t f)
{
uint64_t s = 0;
if (f & GPIO_V2_LINE_FLAG_USED) s |= LG_GPIO_IS_KERNEL;
if (f & GPIO_V2_LINE_FLAG_ACTIVE_LOW) s |= LG_GPIO_IS_ACTIVE_LOW;
if (f & GPIO_V2_LINE_FLAG_INPUT) s |= LG_GPIO_IS_INPUT;
if (f & GPIO_V2_LINE_FLAG_OUTPUT) s |= LG_GPIO_IS_OUTPUT;
if (f & GPIO_V2_LINE_FLAG_EDGE_RISING) s |= LG_GPIO_IS_RISING_EDGE;
if (f & GPIO_V2_LINE_FLAG_EDGE_FALLING) s |= LG_GPIO_IS_FALLING_EDGE;
if (f & GPIO_V2_LINE_FLAG_OPEN_DRAIN) s |= LG_GPIO_IS_OPEN_DRAIN;
if (f & GPIO_V2_LINE_FLAG_OPEN_SOURCE) s |= LG_GPIO_IS_OPEN_SOURCE;
if (f & GPIO_V2_LINE_FLAG_BIAS_PULL_UP) s |= LG_GPIO_IS_PULL_UP;
if (f & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN) s |= LG_GPIO_IS_PULL_DOWN;
if (f & GPIO_V2_LINE_FLAG_BIAS_DISABLED) s |= LG_GPIO_IS_PULL_NONE;
/*
if (f & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME)
s |= LG_GPIO_IS_REALTIME_CLOCK;
*/
return s;
}
static int xClaim(
lgChipObj_p chip,
int lFlags,
int size,
const int *gpios,
const int *values)
{
int i;
uint64_t m=0, v=0;
struct gpio_v2_line_request req;
LG_DBG(LG_DEBUG_USER, "chip=*%p size=%d gpios=[%s] values=[%s] lFlags=%x",
chip, size, lgDbgInt2Str(size, (int*)gpios),
lgDbgInt2Str(size, (int*)values), lFlags);
memset(&req, 0, sizeof(req));
if (size && (size <= GPIO_V2_LINES_MAX))
{
for (i=0; i<size; i++)
{
if (((unsigned)gpios[i] > chip->lines) ||
(chip->LineInf[gpios[i]].banned)) return LG_NOT_PERMITTED;
req.offsets[i] = gpios[i];
}
if (values != NULL)
{
req.config.num_attrs = 1;
req.config.attrs[0].attr.id = GPIO_V2_LINE_ATTR_ID_OUTPUT_VALUES;
for (i=0; i<size; i++)
{
xSetBit(&m, i);
xAssignBit(&v, i, values[i]);
}
}
req.config.flags = xMakeFlags(lFlags);
req.config.attrs[0].mask = m;
req.config.attrs[0].attr.values = v;
strncpy(req.consumer, chip->userLabel, sizeof(req.consumer));
req.num_lines = size;
return xGpioHandleRequest(chip, &req);
}
else return LG_BAD_GROUP_SIZE;
}
// implement public API
static int xSetAsFree(lgChipObj_p chip, int gpio)
{
lgLineInf_p GPIO;
int i, g;
lgTxRec_p pTx;
lgAlertRec_p pEvt;
LG_DBG(LG_DEBUG_TRACE, "chip=*%p gpio=%d", (void*)chip, gpio);
if (gpio >= chip->lines) return LG_BAD_GPIO_NUMBER;
GPIO = &chip->LineInf[gpio];
if (GPIO->mode == LG_CHIP_MODE_UNKNOWN)
{
LG_DBG(LG_DEBUG_ALLOC,
"free unallocated GPIO: %d (mode %d)", gpio, GPIO->mode);
return LG_OKAY;
}
if (GPIO->mode & LG_CHIP_BIT_ALERT)
{
LG_DBG(LG_DEBUG_ALLOC,
"free alert GPIO: %d (mode %d)", gpio, GPIO->mode);
if ((pEvt = lgGpioGetAlertRec(chip, gpio)) != NULL)
pEvt->active = 0;
for (i=0; i<10; i++)
{
LG_DBG(LG_DEBUG_ALLOC, "waiting for inactive: %d", gpio);
usleep(200);
if ((pEvt = lgGpioGetAlertRec(chip, gpio)) == NULL) break;
}
close(GPIO->fd);
GPIO->mode = LG_CHIP_MODE_UNKNOWN;
return LG_OKAY;
}
// legal as long as group leader (a singleton is always a leader)
if (gpio == GPIO->offsets_p[0])
{
LG_DBG(LG_DEBUG_ALLOC,
"group free GPIO: %d (mode %d)", gpio, GPIO->mode);
for (i=0; i<GPIO->group_size; i++)
{
g = GPIO->offsets_p[i];
lgPthTxLock();
if ((pTx = lgGpioGetTxRec(chip, g, LG_TX_PWM)) != NULL)
{
pTx->active = 0;
LG_DBG(LG_DEBUG_ALLOC, "set PWM inactive: %d", gpio);
}
if ((pTx = lgGpioGetTxRec(chip, g, LG_TX_WAVE)) != NULL)
{
pTx->active = 0;
LG_DBG(LG_DEBUG_ALLOC, "set PWM inactive: %d", gpio);
}
lgPthTxUnlock();
chip->LineInf[g].mode = LG_CHIP_MODE_UNKNOWN;
LG_DBG(LG_DEBUG_ALLOC, "set unused: %d", g);
}
LG_DBG(LG_DEBUG_ALLOC, "close fd: %d", GPIO->fd);
close(GPIO->fd);
LG_DBG(LG_DEBUG_ALLOC, "free offsets: *%p, values: *%p",
(void*)GPIO->offsets_p, (void*)GPIO->values_p);
free(GPIO->offsets_p);
free(GPIO->values_p);
return LG_OKAY;
}
return LG_NOT_GROUP_LEADER;
}
static int xSetAsOutput(
lgChipObj_p chip, int lFlags, int size,
const int *gpios, const int *values)
{
int mode;
LG_DBG(LG_DEBUG_TRACE, "chip=*%p gpio=%d", (void*)chip, gpios[0]);
lFlags &= ~(LG_SET_INPUT);
lFlags |= LG_SET_OUTPUT;
if (size == 1)
{
/* single GPIO */
mode = chip->LineInf[gpios[0]].mode;
if (mode & LG_CHIP_BIT_OUTPUT)
{
/* nothing to do, already an output */
return LG_OKAY;
}
else
{
if (!(mode & LG_CHIP_BIT_GROUP))
{
/* do auto free if singleton */
LG_DBG(LG_DEBUG_ALLOC, "set as output auto free %d", gpios[0]);
xSetAsFree(chip, gpios[0]);
}
return xClaim(chip, lFlags, size, gpios, values);
}
}
else
{
return xClaim(chip, lFlags, size, gpios, values);
}
}
static int xSetAsInput(
lgChipObj_p chip, int lFlags, int size, const int *gpios)
{
int mode;
LG_DBG(LG_DEBUG_TRACE, "chip=*%p gpio=%d", (void*)chip, gpios[0]);
lFlags &= ~(LG_SET_OUTPUT);
lFlags |= LG_SET_INPUT;
if (size == 1)
{
/* single GPIO */
mode = chip->LineInf[gpios[0]].mode;
if (mode & LG_CHIP_BIT_INPUT)
{
/* nothing to do, already an input */
return LG_OKAY;
}
else
{
if (!(mode & LG_CHIP_BIT_GROUP))
{
/* do auto free if singleton */
LG_DBG(LG_DEBUG_ALLOC, "set as input auto free %d", gpios[0]);
xSetAsFree(chip, gpios[0]);
}
return xClaim(chip, lFlags, size, gpios, NULL);
}
}
else
{
return xClaim(chip, lFlags, size, gpios, NULL);
}
}
static int xSetAsPwm(
lgChipObj_p chip,
int gpio,
int micros_on,
int micros_off,
int micros_offset,
int cycles)
{
lgLineInf_p GPIO;
lgTxRec_p p;
int zero = 0;
int status = 0;
LG_DBG(LG_DEBUG_TRACE, "chip=*%p gpio=%d", (void*)chip, gpio);
GPIO = &chip->LineInf[gpio];
/* do we need to change the mode? */
if (GPIO->mode == LG_CHIP_MODE_UNKNOWN)
{
xSetAsOutput(
chip, GPIO_V2_LINE_FLAG_OUTPUT, 1, &gpio, &zero);
}
else
{
if (!(GPIO->mode & LG_CHIP_BIT_OUTPUT))
{
/* not an output */
if (!(GPIO->mode & LG_CHIP_BIT_GROUP))
{
/* is a singleton */
xSetAsOutput(
chip, GPIO_V2_LINE_FLAG_OUTPUT, 1, &gpio, &zero);
}
}
}
/* return if in wrong mode */
if (!(GPIO->mode & LG_CHIP_BIT_OUTPUT)) return LG_GPIO_NOT_AN_OUTPUT;
lgPthTxLock();
if (((p = lgGpioGetTxRec(chip, gpio, LG_TX_PWM)) != NULL) && p->active)
{
if (micros_on || micros_off)
{
if ((micros_on + micros_off) > lgMinTxDelay)
{
/* delete prior pending entry if it has infinite cycles */
if ((p->entries > 1) && (p->cycles[p->entries-1] == -1))
{
--p->entries;
}
if (p->entries < LG_TX_BUF)
{
p->micros_on[p->entries] = micros_on;
p->micros_off[p->entries] = micros_off;
if (cycles) p->cycles[p->entries] = cycles;
else p->cycles[p->entries] = -1;
p->entries++;
status = LG_TX_BUF - p->entries;
}
else status = LG_TX_QUEUE_FULL;
}
else status = LG_BAD_PWM_MICROS;
}
else
{
p->active = 0;
}
lgPthTxUnlock();
}
else
{
lgPthTxUnlock();
if ((micros_on + micros_off) > lgMinTxDelay)
{
lgGpioCreateTxRec(
chip, gpio, micros_on, micros_off, micros_offset, cycles);
status = LG_TX_BUF - 1;
}
else return LG_BAD_PWM_MICROS;
}
return status;
}
static int xWave(
lgChipObj_p chip, int gpio, int count, lgPulse_p pulses)
{
lgLineInf_p GPIO;
lgTxRec_p p;
int i;
int zero = 0;
int status = 0;
lgPulse_p pulsesTmp;
LG_DBG(LG_DEBUG_TRACE, "chip=*%p gpio=%d", (void*)chip, gpio);
GPIO = &chip->LineInf[gpio];
if (!(GPIO->mode & LG_CHIP_BIT_OUTPUT))
{
/* not an output */
if (!(GPIO->mode & LG_CHIP_BIT_GROUP))
{
/* auto set output if a singleton */
xSetAsOutput(
chip, GPIO_V2_LINE_FLAG_OUTPUT, 1, &gpio, &zero);
}
}
if (!(GPIO->mode & LG_CHIP_BIT_OUTPUT)) return LG_GPIO_NOT_AN_OUTPUT;
pulsesTmp = malloc(count * sizeof(lgPulse_t));
if (!pulsesTmp) return LG_NO_MEMORY;
for (i=0; i<count; i++)
{
pulsesTmp[i] = pulses[i];
}
lgPthTxLock();
if (((p = lgGpioGetTxRec(chip, gpio, LG_TX_WAVE)) != NULL) && p->active)
{
if (p->entries < LG_TX_BUF)
{
p->pulses[p->entries] = pulsesTmp;
p->num_pulses[p->entries] = count;
p->entries++;
status = LG_TX_BUF - p->entries;
}
else
{
free(pulsesTmp);
status = LG_TX_QUEUE_FULL;
}
lgPthTxUnlock();
}
else
{
lgPthTxUnlock();
lgGroupCreateWaveRec(chip, gpio, count, pulsesTmp);
status = LG_TX_BUF - 1;
}
return status;
}
void xWrite(lgChipObj_p chip, int gpio, int value)
{
lgLineInf_p GPIO;
struct gpio_v2_line_values lv;
uint64_t m = 0;
LG_DBG(LG_DEBUG_TRACE, "chip=*%p gpio=%d value=%d", (void*)chip, gpio, value);
GPIO = &chip->LineInf[gpio];
xSetBit(&m, GPIO->offset);
xAssignBit(GPIO->values_p, GPIO->offset, value);
lv.mask = m;
lv.bits = *GPIO->values_p;
ioctl(GPIO->fd, GPIO_V2_LINE_SET_VALUES_IOCTL, &lv);
}
void xGroupWrite(
lgChipObj_p chip, int gpio, uint64_t groupBits, uint64_t groupMask)
{
int i;
lgLineInf_p GPIO;
struct gpio_v2_line_values lv;
GPIO = &chip->LineInf[gpio];
for (i=0; i<GPIO->group_size; i++)
{
if (groupMask & ((uint64_t)1<<i))
{
xAssignBit(GPIO->values_p, i, groupBits & (1<<i));
}
}
lv.mask = groupMask;
lv.bits = *GPIO->values_p;
ioctl(GPIO->fd, GPIO_V2_LINE_SET_VALUES_IOCTL, &lv);
}
// public API
int lgGpiochipOpen(int gpioDev)
{
int fd;
int handle;
lgChipObj_p chip;
struct gpiochip_info info;
char chipName[128];
lgLineInf_p lInf; /* used to stop -fanalyzer warning */
LG_DBG(LG_DEBUG_TRACE, "gpioDev=%d", gpioDev);
memset(&info, 0, sizeof(info));
if (gpioDev < 0)
PARAM_ERROR(LG_BAD_GPIOCHIP, "bad gpioDev (%d)", gpioDev);
sprintf(chipName, "/dev/gpiochip%d", gpioDev);
fd = open(chipName, O_RDWR | O_CLOEXEC);
if (fd < 0)
PARAM_ERROR(LG_CANNOT_OPEN_CHIP, "can't open gpiochip (%s)", chipName);
if (ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &info))
{
close(fd);
PARAM_ERROR(LG_NOT_A_GPIOCHIP, "ioct failed (%s)", chipName);
}
handle = lgHdlAlloc(
LG_HDL_TYPE_GPIO, sizeof(lgChipObj_t), (void**)&chip, _lgGpiochipClose);
if (handle < 0)
{
close(fd);
return LG_NOT_ENOUGH_MEMORY;
}
chip->gpiochip = gpioDev;
chip->handle = handle;
/* calloc will zero all members */
lInf = calloc(info.lines, sizeof(lgLineInf_t));
if (lInf == NULL)
{
lgHdlFree(handle, LG_HDL_TYPE_GPIO);
ALLOC_ERROR(LG_NOT_ENOUGH_MEMORY, "can't allocate gpio lines");
}
chip->LineInf = lInf;
LG_DBG(LG_DEBUG_ALLOC, "alloc LineInf: *%p", (void*)chip->LineInf);
strncpy(chip->name, info.name, sizeof(chip->name));
strncpy(chip->label, info.label, sizeof(chip->label));
chip->lines = info.lines;
chip->fd = fd;
strncpy(chip->userLabel, "lg", sizeof(chip->userLabel));
lgPthTxStart();
lgPthAlertStart();
return handle;
}
int lgGpiochipClose(int handle)
{
int status;
lgChipObj_p chip;
LG_DBG(LG_DEBUG_TRACE, "handle=%d", handle);
status = lgHdlGetLockedObj(handle, LG_HDL_TYPE_GPIO, (void **)&chip);
if (status == LG_OKAY)
{
status = lgHdlFree(handle, LG_HDL_TYPE_GPIO);
lgHdlUnlock(handle);
}
return status;
}
int lgGpioSetBannedState(int handle, int gpio, int banned)
{
int status;
lgLineInf_p GPIO;
lgChipObj_p chip;
LG_DBG(LG_DEBUG_TRACE,
"handle=%d gpio=%d banned=%d", handle, gpio, banned);
status = lgHdlGetLockedObj(handle, LG_HDL_TYPE_GPIO, (void **)&chip);
if (status == LG_OKAY)
{
if (gpio < chip->lines)
{
GPIO = &chip->LineInf[gpio];
GPIO->banned = banned;
}
else status = LG_BAD_GPIO_NUMBER;
lgHdlUnlock(handle);
}
return status;
}
int lgGpioGetChipInfo(int handle, lgChipInfo_p chipInfo)
{
int status;
struct gpiochip_info cinfo;
lgChipObj_p chip;
LG_DBG(LG_DEBUG_TRACE, "handle=%d chipInfo=*%p", handle, (void*)chipInfo);
memset(&cinfo, 0, sizeof(cinfo));
status = lgHdlGetLockedObj(handle, LG_HDL_TYPE_GPIO, (void **)&chip);
if (status == LG_OKAY)
{
status = ioctl(chip->fd, GPIO_GET_CHIPINFO_IOCTL, &cinfo);
if (status == 0)
{
chipInfo->lines = cinfo.lines;
strncpy(chipInfo->name, cinfo.name, sizeof(chipInfo->name));
strncpy(chipInfo->label, cinfo.label, sizeof(chipInfo->label));
}
else status = LG_BAD_CHIPINFO_IOCTL;
lgHdlUnlock(handle);
}
return status;
}
int lgGpioGetLineInfo(int handle, int gpio, lgLineInfo_p lineInfo)
{
int status;
struct gpio_v2_line_info linfo;
lgChipObj_p chip;
LG_DBG(LG_DEBUG_TRACE, "handle=%d gpio=%d lineInfo=*%p",
handle, gpio, (void*)lineInfo);
memset(&linfo, 0, sizeof(linfo));
status = lgHdlGetLockedObj(handle, LG_HDL_TYPE_GPIO, (void **)&chip);
if (status == LG_OKAY)
{
if (gpio < chip->lines)
{
linfo.offset = gpio;
status = ioctl(chip->fd, GPIO_V2_GET_LINEINFO_IOCTL, &linfo);
if (status == 0)
{
lineInfo->offset = linfo.offset;
lineInfo->lFlags = xMakeStatus(linfo.flags) |
(chip->LineInf[gpio].mode << 8);
strncpy(lineInfo->name, linfo.name, sizeof(lineInfo->name));
strncpy(lineInfo->user, linfo.consumer, sizeof(lineInfo->user));
}
else status = LG_BAD_LINEINFO_IOCTL;
}
else status = LG_BAD_GPIO_NUMBER;
lgHdlUnlock(handle);
}
return status;
}
int lgGpioGetMode(int handle, int gpio)
{
int status;
struct gpio_v2_line_info linfo;
lgChipObj_p chip;
LG_DBG(LG_DEBUG_TRACE, "handle=%d gpio=%d", handle, gpio);
memset(&linfo, 0, sizeof(linfo));
status = lgHdlGetLockedObj(handle, LG_HDL_TYPE_GPIO, (void **)&chip);
if (status == LG_OKAY)
{
if (gpio < chip->lines)
{
linfo.offset = gpio;
status = ioctl(chip->fd, GPIO_V2_GET_LINEINFO_IOCTL, &linfo);
if (status == 0)
{
status = xMakeStatus(linfo.flags) | (chip->LineInf[gpio].mode << 8);
}
else status = LG_BAD_LINEINFO_IOCTL;
}
else status = LG_BAD_GPIO_NUMBER;
lgHdlUnlock(handle);
}
return status;
}
int lgGroupFree(int handle, int gpio)
{
int status;
lgChipObj_p chip;
LG_DBG(LG_DEBUG_TRACE, "handle=%d gpio=%d", handle, gpio);
status = lgHdlGetLockedObj(handle, LG_HDL_TYPE_GPIO, (void **)&chip);
if (status == LG_OKAY)
{
if (gpio < chip->lines)
{
if (chip->LineInf[gpio].mode != LG_CHIP_MODE_UNKNOWN)
{
if (chip->LineInf[gpio].offset == 0)
{
status = xSetAsFree(chip, gpio);
}
else status = LG_NOT_GROUP_LEADER;
}
else status = LG_GPIO_NOT_ALLOCATED;
}
else status = LG_BAD_GPIO_NUMBER;
lgHdlUnlock(handle);
}
return status;
}
int lgGpioFree(int handle, int gpio)
{
int status;
lgChipObj_p chip;
LG_DBG(LG_DEBUG_TRACE, "handle=%d gpio=%d", handle, gpio);
status = lgHdlGetLockedObj(handle, LG_HDL_TYPE_GPIO, (void **)&chip);
if (status == LG_OKAY)
{
if (gpio < chip->lines)
{
if (chip->LineInf[gpio].mode != LG_CHIP_MODE_UNKNOWN)
{
if (chip->LineInf[gpio].offset == 0)
{
status = xSetAsFree(chip, gpio);
}
else status = LG_NOT_GROUP_LEADER;
}
else status = LG_GPIO_NOT_ALLOCATED;
}
else status = LG_BAD_GPIO_NUMBER;
lgHdlUnlock(handle);
}
return status;
}
int lgGpioSetUser(int handle, const char *user)
{
int status;
lgChipObj_p chip;
LG_DBG(LG_DEBUG_TRACE, "handle=%d user=%s", handle, user);
status = lgHdlGetLockedObj(handle, LG_HDL_TYPE_GPIO, (void **)&chip);
if (status == LG_OKAY)