InterruptIn.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #include "drivers/InterruptIn.h"
  17. #if DEVICE_INTERRUPTIN
  18. namespace mbed {
  19. // Note: This single-parameter constructor exists to maintain binary
  20. // compatibility.
  21. // If not for that, we could simplify by having only the 2-param
  22. // constructor, with a default value for the PinMode.
  23. InterruptIn::InterruptIn(PinName pin) : gpio(),
  24. gpio_irq(),
  25. _rise(NULL),
  26. _fall(NULL)
  27. {
  28. // No lock needed in the constructor
  29. irq_init(pin);
  30. gpio_init_in(&gpio, pin);
  31. }
  32. InterruptIn::InterruptIn(PinName pin, PinMode mode) :
  33. gpio(),
  34. gpio_irq(),
  35. _rise(NULL),
  36. _fall(NULL)
  37. {
  38. // No lock needed in the constructor
  39. irq_init(pin);
  40. gpio_init_in_ex(&gpio, pin, mode);
  41. }
  42. void InterruptIn::irq_init(PinName pin)
  43. {
  44. gpio_irq_init(&gpio_irq, pin, (&InterruptIn::_irq_handler), (uint32_t)this);
  45. }
  46. InterruptIn::~InterruptIn()
  47. {
  48. // No lock needed in the destructor
  49. gpio_irq_free(&gpio_irq);
  50. }
  51. int InterruptIn::read()
  52. {
  53. // Read only
  54. return gpio_read(&gpio);
  55. }
  56. void InterruptIn::mode(PinMode pull)
  57. {
  58. core_util_critical_section_enter();
  59. gpio_mode(&gpio, pull);
  60. core_util_critical_section_exit();
  61. }
  62. void InterruptIn::rise(Callback<void()> func)
  63. {
  64. core_util_critical_section_enter();
  65. if (func) {
  66. _rise = func;
  67. gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
  68. } else {
  69. _rise = NULL;
  70. gpio_irq_set(&gpio_irq, IRQ_RISE, 0);
  71. }
  72. core_util_critical_section_exit();
  73. }
  74. void InterruptIn::fall(Callback<void()> func)
  75. {
  76. core_util_critical_section_enter();
  77. if (func) {
  78. _fall = func;
  79. gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
  80. } else {
  81. _fall = NULL;
  82. gpio_irq_set(&gpio_irq, IRQ_FALL, 0);
  83. }
  84. core_util_critical_section_exit();
  85. }
  86. void InterruptIn::_irq_handler(uint32_t id, gpio_irq_event event)
  87. {
  88. InterruptIn *handler = (InterruptIn *)id;
  89. switch (event) {
  90. case IRQ_RISE:
  91. if (handler->_rise) {
  92. handler->_rise();
  93. }
  94. break;
  95. case IRQ_FALL:
  96. if (handler->_fall) {
  97. handler->_fall();
  98. }
  99. break;
  100. case IRQ_NONE:
  101. break;
  102. }
  103. }
  104. void InterruptIn::enable_irq()
  105. {
  106. core_util_critical_section_enter();
  107. gpio_irq_enable(&gpio_irq);
  108. core_util_critical_section_exit();
  109. }
  110. void InterruptIn::disable_irq()
  111. {
  112. core_util_critical_section_enter();
  113. gpio_irq_disable(&gpio_irq);
  114. core_util_critical_section_exit();
  115. }
  116. InterruptIn::operator int()
  117. {
  118. // Underlying call is atomic
  119. return read();
  120. }
  121. } // namespace mbed
  122. #endif