TUKS MCU Introductory course / TUKS-COURSE-2-LED
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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