Porównaj commity

...

6 Commity

Autor SHA1 Wiadomość Data
Addison Schuhardt 148e22799e
Merge cf91e96333 into 331143fa17 2024-04-13 22:56:15 -07:00
Mike Black W9MDB 331143fa17 Change TS890 to use SF command for mode get/set 2024-04-13 22:50:38 -05:00
Mike Black W9MDB a6ad5f0928 Fix TS890 set_vfo in set_mode 2024-04-13 15:23:53 -05:00
Mike Black W9MDB b42aa75dd8 astyle kenwood.c 2024-04-13 07:36:02 -05:00
Mike Black W9MDB 6cfdb9d4ed Fix TS890 set_mode 2024-04-13 06:44:18 -05:00
Addison Schuhardt cf91e96333 Refactoring GPIO push-to-talk logic to use libgpiod rather than the deprecated/broken sysfs method.
TODO: I'm not familiar with autoconf to set up linking to libgpiod correctly, so I'd like to ask someone else to do that.
Fixes #1538
2024-04-11 00:17:15 -07:00
5 zmienionych plików z 165 dodań i 126 usunięć

Wyświetl plik

@ -529,6 +529,9 @@ AS_IF([test x"${cf_with_xml_support}" = "xyes"], [
AC_SUBST([LIBXML2_LIBS])
AC_SUBST([LIBXML2_CFLAGS])
# TODO: I don't know how to use autoconf
LIBS="$LIBS -lgpiod"
## ----------------- ##
## Language bindings ##

Wyświetl plik

@ -2340,6 +2340,7 @@ typedef struct hamlib_port {
int fd; /*!< File descriptor */
void *handle; /*!< handle for USB */
void *gpio; /*!< handle for GPIO */
int write_delay; /*!< Delay between each byte sent out, in mS */
int post_write_delay; /*!< Delay between each commands send out, in mS */

Wyświetl plik

@ -194,12 +194,12 @@ tone_t kenwood42_ctcss_list[] =
*/
tone_t kenwood51_ctcss_list[] =
{
670 , 693, 719, 744, 770, 797, 825, 854, 885, 915, /* 0- 9 */
948 , 974, 1000, 1035, 1072, 1109, 1148, 1188, 1230, 1273, /* 10-19 */
1318, 1365, 1413, 1462, 1514, 1567, 1598, 1622, 1655, 1679, /* 20-29 */
1713, 1738, 1773, 1799, 1835, 1862, 1899, 1928, 1966, 1995, /* 30-39 */
2035, 2065, 2107, 2181, 2257, 2291, 2336, 2418, 2503, 2541, /* 40-49 */
17500, 0 /* 50-99 */
670, 693, 719, 744, 770, 797, 825, 854, 885, 915, /* 0- 9 */
948, 974, 1000, 1035, 1072, 1109, 1148, 1188, 1230, 1273, /* 10-19 */
1318, 1365, 1413, 1462, 1514, 1567, 1598, 1622, 1655, 1679, /* 20-29 */
1713, 1738, 1773, 1799, 1835, 1862, 1899, 1928, 1966, 1995, /* 30-39 */
2035, 2065, 2107, 2181, 2257, 2291, 2336, 2418, 2503, 2541, /* 40-49 */
17500, 0 /* 50-99 */
};
/* Token definitions for .cfgparams in rig_caps
@ -1684,7 +1684,7 @@ int kenwood_get_split_vfo_if(RIG *rig, vfo_t rxvfo, split_t *split,
RETURNFUNC(-RIG_EINVAL);
}
if (RIG_IS_TS990S)
if (RIG_IS_TS990S || RIG_IS_TS890S)
{
char buf[4];
@ -1745,13 +1745,13 @@ int kenwood_get_split_vfo_if(RIG *rig, vfo_t rxvfo, split_t *split,
{
HAMLIB_TRACE;
*txvfo = rs->tx_vfo = priv->tx_vfo = (*split
&& !transmitting) ? RIG_VFO_B : RIG_VFO_A;
&& !transmitting) ? RIG_VFO_B : RIG_VFO_A;
}
else if (rs->rx_vfo == RIG_VFO_B)
{
HAMLIB_TRACE;
*txvfo = rs->tx_vfo = priv->tx_vfo = (*split
&& !transmitting) ? RIG_VFO_B : RIG_VFO_A;
&& !transmitting) ? RIG_VFO_B : RIG_VFO_A;
}
else
{
@ -1841,7 +1841,7 @@ int kenwood_get_vfo_if(RIG *rig, vfo_t *vfo)
{
case '0':
*vfo = rs->rx_vfo = rs->tx_vfo = priv->tx_vfo =
split_and_transmitting ? RIG_VFO_B : RIG_VFO_A;
split_and_transmitting ? RIG_VFO_B : RIG_VFO_A;
if (priv->info[32] == '1') { priv->tx_vfo = rs->tx_vfo = RIG_VFO_B; }
@ -2417,8 +2417,9 @@ int kenwood_set_mode(RIG *rig, vfo_t vfo, rmode_t mode, pbwidth_t width)
if (RIG_IS_TS990S)
{
if (mode == RIG_MODE_PKTUSB) mode = RIG_MODE_USBD1;
if (mode == RIG_MODE_PKTLSB) mode = RIG_MODE_LSBD1;
if (mode == RIG_MODE_PKTUSB) { mode = RIG_MODE_USBD1; }
if (mode == RIG_MODE_PKTLSB) { mode = RIG_MODE_LSBD1; }
}
kmode = rmode2kenwood(mode, caps->mode_table);
@ -2442,7 +2443,47 @@ int kenwood_set_mode(RIG *rig, vfo_t vfo, rmode_t mode, pbwidth_t width)
rig_debug(RIG_DEBUG_VERBOSE, "%s: kmode=%d, cmode=%c, datamode=%c\n", __func__,
kmode, c, data_mode);
if (RIG_IS_TS990S || RIG_IS_TS890S)
if (RIG_IS_TS890S)
{
char sf[20];
// TS890 has SF command -- unique so far
if (vfo == RIG_VFO_A)
{
err = kenwood_transaction(rig, "SF0;", sf, sizeof(sf));
if (err != RIG_OK)
{
rig_debug(RIG_DEBUG_ERR, "%s: SF0; failed: %s\n", __func__, rigerror(err));
return err;
}
sf[14] = c;
err = kenwood_transaction(rig, sf, NULL, 0);
if (err != RIG_OK)
{
rig_debug(RIG_DEBUG_ERR, "%s: %s failed: %s\n", __func__, sf, rigerror(err));
return err;
}
return RIG_OK;
}
else
{
err = kenwood_transaction(rig, "SF1;", sf, sizeof(sf));
if (err != RIG_OK)
{
rig_debug(RIG_DEBUG_ERR, "%s: SF0; failed: %s\n", __func__, rigerror(err));
return err;
}
sf[14] = c;
err = kenwood_transaction(rig, sf, NULL, 0);
if (err != RIG_OK)
{
rig_debug(RIG_DEBUG_ERR, "%s: %s failed: %s\n", __func__, sf, rigerror(err));
return err;
}
return RIG_OK;
}
}
else if (RIG_IS_TS990S)
{
/* The TS990s has targetable read mode but can only set the mode
of the current VFO :( So we need to toggle the operating VFO
@ -2451,13 +2492,28 @@ int kenwood_set_mode(RIG *rig, vfo_t vfo, rmode_t mode, pbwidth_t width)
RIG_TARGETABLE_MODE since the toggle is not required for
reading the mode. */
vfo_t curr_vfo;
err = kenwood_get_vfo_main_sub(rig, &curr_vfo);
if (RIG_IS_TS990S)
{
err = kenwood_get_vfo_main_sub(rig, &curr_vfo);
}
else // RIG_IS_TS890
{
err = kenwood_get_vfo_if(rig, &curr_vfo);
}
if (err != RIG_OK) { RETURNFUNC2(err); }
if (vfo != RIG_VFO_CURR && vfo != curr_vfo)
{
err = kenwood_set_vfo_main_sub(rig, vfo);
if (RIG_IS_TS990S)
{
err = kenwood_set_vfo_main_sub(rig, vfo);
}
else // RIG_IS_TS890
{
err = kenwood_set_vfo(rig, vfo);
}
if (err != RIG_OK) { RETURNFUNC2(err); }
}
@ -2467,10 +2523,20 @@ int kenwood_set_mode(RIG *rig, vfo_t vfo, rmode_t mode, pbwidth_t width)
if (err == RIG_OK && vfo != RIG_VFO_CURR && vfo != curr_vfo)
{
int err2 = kenwood_set_vfo_main_sub(rig, curr_vfo);
int err2;
if (RIG_IS_TS990S)
{
err2 = kenwood_set_vfo_main_sub(rig, curr_vfo);
}
else // RIG_IS_TS890
{
err2 = kenwood_set_vfo(rig, curr_vfo);
}
if (err2 != RIG_OK) { RETURNFUNC2(err2); }
}
return RIG_OK;
}
else
@ -2685,9 +2751,10 @@ static int kenwood_get_filter_width(RIG *rig, rmode_t mode, pbwidth_t *width)
*/
int kenwood_get_mode(RIG *rig, vfo_t vfo, rmode_t *mode, pbwidth_t *width)
{
char cmd[4];
char modebuf[10];
char cmd[5];
char modebuf[20];
int offs;
int len = 6;
int retval;
int kmode;
@ -2710,8 +2777,22 @@ int kenwood_get_mode(RIG *rig, vfo_t vfo, rmode_t *mode, pbwidth_t *width)
STATE(rig)->current_vfo = RIG_VFO_A;
RETURNFUNC2(RIG_OK);
}
if (RIG_IS_TS990S || RIG_IS_TS890S)
if (RIG_IS_TS890S)
{
len = 16;
// TS890 has SF command -- unique so far
if (vfo == RIG_VFO_A)
{
strcpy(cmd,"SF0;");
offs = 14;
}
else
{
strcpy(cmd,"SF1;");
offs = 14;
}
}
else if (RIG_IS_TS990S)
{
char c;
@ -2754,7 +2835,7 @@ int kenwood_get_mode(RIG *rig, vfo_t vfo, rmode_t *mode, pbwidth_t *width)
}
}
retval = kenwood_safe_transaction(rig, cmd, modebuf, 6, offs + 1);
retval = kenwood_safe_transaction(rig, cmd, modebuf, len, offs + 1);
if (retval != RIG_OK)
{
@ -4443,7 +4524,8 @@ int kenwood_set_ctcss_tone(RIG *rig, vfo_t vfo, tone_t tone)
}
/* TODO: replace menu no 57 by a define */
SNPRINTF(tonebuf, sizeof(tonebuf), "EX%03d%04d", 57, i + kenwood_caps(rig)->tone_table_base);
SNPRINTF(tonebuf, sizeof(tonebuf), "EX%03d%04d", 57,
i + kenwood_caps(rig)->tone_table_base);
RETURNFUNC(kenwood_transaction(rig, tonebuf, NULL, 0));
}
@ -4494,7 +4576,8 @@ int kenwood_set_ctcss_tone_tn(RIG *rig, vfo_t vfo, tone_t tone)
RETURNFUNC(-RIG_EINVAL);
}
SNPRINTF(buf, sizeof(buf), "TN%c%02d", c, i + kenwood_caps(rig)->tone_table_base);
SNPRINTF(buf, sizeof(buf), "TN%c%02d", c,
i + kenwood_caps(rig)->tone_table_base);
}
else
{
@ -4526,12 +4609,12 @@ int kenwood_get_ctcss_tone(RIG *rig, vfo_t vfo, tone_t *tone)
caps = rig->caps;
if (RIG_IS_TS890S)
{
char buf[5];
retval = kenwood_safe_transaction(rig, "TN", buf, sizeof(buf), 4);
memcpy(tonebuf, buf + 2, 2);
}
{
char buf[5];
retval = kenwood_safe_transaction(rig, "TN", buf, sizeof(buf), 4);
memcpy(tonebuf, buf + 2, 2);
}
else if (RIG_IS_TS990S)
{
char cmd[4];
@ -4644,7 +4727,8 @@ int kenwood_set_ctcss_sql(RIG *rig, vfo_t vfo, tone_t tone)
RETURNFUNC(-RIG_EINVAL);
}
SNPRINTF(buf, sizeof(buf), "CN%c%02d", c, i + kenwood_caps(rig)->tone_table_base);
SNPRINTF(buf, sizeof(buf), "CN%c%02d", c,
i + kenwood_caps(rig)->tone_table_base);
}
else
{

Wyświetl plik

@ -28,7 +28,7 @@
#include "token.h"
#include "idx_builtin.h"
#define BACKEND_VER "20240405"
#define BACKEND_VER "20240413"
#define EOM_KEN ';'
#define EOM_TH '\r'
@ -211,6 +211,7 @@ int kenwood_set_vfo_main_sub(RIG *rig, vfo_t vfo);
int kenwood_get_vfo_if(RIG *rig, vfo_t *vfo);
int kenwood_get_vfo_main_sub(RIG *rig, vfo_t *vfo);
int kenwood_set_split(RIG *rig, vfo_t vfo, split_t split, vfo_t txvfo);
int kenwood_get_vfo_frft(RIG *rig, vfo_t *vfo);
int kenwood_set_split_vfo(RIG *rig, vfo_t vfo, split_t split, vfo_t txvfo);
int kenwood_get_split_vfo_if(RIG *rig, vfo_t rxvfo, split_t *split,
vfo_t *txvfo);

Wyświetl plik

@ -19,135 +19,91 @@
*
*/
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "gpio.h"
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <gpiod.h>
#ifndef GPIOD_PATH
// This is what's used on the Raspberry Pi 4; I'm not sure about others
#define GPIO_CHIP_NAME "gpiochip0"
#endif
#define GPIO_CHIP_CONSUMER "Hamlib"
int gpio_open(hamlib_port_t *port, int output, int on_value)
{
char pathname[HAMLIB_FILPATHLEN * 2];
FILE *fexp, *fdir;
int fd;
char *dir;
struct gpiod_chip *chip;
struct gpiod_line *line;
port->parm.gpio.on_value = on_value;
SNPRINTF(pathname, HAMLIB_FILPATHLEN, "/sys/class/gpio/export");
fexp = fopen(pathname, "w");
chip = gpiod_chip_open_by_name(GPIO_CHIP_NAME);
if (!fexp)
if (!chip)
{
rig_debug(RIG_DEBUG_ERR,
"Export GPIO%s (using %s): %s\n",
port->pathname,
pathname,
strerror(errno));
rig_debug(RIG_DEBUG_ERR, "Failed to open GPIO chip %s: %s\n", GPIO_CHIP_NAME, strerror(errno));
return -RIG_EIO;
}
fprintf(fexp, "%s\n", port->pathname);
fclose(fexp);
SNPRINTF(pathname,
sizeof(pathname),
"/sys/class/gpio/gpio%s/direction",
port->pathname);
fdir = fopen(pathname, "w");
if (!fdir)
line = gpiod_chip_get_line(chip, atoi(port->pathname));
if (!line)
{
rig_debug(RIG_DEBUG_ERR,
"GPIO%s direction (using %s): %s\n",
port->pathname,
pathname,
strerror(errno));
rig_debug(RIG_DEBUG_ERR, "Failed to acquire GPIO%s: %s\n", port->pathname, strerror(errno));
gpiod_chip_close(chip);
return -RIG_EIO;
}
dir = output ? "out" : "in";
rig_debug(RIG_DEBUG_VERBOSE, "Setting direction of GPIO%s to %s\n",
port->pathname, dir);
fprintf(fdir, "%s\n", dir);
fclose(fdir);
SNPRINTF(pathname,
sizeof(pathname),
"/sys/class/gpio/gpio%s/value",
port->pathname);
fd = open(pathname, O_RDWR);
if (fd < 0)
if ((output && gpiod_line_request_output(line, GPIO_CHIP_CONSUMER, 0) < 0) ||
(!output && gpiod_line_request_input(line, GPIO_CHIP_CONSUMER) < 0))
{
rig_debug(RIG_DEBUG_ERR,
"GPIO%s opening value file %s: %s\n",
port->pathname,
pathname,
strerror(errno));
rig_debug(RIG_DEBUG_ERR, "Failed to set GPIO%s to %s mode: %s\n",
port->pathname, (output ? "OUTPUT" : "INPUT"), strerror(errno));
gpiod_line_release(line);
gpiod_chip_close(chip);
return -RIG_EIO;
}
port->fd = fd;
return fd;
port->gpio = line;
return RIG_OK;
}
int gpio_close(hamlib_port_t *port)
{
int retval;
char pathname[HAMLIB_FILPATHLEN * 2];
FILE *fexp;
retval = close(port->fd);
SNPRINTF(pathname, HAMLIB_FILPATHLEN, "/sys/class/gpio/unexport");
fexp = fopen(pathname, "w");
if (!fexp)
{
rig_debug(RIG_DEBUG_ERR,
"Export GPIO%s (using %s): %s\n",
port->pathname,
pathname,
strerror(errno));
return -RIG_EIO;
}
fprintf(fexp, "%s\n", port->pathname);
fclose(fexp);
return retval;
gpiod_line_close_chip((struct gpiod_line*)port->gpio);
return RIG_OK;
}
int gpio_ptt_set(hamlib_port_t *port, ptt_t pttx)
{
char *val;
int result = 0;
port->parm.gpio.value = pttx != RIG_PTT_OFF;
if ((port->parm.gpio.value && port->parm.gpio.on_value)
|| (!port->parm.gpio.value && !port->parm.gpio.on_value))
if ((port->parm.gpio.value && port->parm.gpio.on_value) ||
(!port->parm.gpio.value && !port->parm.gpio.on_value))
{
val = "1\n";
result = gpiod_line_set_value((struct gpiod_line*)port->gpio, 1);
}
else
{
val = "0\n";
result = gpiod_line_set_value((struct gpiod_line*)port->gpio, 0);
}
if (write(port->fd, val, strlen(val)) <= 0)
if (result)
{
rig_debug(RIG_DEBUG_ERR, "Failed to set the value of GPIO%s: %s\n", port->pathname, strerror(errno));
return -RIG_EIO;
}
return RIG_OK;
}
int gpio_ptt_get(hamlib_port_t *port, ptt_t *pttx)
{
if (port->parm.gpio.value)
@ -164,21 +120,15 @@ int gpio_ptt_get(hamlib_port_t *port, ptt_t *pttx)
int gpio_dcd_get(hamlib_port_t *port, dcd_t *dcdx)
{
char val;
int port_value;
lseek(port->fd, 0, SEEK_SET);
if (read(port->fd, &val, sizeof(val)) <= 0)
int val = gpiod_line_get_value((struct gpiod_line*)port->gpio);
if (val < 0)
{
return -RIG_EIO;
rig_debug(RIG_DEBUG_ERR, "Failed to read the value of GPIO%s: %s\n", port->pathname, strerror(errno));
}
rig_debug(RIG_DEBUG_VERBOSE, "DCD GPIO pin value: %c\n", val);
port_value = val - '0';
if (port_value == port->parm.gpio.on_value)
if (val == port->parm.gpio.on_value)
{
*dcdx = RIG_DCD_ON;
}