mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

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-2013 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 /** \addtogroup drivers */
00028 
00029 /** Use this singleton if you need to chain interrupt handlers.
00030  *  @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.
00031  *
00032  * @note Synchronization level: Thread safe
00033  *
00034  * Example (for LPC1768):
00035  * @code
00036  * #include "InterruptManager.h"
00037  * #include "mbed.h"
00038  *
00039  * Ticker flipper;
00040  * DigitalOut led1(LED1);
00041  * DigitalOut led2(LED2);
00042  *
00043  * void flip(void) {
00044  *     led1 = !led1;
00045  * }
00046  *
00047  * void handler(void) {
00048  *     led2 = !led1;
00049  * }
00050  *
00051  * int main() {
00052  *     led1 = led2 = 0;
00053  *     flipper.attach(&flip, 1.0);
00054  *     InterruptManager::get()->add_handler(handler, TIMER3_IRQn);
00055  * }
00056  * @endcode
00057  * @ingroup drivers
00058  */
00059 class InterruptManager : private NonCopyable<InterruptManager> {
00060 public:
00061     /** Get the instance of InterruptManager Class
00062      *  @deprecated
00063      *  Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
00064      *
00065      *  @return the only instance of this class
00066      */
00067     MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
00068                           "public API of mbed-os and is being removed in the future.")
00069     static InterruptManager *get();
00070 
00071     /** Destroy the current instance of the interrupt manager
00072      *  @deprecated
00073      *  Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
00074      *
00075      */
00076     MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
00077                           "public API of mbed-os and is being removed in the future.")
00078     static void destroy();
00079 
00080     /** Add a handler for an interrupt at the end of the handler list
00081      *  @deprecated
00082      *  Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
00083      *
00084      *  @param function the handler to add
00085      *  @param irq interrupt number
00086      *
00087      *  @returns
00088      *  The function object created for 'function'
00089      */
00090     MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
00091                           "public API of mbed-os and is being removed in the future.")
00092     pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq)
00093     {
00094         // Underlying call is thread safe
00095         return add_common(function, irq);
00096     }
00097 
00098     /** Add a handler for an interrupt at the beginning of the handler list
00099      *  @deprecated
00100      *  Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
00101      *
00102      *  @param function the handler to add
00103      *  @param irq interrupt number
00104      *
00105      *  @returns
00106      *  The function object created for 'function'
00107      */
00108     MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
00109                           "public API of mbed-os and is being removed in the future.")
00110     pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq)
00111     {
00112         // Underlying call is thread safe
00113         return add_common(function, irq, true);
00114     }
00115 
00116     /** Add a handler for an interrupt at the end of the handler list
00117      *  @deprecated
00118      *  Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
00119      *
00120      *  @param tptr pointer to the object that has the handler function
00121      *  @param mptr pointer to the actual handler function
00122      *  @param irq interrupt number
00123      *
00124      *  @returns
00125      *  The function object created for 'tptr' and 'mptr'
00126      */
00127     template<typename T>
00128     MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
00129                           "public API of mbed-os and is being removed in the future.")
00130     pFunctionPointer_t add_handler(T *tptr, void (T::*mptr)(void), IRQn_Type irq)
00131     {
00132         // Underlying call is thread safe
00133         return add_common(tptr, mptr, irq);
00134     }
00135 
00136     /** Add a handler for an interrupt at the beginning of the handler list
00137      *  @deprecated
00138      *  Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
00139      *
00140      *  @param tptr pointer to the object that has the handler function
00141      *  @param mptr pointer to the actual handler function
00142      *  @param irq interrupt number
00143      *
00144      *  @returns
00145      *  The function object created for 'tptr' and 'mptr'
00146      */
00147     template<typename T>
00148     MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
00149                           "public API of mbed-os and is being removed in the future.")
00150     pFunctionPointer_t add_handler_front(T *tptr, void (T::*mptr)(void), IRQn_Type irq)
00151     {
00152         // Underlying call is thread safe
00153         return add_common(tptr, mptr, irq, true);
00154     }
00155 
00156     /** Remove a handler from an interrupt
00157      *  @deprecated
00158      *  Do not use this function, this class is not part of the public API of mbed-os and is being removed in the future.
00159      *
00160      *  @param handler the function object for the handler to remove
00161      *  @param irq the interrupt number
00162      *
00163      *  @returns
00164      *  true if the handler was found and removed, false otherwise
00165      */
00166     MBED_DEPRECATED_SINCE("mbed-os-5.6", "This class is not part of the "
00167                           "public API of mbed-os and is being removed in the future.")
00168     bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq);
00169 
00170 #if !defined(DOXYGEN_ONLY)
00171 private:
00172     InterruptManager();
00173     ~InterruptManager();
00174 
00175     void lock();
00176     void unlock();
00177 
00178     template<typename T>
00179     pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front = false)
00180     {
00181         _mutex.lock();
00182         int irq_pos = get_irq_index(irq);
00183         bool change = must_replace_vector(irq);
00184 
00185         pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr);
00186         if (change) {
00187             NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper);
00188         }
00189         _mutex.unlock();
00190         return pf;
00191     }
00192 
00193     pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front = false);
00194     bool must_replace_vector(IRQn_Type irq);
00195     int get_irq_index(IRQn_Type irq);
00196     void irq_helper();
00197     void add_helper(void (*function)(void), IRQn_Type irq, bool front = false);
00198     static void static_irq_helper();
00199 
00200     CallChain *_chains[NVIC_NUM_VECTORS];
00201     static InterruptManager *_instance;
00202     PlatformMutex _mutex;
00203 #endif
00204 };
00205 
00206 } // namespace mbed
00207 
00208 #endif