My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

twibus.cpp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. Wire.requestFrom(this->addr, bytes);
  103. // Wait for all bytes to arrive
  104. millis_t t = millis() + this->timeout;
  105. while (Wire.available() < bytes)
  106. if (ELAPSED(millis(), t)) {
  107. #if ENABLED(DEBUG_TWIBUS)
  108. SERIAL_ECHO_START;
  109. SERIAL_ECHOLNPGM("i2c timeout");
  110. #endif
  111. return false;
  112. }
  113. return true;
  114. }
  115. void TWIBus::relay(const uint8_t bytes) {
  116. #if ENABLED(DEBUG_TWIBUS)
  117. debug(PSTR("relay"), bytes);
  118. #endif
  119. if (this->request(bytes))
  120. echodata(bytes, PSTR("i2c-reply"), this->addr);
  121. }
  122. uint8_t TWIBus::capture(char *dst, const uint8_t bytes) {
  123. this->reset();
  124. uint8_t count = 0;
  125. while (count < bytes && Wire.available())
  126. dst[count++] = Wire.read();
  127. return count;
  128. }
  129. // static
  130. void TWIBus::flush() {
  131. while (Wire.available()) Wire.read();
  132. }
  133. #if I2C_SLAVE_ADDRESS > 0
  134. void TWIBus::receive(uint8_t bytes) {
  135. #if ENABLED(DEBUG_TWIBUS)
  136. debug(PSTR("receive"), bytes);
  137. #endif
  138. echodata(bytes, PSTR("i2c-receive"), 0);
  139. }
  140. void TWIBus::reply(char str[]/*=NULL*/) {
  141. #if ENABLED(DEBUG_TWIBUS)
  142. debug(PSTR("reply"), str);
  143. #endif
  144. if (str) {
  145. this->reset();
  146. this->addstring(str);
  147. }
  148. Wire.write(this->buffer, this->buffer_s);
  149. this->reset();
  150. }
  151. #endif
  152. #if ENABLED(DEBUG_TWIBUS)
  153. // static
  154. void TWIBus::prefix(const char func[]) {
  155. SERIAL_ECHOPGM("TWIBus::");
  156. serialprintPGM(func);
  157. SERIAL_ECHOPGM(": ");
  158. }
  159. void TWIBus::debug(const char func[], uint32_t adr) {
  160. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(adr); }
  161. }
  162. void TWIBus::debug(const char func[], char c) {
  163. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(c); }
  164. }
  165. void TWIBus::debug(const char func[], char str[]) {
  166. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(str); }
  167. }
  168. #endif
  169. #endif //EXPERIMENTAL_I2CBUS