Skip to content

Commit 0d7003b

Browse files
committedNov 12, 2015
Init for TXINH
1 parent dd2d740 commit 0d7003b

File tree

1 file changed

+103
-87
lines changed

1 file changed

+103
-87
lines changed
 

‎ptt.c

+103-87
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,102 @@ static HANDLE ptt_fd[MAX_CHANS][NUM_OCTYPES];
155155

156156
static char otnames[NUM_OCTYPES][8];
157157

158+
void export_gpio(int gpio, int invert, int direction)
159+
{
160+
HANDLE fd;
161+
char stemp[80];
162+
struct stat finfo;
163+
int err;
164+
165+
fd = open("/sys/class/gpio/export", O_WRONLY);
166+
if (fd < 0) {
167+
text_color_set(DW_COLOR_ERROR);
168+
dw_printf ("Permissions do not allow ordinary users to access GPIO.\n");
169+
dw_printf ("Log in as root and type this command:\n");
170+
dw_printf (" chmod go+w /sys/class/gpio/export /sys/class/gpio/unexport\n");
171+
exit (1);
172+
}
173+
sprintf (stemp, "%d", gpio);
174+
if (write (fd, stemp, strlen(stemp)) != strlen(stemp)) {
175+
int e = errno;
176+
/* Ignore EBUSY error which seems to mean */
177+
/* the device node already exists. */
178+
if (e != EBUSY) {
179+
text_color_set(DW_COLOR_ERROR);
180+
dw_printf ("Error writing \"%s\" to /sys/class/gpio/export, errno=%d\n", stemp, e);
181+
dw_printf ("%s\n", strerror(e));
182+
exit (1);
183+
}
184+
}
185+
close (fd);
186+
187+
/*
188+
* We will have the same permission problem if not root.
189+
* We only care about "direction" and "value".
190+
*/
191+
sprintf (stemp, "sudo chmod go+rw /sys/class/gpio/gpio%d/direction", gpio);
192+
err = system (stemp);
193+
sprintf (stemp, "sudo chmod go+rw /sys/class/gpio/gpio%d/value", gpio);
194+
err = system (stemp);
195+
196+
sprintf (stemp, "/sys/class/gpio/gpio%d/value", gpio);
197+
198+
if (stat(stemp, &finfo) < 0) {
199+
int e = errno;
200+
text_color_set(DW_COLOR_ERROR);
201+
dw_printf ("Failed to get status for %s \n", stemp);
202+
dw_printf ("%s\n", strerror(e));
203+
exit (1);
204+
}
205+
206+
if (geteuid() != 0) {
207+
if ( ! (finfo.st_mode & S_IWOTH)) {
208+
text_color_set(DW_COLOR_ERROR);
209+
dw_printf ("Permissions do not allow ordinary users to access GPIO.\n");
210+
dw_printf ("Log in as root and type these commands:\n");
211+
dw_printf (" chmod go+rw /sys/class/gpio/gpio%d/direction", gpio);
212+
dw_printf (" chmod go+rw /sys/class/gpio/gpio%d/value", gpio);
213+
exit (1);
214+
}
215+
}
216+
217+
/*
218+
* Set direction and initial value.
219+
*/
220+
221+
sprintf (stemp, "/sys/class/gpio/gpio%d/direction", gpio);
222+
fd = open(stemp, O_WRONLY);
223+
if (fd < 0) {
224+
int e = errno;
225+
text_color_set(DW_COLOR_ERROR);
226+
dw_printf ("Error opening %s\n", stemp);
227+
dw_printf ("%s\n", strerror(e));
228+
exit (1);
229+
}
230+
231+
char gpio_val[8];
232+
if (direction) {
233+
if (invert) {
234+
strcpy (gpio_val, "high");
235+
}
236+
else {
237+
strcpy (gpio_val, "low");
238+
}
239+
}
240+
else {
241+
strcpy (gpio_val, "in");
242+
}
243+
244+
if (write (fd, gpio_val, strlen(gpio_val)) != strlen(gpio_val)) {
245+
int e = errno;
246+
text_color_set(DW_COLOR_ERROR);
247+
dw_printf ("Error writing direction to %s\n", stemp);
248+
dw_printf ("%s\n", strerror(e));
249+
exit (1);
250+
}
251+
close (fd);
252+
}
253+
158254
void ptt_init (struct audio_s *audio_config_p)
159255
{
160256
int ch;
@@ -326,6 +422,7 @@ void ptt_init (struct audio_s *audio_config_p)
326422
using_gpio = 1;
327423
}
328424
}
425+
if (audio_config_p->achan[ch].txinh.enabled) using_gpio = 1;
329426
}
330427
}
331428

@@ -343,7 +440,7 @@ void ptt_init (struct audio_s *audio_config_p)
343440
if (stat("/sys/class/gpio/export", &finfo) < 0) {
344441
text_color_set(DW_COLOR_ERROR);
345442
dw_printf ("This system is not configured with the GPIO user interface.\n");
346-
dw_printf ("Use a different method for PTT control.\n");
443+
dw_printf ("Use a different method for PTT, DCD, or TXINH control.\n");
347444
exit (1);
348445
}
349446

