Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Thu Mar 29 07:02:09 2018 +0000
Revision:
0:380207fcb5c1
Encoder, IMU --> OK; Controller --> in bearbeitung

Who changed what in which revision?

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