temp

Dependencies:   mbed SDFileSystem MS5607 ADXL345_I2C FATFileSystem

Committer:
IKobayashi
Date:
Mon Mar 16 23:37:42 2020 +0900
Revision:
0:c88c3b616c00
copy

Who changed what in which revision?

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