My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 <https://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 i2c;
  27. TWIBus::TWIBus() {
  28. #if I2C_SLAVE_ADDRESS == 0
  29. Wire.begin(); // No address joins the BUS as the master
  30. #else
  31. Wire.begin(I2C_SLAVE_ADDRESS); // Join the bus as a slave
  32. #endif
  33. reset();
  34. }
  35. void TWIBus::reset() {
  36. buffer_s = 0;
  37. buffer[0] = 0x00;
  38. }
  39. void TWIBus::address(const uint8_t adr) {
  40. if (!WITHIN(adr, 8, 127)) {
  41. SERIAL_ECHO_MSG("Bad I2C address (8-127)");
  42. }
  43. addr = adr;
  44. debug(PSTR("address"), adr);
  45. }
  46. void TWIBus::addbyte(const char c) {
  47. if (buffer_s >= COUNT(buffer)) return;
  48. buffer[buffer_s++] = c;
  49. debug(PSTR("addbyte"), c);
  50. }
  51. void TWIBus::addbytes(char src[], uint8_t bytes) {
  52. debug(PSTR("addbytes"), bytes);
  53. while (bytes--) addbyte(*src++);
  54. }
  55. void TWIBus::addstring(char str[]) {
  56. debug(PSTR("addstring"), str);
  57. while (char c = *str++) addbyte(c);
  58. }
  59. void TWIBus::send() {
  60. debug(PSTR("send"), addr);
  61. Wire.beginTransmission(I2C_ADDRESS(addr));
  62. Wire.write(buffer, buffer_s);
  63. Wire.endTransmission();
  64. reset();
  65. }
  66. // static
  67. void TWIBus::echoprefix(uint8_t bytes, const char pref[], uint8_t adr) {
  68. SERIAL_ECHO_START();
  69. SERIAL_ECHOPGM_P(pref);
  70. SERIAL_ECHOPGM(": from:", adr, " bytes:", bytes, " data:");
  71. }
  72. // static
  73. void TWIBus::echodata(uint8_t bytes, const char pref[], uint8_t adr) {
  74. echoprefix(bytes, pref, adr);
  75. while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
  76. SERIAL_EOL();
  77. }
  78. void TWIBus::echobuffer(const char pref[], uint8_t adr) {
  79. echoprefix(buffer_s, pref, adr);
  80. LOOP_L_N(i, buffer_s) SERIAL_CHAR(buffer[i]);
  81. SERIAL_EOL();
  82. }
  83. bool TWIBus::request(const uint8_t bytes) {
  84. if (!addr) return false;
  85. debug(PSTR("request"), bytes);
  86. // requestFrom() is a blocking function
  87. if (Wire.requestFrom(I2C_ADDRESS(addr), bytes) == 0) {
  88. debug("request fail", I2C_ADDRESS(addr));
  89. return false;
  90. }
  91. return true;
  92. }
  93. void TWIBus::relay(const uint8_t bytes) {
  94. debug(PSTR("relay"), bytes);
  95. if (request(bytes))
  96. echodata(bytes, PSTR("i2c-reply"), addr);
  97. }
  98. uint8_t TWIBus::capture(char *dst, const uint8_t bytes) {
  99. reset();
  100. uint8_t count = 0;
  101. while (count < bytes && Wire.available())
  102. dst[count++] = Wire.read();
  103. debug(PSTR("capture"), count);
  104. return count;
  105. }
  106. // static
  107. void TWIBus::flush() {
  108. while (Wire.available()) Wire.read();
  109. }
  110. #if I2C_SLAVE_ADDRESS > 0
  111. void TWIBus::receive(uint8_t bytes) {
  112. debug(PSTR("receive"), bytes);
  113. echodata(bytes, PSTR("i2c-receive"), 0);
  114. }
  115. void TWIBus::reply(char str[]/*=nullptr*/) {
  116. debug(PSTR("reply"), str);
  117. if (str) {
  118. reset();
  119. addstring(str);
  120. }
  121. Wire.write(buffer, buffer_s);
  122. reset();
  123. }
  124. void i2c_on_receive(int bytes) { // just echo all bytes received to serial
  125. i2c.receive(bytes);
  126. }
  127. void i2c_on_request() { // just send dummy data for now
  128. i2c.reply("Hello World!\n");
  129. }
  130. #endif
  131. #if ENABLED(DEBUG_TWIBUS)
  132. // static
  133. void TWIBus::prefix(const char func[]) {
  134. SERIAL_ECHOPGM("TWIBus::");
  135. SERIAL_ECHOPGM_P(func);
  136. SERIAL_ECHOPGM(": ");
  137. }
  138. void TWIBus::debug(const char func[], uint32_t adr) {
  139. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(adr); }
  140. }
  141. void TWIBus::debug(const char func[], char c) {
  142. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(c); }
  143. }
  144. void TWIBus::debug(const char func[], char str[]) {
  145. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(str); }
  146. }
  147. #endif
  148. #endif // EXPERIMENTAL_I2CBUS