pigpio: gpioTerminate and interrupt callbacks
If gpioTerminate is called when interrupt callbacks are active the system is left in an inconsistent state. In order to get the system back into a consistent state a reboot is required.
This can be seen with the following program:
#include <unistd.h>
#include <stdio.h>
#include <pigpio.h>
#define GPIO 4
void isr(int gpio, int level, uint32_t tick) {
printf("isr - gpio: %d, level: %d\n", gpio, level);
}
int main(int argc, char *argv[]) {
int version;
if ((version = gpioInitialise()) < 0) {
return -1;
}
printf("version: %d\n", version);
if (gpioSetISRFunc(GPIO, EITHER_EDGE, 200, isr) != 0) {
return -1;
}
sleep(1);
gpioTerminate();
}
The first run of this program outputs the following:
version: 39
isr - gpio: 4, level: 2
isr - gpio: 4, level: 2
isr - gpio: 4, level: 2
isr - gpio: 4, level: 2
2015-10-31 07:47:46 sigHandler: Unhandled signal 11, terminating
The second run outputs the following:
2015-10-31 07:47:50 initMboxBlock: init mbox zaps failed
The first run of the program appears to leave the system in an inconsistent state that prevents the program from running successfully a second time.
If the test program is modified to disable the interrupt before calling gpioTerminate everything functions correctly and the program can be run mutiple times successfully.
#include <unistd.h>
#include <stdio.h>
#include <pigpio.h>
#define GPIO 4
void isr(int gpio, int level, uint32_t tick) {
printf("isr - gpio: %d, level: %d\n", gpio, level);
}
int main(int argc, char *argv[]) {
int version;
if ((version = gpioInitialise()) < 0) {
return -1;
}
printf("version: %d\n", version);
if (gpioSetISRFunc(GPIO, EITHER_EDGE, 200, isr) != 0) {
return -1;
}
sleep(1);
if (gpioSetISRFunc(GPIO, EITHER_EDGE, 0, NULL) != 0) {
return -1;
}
gpioTerminate();
}
Would it not be better if gpioTerminate disabled interrupts and unexported the appropriate gpios instead of requiring gpioSetISRFunc to be called to perform the task?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 15 (5 by maintainers)
@Willjobs94 I just tried out the sample code with three LEDs and everything functioned as expected. The following line was removed from the sample code as
socketis not defined:The output of several test runs can be seen below. During some test runs ctrl-c was hit to see if it had any effect, but it didn’t. Everything still functioned correctly in subsequent test runs.
Above you mention the following:
What exactly does this mean?
Is it the case that the program runs successfully exactly once and then never again?
Additional questions: