InterruptIn.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_INTERRUPTIN_H
  17. #define MBED_INTERRUPTIN_H
  18. #include "platform/platform.h"
  19. #if defined (DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
  20. #include "hal/gpio_api.h"
  21. #include "hal/gpio_irq_api.h"
  22. #include "platform/Callback.h"
  23. #include "platform/mbed_critical.h"
  24. #include "platform/mbed_toolchain.h"
  25. #include "platform/NonCopyable.h"
  26. namespace mbed {
  27. /** \addtogroup drivers */
  28. /** A digital interrupt input, used to call a function on a rising or falling edge
  29. *
  30. * @note Synchronization level: Interrupt safe
  31. *
  32. * Example:
  33. * @code
  34. * // Flash an LED while waiting for events
  35. *
  36. * #include "mbed.h"
  37. *
  38. * InterruptIn event(p16);
  39. * DigitalOut led(LED1);
  40. *
  41. * void trigger() {
  42. * printf("triggered!\n");
  43. * }
  44. *
  45. * int main() {
  46. * event.rise(&trigger);
  47. * while(1) {
  48. * led = !led;
  49. * wait(0.25);
  50. * }
  51. * }
  52. * @endcode
  53. * @ingroup drivers
  54. */
  55. class InterruptIn : private NonCopyable<InterruptIn> {
  56. public:
  57. /** Create an InterruptIn connected to the specified pin
  58. *
  59. * @param pin InterruptIn pin to connect to
  60. */
  61. InterruptIn(PinName pin);
  62. /** Create an InterruptIn connected to the specified pin,
  63. * and the pin configured to the specified mode.
  64. *
  65. * @param pin InterruptIn pin to connect to
  66. * @param mode The mode to set the pin to (PullUp/PullDown/etc.)
  67. */
  68. InterruptIn(PinName pin, PinMode mode);
  69. virtual ~InterruptIn();
  70. /** Read the input, represented as 0 or 1 (int)
  71. *
  72. * @returns
  73. * An integer representing the state of the input pin,
  74. * 0 for logical 0, 1 for logical 1
  75. */
  76. int read();
  77. /** An operator shorthand for read()
  78. */
  79. operator int();
  80. /** Attach a function to call when a rising edge occurs on the input
  81. *
  82. * @param func A pointer to a void function, or 0 to set as none
  83. */
  84. void rise(Callback<void()> func);
  85. /** Attach a member function to call when a rising edge occurs on the input
  86. *
  87. * @param obj pointer to the object to call the member function on
  88. * @param method pointer to the member function to be called
  89. * @deprecated
  90. * The rise function does not support cv-qualifiers. Replaced by
  91. * rise(callback(obj, method)).
  92. */
  93. template<typename T, typename M>
  94. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  95. "The rise function does not support cv-qualifiers. Replaced by "
  96. "rise(callback(obj, method)).")
  97. void rise(T *obj, M method)
  98. {
  99. core_util_critical_section_enter();
  100. rise(callback(obj, method));
  101. core_util_critical_section_exit();
  102. }
  103. /** Attach a function to call when a falling edge occurs on the input
  104. *
  105. * @param func A pointer to a void function, or 0 to set as none
  106. */
  107. void fall(Callback<void()> func);
  108. /** Attach a member function to call when a falling edge occurs on the input
  109. *
  110. * @param obj pointer to the object to call the member function on
  111. * @param method pointer to the member function to be called
  112. * @deprecated
  113. * The rise function does not support cv-qualifiers. Replaced by
  114. * rise(callback(obj, method)).
  115. */
  116. template<typename T, typename M>
  117. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  118. "The fall function does not support cv-qualifiers. Replaced by "
  119. "fall(callback(obj, method)).")
  120. void fall(T *obj, M method)
  121. {
  122. core_util_critical_section_enter();
  123. fall(callback(obj, method));
  124. core_util_critical_section_exit();
  125. }
  126. /** Set the input pin mode
  127. *
  128. * @param pull PullUp, PullDown, PullNone
  129. */
  130. void mode(PinMode pull);
  131. /** Enable IRQ. This method depends on hw implementation, might enable one
  132. * port interrupts. For further information, check gpio_irq_enable().
  133. */
  134. void enable_irq();
  135. /** Disable IRQ. This method depends on hw implementation, might disable one
  136. * port interrupts. For further information, check gpio_irq_disable().
  137. */
  138. void disable_irq();
  139. static void _irq_handler(uint32_t id, gpio_irq_event event);
  140. protected:
  141. gpio_t gpio;
  142. gpio_irq_t gpio_irq;
  143. Callback<void()> _rise;
  144. Callback<void()> _fall;
  145. void irq_init(PinName pin);
  146. };
  147. } // namespace mbed
  148. #endif
  149. #endif