Home Alert System
Dependencies: PWM_Tone_Library DHT
mbed/InterruptManager.h@5:569a4894abc1, 2019-03-08 (annotated)
- Committer:
- aziz111
- Date:
- Fri Mar 08 17:15:02 2019 +0000
- Revision:
- 5:569a4894abc1
- Parent:
- 3:78f223d34f36
Final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ethaderu | 3:78f223d34f36 | 1 | #ifndef MBED_INTERRUPTMANAGER_H |
ethaderu | 3:78f223d34f36 | 2 | #define MBED_INTERRUPTMANAGER_H |
ethaderu | 3:78f223d34f36 | 3 | |
ethaderu | 3:78f223d34f36 | 4 | #include "cmsis.h" |
ethaderu | 3:78f223d34f36 | 5 | #include "CallChain.h" |
ethaderu | 3:78f223d34f36 | 6 | #include <string.h> |
ethaderu | 3:78f223d34f36 | 7 | |
ethaderu | 3:78f223d34f36 | 8 | namespace mbed { |
ethaderu | 3:78f223d34f36 | 9 | |
ethaderu | 3:78f223d34f36 | 10 | /** Use this singleton if you need to chain interrupt handlers. |
ethaderu | 3:78f223d34f36 | 11 | * |
ethaderu | 3:78f223d34f36 | 12 | * Example (for LPC1768): |
ethaderu | 3:78f223d34f36 | 13 | * @code |
ethaderu | 3:78f223d34f36 | 14 | * #include "InterruptManager.h" |
ethaderu | 3:78f223d34f36 | 15 | * #include "mbed.h" |
ethaderu | 3:78f223d34f36 | 16 | * |
ethaderu | 3:78f223d34f36 | 17 | * Ticker flipper; |
ethaderu | 3:78f223d34f36 | 18 | * DigitalOut led1(LED1); |
ethaderu | 3:78f223d34f36 | 19 | * DigitalOut led2(LED2); |
ethaderu | 3:78f223d34f36 | 20 | * |
ethaderu | 3:78f223d34f36 | 21 | * void flip(void) { |
ethaderu | 3:78f223d34f36 | 22 | * led1 = !led1; |
ethaderu | 3:78f223d34f36 | 23 | * } |
ethaderu | 3:78f223d34f36 | 24 | * |
ethaderu | 3:78f223d34f36 | 25 | * void handler(void) { |
ethaderu | 3:78f223d34f36 | 26 | * led2 = !led1; |
ethaderu | 3:78f223d34f36 | 27 | * } |
ethaderu | 3:78f223d34f36 | 28 | * |
ethaderu | 3:78f223d34f36 | 29 | * int main() { |
ethaderu | 3:78f223d34f36 | 30 | * led1 = led2 = 0; |
ethaderu | 3:78f223d34f36 | 31 | * flipper.attach(&flip, 1.0); |
ethaderu | 3:78f223d34f36 | 32 | * InterruptManager::get()->add_handler(handler, TIMER3_IRQn); |
ethaderu | 3:78f223d34f36 | 33 | * } |
ethaderu | 3:78f223d34f36 | 34 | * @endcode |
ethaderu | 3:78f223d34f36 | 35 | */ |
ethaderu | 3:78f223d34f36 | 36 | class InterruptManager { |
ethaderu | 3:78f223d34f36 | 37 | public: |
ethaderu | 3:78f223d34f36 | 38 | /** Return the only instance of this class |
ethaderu | 3:78f223d34f36 | 39 | */ |
ethaderu | 3:78f223d34f36 | 40 | static InterruptManager* get(); |
ethaderu | 3:78f223d34f36 | 41 | |
ethaderu | 3:78f223d34f36 | 42 | /** Destroy the current instance of the interrupt manager |
ethaderu | 3:78f223d34f36 | 43 | */ |
ethaderu | 3:78f223d34f36 | 44 | static void destroy(); |
ethaderu | 3:78f223d34f36 | 45 | |
ethaderu | 3:78f223d34f36 | 46 | /** Add a handler for an interrupt at the end of the handler list |
ethaderu | 3:78f223d34f36 | 47 | * |
ethaderu | 3:78f223d34f36 | 48 | * @param function the handler to add |
ethaderu | 3:78f223d34f36 | 49 | * @param irq interrupt number |
ethaderu | 3:78f223d34f36 | 50 | * |
ethaderu | 3:78f223d34f36 | 51 | * @returns |
ethaderu | 3:78f223d34f36 | 52 | * The function object created for 'function' |
ethaderu | 3:78f223d34f36 | 53 | */ |
ethaderu | 3:78f223d34f36 | 54 | pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) { |
ethaderu | 3:78f223d34f36 | 55 | return add_common(function, irq); |
ethaderu | 3:78f223d34f36 | 56 | } |
ethaderu | 3:78f223d34f36 | 57 | |
ethaderu | 3:78f223d34f36 | 58 | /** Add a handler for an interrupt at the beginning of the handler list |
ethaderu | 3:78f223d34f36 | 59 | * |
ethaderu | 3:78f223d34f36 | 60 | * @param function the handler to add |
ethaderu | 3:78f223d34f36 | 61 | * @param irq interrupt number |
ethaderu | 3:78f223d34f36 | 62 | * |
ethaderu | 3:78f223d34f36 | 63 | * @returns |
ethaderu | 3:78f223d34f36 | 64 | * The function object created for 'function' |
ethaderu | 3:78f223d34f36 | 65 | */ |
ethaderu | 3:78f223d34f36 | 66 | pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) { |
ethaderu | 3:78f223d34f36 | 67 | return add_common(function, irq, true); |
ethaderu | 3:78f223d34f36 | 68 | } |
ethaderu | 3:78f223d34f36 | 69 | |
ethaderu | 3:78f223d34f36 | 70 | /** Add a handler for an interrupt at the end of the handler list |
ethaderu | 3:78f223d34f36 | 71 | * |
ethaderu | 3:78f223d34f36 | 72 | * @param tptr pointer to the object that has the handler function |
ethaderu | 3:78f223d34f36 | 73 | * @param mptr pointer to the actual handler function |
ethaderu | 3:78f223d34f36 | 74 | * @param irq interrupt number |
ethaderu | 3:78f223d34f36 | 75 | * |
ethaderu | 3:78f223d34f36 | 76 | * @returns |
ethaderu | 3:78f223d34f36 | 77 | * The function object created for 'tptr' and 'mptr' |
ethaderu | 3:78f223d34f36 | 78 | */ |
ethaderu | 3:78f223d34f36 | 79 | template<typename T> |
ethaderu | 3:78f223d34f36 | 80 | pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IRQn_Type irq) { |
ethaderu | 3:78f223d34f36 | 81 | return add_common(tptr, mptr, irq); |
ethaderu | 3:78f223d34f36 | 82 | } |
ethaderu | 3:78f223d34f36 | 83 | |
ethaderu | 3:78f223d34f36 | 84 | /** Add a handler for an interrupt at the beginning of the handler list |
ethaderu | 3:78f223d34f36 | 85 | * |
ethaderu | 3:78f223d34f36 | 86 | * @param tptr pointer to the object that has the handler function |
ethaderu | 3:78f223d34f36 | 87 | * @param mptr pointer to the actual handler function |
ethaderu | 3:78f223d34f36 | 88 | * @param irq interrupt number |
ethaderu | 3:78f223d34f36 | 89 | * |
ethaderu | 3:78f223d34f36 | 90 | * @returns |
ethaderu | 3:78f223d34f36 | 91 | * The function object created for 'tptr' and 'mptr' |
ethaderu | 3:78f223d34f36 | 92 | */ |
ethaderu | 3:78f223d34f36 | 93 | template<typename T> |
ethaderu | 3:78f223d34f36 | 94 | pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IRQn_Type irq) { |
ethaderu | 3:78f223d34f36 | 95 | return add_common(tptr, mptr, irq, true); |
ethaderu | 3:78f223d34f36 | 96 | } |
ethaderu | 3:78f223d34f36 | 97 | |
ethaderu | 3:78f223d34f36 | 98 | /** Remove a handler from an interrupt |
ethaderu | 3:78f223d34f36 | 99 | * |
ethaderu | 3:78f223d34f36 | 100 | * @param handler the function object for the handler to remove |
ethaderu | 3:78f223d34f36 | 101 | * @param irq the interrupt number |
ethaderu | 3:78f223d34f36 | 102 | * |
ethaderu | 3:78f223d34f36 | 103 | * @returns |
ethaderu | 3:78f223d34f36 | 104 | * true if the handler was found and removed, false otherwise |
ethaderu | 3:78f223d34f36 | 105 | */ |
ethaderu | 3:78f223d34f36 | 106 | bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq); |
ethaderu | 3:78f223d34f36 | 107 | |
ethaderu | 3:78f223d34f36 | 108 | private: |
ethaderu | 3:78f223d34f36 | 109 | InterruptManager(); |
ethaderu | 3:78f223d34f36 | 110 | ~InterruptManager(); |
ethaderu | 3:78f223d34f36 | 111 | |
ethaderu | 3:78f223d34f36 | 112 | // We declare the copy contructor and the assignment operator, but we don't |
ethaderu | 3:78f223d34f36 | 113 | // implement them. This way, if someone tries to copy/assign our instance, |
ethaderu | 3:78f223d34f36 | 114 | // he will get an error at compile time. |
ethaderu | 3:78f223d34f36 | 115 | InterruptManager(const InterruptManager&); |
ethaderu | 3:78f223d34f36 | 116 | InterruptManager& operator =(const InterruptManager&); |
ethaderu | 3:78f223d34f36 | 117 | |
ethaderu | 3:78f223d34f36 | 118 | template<typename T> |
ethaderu | 3:78f223d34f36 | 119 | pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front=false) { |
ethaderu | 3:78f223d34f36 | 120 | int irq_pos = get_irq_index(irq); |
ethaderu | 3:78f223d34f36 | 121 | bool change = must_replace_vector(irq); |
ethaderu | 3:78f223d34f36 | 122 | |
ethaderu | 3:78f223d34f36 | 123 | pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr); |
ethaderu | 3:78f223d34f36 | 124 | if (change) |
ethaderu | 3:78f223d34f36 | 125 | NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper); |
ethaderu | 3:78f223d34f36 | 126 | return pf; |
ethaderu | 3:78f223d34f36 | 127 | } |
ethaderu | 3:78f223d34f36 | 128 | |
ethaderu | 3:78f223d34f36 | 129 | pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front=false); |
ethaderu | 3:78f223d34f36 | 130 | bool must_replace_vector(IRQn_Type irq); |
ethaderu | 3:78f223d34f36 | 131 | int get_irq_index(IRQn_Type irq); |
ethaderu | 3:78f223d34f36 | 132 | void irq_helper(); |
ethaderu | 3:78f223d34f36 | 133 | void add_helper(void (*function)(void), IRQn_Type irq, bool front=false); |
ethaderu | 3:78f223d34f36 | 134 | static void static_irq_helper(); |
ethaderu | 3:78f223d34f36 | 135 | |
ethaderu | 3:78f223d34f36 | 136 | CallChain* _chains[NVIC_NUM_VECTORS]; |
ethaderu | 3:78f223d34f36 | 137 | static InterruptManager* _instance; |
ethaderu | 3:78f223d34f36 | 138 | }; |
ethaderu | 3:78f223d34f36 | 139 | |
ethaderu | 3:78f223d34f36 | 140 | } // namespace mbed |
ethaderu | 3:78f223d34f36 | 141 | |
ethaderu | 3:78f223d34f36 | 142 | #endif |
ethaderu | 3:78f223d34f36 | 143 |