mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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