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