@@ -367,7 +464,7 @@ void ptt_init (struct audio_s *audio_config_p)
367464
/* Unexpected because we could do it before. */
368465
text_color_set(DW_COLOR_ERROR);
369466
dw_printf ("This system is not configured with the GPIO user interface.\n");
370-
dw_printf ("Use a different method for PTT control.\n");
467+
dw_printf ("Use a different method for PTT, DCD, or TXINH control.\n");
371468
exit (1);
372469
}
373470

@@ -392,93 +489,12 @@ void ptt_init (struct audio_s *audio_config_p)
392489
int ot;
393490
for (ot = 0; ot < NUM_OCTYPES; ot++) {
394491
if (audio_config_p->achan[ch].octrl[ot].ptt_method == PTT_METHOD_GPIO) {
395-
char stemp[80];
396-
struct stat finfo;
397-
int err;
398-
399-
fd = open("/sys/class/gpio/export", O_WRONLY);
400-
if (fd < 0) {
401-
text_color_set(DW_COLOR_ERROR);
402-
dw_printf ("Permissions do not allow ordinary users to access GPIO.\n");
403-
dw_printf ("Log in as root and type this command:\n");
404-
dw_printf (" chmod go+w /sys/class/gpio/export /sys/class/gpio/unexport\n");
405-
exit (1);
406-
}
407-
sprintf (stemp, "%d", audio_config_p->achan[ch].octrl[ot].ptt_gpio);
408-
if (write (fd, stemp, strlen(stemp)) != strlen(stemp)) {
409-
int e = errno;
410-
/* Ignore EBUSY error which seems to mean */
411-
/* the device node already exists. */
412-
if (e != EBUSY) {
413-
text_color_set(DW_COLOR_ERROR);
414-
dw_printf ("Error writing \"%s\" to /sys/class/gpio/export, errno=%d\n", stemp, e);
415-
dw_printf ("%s\n", strerror(e));
416-
exit (1);
417-
}
418-
}
419-
close (fd);
420-
421-
/*
422-
* We will have the same permission problem if not root.
423-
* We only care about "direction" and "value".
424-
*/
425-
sprintf (stemp, "sudo chmod go+rw /sys/class/gpio/gpio%d/direction", audio_config_p->achan[ch].octrl[ot].ptt_gpio);
426-
err = system (stemp);
427-
sprintf (stemp, "sudo chmod go+rw /sys/class/gpio/gpio%d/value", audio_config_p->achan[ch].octrl[ot].ptt_gpio);
428-
err = system (stemp);
429-
430-
sprintf (stemp, "/sys/class/gpio/gpio%d/value", audio_config_p->achan[ch].octrl[ot].ptt_gpio);
431-
432-
if (stat(stemp, &finfo) < 0) {
433-
int e = errno;
434-
text_color_set(DW_COLOR_ERROR);
435-
dw_printf ("Failed to get status for %s \n", stemp);
436-
dw_printf ("%s\n", strerror(e));
437-
exit (1);
438-
}
439-
440-
if (geteuid() != 0) {
441-
if ( ! (finfo.st_mode & S_IWOTH)) {
442-
text_color_set(DW_COLOR_ERROR);
443-
dw_printf ("Permissions do not allow ordinary users to access GPIO.\n");
444-
dw_printf ("Log in as root and type these commands:\n");
445-
dw_printf (" chmod go+rw /sys/class/gpio/gpio%d/direction", audio_config_p->achan[ch].octrl[ot].ptt_gpio);
446-
dw_printf (" chmod go+rw /sys/class/gpio/gpio%d/value", audio_config_p->achan[ch].octrl[ot].ptt_gpio);
447-
exit (1);
448-
}
449-
}
450-
451-
/*
452-
* Set output direction with initial state off.
453-
*/
454-
455-
sprintf (stemp, "/sys/class/gpio/gpio%d/direction", audio_config_p->achan[ch].octrl[ot].ptt_gpio);
456-
fd = open(stemp, O_WRONLY);
457-
if (fd < 0) {
458-
int e = errno;
459-
text_color_set(DW_COLOR_ERROR);
460-
dw_printf ("Error opening %s\n", stemp);
461-
dw_printf ("%s\n", strerror(e));
462-
exit (1);
463-
}
464-
465-
char hilo[8];
466-
if (audio_config_p->achan[ch].octrl[ot].ptt_invert) {
467-
strcpy (hilo, "high");
468-
}
469-
else {
470-
strcpy (hilo, "low");
471-
}
472-
if (write (fd, hilo, strlen(hilo)) != strlen(hilo)) {
473-
int e = errno;
474-
text_color_set(DW_COLOR_ERROR);
475-
dw_printf ("Error writing initial state to %s\n", stemp);
476-
dw_printf ("%s\n", strerror(e));
477-
exit (1);
478-
}
479-
close (fd);
492+
export_gpio(audio_config_p->achan[ch].octrl[ot].ptt_gpio, audio_config_p->achan[ch].octrl[ot].ptt_invert, 1);
480493
}
481494
}
495+
if (audio_config_p->achan[ch].txinh.enabled) {
496+
export_gpio(audio_config_p->achan[ch].txinh.gpio, audio_config_p->achan[ch].txinh.invert, 0);
497+
}
482498
}
483499
}
484500
#endif

0 commit comments

Comments
 (0)