InterruptManager.h 7.2 KB

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