forked from joan2937/lg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgpio.3
5423 lines (3776 loc) · 74.8 KB
/
rgpio.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 lgd_if.3
.\"
.TH rgpio 3 2020-2021 Linux "lg archive"
.SH NAME
rgpio - A C library to manipulate a remote SBC's GPIO.
.SH SYNOPSIS
#include <rgpio.h>
gcc -Wall -o prog prog.c -lrgpio
\&./prog
.SH DESCRIPTION
.ad l
.nh
.br
.br
rgpio is a C library which allows remote control of the GPIO and
other functions of Linux SBCs running the rgpiod daemon.
.br
.br
The rgpiod daemon must be running on the SBCs you wish to control.
.br
.br
.SS Features
.br
.br
.br
o reading and writing GPIO singly and in groups
.br
o software timed PWM and waves
.br
o GPIO callbacks
.br
o pipe notification of GPIO alerts
.br
o I2C wrapper
.br
o SPI wrapper
.br
o serial link wrapper
.br
o simple file handling
.br
o creating and running scripts on the rgpiod daemon
.br
o a simple interface to start and stop new threads
.br
.br
.SS Usage
.br
.br
Include <rgpio.h> in your source files.
.br
.br
Assuming your source is in prog.c use the following command to build
.br
.br
.EX
gcc -Wall -o prog prog.c -lrgpio
.br
.EE
.br
.br
to run make sure the rgpiod daemon is running
.br
.br
.EX
rgpiod&
.br
.br
./prog
.br
.EE
.br
.br
For examples see the lg archive file.
.br
.br
.SS Notes
.br
.br
All the functions which return an int return < 0 on error
.br
.br
.SH OVERVIEW
.br
.SS ESSENTIAL
.br
.br
rgpiod_start Connects to a rgpiod daemon
.br
rgpiod_stop Disconnects from a rgpiod daemon
.br
.SS FILES
.br
.br
file_open Opens a file
.br
file_close Closes a file
.br
.br
file_read Reads bytes from a file
.br
file_write Writes bytes to a file
.br
.br
file_seek Seeks to a position within a file
.br
.br
file_list List files which match a pattern
.br
.SS GPIO
.br
.br
gpiochip_open Opens a gpiochip device
.br
gpiochip_close Closes a gpiochip device
.br
.br
gpio_get_chip_info Gets gpiochip information
.br
gpio_get_line_info Gets gpiochip line information
.br
gpio_get_mode Gets the mode of a GPIO
.br
.br
gpio_claim_input Claims a GPIO for input
.br
gpio_claim_output Claims a GPIO for output
.br
gpio_claim_alert Claims a GPIO for alerts
.br
gpio_free Frees a GPIO
.br
.br
group_claim_input Claims a group of GPIO for inputs
.br
group_claim_output Claims a group of GPIO for outputs
.br
group_free Frees a group of GPIO
.br
.br
gpio_read Reads a GPIO
.br
gpio_write Writes a GPIO
.br
.br
group_read Reads a group of GPIO
.br
group_write Writes a group of GPIO
.br
.br
tx_pulse Starts pulses on a GPIO
.br
tx_pwm Starts PWM on a GPIO
.br
tx_servo Starts servo pulses on a GPIO.
.br
tx_wave Starts a wave on a group of GPIO
.br
tx_busy See if tx is active on a GPIO or group
.br
tx_room See if more room for tx on a GPIO or group
.br
.br
gpio_set_debounce_time Sets the debounce time for a GPIO
.br
gpio_set_watchdog_time Sets the watchdog time for a GPIO
.br
.br
callback Starts a GPIO callback
.br
callback_cancel Stops a GPIO callback
.br
.SS I2C
.br
.br
i2c_open Opens an I2C device
.br
i2c_close Closes an I2C device
.br
.br
i2c_write_quick smbus write quick
.br
.br
i2c_read_byte smbus read byte
.br
i2c_write_byte smbus write byte
.br
.br
i2c_read_byte_data smbus read byte data
.br
i2c_write_byte_data smbus write byte data
.br
.br
i2c_read_word_data smbus read word data
.br
i2c_write_word_data smbus write word data
.br
.br
i2c_read_block_data smbus read block data
.br
i2c_write_block_data smbus write block data
.br
.br
i2c_read_i2c_block_data smbus read I2C block data
.br
i2c_write_i2c_block_data smbus write I2C block data
.br
.br
i2c_read_device Reads the raw I2C device
.br
i2c_write_device Writes the raw I2C device
.br
.br
i2c_process_call smbus process call
.br
i2c_block_process_call smbus block process call
.br
.br
i2c_zip Performs multiple I2C transactions
.br
.SS NOTIFICATIONS
.br
.br
notify_open Request a notification handle
.br
notify_close Close a notification
.br
notify_pause Pause notifications
.br
notify_resume Start notifications for selected GPIO
.br
.SS SCRIPTS
.br
.br
script_store Store a script
.br
script_run Run a stored script
.br
script_update Set a scripts parameters
.br
script_status Get script status and parameters
.br
script_stop Stop a running script
.br
script_delete Delete a stored script
.br
.SS SERIAL
.br
.br
serial_open Opens a serial device
.br
serial_close Closes a serial device
.br
.br
serial_read_byte Reads a byte from a serial device
.br
serial_write_byte Writes a byte to a serial device
.br
.br
serial_read Reads bytes from a serial device
.br
serial_write Writes bytes to a serial device
.br
.br
serial_data_available Returns number of bytes ready to be read
.br
.SS SHELL
.br
.br
shell Executes a shell command
.br
.SS SPI
.br
.br
spi_open Opens a SPI device
.br
spi_close Closes a SPI device
.br
.br
spi_read Reads bytes from a SPI device
.br
spi_write Writes bytes to a SPI device
.br
spi_xfer Transfers bytes with a SPI device
.br
.SS THREADS
.br
.br
thread_start Start a new thread
.br
thread_stop Stop a previously started thread
.br
.SS UTILITIES
.br
.br
lgu_get_sbc_name Get the SBC name
.br
.br
lgu_get_internal Get a SBC configuration value
.br
lgu_set_internal Set a SBC configuration value
.br
.br
lgu_time Returns the number of seconds since the epoch
.br
lgu_timestamp Returns the number of nanoseconds since the epoch
.br
.br
lgu_sleep Sleeps for a number of seconds
.br
.br
lgu_set_user Set the user (and associated permissions)
.br
.br
lgu_set_share_id Set the share id for a resource
.br
lgu_use_share_id Use this share id when asking for a resource
.br
.br
lgu_rgpio_version Get the rgpio library version
.br
lgu_error_text Get the error text for an error code
.br
.SH FUNCTIONS
.IP "\fBint rgpiod_start(const char *addrStr, const char *portStr)\fP"
.IP "" 4
Connect to the rgpiod daemon. Reserving command and
notification streams.
.br
.br
.EX
addrStr: specifies the host or IP address of the SBC running the
.br
rgpiod daemon. It may be NULL in which case localhost
.br
is used unless overridden by the LG_ADDR environment
.br
variable.
.br
.br
portStr: specifies the port address used by the SBC running the
.br
rgpiod daemon. It may be NULL in which case "8889"
.br
is used unless overridden by the LG_PORT environment
.br
variable.
.br
.EE
.br
.br
If OK returns a sbc (>= 0).
.br
.br
On failure returns a negative error code.
.br
.br
This sbc value is passed to the other functions to specify
the SBC to be used.
.br
.br
If the LG_USER environment variable exists that user will be
"logged in" using \fBlgu_set_user\fP. This only has an effect if
the rgpiod daemon is running with access control enabled.
.IP "\fBvoid rgpiod_stop(int sbc)\fP"
.IP "" 4
Terminates the connection to a rgpiod daemon and frees
resources used by the library.
.br
.br
.EX
sbc: >= 0 (as returned by \fBrgpiod_start\fP).
.br
.EE
.IP "\fBint file_open(int sbc, const char *file, int mode)\fP"
.IP "" 4
This function returns a handle to a file opened in a specified mode.
.br
.br
This is a privileged command. See \fBpermits\fP.
.br
.br
.EX
sbc: >= 0 (as returned by \fBrgpiod_start\fP).
.br
file: the file to open.
.br
mode: the file open mode.
.br
.EE
.br
.br
If OK returns a handle (>= 0).
.br
.br
On failure returns a negative error code.
.br
.br
File
.br
.br
A file may only be opened if permission is granted by an entry in
the [files] section of the permits file. This is intended to allow
remote access to files in a controlled manner.
.br
.br
Mode
.br
.br
The mode may have the following values.
.br
.br
Macro Value Meaning
.br
LG_FILE_READ 1 open file for reading
.br
LG_FILE_WRITE 2 open file for writing
.br
LG_FILE_RW 3 open file for reading and writing
.br
.br
.br
The following values may be or'd into the mode.
.br
.br
Macro Value Meaning
.br
LG_FILE_APPEND 4 Writes append data to the end of the file
.br
LG_FILE_CREATE 8 The file is created if it doesn't exist
.br
LG_FILE_TRUNC 16 The file is truncated
.br
.br
.br
Newly created files are owned by the user who launched the daemon
with permissions owner read and write.
.br
.br
\fBExample\fP
.br
.EX
#include <stdio.h>
.br
#include <rgpio.h>
.br
.br
int main(int argc, char *argv[])
.br
{
.br
int sbc, handle, c;
.br
char buf[60000];
.br
.br
sbc = rgpiod_start(NULL, NULL);
.br
.br
if (sbc < 0) return 1;
.br
.br
handle = file_open(sbc, "/ram/lg.c", LG_FILE_READ);
.br
.br
if (handle >= 0)
.br
{
.br
while ((c=file_read(sbc, handle, buf, sizeof(buf)-1)))
.br
{
.br
buf[c] = 0;
.br
printf("%s", buf);
.br
}
.br
.br
file_close(sbc, handle);
.br
}
.br
.br
rgpiod_stop(sbc);
.br
}
.br
.EE
.IP "\fBint file_close(int sbc, int handle)\fP"
.IP "" 4
This function closes the file.
.br
.br
.EX
sbc: >= 0 (as returned by \fBrgpiod_start\fP).
.br
handle: >= 0 (as returned by \fBfile_open\fP).
.br
.EE
.br
.br
If OK returns 0.
.br
.br
On failure returns a negative error code.
.br
.br
\fBExample\fP
.br
.EX
file_close(sbc, handle);
.br
.EE
.IP "\fBint file_write(int sbc, int handle, const char *buf, int count)\fP"
.IP "" 4
This function writes count bytes from buf to the the file.
.br
.br
.EX
sbc: >= 0 (as returned by \fBrgpiod_start\fP).
.br
handle: >= 0 (as returned by \fBfile_open\fP).
.br
buf: the array of bytes to write.
.br
count: the number of bytes to write.
.br
.EE
.br
.br
If OK returns 0.
.br
.br
On failure returns a negative error code.
.br
.br
\fBExample\fP
.br
.EX
if (file_write(sbc, handle, buf, 100) == 0)
.br
{
.br
// file written okay
.br
}
.br
else
.br
{
.br
// error
.br
}
.br
.EE
.IP "\fBint file_read(int sbc, int handle, char *buf, int count)\fP"
.IP "" 4
This function reads up to count bytes from the the file.
.br
.br
.EX
sbc: >= 0 (as returned by \fBrgpiod_start\fP).
.br
handle: >= 0 (as returned by \fBfile_open\fP).
.br
buf: an array to receive the read data.
.br
count: the maximum number of bytes to read.
.br
.EE
.br
.br
If OK returns the count of bytes read and updates buf.
.br
.br
On failure returns a negative error code.
.br
.br
\fBExample\fP
.br
.EX
bytes = file_read(sbc, handle, buf, sizeof(buf));
.br
.br
if (bytes >= 0)
.br
{
.br
// process read data
.br
}
.br
.EE
.IP "\fBint file_seek(int sbc, int handle, int32_t seekOffset, int seekFrom)\fP"
.IP "" 4
This function seeks to a position within the file.
.br
.br
.EX
sbc: >= 0 (as returned by \fBrgpiod_start\fP).
.br
handle: >= 0 (as returned by \fBfile_open\fP).
.br
seekOffset: the number of bytes to move. Positive offsets
.br
move forward, negative offsets backwards.
.br
seekFrom: one of LG_FROM_START (0), LG_FROM_CURRENT (1),
.br
or LG_FROM_END (2).
.br
.EE
.br
.br
If OK returns the new file position.
.br
.br
On failure returns a negative error code.
.br
.br
\fBExample\fP
.br
.EX
file_seek(sbc, handle, 123, LG_FROM_START); // Start plus 123
.br
.br
size = file_seek(sbc, handle, 0, LG_FROM_END); // End, return size
.br
.br
pos = file_seek(sbc, handle, 0, LG_FROM_CURRENT); // Current position
.br
.EE
.IP "\fBint file_list(int sbc, const char *fpat, char *buf, int count)\fP"
.IP "" 4
This function returns a list of files which match a pattern.
.br
.br
.EX
sbc: >= 0 (as returned by \fBrgpiod_start\fP).
.br
fpat: file pattern to match.
.br
buf: an array to receive the matching file names.
.br
count: the maximum number of bytes to read.
.br
.EE
.br
.br
If OK returns the count of bytes read and updates buf with
the matching filenames (the filenames are separated by newline
characters).
.br
.br
On failure returns a negative error code.
.br
.br
\fBExample\fP
.br
.EX
#include <stdio.h>
.br
#include <rgpio.h>
.br
.br
int main(int argc, char *argv[])
.br
{
.br
int sbc, handle, c;
.br
char buf[60000];
.br
.br
sbc = rgpiod_start(NULL, NULL);
.br
.br
if (sbc < 0) return 1;
.br
.br
c = file_list(sbc, "/ram/p*.c", buf, sizeof(buf));
.br
.br
if (c >= 0)
.br
{
.br
buf[c] = 0;
.br
printf("%s", buf);
.br
}
.br
.br
rgpiod_stop(sbc);
.br
}
.br
.EE
.IP "\fBint gpiochip_open(int sbc, int gpioDev)\fP"
.IP "" 4
This returns a handle to a gpiochip device.
.br
.br
This is a privileged command. See \fBpermits\fP.
.br
.br
.EX
sbc: >= 0 (as returned by \fBrgpiod_start\fP).
.br
gpioDev: >= 0
.br
.EE
.br
.br
If OK returns a handle (>= 0).
.br
.br
On failure returns a negative error code.
.br
.br
\fBExample\fP
.br
.EX
h = gpiochip_open(sbc, 0); // open gpiochip0
.br
.br
if (h >= 0)
.br
{
.br
// open ok
.br
}
.br
else
.br
{
.br
// open error
.br
}
.br
.EE
.br
.br
.IP "\fBint gpiochip_close(int sbc, int handle)\fP"
.IP "" 4
This closes a gpiochip device.
.br
.br