The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Thu Nov 08 11:45:42 2018 +0000
Revision:
171:3a7713b1edbc
Parent:
170:e95d10626187
Child:
172:65be27845400
mbed library. Release version 164

Who changed what in which revision?

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