PortInOut.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_PORTINOUT_H
  17. #define MBED_PORTINOUT_H
  18. #include "platform/platform.h"
  19. #if defined (DEVICE_PORTINOUT) || defined(DOXYGEN_ONLY)
  20. #include "hal/port_api.h"
  21. #include "platform/mbed_critical.h"
  22. namespace mbed {
  23. /** \addtogroup drivers */
  24. /** A multiple pin digital in/out used to set/read multiple bi-directional pins
  25. *
  26. * @note Synchronization level: Interrupt safe
  27. * @ingroup drivers
  28. */
  29. class PortInOut {
  30. public:
  31. /** Create an PortInOut, connected to the specified port
  32. *
  33. * @param port Port to connect to (Port0-Port5)
  34. * @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
  35. */
  36. PortInOut(PortName port, int mask = 0xFFFFFFFF)
  37. {
  38. core_util_critical_section_enter();
  39. port_init(&_port, port, mask, PIN_INPUT);
  40. core_util_critical_section_exit();
  41. }
  42. /** Write the value to the output port
  43. *
  44. * @param value An integer specifying a bit to write for every corresponding port pin
  45. */
  46. void write(int value)
  47. {
  48. port_write(&_port, value);
  49. }
  50. /** Read the value currently output on the port
  51. *
  52. * @returns
  53. * An integer with each bit corresponding to associated port pin setting
  54. */
  55. int read()
  56. {
  57. return port_read(&_port);
  58. }
  59. /** Set as an output
  60. */
  61. void output()
  62. {
  63. core_util_critical_section_enter();
  64. port_dir(&_port, PIN_OUTPUT);
  65. core_util_critical_section_exit();
  66. }
  67. /** Set as an input
  68. */
  69. void input()
  70. {
  71. core_util_critical_section_enter();
  72. port_dir(&_port, PIN_INPUT);
  73. core_util_critical_section_exit();
  74. }
  75. /** Set the input pin mode
  76. *
  77. * @param mode PullUp, PullDown, PullNone, OpenDrain
  78. */
  79. void mode(PinMode mode)
  80. {
  81. core_util_critical_section_enter();
  82. port_mode(&_port, mode);
  83. core_util_critical_section_exit();
  84. }
  85. /** A shorthand for write()
  86. * \sa PortInOut::write()
  87. */
  88. PortInOut &operator= (int value)
  89. {
  90. write(value);
  91. return *this;
  92. }
  93. /** A shorthand for write()
  94. * \sa PortInOut::write()
  95. */
  96. PortInOut &operator= (PortInOut &rhs)
  97. {
  98. write(rhs.read());
  99. return *this;
  100. }
  101. /** A shorthand for read()
  102. * \sa PortInOut::read()
  103. */
  104. operator int()
  105. {
  106. return read();
  107. }
  108. private:
  109. port_t _port;
  110. };
  111. } // namespace mbed
  112. #endif
  113. #endif