Reputation: 31
as the title states, I have a problem porting some userspace-interrupt code from another armv7 embedded linux platform onto the Raspberry Pi 2 Model B.
I'm aware of the wiringPi library (and got it to work that way), but for evaluation reasons I want to do run as much identical code as possible on both platforms. For that reason I have to interface with sysfs by hand.
So, here's the relevant code snippet
#define GPIO_TRIGGER_MODE "rising"
#define SYS_GPIO_PIN "2"
#define SYS_GPIO_DIRECTION "/sys/class/gpio/gpio2/direction"
#define SYS_GPIO_EDGE "/sys/class/gpio/gpio2/edge"
#define SYS_GPIO_VALUE "/sys/class/gpio/gpio2/value"
static int fd_gpio;
{...}
//Setup sysfs-Pin
if ((fd_gpio = open("/sys/class/gpio/export", O_WRONLY)) < 0) {
exit(-1);
} else {
write(fd_gpio, SYS_GPIO_PIN, strlen((char*) SYS_GPIO_PIN));
close(fd_gpio);
if ((fd_gpio = open(SYS_GPIO_DIRECTION, O_WRONLY)) < 0) {
exit(-1);
} else {
write(fd_gpio, "in", strlen("in"));
close(fd_gpio);
if ((fd_gpio = open(SYS_GPIO_EDGE, O_WRONLY)) < 0) {
exit(-1);
} else {
write(fd_gpio, GPIO_TRIGGER_MODE, strlen((char*) GPIO_TRIGGER_MODE));
close(fd_gpio);
}
}
}
static int fd_gpio_value;
struct pollfd *fd_poll;
if ((fd_gpio_value = open(SYS_GPIO_VALUE, O_RDWR)) < 0) {
exit(-1);
} else {
fd_poll = malloc(sizeof (*fd_poll));
fd_poll->fd = fd_gpio_value;
fd_poll->events = POLLPRI;
char buf;
while (1) {
read(fd_gpio_value, &buf, 1);
if (poll(fd_poll, 1, -1) == -1) {
exit(-1);
} else {
some_logging_occurs();
}
}
So, whats working is the setup of the Pin: (cat /sys/class/gpio/gpio2/$stuff echoes the right settings). As long as there is no Trigger, the programm waits correctly (on poll(), as intended).
After the first rising edge came, poll() always returns immediately, and thus executes my logging function everytime, not only on rising edges.
What baffles me, is that the exact same code works exactly as intended on the other platform and it's the same interface to the GPIOs.
Upvotes: 2
Views: 1782
Reputation: 31
finally found the answer: a simple
lseek(fd_gpio_value,0,SEEK_SET);
was missing before read()
Upvotes: 1