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

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