use chardev gpiov2
int get_gpio_status(const int gpio_offset) {
int gfd = -1, lfd = -1, ret = 0, rc, offset = -1;
struct gpio_v2_line_request req;
struct gpio_v2_line_values value;
char gpio_dev[20] = "/dev/gpiochip3";
if (offset == -1)
offset = gpio_offset;
memset(&req, 0, sizeof(req));
/* Configure input mode */
req.config.flags = GPIO_V2_LINE_FLAG_INPUT;
req.config.num_attrs = 0;
/* open GPIO group device, get the fd */
gfd = open(gpio_dev, O_WRONLY);
if (gfd == -1) {
ret = -errno;
return ret;
}
/* line offset in requested GPIO group */
req.offsets[0] = offset;
/* just request one line */
req.num_lines = 1;
ret = ioctl(gfd, GPIO_V2_GET_LINE_IOCTL, &req);
if (ret == -1) {
goto __exit_gfd_close;
} else {
lfd = req.fd;
}
ret = ioctl(lfd, GPIO_V2_LINE_GET_VALUES_IOCTL, &value);
if (ret == -1) {
goto __exit_gfd_close;
}
rc = (int)value.bits;
// close
if ((lfd > 2) && (close(lfd) == -1))
perror("Failed to close lfd ");
if ((gfd > 2) && close(gfd) == -1)
perror("Failed to close gfd ");
__exit_gfd_close:
if ((gfd > 2) && close(gfd) == -1)
perror("Failed to close gfd");
__exit_lfd_close:
if ((lfd > 2) && (close(lfd) == -1))
perror("Failed to close lfd");
return rc;
}
use libgpiod
gpiod examples: Request a line as input.
use GPIOD_LINE_DIRECTION_AS_IS There is no change in the result
gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_AS_IS);
static struct gpiod_line_request *request_input_line(const char *chip_path,
unsigned int offset,
const char *consumer) {
struct gpiod_request_config *req_cfg = NULL;
struct gpiod_line_request *request = NULL;
struct gpiod_line_settings *settings;
struct gpiod_line_config *line_cfg;
struct gpiod_chip *chip;
int ret;
chip = gpiod_chip_open(chip_path);
if (!chip)
return NULL;
settings = gpiod_line_settings_new();
if (!settings)
goto close_chip;
gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
line_cfg = gpiod_line_config_new();
if (!line_cfg)
goto free_settings;
ret = gpiod_line_config_add_line_settings(line_cfg, &offset, 1,
settings);
if (ret)
goto free_line_config;
if (consumer) {
req_cfg = gpiod_request_config_new();
if (!req_cfg)
goto free_line_config;
gpiod_request_config_set_consumer(req_cfg, consumer);
}
request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
gpiod_request_config_free(req_cfg);
free_line_config:
gpiod_line_config_free(line_cfg);
free_settings:
gpiod_line_settings_free(settings);
close_chip:
gpiod_chip_close(chip);
return request;
}
int read_gpio_status(const int gpio_chip, const int gpio_offset) {
const char *chip_path = gpio_chip == 0 ? GPIO_CHIP_3 : GPIO_CHIP_5;
struct gpiod_line_request *request;
enum gpiod_line_value value;
int ret;
request = request_input_line(chip_path, gpio_offset, "get-line-value");
if (!request) {
fprintf(stderr, "failed to request line: %s\n", strerror(errno));
return EXIT_FAILURE;
}
value = gpiod_line_request_get_value(request, gpio_offset);
*//* not strictly required here, but if the app wasn't exiting... *//*
gpiod_line_request_release(request);
return value;
}
I used the above two methods to obtain the status of gpio, and the result obtained is after modification.
I wonder if it’s because of iotcl
from linux-5.10/include/uapi/linux/gpio.h
/*
* v2 ioctl()s
*/
#define GPIO_V2_GET_LINEINFO_IOCTL _IOWR(0xB4, 0x05, struct gpio_v2_line_info)
#define GPIO_V2_GET_LINEINFO_WATCH_IOCTL _IOWR(0xB4, 0x06, struct gpio_v2_line_info)
#define GPIO_V2_GET_LINE_IOCTL _IOWR(0xB4, 0x07, struct gpio_v2_line_request)
#define GPIO_V2_LINE_SET_CONFIG_IOCTL _IOWR(0xB4, 0x0D, struct gpio_v2_line_config)
#define GPIO_V2_LINE_GET_VALUES_IOCTL _IOWR(0xB4, 0x0E, struct gpio_v2_line_values)
#define GPIO_V2_LINE_SET_VALUES_IOCTL _IOWR(0xB4, 0x0F, struct gpio_v2_line_values)
They are all _IOWR Could this be the problem?