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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 (adr < 8 || adr > 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 (!this->addr) return;
  69. #if ENABLED(DEBUG_TWIBUS)
  70. debug(PSTR("send"), this->addr);
  71. #endif
  72. Wire.beginTransmission(this->addr);
  73. Wire.write(this->buffer, this->buffer_s);
  74. Wire.endTransmission();
  75. this->reset();
  76. }
  77. // static
  78. void TWIBus::echoprefix(uint8_t bytes, const char prefix[], uint8_t adr) {
  79. SERIAL_ECHO_START;
  80. serialprintPGM(prefix);
  81. SERIAL_ECHOPAIR(": from:", adr);
  82. SERIAL_ECHOPAIR(" bytes:", bytes);
  83. SERIAL_ECHOPGM (" data:");
  84. }
  85. // static
  86. void TWIBus::echodata(uint8_t bytes, const char prefix[], uint8_t adr) {
  87. echoprefix(bytes, prefix, adr);
  88. while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
  89. SERIAL_EOL;
  90. }
  91. void TWIBus::echobuffer(const char prefix[], uint8_t adr) {
  92. echoprefix(this->buffer_s, prefix, adr);
  93. for (uint8_t i = 0; i < this->buffer_s; i++) SERIAL_CHAR(this->buffer[i]);
  94. SERIAL_EOL;
  95. }
  96. bool TWIBus::request(const uint8_t bytes) {
  97. if (!this->addr) return false;
  98. #if ENABLED(DEBUG_TWIBUS)
  99. debug(PSTR("request"), bytes);
  100. #endif
  101. // requestFrom() is a blocking function
  102. if (Wire.requestFrom(this->addr, bytes) == 0) {
  103. #if ENABLED(DEBUG_TWIBUS)
  104. debug("request fail", this->addr);
  105. #endif
  106. return false;
  107. }
  108. return true;
  109. }
  110. void TWIBus::relay(const uint8_t bytes) {
  111. #if ENABLED(DEBUG_TWIBUS)
  112. debug(PSTR("relay"), bytes);
  113. #endif
  114. if (this->request(bytes))
  115. echodata(bytes, PSTR("i2c-reply"), this->addr);
  116. }
  117. uint8_t TWIBus::capture(char *dst, const uint8_t bytes) {
  118. this->reset();
  119. uint8_t count = 0;
  120. while (count < bytes && Wire.available())
  121. dst[count++] = Wire.read();
  122. #if ENABLED(DEBUG_TWIBUS)
  123. debug(PSTR("capture"), count);
  124. #endif
  125. return count;
  126. }
  127. // static
  128. void TWIBus::flush() {
  129. while (Wire.available()) Wire.read();
  130. }
  131. #if I2C_SLAVE_ADDRESS > 0
  132. void TWIBus::receive(uint8_t bytes) {
  133. #if ENABLED(DEBUG_TWIBUS)
  134. debug(PSTR("receive"), bytes);
  135. #endif
  136. echodata(bytes, PSTR("i2c-receive"), 0);
  137. }
  138. void TWIBus::reply(char str[]/*=NULL*/) {
  139. #if ENABLED(DEBUG_TWIBUS)
  140. debug(PSTR("reply"), str);
  141. #endif
  142. if (str) {
  143. this->reset();
  144. this->addstring(str);
  145. }
  146. Wire.write(this->buffer, this->buffer_s);
  147. this->reset();
  148. }
  149. #endif
  150. #if ENABLED(DEBUG_TWIBUS)
  151. // static
  152. void TWIBus::prefix(const char func[]) {
  153. SERIAL_ECHOPGM("TWIBus::");
  154. serialprintPGM(func);
  155. SERIAL_ECHOPGM(": ");
  156. }
  157. void TWIBus::debug(const char func[], uint32_t adr) {
  158. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(adr); }
  159. }
  160. void TWIBus::debug(const char func[], char c) {
  161. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(c); }
  162. }
  163. void TWIBus::debug(const char func[], char str[]) {
  164. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(str); }
  165. }
  166. #endif
  167. #endif //EXPERIMENTAL_I2CBUS