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