mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
drivers/InterruptManager.h@187:fa51feb62426, 2018-10-09 (annotated)
- Committer:
- pmcorreia
- Date:
- Tue Oct 09 14:42:37 2018 +0000
- Revision:
- 187:fa51feb62426
- Parent:
- 184:08ed48f1de7f
Updated version to work with F446RE
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. |
AnnaBridge | 184:08ed48f1de7f | 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. |
<> | 149:156823d33999 | 30 | * |
AnnaBridge | 167:e84263d55307 | 31 | * @note Synchronization level: Thread safe |
<> | 149:156823d33999 | 32 | * |
<> | 149:156823d33999 | 33 | * Example (for LPC1768): |
<> | 149:156823d33999 | 34 | * @code |
<> | 149:156823d33999 | 35 | * #include "InterruptManager.h" |
<> | 149:156823d33999 | 36 | * #include "mbed.h" |
<> | 149:156823d33999 | 37 | * |
<> | 149:156823d33999 | 38 | * Ticker flipper; |
<> | 149:156823d33999 | 39 | * DigitalOut led1(LED1); |
<> | 149:156823d33999 | 40 | * DigitalOut led2(LED2); |
<> | 149:156823d33999 | 41 | * |
<> | 149:156823d33999 | 42 | * void flip(void) { |
<> | 149:156823d33999 | 43 | * led1 = !led1; |
<> | 149:156823d33999 | 44 | * } |
<> | 149:156823d33999 | 45 | * |
<> | 149:156823d33999 | 46 | * void handler(void) { |
<> | 149:156823d33999 | 47 | * led2 = !led1; |
<> | 149:156823d33999 | 48 | * } |
<> | 149:156823d33999 | 49 | * |
<> | 149:156823d33999 | 50 | * int main() { |
<> | 149:156823d33999 | 51 | * led1 = led2 = 0; |
<> | 149:156823d33999 | 52 | * flipper.attach(&flip, 1.0); |
<> | 149:156823d33999 | 53 | * InterruptManager::get()->add_handler(handler, TIMER3_IRQn); |
<> | 149:156823d33999 | 54 | * } |
<> | 149:156823d33999 | 55 | * @endcode |
AnnaBridge | 167:e84263d55307 | 56 | * @ingroup drivers |
<> | 149:156823d33999 | 57 | */ |
AnnaBridge | 168:9672193075cf | 58 | class InterruptManager : private NonCopyable<InterruptManager> { |
<> | 149:156823d33999 | 59 | public: |
AnnaBridge | 167:e84263d55307 | 60 | /** Get the instance of InterruptManager Class |
AnnaBridge | 184:08ed48f1de7f | 61 | * @deprecated |
AnnaBridge | 184:08ed48f1de7f | 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. |
AnnaBridge | 167:e84263d55307 | 63 | * |
AnnaBridge | 167:e84263d55307 | 64 | * @return the only instance of this class |
<> | 149:156823d33999 | 65 | */ |
Anna Bridge |
180:96ed750bd169 | 66 | MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " |
Anna Bridge |
180:96ed750bd169 | 67 | "public API of mbed-os and is being removed in the future.") |
<> | 149:156823d33999 | 68 | static InterruptManager* get(); |
<> | 149:156823d33999 | 69 | |
<> | 149:156823d33999 | 70 | /** Destroy the current instance of the interrupt manager |
AnnaBridge | 184:08ed48f1de7f | 71 | * @deprecated |
AnnaBridge | 184:08ed48f1de7f | 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. |
AnnaBridge | 184:08ed48f1de7f | 73 | * |
<> | 149:156823d33999 | 74 | */ |
Anna Bridge |
180:96ed750bd169 | 75 | MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " |
Anna Bridge |
180:96ed750bd169 | 76 | "public API of mbed-os and is being removed in the future.") |
<> | 149:156823d33999 | 77 | static void destroy(); |
<> | 149:156823d33999 | 78 | |
<> | 149:156823d33999 | 79 | /** Add a handler for an interrupt at the end of the handler list |
AnnaBridge | 184:08ed48f1de7f | 80 | * @deprecated |
AnnaBridge | 184:08ed48f1de7f | 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. |
<> | 149:156823d33999 | 82 | * |
<> | 149:156823d33999 | 83 | * @param function the handler to add |
<> | 149:156823d33999 | 84 | * @param irq interrupt number |
<> | 149:156823d33999 | 85 | * |
<> | 149:156823d33999 | 86 | * @returns |
<> | 149:156823d33999 | 87 | * The function object created for 'function' |
<> | 149:156823d33999 | 88 | */ |
Anna Bridge |
180:96ed750bd169 | 89 | MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " |
Anna Bridge |
180:96ed750bd169 | 90 | "public API of mbed-os and is being removed in the future.") |
<> | 149:156823d33999 | 91 | pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) { |
<> | 149:156823d33999 | 92 | // Underlying call is thread safe |
<> | 149:156823d33999 | 93 | return add_common(function, irq); |
<> | 149:156823d33999 | 94 | } |
<> | 149:156823d33999 | 95 | |
<> | 149:156823d33999 | 96 | /** Add a handler for an interrupt at the beginning of the handler list |
AnnaBridge | 184:08ed48f1de7f | 97 | * @deprecated |
AnnaBridge | 184:08ed48f1de7f | 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. |
<> | 149:156823d33999 | 99 | * |
<> | 149:156823d33999 | 100 | * @param function the handler to add |
<> | 149:156823d33999 | 101 | * @param irq interrupt number |
<> | 149:156823d33999 | 102 | * |
<> | 149:156823d33999 | 103 | * @returns |
<> | 149:156823d33999 | 104 | * The function object created for 'function' |
<> | 149:156823d33999 | 105 | */ |
Anna Bridge |
180:96ed750bd169 | 106 | MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " |
Anna Bridge |
180:96ed750bd169 | 107 | "public API of mbed-os and is being removed in the future.") |
<> | 149:156823d33999 | 108 | pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) { |
<> | 149:156823d33999 | 109 | // Underlying call is thread safe |
<> | 149:156823d33999 | 110 | return add_common(function, irq, true); |
<> | 149:156823d33999 | 111 | } |
<> | 149:156823d33999 | 112 | |
<> | 149:156823d33999 | 113 | /** Add a handler for an interrupt at the end of the handler list |
AnnaBridge | 184:08ed48f1de7f | 114 | * @deprecated |
AnnaBridge | 184:08ed48f1de7f | 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. |
<> | 149:156823d33999 | 116 | * |
<> | 149:156823d33999 | 117 | * @param tptr pointer to the object that has the handler function |
<> | 149:156823d33999 | 118 | * @param mptr pointer to the actual handler function |
<> | 149:156823d33999 | 119 | * @param irq interrupt number |
<> | 149:156823d33999 | 120 | * |
<> | 149:156823d33999 | 121 | * @returns |
<> | 149:156823d33999 | 122 | * The function object created for 'tptr' and 'mptr' |
<> | 149:156823d33999 | 123 | */ |
<> | 149:156823d33999 | 124 | template<typename T> |
Anna Bridge |
180:96ed750bd169 | 125 | MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " |
Anna Bridge |
180:96ed750bd169 | 126 | "public API of mbed-os and is being removed in the future.") |
<> | 149:156823d33999 | 127 | pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IRQn_Type irq) { |
<> | 149:156823d33999 | 128 | // Underlying call is thread safe |
<> | 149:156823d33999 | 129 | return add_common(tptr, mptr, irq); |
<> | 149:156823d33999 | 130 | } |
<> | 149:156823d33999 | 131 | |
<> | 149:156823d33999 | 132 | /** Add a handler for an interrupt at the beginning of the handler list |
AnnaBridge | 184:08ed48f1de7f | 133 | * @deprecated |
AnnaBridge | 184:08ed48f1de7f | 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. |
<> | 149:156823d33999 | 135 | * |
<> | 149:156823d33999 | 136 | * @param tptr pointer to the object that has the handler function |
<> | 149:156823d33999 | 137 | * @param mptr pointer to the actual handler function |
<> | 149:156823d33999 | 138 | * @param irq interrupt number |
<> | 149:156823d33999 | 139 | * |
<> | 149:156823d33999 | 140 | * @returns |
<> | 149:156823d33999 | 141 | * The function object created for 'tptr' and 'mptr' |
<> | 149:156823d33999 | 142 | */ |
<> | 149:156823d33999 | 143 | template<typename T> |
Anna Bridge |
180:96ed750bd169 | 144 | MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " |
Anna Bridge |
180:96ed750bd169 | 145 | "public API of mbed-os and is being removed in the future.") |
<> | 149:156823d33999 | 146 | pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IRQn_Type irq) { |
<> | 149:156823d33999 | 147 | // Underlying call is thread safe |
<> | 149:156823d33999 | 148 | return add_common(tptr, mptr, irq, true); |
<> | 149:156823d33999 | 149 | } |
<> | 149:156823d33999 | 150 | |
<> | 149:156823d33999 | 151 | /** Remove a handler from an interrupt |
AnnaBridge | 184:08ed48f1de7f | 152 | * @deprecated |
AnnaBridge | 184:08ed48f1de7f | 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. |
<> | 149:156823d33999 | 154 | * |
<> | 149:156823d33999 | 155 | * @param handler the function object for the handler to remove |
<> | 149:156823d33999 | 156 | * @param irq the interrupt number |
<> | 149:156823d33999 | 157 | * |
<> | 149:156823d33999 | 158 | * @returns |
<> | 149:156823d33999 | 159 | * true if the handler was found and removed, false otherwise |
<> | 149:156823d33999 | 160 | */ |
Anna Bridge |
180:96ed750bd169 | 161 | MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the " |
Anna Bridge |
180:96ed750bd169 | 162 | "public API of mbed-os and is being removed in the future.") |
<> | 149:156823d33999 | 163 | bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq); |
<> | 149:156823d33999 | 164 | |
<> | 149:156823d33999 | 165 | private: |
<> | 149:156823d33999 | 166 | InterruptManager(); |
<> | 149:156823d33999 | 167 | ~InterruptManager(); |
<> | 149:156823d33999 | 168 | |
<> | 149:156823d33999 | 169 | void lock(); |
<> | 149:156823d33999 | 170 | void unlock(); |
<> | 149:156823d33999 | 171 | |
<> | 149:156823d33999 | 172 | template<typename T> |
<> | 149:156823d33999 | 173 | pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front=false) { |
<> | 149:156823d33999 | 174 | _mutex.lock(); |
<> | 149:156823d33999 | 175 | int irq_pos = get_irq_index(irq); |
<> | 149:156823d33999 | 176 | bool change = must_replace_vector(irq); |
<> | 149:156823d33999 | 177 | |
<> | 149:156823d33999 | 178 | pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr); |
<> | 149:156823d33999 | 179 | if (change) |
<> | 149:156823d33999 | 180 | NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper); |
<> | 149:156823d33999 | 181 | _mutex.unlock(); |
<> | 149:156823d33999 | 182 | return pf; |
<> | 149:156823d33999 | 183 | } |
<> | 149:156823d33999 | 184 | |
<> | 149:156823d33999 | 185 | pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front=false); |
<> | 149:156823d33999 | 186 | bool must_replace_vector(IRQn_Type irq); |
<> | 149:156823d33999 | 187 | int get_irq_index(IRQn_Type irq); |
<> | 149:156823d33999 | 188 | void irq_helper(); |
<> | 149:156823d33999 | 189 | void add_helper(void (*function)(void), IRQn_Type irq, bool front=false); |
<> | 149:156823d33999 | 190 | static void static_irq_helper(); |
<> | 149:156823d33999 | 191 | |
<> | 149:156823d33999 | 192 | CallChain* _chains[NVIC_NUM_VECTORS]; |
<> | 149:156823d33999 | 193 | static InterruptManager* _instance; |
<> | 149:156823d33999 | 194 | PlatformMutex _mutex; |
<> | 149:156823d33999 | 195 | }; |
<> | 149:156823d33999 | 196 | |
<> | 149:156823d33999 | 197 | } // namespace mbed |
<> | 149:156823d33999 | 198 | |
<> | 149:156823d33999 | 199 | #endif |