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