DigitalInOut.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_DIGITALINOUT_H
  17. #define MBED_DIGITALINOUT_H
  18. #include "platform/platform.h"
  19. #include "hal/gpio_api.h"
  20. #include "platform/mbed_critical.h"
  21. namespace mbed {
  22. /** \addtogroup drivers */
  23. /** A digital input/output, used for setting or reading a bi-directional pin
  24. *
  25. * @note Synchronization level: Interrupt safe
  26. * @ingroup drivers
  27. */
  28. class DigitalInOut {
  29. public:
  30. /** Create a DigitalInOut connected to the specified pin
  31. *
  32. * @param pin DigitalInOut pin to connect to
  33. */
  34. DigitalInOut(PinName pin) : gpio()
  35. {
  36. // No lock needed in the constructor
  37. gpio_init_in(&gpio, pin);
  38. }
  39. /** Create a DigitalInOut connected to the specified pin
  40. *
  41. * @param pin DigitalInOut pin to connect to
  42. * @param direction the initial direction of the pin
  43. * @param mode the initial mode of the pin
  44. * @param value the initial value of the pin if is an output
  45. */
  46. DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio()
  47. {
  48. // No lock needed in the constructor
  49. gpio_init_inout(&gpio, pin, direction, mode, value);
  50. }
  51. /** Set the output, specified as 0 or 1 (int)
  52. *
  53. * @param value An integer specifying the pin output value,
  54. * 0 for logical 0, 1 (or any other non-zero value) for logical 1
  55. */
  56. void write(int value)
  57. {
  58. // Thread safe / atomic HAL call
  59. gpio_write(&gpio, value);
  60. }
  61. /** Return the output setting, represented as 0 or 1 (int)
  62. *
  63. * @returns
  64. * an integer representing the output setting of the pin if it is an output,
  65. * or read the input if set as an input
  66. */
  67. int read()
  68. {
  69. // Thread safe / atomic HAL call
  70. return gpio_read(&gpio);
  71. }
  72. /** Set as an output
  73. */
  74. void output()
  75. {
  76. core_util_critical_section_enter();
  77. gpio_dir(&gpio, PIN_OUTPUT);
  78. core_util_critical_section_exit();
  79. }
  80. /** Set as an input
  81. */
  82. void input()
  83. {
  84. core_util_critical_section_enter();
  85. gpio_dir(&gpio, PIN_INPUT);
  86. core_util_critical_section_exit();
  87. }
  88. /** Set the input pin mode
  89. *
  90. * @param pull PullUp, PullDown, PullNone, OpenDrain
  91. */
  92. void mode(PinMode pull)
  93. {
  94. core_util_critical_section_enter();
  95. gpio_mode(&gpio, pull);
  96. core_util_critical_section_exit();
  97. }
  98. /** Return the output setting, represented as 0 or 1 (int)
  99. *
  100. * @returns
  101. * Non zero value if pin is connected to uc GPIO
  102. * 0 if gpio object was initialized with NC
  103. */
  104. int is_connected()
  105. {
  106. // Thread safe / atomic HAL call
  107. return gpio_is_connected(&gpio);
  108. }
  109. /** A shorthand for write()
  110. * \sa DigitalInOut::write()
  111. */
  112. DigitalInOut &operator= (int value)
  113. {
  114. // Underlying write is thread safe
  115. write(value);
  116. return *this;
  117. }
  118. /** A shorthand for write()
  119. * \sa DigitalInOut::write()
  120. */
  121. DigitalInOut &operator= (DigitalInOut &rhs)
  122. {
  123. core_util_critical_section_enter();
  124. write(rhs.read());
  125. core_util_critical_section_exit();
  126. return *this;
  127. }
  128. /** A shorthand for read()
  129. * \sa DigitalInOut::read()
  130. */
  131. operator int()
  132. {
  133. // Underlying call is thread safe
  134. return read();
  135. }
  136. protected:
  137. gpio_t gpio;
  138. };
  139. } // namespace mbed
  140. #endif