inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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