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