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