BusOut.h 4.0 KB

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