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

twibus.cpp 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. this->addr = adr;
  40. #if ENABLED(DEBUG_TWIBUS)
  41. debug(PSTR("address"), adr);
  42. #endif
  43. }
  44. void TWIBus::addbyte(const char c) {
  45. if (this->buffer_s >= COUNT(this->buffer)) return;
  46. this->buffer[this->buffer_s++] = c;
  47. #if ENABLED(DEBUG_TWIBUS)
  48. debug(PSTR("addbyte"), c);
  49. #endif
  50. }
  51. void TWIBus::addbytes(char src[], uint8_t bytes) {
  52. #if ENABLED(DEBUG_TWIBUS)
  53. debug(PSTR("addbytes"), bytes);
  54. #endif
  55. while (bytes--) this->addbyte(*src++);
  56. }
  57. void TWIBus::addstring(char str[]) {
  58. #if ENABLED(DEBUG_TWIBUS)
  59. debug(PSTR("addstring"), str);
  60. #endif
  61. while (char c = *str++) this->addbyte(c);
  62. }
  63. void TWIBus::send() {
  64. if (!this->addr) return;
  65. #if ENABLED(DEBUG_TWIBUS)
  66. debug(PSTR("send"), this->addr);
  67. #endif
  68. Wire.beginTransmission(this->addr);
  69. Wire.write(this->buffer, this->buffer_s);
  70. Wire.endTransmission();
  71. this->reset();
  72. }
  73. // static
  74. void TWIBus::echoprefix(uint8_t bytes, const char prefix[], uint8_t adr) {
  75. SERIAL_ECHO_START;
  76. serialprintPGM(prefix);
  77. SERIAL_ECHOPAIR(": from:", adr);
  78. SERIAL_ECHOPAIR(" bytes:", bytes);
  79. SERIAL_ECHOPGM (" data:");
  80. }
  81. // static
  82. void TWIBus::echodata(uint8_t bytes, const char prefix[], uint8_t adr) {
  83. echoprefix(bytes, prefix, adr);
  84. while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
  85. SERIAL_EOL;
  86. }
  87. void TWIBus::echobuffer(const char prefix[], uint8_t adr) {
  88. echoprefix(this->buffer_s, prefix, adr);
  89. for (uint8_t i = 0; i < this->buffer_s; i++) SERIAL_CHAR(this->buffer[i]);
  90. SERIAL_EOL;
  91. }
  92. bool TWIBus::request(const uint8_t bytes) {
  93. if (!this->addr) return false;
  94. #if ENABLED(DEBUG_TWIBUS)
  95. debug(PSTR("request"), bytes);
  96. #endif
  97. // requestFrom() is a blocking function
  98. Wire.requestFrom(this->addr, bytes);
  99. // Wait for all bytes to arrive
  100. millis_t t = millis() + this->timeout;
  101. while (Wire.available() < bytes)
  102. if (ELAPSED(millis(), t)) {
  103. #if ENABLED(DEBUG_TWIBUS)
  104. SERIAL_ECHO_START;
  105. SERIAL_ECHOLNPGM("i2c timeout");
  106. #endif
  107. return false;
  108. }
  109. return true;
  110. }
  111. void TWIBus::relay(const uint8_t bytes) {
  112. #if ENABLED(DEBUG_TWIBUS)
  113. debug(PSTR("relay"), bytes);
  114. #endif
  115. if (this->request(bytes))
  116. echodata(bytes, PSTR("i2c-reply"), this->addr);
  117. }
  118. uint8_t TWIBus::capture(char *dst, const uint8_t bytes) {
  119. this->reset();
  120. uint8_t count = 0;
  121. while (count < bytes && Wire.available())
  122. dst[count++] = Wire.read();
  123. return count;
  124. }
  125. // static
  126. void TWIBus::flush() {
  127. while (Wire.available()) Wire.read();
  128. }
  129. #if I2C_SLAVE_ADDRESS > 0
  130. void TWIBus::receive(uint8_t bytes) {
  131. #if ENABLED(DEBUG_TWIBUS)
  132. debug(PSTR("receive"), bytes);
  133. #endif
  134. echodata(bytes, PSTR("i2c-receive"), 0);
  135. }
  136. void TWIBus::reply(char str[]/*=NULL*/) {
  137. #if ENABLED(DEBUG_TWIBUS)
  138. debug(PSTR("reply"), str);
  139. #endif
  140. if (str) {
  141. this->reset();
  142. this->addstring(str);
  143. }
  144. Wire.write(this->buffer, this->buffer_s);
  145. this->reset();
  146. }
  147. #endif
  148. #if ENABLED(DEBUG_TWIBUS)
  149. // static
  150. void TWIBus::prefix(const char func[]) {
  151. SERIAL_ECHOPGM("TWIBus::");
  152. serialprintPGM(func);
  153. SERIAL_ECHOPGM(": ");
  154. }
  155. void TWIBus::debug(const char func[], uint32_t adr) {
  156. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(adr); }
  157. }
  158. void TWIBus::debug(const char func[], char c) {
  159. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(c); }
  160. }
  161. void TWIBus::debug(const char func[], char str[]) {
  162. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(str); }
  163. }
  164. #endif
  165. #endif //EXPERIMENTAL_I2CBUS