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