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