Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers InterruptManager.h Source File

InterruptManager.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2019 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #ifndef MBED_INTERRUPTMANAGER_H
00018 #define MBED_INTERRUPTMANAGER_H
00019 
00020 #include "cmsis.h"
00021 #include "platform/CallChain.h"
00022 #include "platform/PlatformMutex.h"
00023 #include "platform/NonCopyable.h"
00024 #include <string.h>
00025 
00026 namespace mbed {
00027 /**
00028  * \defgroup drivers_InterruptManager InterruptManager class
00029  * \ingroup drivers-public-api-gpio
00030  * @{
00031  */
00032 
00033 /** Use this singleton if you need to chain interrupt handlers.
00034  *  @deprecated Do not use this class. This class is not part of the public API of mbed-os and is being removed in the future.
00035  *
00036  * @note Synchronization level: Thread safe
00037  *
00038  * Example (for LPC1768):
00039  * @code
00040  * #include "InterruptManager.h"
00041  * #include "mbed.h"
00042  *
00043  * Ticker flipper;
00044  * DigitalOut led1(LED1);
00045  * DigitalOut led2(LED2);
00046  *
00047  * void flip(void) {
00048  *     led1 = !led1;
00049  * }
00050  *
00051  * void handler(void) {
00052  *     led2 = !led1;
00053  * }
00054  *
00055  * int main() {
00056  *     led1 = led2 = 0;
00057  *     flipper.attach(&flip, 1.0);
00058  *     InterruptManager::get()->add_handler(handler, TIMER3_IRQn);
00059  * }
00060  * @endcode
00061  */
00062 class InterruptManager : private NonCopyable<InterruptManager> {
00063 public:
00064     /** Get the instance of InterruptManager Class
00065      *  @deprecated
00066      *  Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
00067      *
00068      *  @return the only instance of this class
00069      */
00070     MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
00071                           "public API of mbed-os and is being removed in the future.")
00072     static InterruptManager *get();
00073 
00074     /** Destroy the current instance of the interrupt manager
00075      *  @deprecated
00076      *  Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
00077      *
00078      */
00079     MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
00080                           "public API of mbed-os and is being removed in the future.")
00081     static void destroy();
00082 
00083     /** Add a handler for an interrupt at the end of the handler list
00084      *  @deprecated
00085      *  Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
00086      *
00087      *  @param function the handler to add
00088      *  @param irq interrupt number
00089      *
00090      *  @returns
00091      *  The function object created for 'function'
00092      */
00093     MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
00094                           "public API of mbed-os and is being removed in the future.")
00095     pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq)
00096     {
00097         // Underlying call is thread safe
00098         return add_common(function, irq);
00099     }
00100 
00101     /** Add a handler for an interrupt at the beginning of the handler list
00102      *  @deprecated
00103      *  Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
00104      *
00105      *  @param function the handler to add
00106      *  @param irq interrupt number
00107      *
00108      *  @returns
00109      *  The function object created for 'function'
00110      */
00111     MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
00112                           "public API of mbed-os and is being removed in the future.")
00113     pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq)
00114     {
00115         // Underlying call is thread safe
00116         return add_common(function, irq, true);
00117     }
00118 
00119     /** Add a handler for an interrupt at the end of the handler list
00120      *  @deprecated
00121      *  Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
00122      *
00123      *  @param tptr pointer to the object that has the handler function
00124      *  @param mptr pointer to the actual handler function
00125      *  @param irq interrupt number
00126      *
00127      *  @returns
00128      *  The function object created for 'tptr' and 'mptr'
00129      */
00130     template<typename T>
00131     MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
00132                           "public API of mbed-os and is being removed in the future.")
00133     pFunctionPointer_t add_handler(T *tptr, void (T::*mptr)(void), IRQn_Type irq)
00134     {
00135         // Underlying call is thread safe
00136         return add_common(tptr, mptr, irq);
00137     }
00138 
00139     /** Add a handler for an interrupt at the beginning of the handler list
00140      *  @deprecated
00141      *  Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
00142      *
00143      *  @param tptr pointer to the object that has the handler function
00144      *  @param mptr pointer to the actual handler function
00145      *  @param irq interrupt number
00146      *
00147      *  @returns
00148      *  The function object created for 'tptr' and 'mptr'
00149      */
00150     template<typename T>
00151     MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
00152                           "public API of mbed-os and is being removed in the future.")
00153     pFunctionPointer_t add_handler_front(T *tptr, void (T::*mptr)(void), IRQn_Type irq)
00154     {
00155         // Underlying call is thread safe
00156         return add_common(tptr, mptr, irq, true);
00157     }
00158 
00159     /** Remove a handler from an interrupt
00160      *  @deprecated
00161      *  Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
00162      *
00163      *  @param handler the function object for the handler to remove
00164      *  @param irq the interrupt number
00165      *
00166      *  @returns
00167      *  true if the handler was found and removed, false otherwise
00168      */
00169     MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
00170                           "public API of mbed-os and is being removed in the future.")
00171     bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq);
00172 
00173 #if !defined(DOXYGEN_ONLY)
00174 private:
00175     InterruptManager();
00176 
00177     ~InterruptManager();
00178 
00179     void lock();
00180     void unlock();
00181 
00182     template<typename T>
00183     pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front = false)
00184     {
00185         _mutex.lock();
00186         int irq_pos = get_irq_index(irq);
00187         bool change = must_replace_vector(irq);
00188 
00189         pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr);
00190         if (change) {
00191             NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper);
00192         }
00193         _mutex.unlock();
00194         return pf;
00195     }
00196 
00197     pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front = false);
00198     bool must_replace_vector(IRQn_Type irq);
00199     int get_irq_index(IRQn_Type irq);
00200     void irq_helper();
00201     void add_helper(void (*function)(void), IRQn_Type irq, bool front = false);
00202     static void static_irq_helper();
00203 
00204     CallChain *_chains[NVIC_NUM_VECTORS];
00205     static InterruptManager *_instance;
00206     PlatformMutex _mutex;
00207 #endif
00208 };
00209 
00210 /** @}*/
00211 
00212 } // namespace mbed
00213 
00214 #endif