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