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