ads1115 only
Fork of mbed by
InterruptManager.h@122:f9eeca106725, 2016-07-07 (annotated)
- Committer:
- Kojto
- Date:
- Thu Jul 07 14:34:11 2016 +0100
- Revision:
- 122:f9eeca106725
- Parent:
- 85:024bf7f99721
- Child:
- 123:b0220dba8be7
Release 122 of the mbed library
Changes:
- new targets - Nucleo L432KC, Beetle, Nucleo F446ZE, Nucleo L011K4
- Thread safety addition - mbed API should contain a statement about thread safety
- critical section API addition
- CAS API (core_util_atomic_incr/decr)
- DEVICE_ are generated from targets.json file, device.h deprecated
- Callback replaces FunctionPointer to provide std like interface
- mbed HAL API docs improvements
- toolchain - prexif attributes with MBED_
- add new attributes - packed, weak, forcedinline, align
- target.json - contains targets definitions
- ST - L1XX - Cube update to 1.5
- SPI clock selection fix (clock from APB domain)
- F7 - Cube update v1.4.0
- L0 - baudrate init fix
- L1 - Cube update v1.5
- F3 - baudrate init fix, 3 targets CAN support
- F4 - Cube update v1.12.0, 3 targets CAN support
- L4XX - Cube update v1.5.1
- F0 - update Cube to v1.5.0
- L4 - 2 targets (L476RG/VG) CAN support
- NXP - pwm clock fix for KSDK2 MCU
- LPC2368 - remove ARM toolchain support - due to regression
- KSDK2 - fix SPI , I2C address and repeat start
- Silabs - some fixes backported from mbed 3
- Renesas - RZ_A1H - SystemCoreClockUpdate addition
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 | |
Kojto | 122:f9eeca106725 | 4 | #include "platform.h" |
Kojto | 122:f9eeca106725 | 5 | |
bogdanm | 66:9c8f0e3462fb | 6 | #include "cmsis.h" |
bogdanm | 66:9c8f0e3462fb | 7 | #include "CallChain.h" |
bogdanm | 66:9c8f0e3462fb | 8 | #include <string.h> |
bogdanm | 66:9c8f0e3462fb | 9 | |
bogdanm | 66:9c8f0e3462fb | 10 | namespace mbed { |
bogdanm | 66:9c8f0e3462fb | 11 | |
bogdanm | 66:9c8f0e3462fb | 12 | /** Use this singleton if you need to chain interrupt handlers. |
bogdanm | 66:9c8f0e3462fb | 13 | * |
Kojto | 122:f9eeca106725 | 14 | * @Note Synchronization level: Thread safe |
Kojto | 122:f9eeca106725 | 15 | * |
bogdanm | 66:9c8f0e3462fb | 16 | * Example (for LPC1768): |
bogdanm | 66:9c8f0e3462fb | 17 | * @code |
bogdanm | 66:9c8f0e3462fb | 18 | * #include "InterruptManager.h" |
bogdanm | 66:9c8f0e3462fb | 19 | * #include "mbed.h" |
bogdanm | 85:024bf7f99721 | 20 | * |
bogdanm | 66:9c8f0e3462fb | 21 | * Ticker flipper; |
bogdanm | 66:9c8f0e3462fb | 22 | * DigitalOut led1(LED1); |
bogdanm | 66:9c8f0e3462fb | 23 | * DigitalOut led2(LED2); |
bogdanm | 85:024bf7f99721 | 24 | * |
bogdanm | 66:9c8f0e3462fb | 25 | * void flip(void) { |
bogdanm | 66:9c8f0e3462fb | 26 | * led1 = !led1; |
bogdanm | 66:9c8f0e3462fb | 27 | * } |
bogdanm | 85:024bf7f99721 | 28 | * |
bogdanm | 66:9c8f0e3462fb | 29 | * void handler(void) { |
bogdanm | 66:9c8f0e3462fb | 30 | * led2 = !led1; |
bogdanm | 66:9c8f0e3462fb | 31 | * } |
bogdanm | 85:024bf7f99721 | 32 | * |
bogdanm | 66:9c8f0e3462fb | 33 | * int main() { |
bogdanm | 66:9c8f0e3462fb | 34 | * led1 = led2 = 0; |
bogdanm | 66:9c8f0e3462fb | 35 | * flipper.attach(&flip, 1.0); |
bogdanm | 66:9c8f0e3462fb | 36 | * InterruptManager::get()->add_handler(handler, TIMER3_IRQn); |
bogdanm | 66:9c8f0e3462fb | 37 | * } |
bogdanm | 66:9c8f0e3462fb | 38 | * @endcode |
bogdanm | 66:9c8f0e3462fb | 39 | */ |
bogdanm | 66:9c8f0e3462fb | 40 | class InterruptManager { |
bogdanm | 66:9c8f0e3462fb | 41 | public: |
bogdanm | 66:9c8f0e3462fb | 42 | /** Return the only instance of this class |
bogdanm | 66:9c8f0e3462fb | 43 | */ |
bogdanm | 66:9c8f0e3462fb | 44 | static InterruptManager* get(); |
bogdanm | 66:9c8f0e3462fb | 45 | |
bogdanm | 66:9c8f0e3462fb | 46 | /** Destroy the current instance of the interrupt manager |
bogdanm | 66:9c8f0e3462fb | 47 | */ |
bogdanm | 66:9c8f0e3462fb | 48 | static void destroy(); |
bogdanm | 85:024bf7f99721 | 49 | |
bogdanm | 66:9c8f0e3462fb | 50 | /** Add a handler for an interrupt at the end of the handler list |
bogdanm | 66:9c8f0e3462fb | 51 | * |
bogdanm | 66:9c8f0e3462fb | 52 | * @param function the handler to add |
bogdanm | 66:9c8f0e3462fb | 53 | * @param irq interrupt number |
bogdanm | 66:9c8f0e3462fb | 54 | * |
bogdanm | 66:9c8f0e3462fb | 55 | * @returns |
bogdanm | 66:9c8f0e3462fb | 56 | * The function object created for 'function' |
bogdanm | 66:9c8f0e3462fb | 57 | */ |
bogdanm | 66:9c8f0e3462fb | 58 | pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) { |
Kojto | 122:f9eeca106725 | 59 | // Underlying call is thread safe |
bogdanm | 66:9c8f0e3462fb | 60 | return add_common(function, irq); |
bogdanm | 66:9c8f0e3462fb | 61 | } |
bogdanm | 66:9c8f0e3462fb | 62 | |
bogdanm | 66:9c8f0e3462fb | 63 | /** Add a handler for an interrupt at the beginning of the handler list |
bogdanm | 66:9c8f0e3462fb | 64 | * |
bogdanm | 66:9c8f0e3462fb | 65 | * @param function the handler to add |
bogdanm | 66:9c8f0e3462fb | 66 | * @param irq interrupt number |
bogdanm | 66:9c8f0e3462fb | 67 | * |
bogdanm | 66:9c8f0e3462fb | 68 | * @returns |
bogdanm | 66:9c8f0e3462fb | 69 | * The function object created for 'function' |
bogdanm | 66:9c8f0e3462fb | 70 | */ |
bogdanm | 66:9c8f0e3462fb | 71 | pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) { |
Kojto | 122:f9eeca106725 | 72 | // Underlying call is thread safe |
bogdanm | 66:9c8f0e3462fb | 73 | return add_common(function, irq, true); |
bogdanm | 66:9c8f0e3462fb | 74 | } |
bogdanm | 66:9c8f0e3462fb | 75 | |
bogdanm | 66:9c8f0e3462fb | 76 | /** Add a handler for an interrupt at the end of the handler list |
bogdanm | 66:9c8f0e3462fb | 77 | * |
bogdanm | 66:9c8f0e3462fb | 78 | * @param tptr pointer to the object that has the handler function |
bogdanm | 66:9c8f0e3462fb | 79 | * @param mptr pointer to the actual handler function |
bogdanm | 66:9c8f0e3462fb | 80 | * @param irq interrupt number |
bogdanm | 66:9c8f0e3462fb | 81 | * |
bogdanm | 66:9c8f0e3462fb | 82 | * @returns |
bogdanm | 66:9c8f0e3462fb | 83 | * The function object created for 'tptr' and 'mptr' |
bogdanm | 66:9c8f0e3462fb | 84 | */ |
bogdanm | 66:9c8f0e3462fb | 85 | template<typename T> |
bogdanm | 66:9c8f0e3462fb | 86 | pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IRQn_Type irq) { |
Kojto | 122:f9eeca106725 | 87 | // Underlying call is thread safe |
bogdanm | 66:9c8f0e3462fb | 88 | return add_common(tptr, mptr, irq); |
bogdanm | 66:9c8f0e3462fb | 89 | } |
bogdanm | 66:9c8f0e3462fb | 90 | |
bogdanm | 66:9c8f0e3462fb | 91 | /** Add a handler for an interrupt at the beginning of the handler list |
bogdanm | 66:9c8f0e3462fb | 92 | * |
bogdanm | 66:9c8f0e3462fb | 93 | * @param tptr pointer to the object that has the handler function |
bogdanm | 66:9c8f0e3462fb | 94 | * @param mptr pointer to the actual handler function |
bogdanm | 66:9c8f0e3462fb | 95 | * @param irq interrupt number |
bogdanm | 66:9c8f0e3462fb | 96 | * |
bogdanm | 66:9c8f0e3462fb | 97 | * @returns |
bogdanm | 66:9c8f0e3462fb | 98 | * The function object created for 'tptr' and 'mptr' |
bogdanm | 66:9c8f0e3462fb | 99 | */ |
bogdanm | 66:9c8f0e3462fb | 100 | template<typename T> |
bogdanm | 66:9c8f0e3462fb | 101 | pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IRQn_Type irq) { |
Kojto | 122:f9eeca106725 | 102 | // Underlying call is thread safe |
bogdanm | 66:9c8f0e3462fb | 103 | return add_common(tptr, mptr, irq, true); |
bogdanm | 66:9c8f0e3462fb | 104 | } |
bogdanm | 66:9c8f0e3462fb | 105 | |
bogdanm | 66:9c8f0e3462fb | 106 | /** Remove a handler from an interrupt |
bogdanm | 66:9c8f0e3462fb | 107 | * |
bogdanm | 66:9c8f0e3462fb | 108 | * @param handler the function object for the handler to remove |
bogdanm | 66:9c8f0e3462fb | 109 | * @param irq the interrupt number |
bogdanm | 66:9c8f0e3462fb | 110 | * |
bogdanm | 66:9c8f0e3462fb | 111 | * @returns |
bogdanm | 66:9c8f0e3462fb | 112 | * true if the handler was found and removed, false otherwise |
bogdanm | 66:9c8f0e3462fb | 113 | */ |
bogdanm | 66:9c8f0e3462fb | 114 | bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq); |
bogdanm | 66:9c8f0e3462fb | 115 | |
bogdanm | 66:9c8f0e3462fb | 116 | private: |
bogdanm | 66:9c8f0e3462fb | 117 | InterruptManager(); |
bogdanm | 66:9c8f0e3462fb | 118 | ~InterruptManager(); |
bogdanm | 66:9c8f0e3462fb | 119 | |
Kojto | 122:f9eeca106725 | 120 | void lock(); |
Kojto | 122:f9eeca106725 | 121 | void unlock(); |
Kojto | 122:f9eeca106725 | 122 | |
bogdanm | 66:9c8f0e3462fb | 123 | // We declare the copy contructor and the assignment operator, but we don't |
bogdanm | 66:9c8f0e3462fb | 124 | // implement them. This way, if someone tries to copy/assign our instance, |
bogdanm | 66:9c8f0e3462fb | 125 | // he will get an error at compile time. |
bogdanm | 66:9c8f0e3462fb | 126 | InterruptManager(const InterruptManager&); |
bogdanm | 66:9c8f0e3462fb | 127 | InterruptManager& operator =(const InterruptManager&); |
bogdanm | 66:9c8f0e3462fb | 128 | |
bogdanm | 66:9c8f0e3462fb | 129 | template<typename T> |
bogdanm | 66:9c8f0e3462fb | 130 | pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front=false) { |
Kojto | 122:f9eeca106725 | 131 | _mutex.lock(); |
bogdanm | 66:9c8f0e3462fb | 132 | int irq_pos = get_irq_index(irq); |
bogdanm | 66:9c8f0e3462fb | 133 | bool change = must_replace_vector(irq); |
bogdanm | 66:9c8f0e3462fb | 134 | |
bogdanm | 66:9c8f0e3462fb | 135 | pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr); |
bogdanm | 66:9c8f0e3462fb | 136 | if (change) |
bogdanm | 66:9c8f0e3462fb | 137 | NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper); |
Kojto | 122:f9eeca106725 | 138 | _mutex.unlock(); |
bogdanm | 66:9c8f0e3462fb | 139 | return pf; |
bogdanm | 66:9c8f0e3462fb | 140 | } |
bogdanm | 66:9c8f0e3462fb | 141 | |
bogdanm | 66:9c8f0e3462fb | 142 | pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front=false); |
bogdanm | 66:9c8f0e3462fb | 143 | bool must_replace_vector(IRQn_Type irq); |
bogdanm | 66:9c8f0e3462fb | 144 | int get_irq_index(IRQn_Type irq); |
bogdanm | 66:9c8f0e3462fb | 145 | void irq_helper(); |
bogdanm | 66:9c8f0e3462fb | 146 | void add_helper(void (*function)(void), IRQn_Type irq, bool front=false); |
bogdanm | 66:9c8f0e3462fb | 147 | static void static_irq_helper(); |
bogdanm | 66:9c8f0e3462fb | 148 | |
bogdanm | 66:9c8f0e3462fb | 149 | CallChain* _chains[NVIC_NUM_VECTORS]; |
bogdanm | 66:9c8f0e3462fb | 150 | static InterruptManager* _instance; |
Kojto | 122:f9eeca106725 | 151 | PlatformMutex _mutex; |
bogdanm | 66:9c8f0e3462fb | 152 | }; |
bogdanm | 66:9c8f0e3462fb | 153 | |
bogdanm | 66:9c8f0e3462fb | 154 | } // namespace mbed |
bogdanm | 66:9c8f0e3462fb | 155 | |
bogdanm | 66:9c8f0e3462fb | 156 | #endif |
bogdanm | 66:9c8f0e3462fb | 157 |