STM32 interrupts not working

Hello all,

Could anyone explain me what's wrong with this code in a C++ library:

.h file

class PSM
{
public:
    PSM(uint32_t sensePin, uint32_t mode = RISING);
    
    static void onInterrupt();
    
    unsigned int cps();
    
private:
	void calculateSkip();
    
	volatile unsigned int _a;
};

.cpp file

static PSM * _thePSM;

PSM::PSM(uint32_t sensePin, uint32_t mode)
{
    _thePSM = this;

    pinMode(sensePin, INPUT_PULLUP);

    attachInterrupt(digitalPinToInterrupt(sensePin), onInterrupt, mode);
}

void PSM::onInterrupt(void)
{    
    _thePSM->calculateSkip();
}

void PSM::calculateSkip(void)
{
    PSM::_a++;
}

unsigned int PSM::cps()
{
    return PSM::_a
}

The point is obviously to count impulses on a sensePin.

This code works on AVR, but doesn't work on STM32.

If I write the same attachInterrupt in the .ino, it works.

What could be wrong with the code? I'm not a C++ pro, maybe it's something obvious.

I've also used this as a reference.

What STM32 Arduino are you using?

Note: Missing a ';' at the end of this line:

Note: You can access member variables through a pointer so this:

void PSM::onInterrupt(void)
{    
    _thePSM->calculateSkip();
}

void PSM::calculateSkip(void)
{
    PSM::_a++;
}

can be reduced to this:

void PSM::onInterrupt(void)
{    
    _thePSM->_a++;
}

I'm using STM32Duino with a Blackpill on STM32F411.

I lost ; while editing the code for the post, it present in the actual code

Note: Missing a ';' at the end of this line:

Apparently the code works if built in PlatformIO. For now it seems isolated to the Arduino IDE. I'm going to do more tests to locate the problem.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.