BusInOut.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_BUSINOUT_H
  17. #define MBED_BUSINOUT_H
  18. #include "drivers/DigitalInOut.h"
  19. #include "platform/PlatformMutex.h"
  20. #include "platform/NonCopyable.h"
  21. namespace mbed {
  22. /** \addtogroup drivers */
  23. /** A digital input output bus, used for setting the state of a collection of pins
  24. *
  25. * @note Synchronization level: Thread safe
  26. * @ingroup drivers
  27. */
  28. class BusInOut : private NonCopyable<BusInOut> {
  29. public:
  30. /** Create an BusInOut, connected to the specified pins
  31. *
  32. * @param p0 DigitalInOut pin to connect to bus bit
  33. * @param p1 DigitalInOut pin to connect to bus bit
  34. * @param p2 DigitalInOut pin to connect to bus bit
  35. * @param p3 DigitalInOut pin to connect to bus bit
  36. * @param p4 DigitalInOut pin to connect to bus bit
  37. * @param p5 DigitalInOut pin to connect to bus bit
  38. * @param p6 DigitalInOut pin to connect to bus bit
  39. * @param p7 DigitalInOut pin to connect to bus bit
  40. * @param p8 DigitalInOut pin to connect to bus bit
  41. * @param p9 DigitalInOut pin to connect to bus bit
  42. * @param p10 DigitalInOut pin to connect to bus bit
  43. * @param p11 DigitalInOut pin to connect to bus bit
  44. * @param p12 DigitalInOut pin to connect to bus bit
  45. * @param p13 DigitalInOut pin to connect to bus bit
  46. * @param p14 DigitalInOut pin to connect to bus bit
  47. * @param p15 DigitalInOut pin to connect to bus bit
  48. *
  49. * @note
  50. * It is only required to specify as many pin variables as is required
  51. * for the bus; the rest will default to NC (not connected)
  52. */
  53. BusInOut(PinName p0, PinName p1 = NC, PinName p2 = NC, PinName p3 = NC,
  54. PinName p4 = NC, PinName p5 = NC, PinName p6 = NC, PinName p7 = NC,
  55. PinName p8 = NC, PinName p9 = NC, PinName p10 = NC, PinName p11 = NC,
  56. PinName p12 = NC, PinName p13 = NC, PinName p14 = NC, PinName p15 = NC);
  57. /** Create an BusInOut, connected to the specified pins
  58. *
  59. * @param pins An array of pins to construct a BusInOut from
  60. */
  61. BusInOut(PinName pins[16]);
  62. virtual ~BusInOut();
  63. /* Group: Access Methods */
  64. /** Write the value to the output bus
  65. *
  66. * @param value An integer specifying a bit to write for every corresponding DigitalInOut pin
  67. */
  68. void write(int value);
  69. /** Read the value currently output on the bus
  70. *
  71. * @returns
  72. * An integer with each bit corresponding to associated DigitalInOut pin setting
  73. */
  74. int read();
  75. /** Set as an output
  76. */
  77. void output();
  78. /** Set as an input
  79. */
  80. void input();
  81. /** Set the input pin mode
  82. *
  83. * @param pull PullUp, PullDown, PullNone
  84. */
  85. void mode(PinMode pull);
  86. /** Binary mask of bus pins connected to actual pins (not NC pins)
  87. * If bus pin is in NC state make corresponding bit will be cleared (set to 0), else bit will be set to 1
  88. *
  89. * @returns
  90. * Binary mask of connected pins
  91. */
  92. int mask()
  93. {
  94. // No lock needed since _nc_mask is not modified outside the constructor
  95. return _nc_mask;
  96. }
  97. /** A shorthand for write()
  98. * \sa BusInOut::write()
  99. */
  100. BusInOut &operator= (int v);
  101. BusInOut &operator= (BusInOut &rhs);
  102. /** Access to particular bit in random-iterator fashion
  103. * @param index Bit Position
  104. */
  105. DigitalInOut &operator[](int index);
  106. /** A shorthand for read()
  107. * \sa BusInOut::read()
  108. */
  109. operator int();
  110. protected:
  111. virtual void lock();
  112. virtual void unlock();
  113. DigitalInOut *_pin[16];
  114. /* Mask of bus's NC pins
  115. * If bit[n] is set to 1 - pin is connected
  116. * if bit[n] is cleared - pin is not connected (NC)
  117. */
  118. int _nc_mask;
  119. PlatformMutex _mutex;
  120. };
  121. } // namespace mbed
  122. #endif