My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

twibus.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 "../inc/MarlinConfig.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. reset();
  33. }
  34. void TWIBus::reset() {
  35. buffer_s = 0;
  36. buffer[0] = 0x00;
  37. }
  38. void TWIBus::address(const uint8_t adr) {
  39. if (!WITHIN(adr, 8, 127)) {
  40. SERIAL_ECHO_MSG("Bad I2C address (8-127)");
  41. }
  42. addr = adr;
  43. #if ENABLED(DEBUG_TWIBUS)
  44. debug(PSTR("address"), adr);
  45. #endif
  46. }
  47. void TWIBus::addbyte(const char c) {
  48. if (buffer_s >= COUNT(buffer)) return;
  49. buffer[buffer_s++] = c;
  50. #if ENABLED(DEBUG_TWIBUS)
  51. debug(PSTR("addbyte"), c);
  52. #endif
  53. }
  54. void TWIBus::addbytes(char src[], uint8_t bytes) {
  55. #if ENABLED(DEBUG_TWIBUS)
  56. debug(PSTR("addbytes"), bytes);
  57. #endif
  58. while (bytes--) addbyte(*src++);
  59. }
  60. void TWIBus::addstring(char str[]) {
  61. #if ENABLED(DEBUG_TWIBUS)
  62. debug(PSTR("addstring"), str);
  63. #endif
  64. while (char c = *str++) addbyte(c);
  65. }
  66. void TWIBus::send() {
  67. #if ENABLED(DEBUG_TWIBUS)
  68. debug(PSTR("send"), addr);
  69. #endif
  70. Wire.beginTransmission(I2C_ADDRESS(addr));
  71. Wire.write(buffer, buffer_s);
  72. Wire.endTransmission();
  73. reset();
  74. }
  75. // static
  76. void TWIBus::echoprefix(uint8_t bytes, const char prefix[], uint8_t adr) {
  77. SERIAL_ECHO_START();
  78. serialprintPGM(prefix);
  79. SERIAL_ECHOPAIR(": from:", adr, " bytes:", bytes, " 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(buffer_s, prefix, adr);
  89. LOOP_L_N(i, buffer_s) SERIAL_CHAR(buffer[i]);
  90. SERIAL_EOL();
  91. }
  92. bool TWIBus::request(const uint8_t bytes) {
  93. if (!addr) return false;
  94. #if ENABLED(DEBUG_TWIBUS)
  95. debug(PSTR("request"), bytes);
  96. #endif
  97. // requestFrom() is a blocking function
  98. if (Wire.requestFrom(addr, bytes) == 0) {
  99. #if ENABLED(DEBUG_TWIBUS)
  100. debug("request fail", addr);
  101. #endif
  102. return false;
  103. }
  104. return true;
  105. }
  106. void TWIBus::relay(const uint8_t bytes) {
  107. #if ENABLED(DEBUG_TWIBUS)
  108. debug(PSTR("relay"), bytes);
  109. #endif
  110. if (request(bytes))
  111. echodata(bytes, PSTR("i2c-reply"), addr);
  112. }
  113. uint8_t TWIBus::capture(char *dst, const uint8_t bytes) {
  114. reset();
  115. uint8_t count = 0;
  116. while (count < bytes && Wire.available())
  117. dst[count++] = Wire.read();
  118. #if ENABLED(DEBUG_TWIBUS)
  119. debug(PSTR("capture"), count);
  120. #endif
  121. return count;
  122. }
  123. // static
  124. void TWIBus::flush() {
  125. while (Wire.available()) Wire.read();
  126. }
  127. #if I2C_SLAVE_ADDRESS > 0
  128. void TWIBus::receive(uint8_t bytes) {
  129. #if ENABLED(DEBUG_TWIBUS)
  130. debug(PSTR("receive"), bytes);
  131. #endif
  132. echodata(bytes, PSTR("i2c-receive"), 0);
  133. }
  134. void TWIBus::reply(char str[]/*=nullptr*/) {
  135. #if ENABLED(DEBUG_TWIBUS)
  136. debug(PSTR("reply"), str);
  137. #endif
  138. if (str) {
  139. reset();
  140. addstring(str);
  141. }
  142. Wire.write(buffer, buffer_s);
  143. reset();
  144. }
  145. #endif
  146. #if ENABLED(DEBUG_TWIBUS)
  147. // static
  148. void TWIBus::prefix(const char func[]) {
  149. SERIAL_ECHOPGM("TWIBus::");
  150. serialprintPGM(func);
  151. SERIAL_ECHOPGM(": ");
  152. }
  153. void TWIBus::debug(const char func[], uint32_t adr) {
  154. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(adr); }
  155. }
  156. void TWIBus::debug(const char func[], char c) {
  157. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(c); }
  158. }
  159. void TWIBus::debug(const char func[], char str[]) {
  160. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(str); }
  161. }
  162. #endif
  163. #endif // EXPERIMENTAL_I2CBUS