Skip to content

Commit 4f50370

Browse files
committed
Change GPIOD usage case from ctxless to request mode
1 parent 5736b0f commit 4f50370

File tree

2 files changed

+71
-27
lines changed

2 files changed

+71
-27
lines changed

Diff for: src/audio.h

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#include <hamlib/rig.h>
1717
#endif
1818

19+
#ifdef USE_GPIOD
20+
#include <gpiod.h>
21+
#endif
22+
1923
#include "direwolf.h" /* for MAX_RADIO_CHANS and MAX_TOTAL_CHANS used throughout the application. */
2024
#include "ax25_pad.h" /* for AX25_MAX_ADDR_LEN */
2125
#include "version.h"
@@ -338,6 +342,10 @@ struct audio_s {
338342
/* If zero, hamlib will come up with a default for pariticular rig. */
339343
#endif
340344

345+
#if defined(USE_GPIOD)
346+
struct gpiod_line *gpiod_line_handle;
347+
#endif
348+
341349
} octrl[NUM_OCTYPES];
342350

343351

Diff for: src/ptt.c

+63-27
Original file line numberDiff line numberDiff line change
@@ -653,28 +653,27 @@ void export_gpio(int ch, int ot, int invert, int direction)
653653
}
654654

655655
#if defined(USE_GPIOD)
656-
int gpiod_probe(const char *chip_name, int line_number)
657-
{
658-
struct gpiod_chip *chip;
659-
chip = gpiod_chip_open_by_name(chip_name);
660-
if (chip == NULL) {
661-
text_color_set(DW_COLOR_ERROR);
662-
dw_printf ("Can't open GPIOD chip %s.\n", chip_name);
663-
return -1;
664-
}
665656

666-
struct gpiod_line *line;
667-
line = gpiod_chip_get_line(chip, line_number);
668-
if (line == NULL) {
657+
int gpiod_set(struct gpiod_line* line, int ptt){
658+
int rc=0;
659+
bool owner = gpiod_line_is_requested(line);
660+
if(owner){
661+
int rc2 = gpiod_line_set_value(line, ptt);
662+
if (rc2 !=0) {
663+
text_color_set(DW_COLOR_ERROR);
664+
dw_printf ("Error: gpiod_line_set_value %s %d, errno=%d\n", gpiod_chip_name(gpiod_line_get_chip(line)), gpiod_line_offset(line),errno);
665+
rc=rc-2;
666+
}
667+
}else{
669668
text_color_set(DW_COLOR_ERROR);
670-
dw_printf ("Can't get GPIOD line %d.\n", line_number);
671-
return -1;
672-
}
673-
if (ptt_debug_level >= 2) {
674-
text_color_set(DW_COLOR_DEBUG);
675-
dw_printf("GPIOD probe OK. Chip: %s line: %d\n", chip_name, line_number);
669+
dw_printf ("Error: didnot request %s %d,\n", gpiod_chip_name(gpiod_line_get_chip(line)), gpiod_line_offset(line));
670+
rc=rc-1;
676671
}
677-
return 0;
672+
if (ptt_debug_level >= 1) {
673+
text_color_set(DW_COLOR_DEBUG);
674+
dw_printf("PTT_METHOD_GPIOD chip: %s line: %d ptt: %d rc: %d\n", gpiod_chip_name(gpiod_line_get_chip(line)), gpiod_line_offset(line), ptt, rc);
675+
}
676+
return rc;
678677
}
679678
#endif /* USE_GPIOD */
680679
#endif /* not __WIN32__ */
@@ -933,7 +932,34 @@ void ptt_init (struct audio_s *audio_config_p)
933932
if (audio_config_p->achan[ch].octrl[ot].ptt_method == PTT_METHOD_GPIOD) {
934933
const char *chip_name = audio_config_p->achan[ch].octrl[ot].out_gpio_name;
935934
int line_number = audio_config_p->achan[ch].octrl[ot].out_gpio_num;
936-
int rc = gpiod_probe(chip_name, line_number);
935+
int rc=0;
936+
struct gpiod_chip *chip;
937+
chip = gpiod_chip_open_by_name(chip_name);
938+
if (chip == NULL) {
939+
text_color_set(DW_COLOR_ERROR);
940+
dw_printf ("Can't open GPIOD chip %s.\n", chip_name);
941+
rc=rc-1;
942+
}
943+
struct gpiod_line *line;
944+
line = gpiod_chip_get_line(chip, line_number);
945+
if (line == NULL) {
946+
text_color_set(DW_COLOR_ERROR);
947+
dw_printf ("Can't get GPIOD line %d.\n", line_number);
948+
rc=rc-2;
949+
}else{
950+
audio_config_p->achan[ch].octrl[ot].gpiod_line_handle=line;
951+
}
952+
if ( gpiod_line_request_output(line, "direwolf", 0) != 0 ) {
953+
text_color_set(DW_COLOR_ERROR);
954+
dw_printf ("Can't request GPIOD line %d.\n", line_number);
955+
rc=rc-4;
956+
}else{
957+
dw_printf ("Request GPIOD line %d.\n", line_number);
958+
}
959+
if (ptt_debug_level >= 2) {
960+
text_color_set(DW_COLOR_DEBUG);
961+
dw_printf("GPIOD probe OK. Chip: %s line: %d\n", chip_name, line_number);
962+
}
937963
if (rc < 0) {
938964
text_color_set(DW_COLOR_ERROR);
939965
dw_printf ("Disable PTT for channel %d\n", ch);
@@ -1387,13 +1413,9 @@ void ptt_set (int ot, int chan, int ptt_signal)
13871413

13881414
#if defined(USE_GPIOD)
13891415
if (save_audio_config_p->achan[chan].octrl[ot].ptt_method == PTT_METHOD_GPIOD) {
1390-
const char *chip = save_audio_config_p->achan[chan].octrl[ot].out_gpio_name;
1391-
int line = save_audio_config_p->achan[chan].octrl[ot].out_gpio_num;
1392-
int rc = gpiod_ctxless_set_value(chip, line, ptt, false, "direwolf", NULL, NULL);
1393-
if (ptt_debug_level >= 1) {
1394-
text_color_set(DW_COLOR_DEBUG);
1395-
dw_printf("PTT_METHOD_GPIOD chip: %s line: %d ptt: %d rc: %d\n", chip, line, ptt, rc);
1396-
}
1416+
struct gpiod_line *line = save_audio_config_p->achan[chan].octrl[ot].gpiod_line_handle;
1417+
gpiod_set(line, ptt);
1418+
13971419
}
13981420
#endif /* USE_GPIOD */
13991421
#endif
@@ -1600,6 +1622,20 @@ void ptt_term (void)
16001622
}
16011623
}
16021624
#endif
1625+
#if defined(USE_GPIOD)
1626+
// GPIOD
1627+
for (int ch = 0; ch < MAX_RADIO_CHANS; ch++) {
1628+
if (save_audio_config_p->chan_medium[ch] == MEDIUM_RADIO) {
1629+
for (int ot = 0; ot < NUM_OCTYPES; ot++) {
1630+
if (save_audio_config_p->achan[ch].octrl[ot].ptt_method == PTT_METHOD_GPIOD) {
1631+
struct gpiod_line *line = save_audio_config_p->achan[ch].octrl[ot].gpiod_line_handle;
1632+
gpiod_line_release(line);
1633+
save_audio_config_p->achan[ch].octrl[ot].gpiod_line_handle=NULL;
1634+
}
1635+
}
1636+
}
1637+
}
1638+
#endif /* USE_GPIOD */
16031639
}
16041640

16051641

0 commit comments

Comments
 (0)