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