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