166
166
#include "cm108.h"
167
167
#endif
168
168
169
+ #ifdef USE_GPIOD
170
+ #include <gpiod.h>
171
+ #endif
172
+
169
173
/* So we can have more common code for fd. */
170
174
typedef int HANDLE ;
171
175
#define INVALID_HANDLE_VALUE (-1)
@@ -623,6 +627,31 @@ void export_gpio(int ch, int ot, int invert, int direction)
623
627
get_access_to_gpio (gpio_value_path );
624
628
}
625
629
630
+ #if defined(USE_GPIOD )
631
+ int gpiod_probe (const char * chip_name , int line_number )
632
+ {
633
+ struct gpiod_chip * chip ;
634
+ chip = gpiod_chip_open_by_name (chip_name );
635
+ if (chip == NULL ) {
636
+ text_color_set (DW_COLOR_ERROR );
637
+ dw_printf ("Can't open GPIOD chip %s.\n" , chip_name );
638
+ return -1 ;
639
+ }
640
+
641
+ struct gpiod_line * line ;
642
+ line = gpiod_chip_get_line (chip , line_number );
643
+ if (line == NULL ) {
644
+ text_color_set (DW_COLOR_ERROR );
645
+ dw_printf ("Can't get GPIOD line %d.\n" , line_number );
646
+ return -1 ;
647
+ }
648
+ if (ptt_debug_level >= 2 ) {
649
+ text_color_set (DW_COLOR_DEBUG );
650
+ dw_printf ("GPIOD probe OK. Chip: %s line: %d\n" , chip_name , line_number );
651
+ }
652
+ return 0 ;
653
+ }
654
+ #endif /* USE_GPIOD */
626
655
#endif /* not __WIN32__ */
627
656
628
657
@@ -639,7 +668,8 @@ void export_gpio(int ch, int ot, int invert, int direction)
639
668
* ptt_method Method for PTT signal.
640
669
* PTT_METHOD_NONE - not configured. Could be using VOX.
641
670
* PTT_METHOD_SERIAL - serial (com) port.
642
- * PTT_METHOD_GPIO - general purpose I/O.
671
+ * PTT_METHOD_GPIO - general purpose I/O (sysfs).
672
+ * PTT_METHOD_GPIOD - general purpose I/O (libgpiod).
643
673
* PTT_METHOD_LPT - Parallel printer port.
644
674
* PTT_METHOD_HAMLIB - HAMLib rig control.
645
675
* PTT_METHOD_CM108 - GPIO pins of CM108 etc. USB Audio.
@@ -718,12 +748,13 @@ void ptt_init (struct audio_s *audio_config_p)
718
748
if (ptt_debug_level >= 2 ) {
719
749
720
750
text_color_set (DW_COLOR_DEBUG );
721
- dw_printf ("ch=%d, %s method=%d, device=%s, line=%d, gpio=%d, lpt_bit=%d, invert=%d\n" ,
751
+ dw_printf ("ch=%d, %s method=%d, device=%s, line=%d, name=%s, gpio=%d, lpt_bit=%d, invert=%d\n" ,
722
752
ch ,
723
753
otnames [ot ],
724
754
audio_config_p -> achan [ch ].octrl [ot ].ptt_method ,
725
755
audio_config_p -> achan [ch ].octrl [ot ].ptt_device ,
726
756
audio_config_p -> achan [ch ].octrl [ot ].ptt_line ,
757
+ audio_config_p -> achan [ch ].octrl [ot ].out_gpio_name ,
727
758
audio_config_p -> achan [ch ].octrl [ot ].out_gpio_num ,
728
759
audio_config_p -> achan [ch ].octrl [ot ].ptt_lpt_bit ,
729
760
audio_config_p -> achan [ch ].octrl [ot ].ptt_invert );
@@ -869,7 +900,28 @@ void ptt_init (struct audio_s *audio_config_p)
869
900
if (using_gpio ) {
870
901
get_access_to_gpio ("/sys/class/gpio/export" );
871
902
}
872
-
903
+ #if defined(USE_GPIOD )
904
+ // GPIOD
905
+ for (ch = 0 ; ch < MAX_CHANS ; ch ++ ) {
906
+ if (save_audio_config_p -> achan [ch ].medium == MEDIUM_RADIO ) {
907
+ for (int ot = 0 ; ot < NUM_OCTYPES ; ot ++ ) {
908
+ if (audio_config_p -> achan [ch ].octrl [ot ].ptt_method == PTT_METHOD_GPIOD ) {
909
+ const char * chip_name = audio_config_p -> achan [ch ].octrl [ot ].out_gpio_name ;
910
+ int line_number = audio_config_p -> achan [ch ].octrl [ot ].out_gpio_num ;
911
+ int rc = gpiod_probe (chip_name , line_number );
912
+ if (rc < 0 ) {
913
+ text_color_set (DW_COLOR_ERROR );
914
+ dw_printf ("Disable PTT for channel %d\n" , ch );
915
+ audio_config_p -> achan [ch ].octrl [ot ].ptt_method = PTT_METHOD_NONE ;
916
+ } else {
917
+ // Set initial state off ptt_set will invert output signal if appropriate.
918
+ ptt_set (ot , ch , 0 );
919
+ }
920
+ }
921
+ }
922
+ }
923
+ }
924
+ #endif /* USE_GPIOD */
873
925
/*
874
926
* We should now be able to create the device nodes for
875
927
* the pins we want to use.
@@ -1226,6 +1278,18 @@ void ptt_set (int ot, int chan, int ptt_signal)
1226
1278
close (fd );
1227
1279
1228
1280
}
1281
+
1282
+ #if defined(USE_GPIOD )
1283
+ if (save_audio_config_p -> achan [chan ].octrl [ot ].ptt_method == PTT_METHOD_GPIOD ) {
1284
+ const char * chip = save_audio_config_p -> achan [chan ].octrl [ot ].out_gpio_name ;
1285
+ int line = save_audio_config_p -> achan [chan ].octrl [ot ].out_gpio_num ;
1286
+ int rc = gpiod_ctxless_set_value (chip , line , ptt , false, "direwolf" , NULL , NULL );
1287
+ if (ptt_debug_level >= 1 ) {
1288
+ text_color_set (DW_COLOR_DEBUG );
1289
+ dw_printf ("PTT_METHOD_GPIOD chip: %s line: %d ptt: %d rc: %d\n" , chip , line , ptt , rc );
1290
+ }
1291
+ }
1292
+ #endif /* USE_GPIOD */
1229
1293
#endif
1230
1294
1231
1295
/*
0 commit comments