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)

Most upvoted comments

@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 socket is not defined:

socket.emit('setRGB', parsedRGB);

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.

root@raspberrypi:/home/pi/pigpio# node test.js 
rgb TEst
creating rgb object
setting color
root@raspberrypi:/home/pi/pigpio# node test.js 
rgb TEst
creating rgb object
setting color
root@raspberrypi:/home/pi/pigpio# node test.js 
rgb TEst
creating rgb object
setting color
root@raspberrypi:/home/pi/pigpio# node test.js 
rgb TEst
creating rgb object
setting color
root@raspberrypi:/home/pi/pigpio# node test.js 
rgb TEst
creating rgb object
setting color
root@raspberrypi:/home/pi/pigpio# node test.js 
rgb TEst
creating rgb object
setting color
^C2016-03-30 18:07:09 sigHandler: Unhandled signal 2, terminating

root@raspberrypi:/home/pi/pigpio# node test.js 
rgb TEst
creating rgb object
setting color
root@raspberrypi:/home/pi/pigpio# node test.js 
rgb TEst
creating rgb object
setting color
^C2016-03-30 18:07:25 sigHandler: Unhandled signal 2, terminating

root@raspberrypi:/home/pi/pigpio# node test.js 
rgb TEst
creating rgb object
setting color
root@raspberrypi:/home/pi/pigpio# node test.js 
rgb TEst
creating rgb object
setting color
^C2016-03-30 18:07:36 sigHandler: Unhandled signal 2, terminating

root@raspberrypi:/home/pi/pigpio# node test.js 
rgb TEst
creating rgb object
setting color
root@raspberrypi:/home/pi/pigpio# node test.js 
rgb TEst
creating rgb object
setting color
root@raspberrypi:/home/pi/pigpio# 

Above you mention the following:

when I stop my scripts and run it again, I loose gpio control until I reboot the system

What exactly does this mean?

Is it the case that the program runs successfully exactly once and then never again?

Additional questions:

  • What version of the pigpio C library is being used (grep PIGPIO_VERSION /usr/local/include/pigpio.h)?
  • What version of the pigpio Node.js package is being used?
  • What version of Node.js is being used?
  • What version of Rasobian is being used?