C++ interrupt handle

 

 

Method one:

It comes from embedded.com.

 

The interrupt class is the interface. The device interrupt class is the specific driver interrupt handler, it is the friend class of driver class.

 

 

 

The demo code:

class Interrupt{public:
    Interrupt(void);
    static void Register(int interrupt_numberber, Interrupt* intThisPtr);
    // wrapper functions to ISR()
    static void Interrupt_0(void);
    static void Interrupt_1(void);
    static void Interrupt_2(void);
    /* etc.*/
    virtual void ISR(void) = 0;

private:
    static Interrupt* ISRVectorTable[MAX_INTERRUPTS];
    };

void Interrupt::Interrupt_0(void)
{
    ISRVectorTable[0]->ISR();
}

void Interrupt::Interrupt_1(void)
{
ISRVectorTable[1]->ISR();
}

/* etc. */

void Interrupt::Register(int interrupt_number, Interrupt* intThisPtr)
{
    ISRVectorTable[interrupt_number] = intThisPtr;
}
class Timer; // forward declaration

class TimerInterrupt : public Interrupt
{
public:
    TimerInterrupt(Timer* ownerptr);
    virtual void ISR(void);
private:
    Timer* InterruptOwnerPtr;
};

class Timer
{
friend class TimerInterrupt;

public:
    Timer(void);
    int GetCount(void) { return Count; }
private:
    TimerInterrupt* InterruptPtr;
    int Count;
};
Timer::Timer(void)
{
    InterruptPtr = new TimerInterrupt(this);
}

TimerInterrupt::TimerInterrupt(Timer* owner)
{
    InterruptOwnerPtr = owner;    
    // Allows interrupt to access owner's data
    Interrupt::Register(TIMER_INTERRUPT_NUMBER, this);
}

 

posted on 2021-05-08 17:00  荷树栋  阅读(194)  评论(0编辑  收藏  举报

导航