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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.