My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

twibus.cpp 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "Marlin.h"
  23. #if ENABLED(EXPERIMENTAL_I2CBUS)
  24. #include "twibus.h"
  25. #include <Wire.h>
  26. TWIBus::TWIBus() {
  27. #if I2C_SLAVE_ADDRESS == 0
  28. Wire.begin(); // No address joins the BUS as the master
  29. #else
  30. Wire.begin(I2C_SLAVE_ADDRESS); // Join the bus as a slave
  31. #endif
  32. this->reset();
  33. }
  34. void TWIBus::reset() {
  35. this->buffer_s = 0;
  36. this->buffer[0] = 0x00;
  37. }
  38. void TWIBus::address(const uint8_t adr) {
  39. if (!WITHIN(adr, 8, 127)) {
  40. SERIAL_ECHO_START();
  41. SERIAL_ECHOLNPGM("Bad I2C address (8-127)");
  42. }
  43. this->addr = adr;
  44. #if ENABLED(DEBUG_TWIBUS)
  45. debug(PSTR("address"), adr);
  46. #endif
  47. }
  48. void TWIBus::addbyte(const char c) {
  49. if (this->buffer_s >= COUNT(this->buffer)) return;
  50. this->buffer[this->buffer_s++] = c;
  51. #if ENABLED(DEBUG_TWIBUS)
  52. debug(PSTR("addbyte"), c);
  53. #endif
  54. }
  55. void TWIBus::addbytes(char src[], uint8_t bytes) {
  56. #if ENABLED(DEBUG_TWIBUS)
  57. debug(PSTR("addbytes"), bytes);
  58. #endif
  59. while (bytes--) this->addbyte(*src++);
  60. }
  61. void TWIBus::addstring(char str[]) {
  62. #if ENABLED(DEBUG_TWIBUS)
  63. debug(PSTR("addstring"), str);
  64. #endif
  65. while (char c = *str++) this->addbyte(c);
  66. }
  67. void TWIBus::send() {
  68. #if ENABLED(DEBUG_TWIBUS)
  69. debug(PSTR("send"), this->addr);
  70. #endif
  71. Wire.beginTransmission(this->addr);
  72. Wire.write(this->buffer, this->buffer_s);
  73. Wire.endTransmission();
  74. this->reset();
  75. }
  76. // static
  77. void TWIBus::echoprefix(uint8_t bytes, const char prefix[], uint8_t adr) {
  78. SERIAL_ECHO_START();
  79. serialprintPGM(prefix);
  80. SERIAL_ECHOPAIR(": from:", adr);
  81. SERIAL_ECHOPAIR(" bytes:", bytes);
  82. SERIAL_ECHOPGM (" data:");
  83. }
  84. // static
  85. void TWIBus::echodata(uint8_t bytes, const char prefix[], uint8_t adr) {
  86. echoprefix(bytes, prefix, adr);
  87. while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
  88. SERIAL_EOL();
  89. }
  90. void TWIBus::echobuffer(const char prefix[], uint8_t adr) {
  91. echoprefix(this->buffer_s, prefix, adr);
  92. for (uint8_t i = 0; i < this->buffer_s; i++) SERIAL_CHAR(this->buffer[i]);
  93. SERIAL_EOL();
  94. }
  95. bool TWIBus::request(const uint8_t bytes) {
  96. if (!this->addr) return false;
  97. #if ENABLED(DEBUG_TWIBUS)
  98. debug(PSTR("request"), bytes);
  99. #endif
  100. // requestFrom() is a blocking function
  101. if (Wire.requestFrom(this->addr, bytes) == 0) {
  102. #if ENABLED(DEBUG_TWIBUS)
  103. debug("request fail", this->addr);
  104. #endif
  105. return false;
  106. }
  107. return true;
  108. }
  109. void TWIBus::relay(const uint8_t bytes) {
  110. #if ENABLED(DEBUG_TWIBUS)
  111. debug(PSTR("relay"), bytes);
  112. #endif
  113. if (this->request(bytes))
  114. echodata(bytes, PSTR("i2c-reply"), this->addr);
  115. }
  116. uint8_t TWIBus::capture(char *dst, const uint8_t bytes) {
  117. this->reset();
  118. uint8_t count = 0;
  119. while (count < bytes && Wire.available())
  120. dst[count++] = Wire.read();
  121. #if ENABLED(DEBUG_TWIBUS)
  122. debug(PSTR("capture"), count);
  123. #endif
  124. return count;
  125. }
  126. // static
  127. void TWIBus::flush() {
  128. while (Wire.available()) Wire.read();
  129. }
  130. #if I2C_SLAVE_ADDRESS > 0
  131. void TWIBus::receive(uint8_t bytes) {
  132. #if ENABLED(DEBUG_TWIBUS)
  133. debug(PSTR("receive"), bytes);
  134. #endif
  135. echodata(bytes, PSTR("i2c-receive"), 0);
  136. }
  137. void TWIBus::reply(char str[]/*=NULL*/) {
  138. #if ENABLED(DEBUG_TWIBUS)
  139. debug(PSTR("reply"), str);
  140. #endif
  141. if (str) {
  142. this->reset();
  143. this->addstring(str);
  144. }
  145. Wire.write(this->buffer, this->buffer_s);
  146. this->reset();
  147. }
  148. #endif
  149. #if ENABLED(DEBUG_TWIBUS)
  150. // static
  151. void TWIBus::prefix(const char func[]) {
  152. SERIAL_ECHOPGM("TWIBus::");
  153. serialprintPGM(func);
  154. SERIAL_ECHOPGM(": ");
  155. }
  156. void TWIBus::debug(const char func[], uint32_t adr) {
  157. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(adr); }
  158. }
  159. void TWIBus::debug(const char func[], char c) {
  160. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(c); }
  161. }
  162. void TWIBus::debug(const char func[], char str[]) {
  163. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(str); }
  164. }
  165. #endif
  166. #endif // EXPERIMENTAL_I2CBUS