首页 > 解决方案 > 尽管发生中断,但未调用 WiringPi ISR?

问题描述

我已将我的树莓派 1 的 GPIO 引脚 17(在 WiringPi Pin17 = Pin0 中)与中断源(一个 IR-LED 发射器/接收器连接,当红外线被某些障碍物中断时触发中断)。为了设置 ISR,我一直在使用 WiringPi 库(我已经用 pigpio 库尝试过,但我也有同样的问题)。为了验证我实际上在 Pin17 上接收到中断,我用我的逻辑分析仪检查了它,并且肯定有一些中断发生在这个引脚上,如您所见:在此处输入图像描述

这是我的代码:

#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <wiringPi.h>
#include "MCP3008Driver.h"
#include "DHT11.h"

#define INT_PIN 0

volatile int eventCounter = 0;

void myInterrupt(void){
    printf("hello ISR!\n");
    eventCounter++;
}

volatile sig_atomic_t stopFlag = 0;
static void stopHandler(int sign) { /* can be called asynchronously */
    stopFlag = 1; /* set flag */
}

int main(void) {
    signal(SIGINT, stopHandler);
    signal(SIGTERM, stopHandler);

    // sets up the wiringPi library
    if (wiringPiSetup () < 0) {
        printf("Unable to setup wiring pi\n");
        fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror 
(errno));
        return 1;
    }

    // set Pin 17/0 to generate an interrupt on high-to-low transitions
    // and attach myInterrupt() to the interrupt
    if(wiringPiISR(INT_PIN, INT_EDGE_FALLING, &myInterrupt) < 0){
        printf("unable to setup ISR\n");
        fprintf(stderr, "Unable to setup ISR: %s\n", strerror(errno));
    }

    DHT11_data data;
    configureSPI();

    while(1){
        if(stopFlag){
            printf("\n Ctrl-C signal caught! \n");
            printf("Closing application. \n");
            return 0;
        }

        //read_dht_data(&data);
        int analogBoiler = readChannel(0);
        int analogHeater = readChannel(1);
        int analogPress = readChannel(2);
        int analogACS712 = readChannel(3);
        int analogDynamo = readChannel(4);
        printf("Channel 0 / Boiler = %f\n", evaluateChannelValue(ePT100_BOILER, analogBoiler));
        printf("Channel 1 / Heater = %f\n", evaluateChannelValue(ePT100_HEATER, analogHeater));
        printf("Channel 2 / Pressure = %f\n", evaluateChannelValue(ePRESS, analogPress));
        printf("Channel 3 / Power ACS712 = %f\n", evaluateChannelValue(eACS712, analogACS712));
        printf("Channel 4 / Power Dynamo = %f\n", evaluateChannelValue(eDYNAMO, analogDynamo));
        //printf("Humidity Environment: %f\n", data.humidity);
         //printf("Temperature (Celsius) Environment: %f\n", data.temp_celsius);

        // display counter value every second.
        printf("%d\n", eventCounter);



        sleep(5);  
    } 

    return 0;
}

方法wiringPiSetup 和wiringPiISR 被成功调用并且没有返回错误。

我正在使用以下链接选项构建此示例:-lwiringPi -lm -lpthread。也许我错过了链接选项?

我一直在这里使用此代码作为参考。那么我在这里做错了什么?谢谢你能给我的任何建议!

标签: craspberry-piinterruptinterrupt-handlingwiringpi

解决方案


我不完全确定为什么,但我发现删除函数调用输入前面的一元运算符可以wiringPiISR解决我的问题。

所以而不是打电话

wiringPiISR(INT_PIN, INT_FALLING_EDGE, &MyInterrupt)

称呼

wiringPiISR(INT_PIN, INT_FALLING_EDGE, MyInterrupt)

我的猜测是,这与将wiringPiISR参数作为指针(*函数)的事实有关,因此将调用地址放在它前面会导致在尝试调用MyInterrupt函数时发生一些奇怪的事情,对我来说它导致我的程序崩溃!

希望这会有所帮助/也许其他人能够详细说明为什么会发生这种情况。


推荐阅读