interrupt timer not work with library

albi.so
Posts: 4
Joined: Wed Apr 01, 2020 9:39 pm

interrupt timer not work with library

Postby albi.so » Wed Apr 01, 2020 9:59 pm

Hello everyone,

I want to put this code, that i used for generate a precise interrupt into a library:

Code: Select all

hw_timer_t * timer0 = NULL;
portMUX_TYPE timerMux0 = portMUX_INITIALIZER_UNLOCKED;

void IRAM_ATTR onTimer0(){
  portENTER_CRITICAL_ISR(&timerMux0);
  //Code
  portEXIT_CRITICAL_ISR(&timerMux0);
}


void setup() {
  timer0 = timerBegin(0, 80, true);  
  timerAttachInterrupt(timer0, &onTimer0, true); 
  timerAlarmWrite(timer0, 2000000, true); 
  timerAlarmEnable(timer0); // enable
}
When compiling i get an error related to the void IRAM_ATTR onTimer0() (line timerAttachInterrupt(timer0, &onTimer0... )

Does anybody know how to implement it ?

I hope to have explain it well, Many thanks

ESP_Sprite
Posts: 8926
Joined: Thu Nov 26, 2015 4:08 am

Re: interrupt timer not work with library

Postby ESP_Sprite » Thu Apr 02, 2020 8:14 am

It would help if you were to post the actual error...

albi.so
Posts: 4
Joined: Wed Apr 01, 2020 9:39 pm

Re: interrupt timer not work with library

Postby albi.so » Thu Apr 02, 2020 1:18 pm

Of course! for be more precise i posted also the:

.h file

Code: Select all

#ifndef int_lib
#define int_lib

#include "Arduino.h"

class InterruptLibrary
{
  public:
    void interrupt_setup();
    void IRAM_ATTR onTimer0();

    hw_timer_t * timer0 = NULL;
    portMUX_TYPE timerMux0 = portMUX_INITIALIZER_UNLOCKED;
};
#endif
.cpp file

Code: Select all

#include "int_lib.h"

void InterruptLibrary::interrupt_setup() {
  timer0 = timerBegin(0, 40, true);
  timerAttachInterrupt(timer0, &onTimer0, true);
  timerAlarmWrite(timer0, 100, true);

  timerAlarmEnable(timer0); // enable
}
void IRAM_ATTR InterruptLibrary::onTimer0() {
  //Critical Code here
  portENTER_CRITICAL_ISR(&timerMux0);
  //code
  portEXIT_CRITICAL_ISR(&timerMux0);
}
And i got this error:

int_lib.cpp:5:33: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&InterruptLibrary::onTimer0' [-fpermissive]

timerAttachInterrupt(timer0, &onTimer0, true);

int_lib.cpp:5:47: error: cannot convert 'void (InterruptLibrary::*)()' to 'void (*)()' for argument '2' to 'void timerAttachInterrupt(hw_timer_t*, void (*)(), bool)'

timerAttachInterrupt(timer0, &onTimer0, true);

exit status 1
ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&InterruptLibrary::onTimer0' [-fpermissive]

ESP_Sprite
Posts: 8926
Joined: Thu Nov 26, 2015 4:08 am

Re: interrupt timer not work with library

Postby ESP_Sprite » Thu Apr 02, 2020 1:36 pm

The IRAM_ATTR has very little to do with this. Your basic problem is that you cannot directly use an object method as a callback, as the information wrt *which* object the function is called on is lost. The solution for this would be to work around the issue by passing the object in the argument to the interrupt callback. You then declare the interrupt callback as a static function that calls the *actual* callback on the object. However, on the Arduino timer code this is impossible as the timerAttachInterrupt doesn't provide for an argument to pass to the ISR.

The alternative would be to make InterruptLibrary into a singleton, and then use a static function to call the actual function on that singleton. As there is no need for an interrupt handler argument, this would work.

(PS: In general, your OO setup is a little bit weird... a library usually isn't encapsulated in an object. You'd encapsulate something more physical, like a OneshotTimer or something, in an object.)

albi.so
Posts: 4
Joined: Wed Apr 01, 2020 9:39 pm

Re: interrupt timer not work with library

Postby albi.so » Thu Apr 02, 2020 2:34 pm

Thanks for the answer,

could you please provide an example ?

albi.so
Posts: 4
Joined: Wed Apr 01, 2020 9:39 pm

SOLVED: interrupt timer not work with library

Postby albi.so » Tue Apr 07, 2020 9:17 am

SOLVED: i manage to not use class and everithing work fine. Thanks

Who is online

Users browsing this forum: No registered users and 59 guests