int monitor_device(constchar *device_name, unsignedint *lines, unsignedint num_lines, struct gpio_v2_line_config *config, unsignedint loops)
{ struct gpio_v2_line_values values; char *chrdev_name; int cfd, lfd; int ret; int i = 0;
ret = asprintf(&chrdev_name, "/dev/%s", device_name); if (ret < 0) return -ENOMEM;
cfd = open(chrdev_name, 0); if (cfd == -1) {
ret = -errno;
fprintf(stderr, "Failed to open %s\n", chrdev_name); goto exit_free_name;
}
ret = gpiotools_request_line(device_name, lines, num_lines, config, "gpio-event-mon"); if (ret < 0) goto exit_device_close; else
lfd = ret;
/* Read initial states */
values.mask = 0;
values.bits = 0; for (i = 0; i < num_lines; i++)
gpiotools_set_bit(&values.mask, i);
ret = gpiotools_get_values(lfd, &values); if (ret < 0) {
fprintf(stderr, "Failed to issue GPIO LINE GET VALUES IOCTL (%d)\n",
ret); goto exit_line_close;
}
if (num_lines == 1) {
fprintf(stdout, "Monitoring line %u on %s\n", lines[0], device_name);
fprintf(stdout, "Initial line value: %d\n",
gpiotools_test_bit(values.bits, 0));
} else {
fprintf(stdout, "Monitoring lines %u", lines[0]); for (i = 1; i < num_lines - 1; i++)
fprintf(stdout, ", %u", lines[i]);
fprintf(stdout, " and %u on %s\n", lines[i], device_name);
fprintf(stdout, "Initial line values: %d",
gpiotools_test_bit(values.bits, 0)); for (i = 1; i < num_lines - 1; i++)
fprintf(stdout, ", %d",
gpiotools_test_bit(values.bits, i));
fprintf(stdout, " and %d\n",
gpiotools_test_bit(values.bits, i));
}
i = 0; while (1) { struct gpio_v2_line_event event;
ret = read(lfd, &event, sizeof(event)); if (ret == -1) { if (errno == -EAGAIN) {
fprintf(stderr, "nothing available\n"); continue;
} else {
ret = -errno;
fprintf(stderr, "Failed to read event (%d)\n",
ret); break;
}
}
if (ret != sizeof(event)) {
fprintf(stderr, "Reading event failed\n");
ret = -EIO; break;
}
fprintf(stdout, "GPIO EVENT at %" PRIu64 " on line %d (%d|%d) ",
(uint64_t)event.timestamp_ns, event.offset, event.line_seqno,
event.seqno); switch (event.id) { case GPIO_V2_LINE_EVENT_RISING_EDGE:
fprintf(stdout, "rising edge"); break; case GPIO_V2_LINE_EVENT_FALLING_EDGE:
fprintf(stdout, "falling edge"); break; default:
fprintf(stdout, "unknown event");
}
fprintf(stdout, "\n");
i++; if (i == loops) break;
}
exit_line_close: if (close(lfd) == -1)
perror("Failed to close line file");
exit_device_close: if (close(cfd) == -1)
perror("Failed to close GPIO character device file");
exit_free_name:
free(chrdev_name); return ret;
}
void print_usage(void)
{
fprintf(stderr, "Usage: gpio-event-mon [options]...\n" "Listen to events on GPIO lines, 0->1 1->0\n" " -n Listen on GPIOs on a named device (must be stated)\n" " -o Offset of line to monitor (may be repeated)\n" " -d Set line as open drain\n" " -s Set line as open source\n" " -r Listen for rising edges\n" " -f Listen for falling edges\n" " -w Report the wall-clock time for events\n" " -t Report the hardware timestamp for events\n" " -b Debounce the line with period n microseconds\n" " [-c ] Do loops (optional, infinite loop if not stated)\n" " -? This helptext\n" "\n" "Example:\n" "gpio-event-mon -n gpiochip0 -o 4 -r -f -b 10000\n"
);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.