CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Sun Nov 04 18:44:51 2018 -0700
Revision:
0:aa13e1c335cd
create new repo for show2me

Who changed what in which revision?

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