2022-05-19 05:11:57 +08:00
|
|
|
#include "common/gpio.h"
|
2021-05-09 13:15:17 +08:00
|
|
|
|
2023-08-24 04:25:17 +08:00
|
|
|
#include <string>
|
|
|
|
|
2023-01-17 02:36:06 +08:00
|
|
|
#ifdef __APPLE__
|
|
|
|
int gpio_init(int pin_nr, bool output) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gpio_set(int pin_nr, bool high) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gpiochip_get_ro_value_fd(const char* consumer_label, int gpiochiop_id, int pin_nr) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2020-08-13 16:10:50 +08:00
|
|
|
#include <fcntl.h>
|
2021-05-09 13:15:17 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-06-09 04:46:22 +08:00
|
|
|
#include <cstring>
|
2022-09-13 13:29:31 +08:00
|
|
|
#include <linux/gpio.h>
|
|
|
|
#include <sys/ioctl.h>
|
2021-06-09 04:46:22 +08:00
|
|
|
|
2022-05-19 05:11:57 +08:00
|
|
|
#include "common/util.h"
|
2022-09-13 13:29:31 +08:00
|
|
|
#include "common/swaglog.h"
|
2020-08-13 16:10:50 +08:00
|
|
|
|
2021-06-11 16:17:52 +08:00
|
|
|
int gpio_init(int pin_nr, bool output) {
|
2020-08-13 16:10:50 +08:00
|
|
|
char pin_dir_path[50];
|
|
|
|
int pin_dir_path_len = snprintf(pin_dir_path, sizeof(pin_dir_path),
|
|
|
|
"/sys/class/gpio/gpio%d/direction", pin_nr);
|
2023-08-24 05:25:05 +08:00
|
|
|
if (pin_dir_path_len <= 0) {
|
2020-09-21 22:10:38 +08:00
|
|
|
return -1;
|
2020-08-13 16:10:50 +08:00
|
|
|
}
|
2020-09-21 22:10:38 +08:00
|
|
|
const char *value = output ? "out" : "in";
|
2021-04-14 02:43:43 +08:00
|
|
|
return util::write_file(pin_dir_path, (void*)value, strlen(value));
|
2020-08-13 16:10:50 +08:00
|
|
|
}
|
|
|
|
|
2021-06-11 16:17:52 +08:00
|
|
|
int gpio_set(int pin_nr, bool high) {
|
2020-08-13 16:10:50 +08:00
|
|
|
char pin_val_path[50];
|
|
|
|
int pin_val_path_len = snprintf(pin_val_path, sizeof(pin_val_path),
|
|
|
|
"/sys/class/gpio/gpio%d/value", pin_nr);
|
2023-08-24 05:25:05 +08:00
|
|
|
if (pin_val_path_len <= 0) {
|
2020-09-21 22:10:38 +08:00
|
|
|
return -1;
|
2020-08-13 16:10:50 +08:00
|
|
|
}
|
2021-04-14 02:43:43 +08:00
|
|
|
return util::write_file(pin_val_path, (void*)(high ? "1" : "0"), 1);
|
2020-08-13 16:10:50 +08:00
|
|
|
}
|
2022-09-13 13:29:31 +08:00
|
|
|
|
|
|
|
int gpiochip_get_ro_value_fd(const char* consumer_label, int gpiochiop_id, int pin_nr) {
|
|
|
|
|
|
|
|
// Assumed that all interrupt pins are unexported and rights are given to
|
|
|
|
// read from gpiochip0.
|
|
|
|
std::string gpiochip_path = "/dev/gpiochip" + std::to_string(gpiochiop_id);
|
|
|
|
int fd = open(gpiochip_path.c_str(), O_RDONLY);
|
|
|
|
if (fd < 0) {
|
2023-07-20 10:46:42 +08:00
|
|
|
LOGE("Error opening gpiochip0 fd");
|
2022-09-13 13:29:31 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup event
|
|
|
|
struct gpioevent_request rq;
|
|
|
|
rq.lineoffset = pin_nr;
|
|
|
|
rq.handleflags = GPIOHANDLE_REQUEST_INPUT;
|
|
|
|
|
|
|
|
/* Requesting both edges as the data ready pulse from the lsm6ds sensor is
|
|
|
|
very short(75us) and is mostly detected as falling edge instead of rising.
|
|
|
|
So if it is detected as rising the following falling edge is skipped. */
|
|
|
|
rq.eventflags = GPIOEVENT_REQUEST_BOTH_EDGES;
|
|
|
|
|
|
|
|
strncpy(rq.consumer_label, consumer_label, std::size(rq.consumer_label) - 1);
|
2023-08-25 23:20:43 +08:00
|
|
|
int ret = util::safe_ioctl(fd, GPIO_GET_LINEEVENT_IOCTL, &rq);
|
2022-09-13 13:29:31 +08:00
|
|
|
if (ret == -1) {
|
|
|
|
LOGE("Unable to get line event from ioctl : %s", strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return rq.fd;
|
|
|
|
}
|
2023-01-17 02:36:06 +08:00
|
|
|
|
|
|
|
#endif
|