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