Fredyy
Posts: 11
Joined: Mon Sep 10, 2012 9:04 am

How to clear WiringPi Interrupts?

Tue Apr 23, 2013 8:10 am

Hi,

how to I clear no longer used interrupts in my c programm? right now, they stay active and still use a a lot of resources, even if I never need them again in my programm.

I already took a deep look into the documentation but couldn't find a way to do so.

I hope you can help me out.

Grretings,
Fredyy

Fredyy
Posts: 11
Joined: Mon Sep 10, 2012 9:04 am

Re: How to clear WiringPi Interrupts?

Tue Apr 23, 2013 8:45 pm

Is there really no solution for this problem? :(

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: How to clear WiringPi Interrupts?

Thu Apr 25, 2013 2:46 pm

Fredyy wrote:Hi,

how to I clear no longer used interrupts in my c programm? right now, they stay active and still use a a lot of resources, even if I never need them again in my programm.

I already took a deep look into the documentation but couldn't find a way to do so.

I hope you can help me out.

Grretings,
Fredyy
What do you mean by "clear" and "resources"?

WiringPiISR clears the current interrupt when it happens, so a 2nd interrupt can be "remembered" the next time round, but that won't consume "resources" (other than a tiny bit of memory)

If your program is sitting with 100% CPU (if that's what you mean by resources), then I'd have a look at your program to see what it's doing - I suspect it's not doing what you think it's doing...

More information? Program listing, etc. ?

-Gordon
--
Gordons projects: https://projects.drogon.net/

Fredyy
Posts: 11
Joined: Mon Sep 10, 2012 9:04 am

Re: How to clear WiringPi Interrupts?

Thu Apr 25, 2013 5:26 pm

[email protected] wrote:More information? Program listing, etc. ?
Hi Gordon,

thank you for your answer, I've created a little example program of my problem, I hope you'll see my problem, the Interrupt continues to count all the time, even when it's no longer in use.

Greetings,
Fredyy

Code: Select all

/*
 *  Modified (shortened) code example:
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <wiringPi.h>

double time_diff(struct timeval x , struct timeval y);
int setupWiringPiRPM( void );
int getRpm( void );
int main( void );

static volatile int globalCounter = 0;
static int rpmPin                 = 0;
static int testInterval           = 25;
static int ticks                  = 0;
static int initRPM                = 0; 

/*
 *********************************************************************************
 * interruptCounter()
 * count the interrupts
 *********************************************************************************
 */

void interruptCounter (void)
{
	++globalCounter ;
}

/*
 *********************************************************************************
 * time_diff()
 * calculate the duration between 2 timevals
 *********************************************************************************
 */

double time_diff(struct timeval x , struct timeval y)
{
    double x_ms , y_ms , diff;

    x_ms = (double)x.tv_sec*1000 + (double)x.tv_usec/1000;
    y_ms = (double)y.tv_sec*1000 + (double)y.tv_usec/1000;

    diff = (double)y_ms - (double)x_ms;

    return (diff);
}


/*
 *********************************************************************************
 * setupWiringPi()
 * Initialize wiringPi and the interrupt
 *********************************************************************************
 */


int setupWiringPiRPM( void )
{

    if ( wiringPiSetup() != 0 )
    {
        printf ("Unable to setup wiringPi: %s\n", strerror (errno)) ;
        return (-1);
    }

    pullUpDnControl (rpmPin, PUD_UP);
    if ( wiringPiISR (rpmPin, INT_EDGE_RISING, &interruptCounter) != 0 )
    {
        printf ("Unable to setup ISR: %s\n", strerror (errno)) ;
        return (-1) ;
    }

    return (0) ;

}

/*
 *********************************************************************************
 * getRpm()
 * I need to run this function ever ~30s, 1 Run takes usually ~ 30-40ms.
 * I would like to stop the Interrupt on "rpmPin" after the run ends and
 * reinitialize it when the next run starts. Because when the Interrupt
 * stays active it eats up a lot of cpu power. :-(
 *********************************************************************************
 */

int getRpm( void )
{

    if (initRPM == 0 && setupWiringPiRPM() != 0)
    {
      printf ("RPM setup Failed\n") ;
      exit (EXIT_FAILURE) ;
    }
    initRPM = 1;

    struct timeval before, after;
    globalCounter =  0;

    gettimeofday(&before , NULL);

    while (globalCounter < testInterval)
    {
        delay(10);
    }

    ticks = globalCounter;
    gettimeofday(&after , NULL);

    long rpm = ticks * ( (1000 * 60) / time_diff(before, after));

    return( (int)rpm );
}



/*
 *********************************************************************************
 * example Main for demonstration on the forums
 *********************************************************************************
 */


int main( void )
{

    while (true)
    {
        printf( "rpm: %i", getRpm());
        sleep (30);
    }

    return (0);

}

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: How to clear WiringPi Interrupts?

Thu Apr 25, 2013 6:59 pm

OK. I think I see what you need - the input signal keeps on wiggling - so as long as it's pulsing, the interrupt will fire, so the code will run, so it will take time. I initially thought the interrupts were firing after the input pulses had stopped (at least that's what I'm assuming!)

So to disable interrupts - well, there's no easy built-in way, but this will work:

system ("/usr/local/bin/gpio edge 17 none") ;

and to allow them again:

system ("/usr/local/bin/gpio edge 17 rising") ;

which is somewhat crude, but it will work.

-Gordon
--
Gordons projects: https://projects.drogon.net/

Fredyy
Posts: 11
Joined: Mon Sep 10, 2012 9:04 am

Re: How to clear WiringPi Interrupts?

Thu Apr 25, 2013 8:37 pm

[email protected] wrote:OK. I think I see what you need - the input signal keeps on wiggling - so as long as it's pulsing, the interrupt will fire, so the code will run, so it will take time
Yeah that's right, its a tachosignal of a motor and on a regular basis i need to read its rpm. But the motor keeps on running while I'm not measuring. its rpm and of cause the thread, that waits for and counts the interrupts is still running. And with a frequency between 7kHz and 10kHz this takes a lot of cpu ussage, that is need by other parts of my program.

Is there no way to pause the thread complete, instead of just "muting" the gpio?

Thank you,
Fredyy

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: How to clear WiringPi Interrupts?

Thu Apr 25, 2013 9:28 pm

Fredyy wrote:
[email protected] wrote:OK. I think I see what you need - the input signal keeps on wiggling - so as long as it's pulsing, the interrupt will fire, so the code will run, so it will take time
Yeah that's right, its a tachosignal of a motor and on a regular basis i need to read its rpm. But the motor keeps on running while I'm not measuring. its rpm and of cause the thread, that waits for and counts the interrupts is still running. And with a frequency between 7kHz and 10kHz this takes a lot of cpu ussage, that is need by other parts of my program.

Is there no way to pause the thread complete, instead of just "muting" the gpio?

Thank you,
Fredyy
Yes. But you'll need to dive into the lovely world of pthreads yourself I'm afraid - I've no provision for it in wiringPi - yet.

-Gordon
--
Gordons projects: https://projects.drogon.net/

Paljas
Posts: 1
Joined: Tue Feb 13, 2018 3:10 pm

Re: How to clear WiringPi Interrupts?

Tue Feb 13, 2018 3:35 pm

How do you unmap a pin from the ISR in C?
When I use wiringPiISR and exit my program, I still see:

Code: Select all

    4 root      20   0       0      0      0 S  17.9  0.0   3:36.13 kworker/0:0
When using gpio edge # none, this gets rid of the CPU load, but how do I clean this up in my C code already?

Return to “C/C++”