BusOut.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #include "drivers/BusOut.h"
  17. #include "platform/mbed_assert.h"
  18. namespace mbed {
  19. BusOut::BusOut(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName p5, PinName p6, PinName p7, PinName p8, PinName p9, PinName p10, PinName p11, PinName p12, PinName p13, PinName p14, PinName p15)
  20. {
  21. PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};
  22. // No lock needed in the constructor
  23. _nc_mask = 0;
  24. for (int i = 0; i < 16; i++) {
  25. _pin[i] = (pins[i] != NC) ? new DigitalOut(pins[i]) : 0;
  26. if (pins[i] != NC) {
  27. _nc_mask |= (1 << i);
  28. }
  29. }
  30. }
  31. BusOut::BusOut(PinName pins[16])
  32. {
  33. // No lock needed in the constructor
  34. _nc_mask = 0;
  35. for (int i = 0; i < 16; i++) {
  36. _pin[i] = (pins[i] != NC) ? new DigitalOut(pins[i]) : 0;
  37. if (pins[i] != NC) {
  38. _nc_mask |= (1 << i);
  39. }
  40. }
  41. }
  42. BusOut::~BusOut()
  43. {
  44. // No lock needed in the destructor
  45. for (int i = 0; i < 16; i++) {
  46. if (_pin[i] != 0) {
  47. delete _pin[i];
  48. }
  49. }
  50. }
  51. void BusOut::write(int value)
  52. {
  53. lock();
  54. for (int i = 0; i < 16; i++) {
  55. if (_pin[i] != 0) {
  56. _pin[i]->write((value >> i) & 1);
  57. }
  58. }
  59. unlock();
  60. }
  61. int BusOut::read()
  62. {
  63. lock();
  64. int v = 0;
  65. for (int i = 0; i < 16; i++) {
  66. if (_pin[i] != 0) {
  67. v |= _pin[i]->read() << i;
  68. }
  69. }
  70. unlock();
  71. return v;
  72. }
  73. BusOut &BusOut::operator= (int v)
  74. {
  75. // Underlying write is thread safe
  76. write(v);
  77. return *this;
  78. }
  79. BusOut &BusOut::operator= (BusOut &rhs)
  80. {
  81. // Underlying write is thread safe
  82. write(rhs.read());
  83. return *this;
  84. }
  85. DigitalOut &BusOut::operator[](int index)
  86. {
  87. // No lock needed since _pin is not modified outside the constructor
  88. MBED_ASSERT(index >= 0 && index <= 16);
  89. MBED_ASSERT(_pin[index]);
  90. return *_pin[index];
  91. }
  92. BusOut::operator int()
  93. {
  94. // Underlying read is thread safe
  95. return read();
  96. }
  97. void BusOut::lock()
  98. {
  99. _mutex.lock();
  100. }
  101. void BusOut::unlock()
  102. {
  103. _mutex.unlock();
  104. }
  105. } // namespace mbed