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