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