PortOut.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_PORTOUT_H
  17. #define MBED_PORTOUT_H
  18. #include "platform/platform.h"
  19. #if defined (DEVICE_PORTOUT) || 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 out
  25. *
  26. * @note Synchronization level: Interrupt safe
  27. *
  28. * Example:
  29. * @code
  30. * // Toggle all four LEDs
  31. *
  32. * #include "mbed.h"
  33. *
  34. * // LED1 = P1.18 LED2 = P1.20 LED3 = P1.21 LED4 = P1.23
  35. * #define LED_MASK 0x00B40000
  36. *
  37. * PortOut ledport(Port1, LED_MASK);
  38. *
  39. * int main() {
  40. * while(1) {
  41. * ledport = LED_MASK;
  42. * wait(1);
  43. * ledport = 0;
  44. * wait(1);
  45. * }
  46. * }
  47. * @endcode
  48. * @ingroup drivers
  49. */
  50. class PortOut {
  51. public:
  52. /** Create an PortOut, connected to the specified port
  53. *
  54. * @param port Port to connect to (Port0-Port5)
  55. * @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
  56. */
  57. PortOut(PortName port, int mask = 0xFFFFFFFF)
  58. {
  59. core_util_critical_section_enter();
  60. port_init(&_port, port, mask, PIN_OUTPUT);
  61. core_util_critical_section_exit();
  62. }
  63. /** Write the value to the output port
  64. *
  65. * @param value An integer specifying a bit to write for every corresponding PortOut pin
  66. */
  67. void write(int value)
  68. {
  69. port_write(&_port, value);
  70. }
  71. /** Read the value currently output on the port
  72. *
  73. * @returns
  74. * An integer with each bit corresponding to associated PortOut pin setting
  75. */
  76. int read()
  77. {
  78. return port_read(&_port);
  79. }
  80. /** A shorthand for write()
  81. * \sa PortOut::write()
  82. */
  83. PortOut &operator= (int value)
  84. {
  85. write(value);
  86. return *this;
  87. }
  88. /** A shorthand for read()
  89. * \sa PortOut::read()
  90. */
  91. PortOut &operator= (PortOut &rhs)
  92. {
  93. write(rhs.read());
  94. return *this;
  95. }
  96. /** A shorthand for read()
  97. * \sa PortOut::read()
  98. */
  99. operator int()
  100. {
  101. return read();
  102. }
  103. private:
  104. port_t _port;
  105. };
  106. } // namespace mbed
  107. #endif
  108. #endif