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