mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
drivers/InterruptManager.h@181:57724642e740, 2018-02-16 (annotated)
- Committer:
- AnnaBridge
- Date:
- Fri Feb 16 16:09:33 2018 +0000
- Revision:
- 181:57724642e740
- Parent:
- 180:96ed750bd169
- Child:
- 184:08ed48f1de7f
mbed-dev library. Release version 159.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /* mbed Microcontroller Library |
<> | 149:156823d33999 | 2 | * Copyright (c) 2006-2013 ARM Limited |
<> | 149:156823d33999 | 3 | * |
<> | 149:156823d33999 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 149:156823d33999 | 5 | * you may not use this file except in compliance with the License. |
<> | 149:156823d33999 | 6 | * You may obtain a copy of the License at |
<> | 149:156823d33999 | 7 | * |
<> | 149:156823d33999 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 149:156823d33999 | 9 | * |
<> | 149:156823d33999 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 149:156823d33999 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 149:156823d33999 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 149:156823d33999 | 13 | * See the License for the specific language governing permissions and |
<> | 149:156823d33999 | 14 | * limitations under the License. |
<> | 149:156823d33999 | 15 | */ |
<> | 149:156823d33999 | 16 | #ifndef MBED_INTERRUPTMANAGER_H |
<> | 149:156823d33999 | 17 | #define MBED_INTERRUPTMANAGER_H |
<> | 149:156823d33999 | 18 | |
<> | 149:156823d33999 | 19 | #include "cmsis.h" |
<> | 149:156823d33999 | 20 | #include "platform/CallChain.h" |
<> | 149:156823d33999 | 21 | #include "platform/PlatformMutex.h" |
AnnaBridge | 168:9672193075cf | 22 | #include "platform/NonCopyable.h" |
<> | 149:156823d33999 | 23 | #include <string.h> |
<> | 149:156823d33999 | 24 | |
<> | 149:156823d33999 | 25 | namespace mbed { |
<> | 149:156823d33999 | 26 | /** \addtogroup drivers */ |
<> | 149:156823d33999 | 27 | |
<> | 149:156823d33999 | 28 | /** Use this singleton if you need to chain interrupt handlers. |
<> | 149:156823d33999 | 29 | * |
AnnaBridge | 167:e84263d55307 | 30 | * @note Synchronization level: Thread safe |
<> | 149:156823d33999 | 31 | * |
<> | 149:156823d33999 | 32 | * Example (for LPC1768): |
<> | 149:156823d33999 | 33 | * @code |
<> | 149:156823d33999 | 34 | * #include "InterruptManager.h" |
<> | 149:156823d33999 | 35 | * #include "mbed.h" |
<> | 149:156823d33999 | 36 | * |
<> | 149:156823d33999 | 37 | * Ticker flipper; |
<> | 149:156823d33999 | 38 | * DigitalOut led1(LED1); |
<> | 149:156823d33999 | 39 | * DigitalOut led2(LED2); |
<> | 149:156823d33999 | 40 | * |
<> | 149:156823d33999 | 41 | * void flip(void) { |
<> | 149:156823d33999 | 42 | * led1 = !led1; |
<> | 149:156823d33999 | 43 | * } |
<> | 149:156823d33999 | 44 | * |
<> | 149:156823d33999 | 45 | * void handler(void) { |
<> | 149:156823d33999 | 46 | * led2 = !led1; |
<> | 149:156823d33999 | 47 | * } |
<> | 149:156823d33999 | 48 | * |
<> | 149:156823d33999 | 49 | * int main() { |
<> | 149:156823d33999 | 50 | * led1 = led2 = 0; |
<> | 149:156823d33999 | 51 | * flipper.attach(&flip, 1.0); |
<> | 149:156823d33999 | 52 | * InterruptManager::get()->add_handler(handler, TIMER3_IRQn); |
<> | 149:156823d33999 | 53 | * } |
<> | 149:156823d33999 | 54 | * @endcode |
AnnaBridge | 167:e84263d55307 | 55 | * @ingroup drivers |
<> | 149:156823d33999 | 56 | */ |
AnnaBridge | 168:9672193075cf | 57 | class InterruptManager : private NonCopyable<InterruptManager> { |
<> | 149:156823d33999 | 58 | public: |
AnnaBridge | 167:e84263d55307 | 59 | /** Get the instance of InterruptManager Class |
AnnaBridge | 167:e84263d55307 | 60 | * |
AnnaBridge | 167:e84263d55307 | 61 | * @return the only instance of this class |
<> | 149:156823d33999 | 62 | */ |
Anna Bridge |
180:96ed750bd169 | 63 | MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " |
Anna Bridge |
180:96ed750bd169 | 64 | "public API of mbed-os and is being removed in the future.") |
<> | 149:156823d33999 | 65 | static InterruptManager* get(); |
<> | 149:156823d33999 | 66 | |
<> | 149:156823d33999 | 67 | /** Destroy the current instance of the interrupt manager |
<> | 149:156823d33999 | 68 | */ |
Anna Bridge |
180:96ed750bd169 | 69 | MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " |
Anna Bridge |
180:96ed750bd169 | 70 | "public API of mbed-os and is being removed in the future.") |
<> | 149:156823d33999 | 71 | static void destroy(); |
<> | 149:156823d33999 | 72 | |
<> | 149:156823d33999 | 73 | /** Add a handler for an interrupt at the end of the handler list |
<> | 149:156823d33999 | 74 | * |
<> | 149:156823d33999 | 75 | * @param function the handler to add |
<> | 149:156823d33999 | 76 | * @param irq interrupt number |
<> | 149:156823d33999 | 77 | * |
<> | 149:156823d33999 | 78 | * @returns |
<> | 149:156823d33999 | 79 | * The function object created for 'function' |
<> | 149:156823d33999 | 80 | */ |
Anna Bridge |
180:96ed750bd169 | 81 | MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " |
Anna Bridge |
180:96ed750bd169 | 82 | "public API of mbed-os and is being removed in the future.") |
<> | 149:156823d33999 | 83 | pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) { |
<> | 149:156823d33999 | 84 | // Underlying call is thread safe |
<> | 149:156823d33999 | 85 | return add_common(function, irq); |
<> | 149:156823d33999 | 86 | } |
<> | 149:156823d33999 | 87 | |
<> | 149:156823d33999 | 88 | /** Add a handler for an interrupt at the beginning of the handler list |
<> | 149:156823d33999 | 89 | * |
<> | 149:156823d33999 | 90 | * @param function the handler to add |
<> | 149:156823d33999 | 91 | * @param irq interrupt number |
<> | 149:156823d33999 | 92 | * |
<> | 149:156823d33999 | 93 | * @returns |
<> | 149:156823d33999 | 94 | * The function object created for 'function' |
<> | 149:156823d33999 | 95 | */ |
Anna Bridge |
180:96ed750bd169 | 96 | MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " |
Anna Bridge |
180:96ed750bd169 | 97 | "public API of mbed-os and is being removed in the future.") |
<> | 149:156823d33999 | 98 | pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) { |
<> | 149:156823d33999 | 99 | // Underlying call is thread safe |
<> | 149:156823d33999 | 100 | return add_common(function, irq, true); |
<> | 149:156823d33999 | 101 | } |
<> | 149:156823d33999 | 102 | |
<> | 149:156823d33999 | 103 | /** Add a handler for an interrupt at the end of the handler list |
<> | 149:156823d33999 | 104 | * |
<> | 149:156823d33999 | 105 | * @param tptr pointer to the object that has the handler function |
<> | 149:156823d33999 | 106 | * @param mptr pointer to the actual handler function |
<> | 149:156823d33999 | 107 | * @param irq interrupt number |
<> | 149:156823d33999 | 108 | * |
<> | 149:156823d33999 | 109 | * @returns |
<> | 149:156823d33999 | 110 | * The function object created for 'tptr' and 'mptr' |
<> | 149:156823d33999 | 111 | */ |
<> | 149:156823d33999 | 112 | template<typename T> |
Anna Bridge |
180:96ed750bd169 | 113 | MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " |
Anna Bridge |
180:96ed750bd169 | 114 | "public API of mbed-os and is being removed in the future.") |
<> | 149:156823d33999 | 115 | pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IRQn_Type irq) { |
<> | 149:156823d33999 | 116 | // Underlying call is thread safe |
<> | 149:156823d33999 | 117 | return add_common(tptr, mptr, irq); |
<> | 149:156823d33999 | 118 | } |
<> | 149:156823d33999 | 119 | |
<> | 149:156823d33999 | 120 | /** Add a handler for an interrupt at the beginning of the handler list |
<> | 149:156823d33999 | 121 | * |
<> | 149:156823d33999 | 122 | * @param tptr pointer to the object that has the handler function |
<> | 149:156823d33999 | 123 | * @param mptr pointer to the actual handler function |
<> | 149:156823d33999 | 124 | * @param irq interrupt number |
<> | 149:156823d33999 | 125 | * |
<> | 149:156823d33999 | 126 | * @returns |
<> | 149:156823d33999 | 127 | * The function object created for 'tptr' and 'mptr' |
<> | 149:156823d33999 | 128 | */ |
<> | 149:156823d33999 | 129 | template<typename T> |
Anna Bridge |
180:96ed750bd169 | 130 | MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " |
Anna Bridge |
180:96ed750bd169 | 131 | "public API of mbed-os and is being removed in the future.") |
<> | 149:156823d33999 | 132 | pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IRQn_Type irq) { |
<> | 149:156823d33999 | 133 | // Underlying call is thread safe |
<> | 149:156823d33999 | 134 | return add_common(tptr, mptr, irq, true); |
<> | 149:156823d33999 | 135 | } |
<> | 149:156823d33999 | 136 | |
<> | 149:156823d33999 | 137 | /** Remove a handler from an interrupt |
<> | 149:156823d33999 | 138 | * |
<> | 149:156823d33999 | 139 | * @param handler the function object for the handler to remove |
<> | 149:156823d33999 | 140 | * @param irq the interrupt number |
<> | 149:156823d33999 | 141 | * |
<> | 149:156823d33999 | 142 | * @returns |
<> | 149:156823d33999 | 143 | * true if the handler was found and removed, false otherwise |
<> | 149:156823d33999 | 144 | */ |
Anna Bridge |
180:96ed750bd169 | 145 | MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " |
Anna Bridge |
180:96ed750bd169 | 146 | "public API of mbed-os and is being removed in the future.") |
<> | 149:156823d33999 | 147 | bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq); |
<> | 149:156823d33999 | 148 | |
<> | 149:156823d33999 | 149 | private: |
<> | 149:156823d33999 | 150 | InterruptManager(); |
<> | 149:156823d33999 | 151 | ~InterruptManager(); |
<> | 149:156823d33999 | 152 | |
<> | 149:156823d33999 | 153 | void lock(); |
<> | 149:156823d33999 | 154 | void unlock(); |
<> | 149:156823d33999 | 155 | |
<> | 149:156823d33999 | 156 | template<typename T> |
<> | 149:156823d33999 | 157 | pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front=false) { |
<> | 149:156823d33999 | 158 | _mutex.lock(); |
<> | 149:156823d33999 | 159 | int irq_pos = get_irq_index(irq); |
<> | 149:156823d33999 | 160 | bool change = must_replace_vector(irq); |
<> | 149:156823d33999 | 161 | |
<> | 149:156823d33999 | 162 | pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr); |
<> | 149:156823d33999 | 163 | if (change) |
<> | 149:156823d33999 | 164 | NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper); |
<> | 149:156823d33999 | 165 | _mutex.unlock(); |
<> | 149:156823d33999 | 166 | return pf; |
<> | 149:156823d33999 | 167 | } |
<> | 149:156823d33999 | 168 | |
<> | 149:156823d33999 | 169 | pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front=false); |
<> | 149:156823d33999 | 170 | bool must_replace_vector(IRQn_Type irq); |
<> | 149:156823d33999 | 171 | int get_irq_index(IRQn_Type irq); |
<> | 149:156823d33999 | 172 | void irq_helper(); |
<> | 149:156823d33999 | 173 | void add_helper(void (*function)(void), IRQn_Type irq, bool front=false); |
<> | 149:156823d33999 | 174 | static void static_irq_helper(); |
<> | 149:156823d33999 | 175 | |
<> | 149:156823d33999 | 176 | CallChain* _chains[NVIC_NUM_VECTORS]; |
<> | 149:156823d33999 | 177 | static InterruptManager* _instance; |
<> | 149:156823d33999 | 178 | PlatformMutex _mutex; |
<> | 149:156823d33999 | 179 | }; |
<> | 149:156823d33999 | 180 | |
<> | 149:156823d33999 | 181 | } // namespace mbed |
<> | 149:156823d33999 | 182 | |
<> | 149:156823d33999 | 183 | #endif |