BusInOut.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/BusInOut.h"
  17. #include "platform/mbed_assert.h"
  18. namespace mbed {
  19. BusInOut::BusInOut(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 DigitalInOut(pins[i]) : 0;
  26. if (pins[i] != NC) {
  27. _nc_mask |= (1 << i);
  28. }
  29. }
  30. }
  31. BusInOut::BusInOut(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 DigitalInOut(pins[i]) : 0;
  37. if (pins[i] != NC) {
  38. _nc_mask |= (1 << i);
  39. }
  40. }
  41. }
  42. BusInOut::~BusInOut()
  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 BusInOut::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 BusInOut::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. void BusInOut::output()
  74. {
  75. lock();
  76. for (int i = 0; i < 16; i++) {
  77. if (_pin[i] != 0) {
  78. _pin[i]->output();
  79. }
  80. }
  81. unlock();
  82. }
  83. void BusInOut::input()
  84. {
  85. lock();
  86. for (int i = 0; i < 16; i++) {
  87. if (_pin[i] != 0) {
  88. _pin[i]->input();
  89. }
  90. }
  91. unlock();
  92. }
  93. void BusInOut::mode(PinMode pull)
  94. {
  95. lock();
  96. for (int i = 0; i < 16; i++) {
  97. if (_pin[i] != 0) {
  98. _pin[i]->mode(pull);
  99. }
  100. }
  101. unlock();
  102. }
  103. BusInOut &BusInOut::operator= (int v)
  104. {
  105. // Underlying write is thread safe
  106. write(v);
  107. return *this;
  108. }
  109. BusInOut &BusInOut::operator= (BusInOut &rhs)
  110. {
  111. // Underlying read is thread safe
  112. write(rhs.read());
  113. return *this;
  114. }
  115. DigitalInOut &BusInOut::operator[](int index)
  116. {
  117. // No lock needed since _pin is not modified outside the constructor
  118. MBED_ASSERT(index >= 0 && index <= 16);
  119. MBED_ASSERT(_pin[index]);
  120. return *_pin[index];
  121. }
  122. BusInOut::operator int()
  123. {
  124. // Underlying read is thread safe
  125. return read();
  126. }
  127. void BusInOut::lock()
  128. {
  129. _mutex.lock();
  130. }
  131. void BusInOut::unlock()
  132. {
  133. _mutex.unlock();
  134. }
  135. } // namespace mbed