Hello all,
I think I've found a bug in the implementation of InterruptIn for STM32F411. I wrote a simple encoder class that uses InterruptIn. When I use a single implementation of my class, it works fine. When I use two of them, only the second one that is instantiated works. My guess is that it has to do with the way that InterruptIn enables interrupts in the STM32 hal. I tried looking at the hal code, but wasn't able to figure it out. Can anyone find a bug in the STM32 hal related to this? Am I missing something obvious in my code??? My code is below:
#ifndef ENCODER_H
#define ENCODER_H
#include "mbed.h"
typedef void (*voidfunc)(void);
class Encoder
{
public:
Encoder(PinName chA, PinName chB): chanA(chA), chanB(chB), state(0), pulses(0), notifyFunc(0) { enable(); }
void reset(void) { pulses = 0; }
int getCount(void) { return(pulses); }
void setNotifyFunc(voidfunc func) { notifyFunc = func; }
void enable(void)
{
state = (chanB.read() << 1) | (chanA.read());
chanA.rise(this, &Encoder::p1up);
chanA.fall(this, &Encoder::p1dn);
chanB.rise(this, &Encoder::p2up);
chanB.fall(this, &Encoder::p2dn);
}
void disable(void)
{
chanA.rise(0);
chanA.fall(0);
chanB.rise(0);
chanB.fall(0);
}
private:
void p1up(void)
{
state|=1;
if(state&2)
pulses--;
else
pulses++;
if(notifyFunc)
notifyFunc();
}
void p1dn(void)
{
state&=~1;
if(state&2)
pulses++;
else
pulses--;
if(notifyFunc)
notifyFunc();
}
void p2up(void)
{
state|=2;
if(state&1)
pulses++;
else
pulses--;
if(notifyFunc)
notifyFunc();
}
void p2dn(void)
{
state&=~2;
if(state&1)
pulses--;
else
pulses++;
if(notifyFunc)
notifyFunc();
}
InterruptIn chanA;
InterruptIn chanB;
int state;
int pulses;
voidfunc notifyFunc;
};
#endif
Then in my main function, I have:
Encoder enc2(PC_12, PC_11);
Encoder enc1(PC_10, PA_12);
I use the two classes. Whichever encoder I put first, doesn't work. If I switch the order of the above two lines, the other one works...
Hello all,
I think I've found a bug in the implementation of InterruptIn for STM32F411. I wrote a simple encoder class that uses InterruptIn. When I use a single implementation of my class, it works fine. When I use two of them, only the second one that is instantiated works. My guess is that it has to do with the way that InterruptIn enables interrupts in the STM32 hal. I tried looking at the hal code, but wasn't able to figure it out. Can anyone find a bug in the STM32 hal related to this? Am I missing something obvious in my code??? My code is below:
Then in my main function, I have:
I use the two classes. Whichever encoder I put first, doesn't work. If I switch the order of the above two lines, the other one works...