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:
Anna Bridge
Date:
Fri Jun 22 15:38:59 2018 +0100
Revision:
169:a7c7b631e539
Parent:
165:d1b4690b3f8b
Child:
170:e95d10626187
mbed library. Release version 162

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 165:d1b4690b3f8b 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 "
Anna Bridge 160:5571c4ff569f 67 "public API of mbed-os and is being removed in the future.")
AnnaBridge 156:ff21514d8981 68 static InterruptManager* get();
AnnaBridge 156:ff21514d8981 69
AnnaBridge 156:ff21514d8981 70 /** Destroy the current instance of the interrupt manager
AnnaBridge 165:d1b4690b3f8b 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 "
Anna Bridge 160:5571c4ff569f 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 165:d1b4690b3f8b 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 "
Anna Bridge 160:5571c4ff569f 90 "public API of mbed-os and is being removed in the future.")
AnnaBridge 156:ff21514d8981 91 pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) {
AnnaBridge 156:ff21514d8981 92 // Underlying call is thread safe
AnnaBridge 156:ff21514d8981 93 return add_common(function, irq);
AnnaBridge 156:ff21514d8981 94 }
AnnaBridge 156:ff21514d8981 95
AnnaBridge 156:ff21514d8981 96 /** Add a handler for an interrupt at the beginning of the handler list
AnnaBridge 165:d1b4690b3f8b 97 * @deprecated
AnnaBridge 165:d1b4690b3f8b 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.
AnnaBridge 156:ff21514d8981 99 *
AnnaBridge 156:ff21514d8981 100 * @param function the handler to add
AnnaBridge 156:ff21514d8981 101 * @param irq interrupt number
AnnaBridge 156:ff21514d8981 102 *
AnnaBridge 156:ff21514d8981 103 * @returns
AnnaBridge 156:ff21514d8981 104 * The function object created for 'function'
AnnaBridge 156:ff21514d8981 105 */
Anna Bridge 160:5571c4ff569f 106 MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
Anna Bridge 160:5571c4ff569f 107 "public API of mbed-os and is being removed in the future.")
AnnaBridge 156:ff21514d8981 108 pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) {
AnnaBridge 156:ff21514d8981 109 // Underlying call is thread safe
AnnaBridge 156:ff21514d8981 110 return add_common(function, irq, true);
AnnaBridge 156:ff21514d8981 111 }
AnnaBridge 156:ff21514d8981 112
AnnaBridge 156:ff21514d8981 113 /** Add a handler for an interrupt at the end of the handler list
AnnaBridge 165:d1b4690b3f8b 114 * @deprecated
AnnaBridge 165:d1b4690b3f8b 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.
AnnaBridge 156:ff21514d8981 116 *
AnnaBridge 156:ff21514d8981 117 * @param tptr pointer to the object that has the handler function
AnnaBridge 156:ff21514d8981 118 * @param mptr pointer to the actual handler function
AnnaBridge 156:ff21514d8981 119 * @param irq interrupt number
AnnaBridge 156:ff21514d8981 120 *
AnnaBridge 156:ff21514d8981 121 * @returns
AnnaBridge 156:ff21514d8981 122 * The function object created for 'tptr' and 'mptr'
AnnaBridge 156:ff21514d8981 123 */
AnnaBridge 156:ff21514d8981 124 template<typename T>
Anna Bridge 160:5571c4ff569f 125 MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
Anna Bridge 160:5571c4ff569f 126 "public API of mbed-os and is being removed in the future.")
AnnaBridge 156:ff21514d8981 127 pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
AnnaBridge 156:ff21514d8981 128 // Underlying call is thread safe
AnnaBridge 156:ff21514d8981 129 return add_common(tptr, mptr, irq);
AnnaBridge 156:ff21514d8981 130 }
AnnaBridge 156:ff21514d8981 131
AnnaBridge 156:ff21514d8981 132 /** Add a handler for an interrupt at the beginning of the handler list
AnnaBridge 165:d1b4690b3f8b 133 * @deprecated
AnnaBridge 165:d1b4690b3f8b 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.
AnnaBridge 156:ff21514d8981 135 *
AnnaBridge 156:ff21514d8981 136 * @param tptr pointer to the object that has the handler function
AnnaBridge 156:ff21514d8981 137 * @param mptr pointer to the actual handler function
AnnaBridge 156:ff21514d8981 138 * @param irq interrupt number
AnnaBridge 156:ff21514d8981 139 *
AnnaBridge 156:ff21514d8981 140 * @returns
AnnaBridge 156:ff21514d8981 141 * The function object created for 'tptr' and 'mptr'
AnnaBridge 156:ff21514d8981 142 */
AnnaBridge 156:ff21514d8981 143 template<typename T>
Anna Bridge 160:5571c4ff569f 144 MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
Anna Bridge 160:5571c4ff569f 145 "public API of mbed-os and is being removed in the future.")
AnnaBridge 156:ff21514d8981 146 pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
AnnaBridge 156:ff21514d8981 147 // Underlying call is thread safe
AnnaBridge 156:ff21514d8981 148 return add_common(tptr, mptr, irq, true);
AnnaBridge 156:ff21514d8981 149 }
AnnaBridge 156:ff21514d8981 150
AnnaBridge 156:ff21514d8981 151 /** Remove a handler from an interrupt
AnnaBridge 165:d1b4690b3f8b 152 * @deprecated
AnnaBridge 165:d1b4690b3f8b 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.
AnnaBridge 156:ff21514d8981 154 *
AnnaBridge 156:ff21514d8981 155 * @param handler the function object for the handler to remove
AnnaBridge 156:ff21514d8981 156 * @param irq the interrupt number
AnnaBridge 156:ff21514d8981 157 *
AnnaBridge 156:ff21514d8981 158 * @returns
AnnaBridge 156:ff21514d8981 159 * true if the handler was found and removed, false otherwise
AnnaBridge 156:ff21514d8981 160 */
Anna Bridge 160:5571c4ff569f 161 MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
Anna Bridge 160:5571c4ff569f 162 "public API of mbed-os and is being removed in the future.")
AnnaBridge 156:ff21514d8981 163 bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq);
AnnaBridge 156:ff21514d8981 164
AnnaBridge 156:ff21514d8981 165 private:
AnnaBridge 156:ff21514d8981 166 InterruptManager();
AnnaBridge 156:ff21514d8981 167 ~InterruptManager();
AnnaBridge 156:ff21514d8981 168
AnnaBridge 156:ff21514d8981 169 void lock();
AnnaBridge 156:ff21514d8981 170 void unlock();
AnnaBridge 156:ff21514d8981 171
AnnaBridge 156:ff21514d8981 172 template<typename T>
AnnaBridge 156:ff21514d8981 173 pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front=false) {
AnnaBridge 156:ff21514d8981 174 _mutex.lock();
AnnaBridge 156:ff21514d8981 175 int irq_pos = get_irq_index(irq);
AnnaBridge 156:ff21514d8981 176 bool change = must_replace_vector(irq);
AnnaBridge 156:ff21514d8981 177
AnnaBridge 156:ff21514d8981 178 pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr);
AnnaBridge 156:ff21514d8981 179 if (change)
AnnaBridge 156:ff21514d8981 180 NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper);
AnnaBridge 156:ff21514d8981 181 _mutex.unlock();
AnnaBridge 156:ff21514d8981 182 return pf;
AnnaBridge 156:ff21514d8981 183 }
AnnaBridge 156:ff21514d8981 184
AnnaBridge 156:ff21514d8981 185 pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front=false);
AnnaBridge 156:ff21514d8981 186 bool must_replace_vector(IRQn_Type irq);
AnnaBridge 156:ff21514d8981 187 int get_irq_index(IRQn_Type irq);
AnnaBridge 156:ff21514d8981 188 void irq_helper();
AnnaBridge 156:ff21514d8981 189 void add_helper(void (*function)(void), IRQn_Type irq, bool front=false);
AnnaBridge 156:ff21514d8981 190 static void static_irq_helper();
AnnaBridge 156:ff21514d8981 191
AnnaBridge 156:ff21514d8981 192 CallChain* _chains[NVIC_NUM_VECTORS];
AnnaBridge 156:ff21514d8981 193 static InterruptManager* _instance;
AnnaBridge 156:ff21514d8981 194 PlatformMutex _mutex;
AnnaBridge 156:ff21514d8981 195 };
AnnaBridge 156:ff21514d8981 196
AnnaBridge 156:ff21514d8981 197 } // namespace mbed
AnnaBridge 156:ff21514d8981 198
AnnaBridge 156:ff21514d8981 199 #endif