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