2016-05-28 14:00:03 +00:00
|
|
|
/*
|
|
|
|
* Hamlib Interface - gpio support
|
|
|
|
* Copyright (c) 2016 by Jeroen Vreeken
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2017-08-05 14:09:12 +00:00
|
|
|
#include "gpio.h"
|
|
|
|
|
2016-05-28 14:00:03 +00:00
|
|
|
|
2018-03-29 21:55:25 +00:00
|
|
|
int gpio_open(hamlib_port_t *port, int output, int on_value)
|
2016-05-28 14:00:03 +00:00
|
|
|
{
|
2021-03-06 18:37:53 +00:00
|
|
|
char pathname[HAMLIB_FILPATHLEN * 2];
|
2017-08-05 14:09:12 +00:00
|
|
|
FILE *fexp, *fdir;
|
|
|
|
int fd;
|
2019-12-09 23:12:13 +00:00
|
|
|
char *dir;
|
2017-08-05 14:09:12 +00:00
|
|
|
|
|
|
|
port->parm.gpio.on_value = on_value;
|
|
|
|
|
2022-01-19 15:11:04 +00:00
|
|
|
SNPRINTF(pathname, HAMLIB_FILPATHLEN, "/sys/class/gpio/export");
|
2017-08-05 14:09:12 +00:00
|
|
|
fexp = fopen(pathname, "w");
|
|
|
|
|
2017-10-05 02:32:08 +00:00
|
|
|
if (!fexp)
|
|
|
|
{
|
2017-08-05 14:09:12 +00:00
|
|
|
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);
|
|
|
|
|
2022-01-19 15:11:04 +00:00
|
|
|
SNPRINTF(pathname,
|
2018-04-16 15:38:47 +00:00
|
|
|
sizeof(pathname),
|
2017-08-05 14:09:12 +00:00
|
|
|
"/sys/class/gpio/gpio%s/direction",
|
|
|
|
port->pathname);
|
|
|
|
fdir = fopen(pathname, "w");
|
|
|
|
|
2017-10-05 02:32:08 +00:00
|
|
|
if (!fdir)
|
|
|
|
{
|
2017-08-05 14:09:12 +00:00
|
|
|
rig_debug(RIG_DEBUG_ERR,
|
|
|
|
"GPIO%s direction (using %s): %s\n",
|
|
|
|
port->pathname,
|
|
|
|
pathname,
|
|
|
|
strerror(errno));
|
|
|
|
return -RIG_EIO;
|
|
|
|
}
|
|
|
|
|
2019-12-09 23:12:13 +00:00
|
|
|
dir = output ? "out" : "in";
|
2019-11-30 16:19:08 +00:00
|
|
|
rig_debug(RIG_DEBUG_VERBOSE, "Setting direction of GPIO%s to %s\n",
|
|
|
|
port->pathname, dir);
|
2018-06-30 14:09:56 +00:00
|
|
|
fprintf(fdir, "%s\n", dir);
|
2017-08-05 14:09:12 +00:00
|
|
|
fclose(fdir);
|
|
|
|
|
2022-01-19 15:11:04 +00:00
|
|
|
SNPRINTF(pathname,
|
2018-04-16 15:38:47 +00:00
|
|
|
sizeof(pathname),
|
2017-08-05 14:09:12 +00:00
|
|
|
"/sys/class/gpio/gpio%s/value",
|
|
|
|
port->pathname);
|
2018-06-30 14:09:56 +00:00
|
|
|
fd = open(pathname, O_RDWR);
|
2017-08-05 14:09:12 +00:00
|
|
|
|
2017-10-05 02:32:08 +00:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
2017-08-05 14:09:12 +00:00
|
|
|
rig_debug(RIG_DEBUG_ERR,
|
|
|
|
"GPIO%s opening value file %s: %s\n",
|
|
|
|
port->pathname,
|
|
|
|
pathname,
|
|
|
|
strerror(errno));
|
|
|
|
return -RIG_EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
port->fd = fd;
|
|
|
|
return fd;
|
2016-05-28 14:00:03 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 14:09:12 +00:00
|
|
|
|
2016-05-28 14:00:03 +00:00
|
|
|
int gpio_close(hamlib_port_t *port)
|
|
|
|
{
|
2023-06-17 16:57:07 +00:00
|
|
|
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;
|
2016-05-28 14:00:03 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 14:09:12 +00:00
|
|
|
|
2016-05-28 14:00:03 +00:00
|
|
|
int gpio_ptt_set(hamlib_port_t *port, ptt_t pttx)
|
|
|
|
{
|
2017-08-05 14:09:12 +00:00
|
|
|
char *val;
|
|
|
|
port->parm.gpio.value = pttx != RIG_PTT_OFF;
|
|
|
|
|
|
|
|
if ((port->parm.gpio.value && port->parm.gpio.on_value)
|
2017-10-05 02:32:08 +00:00
|
|
|
|| (!port->parm.gpio.value && !port->parm.gpio.on_value))
|
|
|
|
{
|
|
|
|
val = "1\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-05 14:09:12 +00:00
|
|
|
val = "0\n";
|
|
|
|
}
|
|
|
|
|
2017-10-05 02:32:08 +00:00
|
|
|
if (write(port->fd, val, strlen(val)) <= 0)
|
|
|
|
{
|
2017-08-05 14:09:12 +00:00
|
|
|
return -RIG_EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RIG_OK;
|
2016-05-28 14:00:03 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 14:09:12 +00:00
|
|
|
|
2016-05-28 14:00:03 +00:00
|
|
|
int gpio_ptt_get(hamlib_port_t *port, ptt_t *pttx)
|
|
|
|
{
|
2017-10-05 02:32:08 +00:00
|
|
|
if (port->parm.gpio.value)
|
|
|
|
{
|
2018-06-30 14:09:56 +00:00
|
|
|
*pttx = RIG_PTT_ON;
|
2017-10-05 02:32:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-30 14:09:56 +00:00
|
|
|
*pttx = RIG_PTT_OFF;
|
2017-08-05 14:09:12 +00:00
|
|
|
}
|
2019-11-30 16:19:08 +00:00
|
|
|
|
2018-06-30 14:09:56 +00:00
|
|
|
return RIG_OK;
|
2016-05-28 14:00:03 +00:00
|
|
|
}
|
2018-03-29 21:55:25 +00:00
|
|
|
|
|
|
|
int gpio_dcd_get(hamlib_port_t *port, dcd_t *dcdx)
|
|
|
|
{
|
|
|
|
char val;
|
|
|
|
int port_value;
|
2019-11-30 16:19:08 +00:00
|
|
|
|
2018-03-29 21:55:25 +00:00
|
|
|
lseek(port->fd, 0, SEEK_SET);
|
2019-11-30 16:19:08 +00:00
|
|
|
|
2018-03-29 21:55:25 +00:00
|
|
|
if (read(port->fd, &val, sizeof(val)) <= 0)
|
|
|
|
{
|
|
|
|
return -RIG_EIO;
|
|
|
|
}
|
2019-11-30 16:19:08 +00:00
|
|
|
|
2018-06-30 14:09:56 +00:00
|
|
|
rig_debug(RIG_DEBUG_VERBOSE, "DCD GPIO pin value: %c\n", val);
|
2019-11-30 16:19:08 +00:00
|
|
|
|
2018-03-29 21:55:25 +00:00
|
|
|
port_value = val - '0';
|
2019-11-30 16:19:08 +00:00
|
|
|
|
2018-03-29 21:55:25 +00:00
|
|
|
if (port_value == port->parm.gpio.on_value)
|
|
|
|
{
|
2018-06-30 14:09:56 +00:00
|
|
|
*dcdx = RIG_DCD_ON;
|
2019-11-30 16:19:08 +00:00
|
|
|
}
|
|
|
|
else
|
2018-03-29 21:55:25 +00:00
|
|
|
{
|
2018-06-30 14:09:56 +00:00
|
|
|
*dcdx = RIG_DCD_OFF;
|
2018-03-29 21:55:25 +00:00
|
|
|
}
|
2019-11-30 16:19:08 +00:00
|
|
|
|
2018-06-30 14:09:56 +00:00
|
|
|
return RIG_OK;
|
2018-03-29 21:55:25 +00:00
|
|
|
}
|