Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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