Applied sysfs gpio RS-485 directional control patch from APRS World

This commit is contained in:
James Jarvis 2017-02-16 16:34:31 -06:00
parent 9916b2003e
commit 54fffd0895
5 changed files with 49 additions and 13 deletions

View File

@ -54,7 +54,7 @@ typedef struct
int ttyspeed;
/* tty mode */
char *ttymode;
/* trx control type (0 - ADDC, 1 - by RTS) */
/* trx control type (0 - ADDC, 1 - by RTS, 2 - by sysfs GPIO with 1 activating transmit, 3 - by sysfs GPIO with 0 activating transmit) */
int trxcntl;
/* TCP server port number */
int serverport;

View File

@ -248,7 +248,7 @@ conn_write(int d, void *buf, size_t nbytes, int istty)
long delay;
#ifdef TRXCTL
if (istty) {
if (cfg.trxcntl == TRX_RTS)
if (cfg.trxcntl != TRX_ADDC )
tty_set_rts(d);
usleep(35000000l/cfg.ttyspeed);
}
@ -278,7 +278,7 @@ conn_write(int d, void *buf, size_t nbytes, int istty)
usleep(delay);
#endif
/* tcdrain(d); - hangs sometimes, so make calculated delay */
if (cfg.trxcntl == TRX_RTS) {
if (cfg.trxcntl != TRX_ADDC ) {
tty_clr_rts(d);
}
}

View File

@ -104,7 +104,7 @@ usage(char *exename)
"Andrew Denysenko <nitr0@seti.kr.ua>\n\n"
"Usage: %s [-h] [-d] "
#ifdef TRXCTL
"[-t] "
"[-t] [-y sysfsfile] [-Y sysfsfile]\n"
#endif
"[-v level] [-L logfile] [-p device] [-s speed] [-m mode] [-P port]\n"
" [-C maxconn] [-N retries] [-R pause] [-W wait] [-T timeout]\n\n"
@ -112,7 +112,9 @@ usage(char *exename)
" -h : this help\n"
" -d : don't daemonize\n"
#ifdef TRXCTL
" -t : enable RTS RS-485 data direction control\n"
" -t : enable RTS RS-485 data direction control using RTS\n"
" -y : enable RTS RS-485 data direction control using sysfs file, active transmit\n"
" -Y : enable RTS RS-485 data direction control using sysfs file, active receive\n"
#endif
#ifdef LOG
#ifdef DEBUG
@ -165,7 +167,7 @@ main(int argc, char *argv[])
while ((rc = getopt(argc, argv,
"dh"
#ifdef TRXCTL
"t"
"ty:Y:"
#endif
#ifdef LOG
"v:L:"
@ -183,6 +185,14 @@ main(int argc, char *argv[])
case 't':
cfg.trxcntl = TRX_RTS;
break;
case 'y':
cfg.trxcntl = TRX_SYSFS_1;
strncpy(cfg.trxcntl_file, optarg, INTBUFSIZE);
break;
case 'Y':
cfg.trxcntl = TRX_SYSFS_0;
strncpy(cfg.trxcntl_file, optarg, INTBUFSIZE);
break;
#endif
#ifdef LOG
case 'v':

View File

@ -362,20 +362,44 @@ tty_close(ttydata_t *mod)
}
#ifdef TRXCTL
void sysfs_gpio_set(char *filename, char *value) {
int fd;
fd = open(filename, O_WRONLY);
/* write first byte. Should only be 0 or 1 */
write(fd, value, 1);
close(fd);
logw(9, "tty: sysfs_gpio_set(%s,%s)\n",filename,value);
}
/* Set RTS line to active state */
void
tty_set_rts(int fd)
{
int mstat = TIOCM_RTS;
ioctl(fd, TIOCMBIS, &mstat);
if ( TRX_RTS == cfg.trxcntl ) {
int mstat = TIOCM_RTS;
ioctl(fd, TIOCMBIS, &mstat);
} else if ( TRX_SYSFS_1 == cfg.trxcntl) {
sysfs_gpio_set(cfg.trxcntl_file,"1");
} else if ( TRX_SYSFS_0 == cfg.trxcntl) {
sysfs_gpio_set(cfg.trxcntl_file,"0");
}
}
/* Set RTS line to passive state */
void
tty_clr_rts(int fd)
{
int mstat = TIOCM_RTS;
ioctl(fd, TIOCMBIC, &mstat);
if ( TRX_RTS == cfg.trxcntl ) {
int mstat = TIOCM_RTS;
ioctl(fd, TIOCMBIC, &mstat);
} else if ( TRX_SYSFS_1 == cfg.trxcntl) {
sysfs_gpio_set(cfg.trxcntl_file,"0");
} else if ( TRX_SYSFS_0 == cfg.trxcntl) {
sysfs_gpio_set(cfg.trxcntl_file,"1");
}
}
#endif

View File

@ -67,8 +67,10 @@
* TRX control types
*/
#ifdef TRXCTL
#define TRX_ADDC 0
#define TRX_RTS 1
#define TRX_ADDC 0
#define TRX_RTS 1
#define TRX_SYSFS_1 2
#define TRX_SYSFS_0 3
#endif
/*
@ -89,7 +91,7 @@ typedef struct
int speed; /* serial port speed */
char *port; /* serial port device name */
#ifdef TRXCTL
int trxcntl; /* trx control type (0 - Automatic Data Direction Control (ADDC), 1 - by RTS) */
int trxcntl; /* trx control type (enum - see values in config.h) */
#endif
struct termios tios; /* working termios structure */
struct termios savedtios; /* saved termios structure */