mbed library sources. Supersedes mbed-src.

Dependents:   BREAK_SENSOR_LED

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Jun 21 17:46:44 2017 +0100
Revision:
167:e84263d55307
Parent:
149:156823d33999
Child:
168:9672193075cf
This updates the lib to the mbed lib v 145

Who changed what in which revision?

UserRevisionLine numberNew 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"
<> 149:156823d33999 22 #include <string.h>
<> 149:156823d33999 23
<> 149:156823d33999 24 namespace mbed {
<> 149:156823d33999 25 /** \addtogroup drivers */
<> 149:156823d33999 26
<> 149:156823d33999 27 /** Use this singleton if you need to chain interrupt handlers.
<> 149:156823d33999 28 *
AnnaBridge 167:e84263d55307 29 * @note Synchronization level: Thread safe
<> 149:156823d33999 30 *
<> 149:156823d33999 31 * Example (for LPC1768):
<> 149:156823d33999 32 * @code
<> 149:156823d33999 33 * #include "InterruptManager.h"
<> 149:156823d33999 34 * #include "mbed.h"
<> 149:156823d33999 35 *
<> 149:156823d33999 36 * Ticker flipper;
<> 149:156823d33999 37 * DigitalOut led1(LED1);
<> 149:156823d33999 38 * DigitalOut led2(LED2);
<> 149:156823d33999 39 *
<> 149:156823d33999 40 * void flip(void) {
<> 149:156823d33999 41 * led1 = !led1;
<> 149:156823d33999 42 * }
<> 149:156823d33999 43 *
<> 149:156823d33999 44 * void handler(void) {
<> 149:156823d33999 45 * led2 = !led1;
<> 149:156823d33999 46 * }
<> 149:156823d33999 47 *
<> 149:156823d33999 48 * int main() {
<> 149:156823d33999 49 * led1 = led2 = 0;
<> 149:156823d33999 50 * flipper.attach(&flip, 1.0);
<> 149:156823d33999 51 * InterruptManager::get()->add_handler(handler, TIMER3_IRQn);
<> 149:156823d33999 52 * }
<> 149:156823d33999 53 * @endcode
AnnaBridge 167:e84263d55307 54 * @ingroup drivers
<> 149:156823d33999 55 */
<> 149:156823d33999 56 class InterruptManager {
<> 149:156823d33999 57 public:
AnnaBridge 167:e84263d55307 58 /** Get the instance of InterruptManager Class
AnnaBridge 167:e84263d55307 59 *
AnnaBridge 167:e84263d55307 60 * @return the only instance of this class
<> 149:156823d33999 61 */
<> 149:156823d33999 62 static InterruptManager* get();
<> 149:156823d33999 63
<> 149:156823d33999 64 /** Destroy the current instance of the interrupt manager
<> 149:156823d33999 65 */
<> 149:156823d33999 66 static void destroy();
<> 149:156823d33999 67
<> 149:156823d33999 68 /** Add a handler for an interrupt at the end of the handler list
<> 149:156823d33999 69 *
<> 149:156823d33999 70 * @param function the handler to add
<> 149:156823d33999 71 * @param irq interrupt number
<> 149:156823d33999 72 *
<> 149:156823d33999 73 * @returns
<> 149:156823d33999 74 * The function object created for 'function'
<> 149:156823d33999 75 */
<> 149:156823d33999 76 pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) {
<> 149:156823d33999 77 // Underlying call is thread safe
<> 149:156823d33999 78 return add_common(function, irq);
<> 149:156823d33999 79 }
<> 149:156823d33999 80
<> 149:156823d33999 81 /** Add a handler for an interrupt at the beginning of the handler list
<> 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 */
<> 149:156823d33999 89 pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) {
<> 149:156823d33999 90 // Underlying call is thread safe
<> 149:156823d33999 91 return add_common(function, irq, true);
<> 149:156823d33999 92 }
<> 149:156823d33999 93
<> 149:156823d33999 94 /** Add a handler for an interrupt at the end of the handler list
<> 149:156823d33999 95 *
<> 149:156823d33999 96 * @param tptr pointer to the object that has the handler function
<> 149:156823d33999 97 * @param mptr pointer to the actual handler function
<> 149:156823d33999 98 * @param irq interrupt number
<> 149:156823d33999 99 *
<> 149:156823d33999 100 * @returns
<> 149:156823d33999 101 * The function object created for 'tptr' and 'mptr'
<> 149:156823d33999 102 */
<> 149:156823d33999 103 template<typename T>
<> 149:156823d33999 104 pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
<> 149:156823d33999 105 // Underlying call is thread safe
<> 149:156823d33999 106 return add_common(tptr, mptr, irq);
<> 149:156823d33999 107 }
<> 149:156823d33999 108
<> 149:156823d33999 109 /** Add a handler for an interrupt at the beginning of the handler list
<> 149:156823d33999 110 *
<> 149:156823d33999 111 * @param tptr pointer to the object that has the handler function
<> 149:156823d33999 112 * @param mptr pointer to the actual handler function
<> 149:156823d33999 113 * @param irq interrupt number
<> 149:156823d33999 114 *
<> 149:156823d33999 115 * @returns
<> 149:156823d33999 116 * The function object created for 'tptr' and 'mptr'
<> 149:156823d33999 117 */
<> 149:156823d33999 118 template<typename T>
<> 149:156823d33999 119 pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
<> 149:156823d33999 120 // Underlying call is thread safe
<> 149:156823d33999 121 return add_common(tptr, mptr, irq, true);
<> 149:156823d33999 122 }
<> 149:156823d33999 123
<> 149:156823d33999 124 /** Remove a handler from an interrupt
<> 149:156823d33999 125 *
<> 149:156823d33999 126 * @param handler the function object for the handler to remove
<> 149:156823d33999 127 * @param irq the interrupt number
<> 149:156823d33999 128 *
<> 149:156823d33999 129 * @returns
<> 149:156823d33999 130 * true if the handler was found and removed, false otherwise
<> 149:156823d33999 131 */
<> 149:156823d33999 132 bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq);
<> 149:156823d33999 133
<> 149:156823d33999 134 private:
<> 149:156823d33999 135 InterruptManager();
<> 149:156823d33999 136 ~InterruptManager();
<> 149:156823d33999 137
<> 149:156823d33999 138 void lock();
<> 149:156823d33999 139 void unlock();
<> 149:156823d33999 140
<> 149:156823d33999 141 // We declare the copy contructor and the assignment operator, but we don't
<> 149:156823d33999 142 // implement them. This way, if someone tries to copy/assign our instance,
<> 149:156823d33999 143 // he will get an error at compile time.
<> 149:156823d33999 144 InterruptManager(const InterruptManager&);
<> 149:156823d33999 145 InterruptManager& operator =(const InterruptManager&);
<> 149:156823d33999 146
<> 149:156823d33999 147 template<typename T>
<> 149:156823d33999 148 pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front=false) {
<> 149:156823d33999 149 _mutex.lock();
<> 149:156823d33999 150 int irq_pos = get_irq_index(irq);
<> 149:156823d33999 151 bool change = must_replace_vector(irq);
<> 149:156823d33999 152
<> 149:156823d33999 153 pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr);
<> 149:156823d33999 154 if (change)
<> 149:156823d33999 155 NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper);
<> 149:156823d33999 156 _mutex.unlock();
<> 149:156823d33999 157 return pf;
<> 149:156823d33999 158 }
<> 149:156823d33999 159
<> 149:156823d33999 160 pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front=false);
<> 149:156823d33999 161 bool must_replace_vector(IRQn_Type irq);
<> 149:156823d33999 162 int get_irq_index(IRQn_Type irq);
<> 149:156823d33999 163 void irq_helper();
<> 149:156823d33999 164 void add_helper(void (*function)(void), IRQn_Type irq, bool front=false);
<> 149:156823d33999 165 static void static_irq_helper();
<> 149:156823d33999 166
<> 149:156823d33999 167 CallChain* _chains[NVIC_NUM_VECTORS];
<> 149:156823d33999 168 static InterruptManager* _instance;
<> 149:156823d33999 169 PlatformMutex _mutex;
<> 149:156823d33999 170 };
<> 149:156823d33999 171
<> 149:156823d33999 172 } // namespace mbed
<> 149:156823d33999 173
<> 149:156823d33999 174 #endif