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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. this->addr = adr;
  40. #if ENABLED(DEBUG_TWIBUS)
  41. debug(PSTR("address"), adr);
  42. #endif
  43. }
  44. void TWIBus::addbyte(const char c) {
  45. if (this->buffer_s >= COUNT(this->buffer)) return;
  46. this->buffer[this->buffer_s++] = c;
  47. #if ENABLED(DEBUG_TWIBUS)
  48. debug(PSTR("addbyte"), c);
  49. #endif
  50. }
  51. void TWIBus::addbytes(char src[], uint8_t bytes) {
  52. #if ENABLED(DEBUG_TWIBUS)
  53. debug(PSTR("addbytes"), bytes);
  54. #endif
  55. while (bytes--) this->addbyte(*src++);
  56. }
  57. void TWIBus::addstring(char str[]) {
  58. #if ENABLED(DEBUG_TWIBUS)
  59. debug(PSTR("addstring"), str);
  60. #endif
  61. while (char c = *str++) this->addbyte(c);
  62. }
  63. void TWIBus::send() {
  64. if (!this->addr) return;
  65. #if ENABLED(DEBUG_TWIBUS)
  66. debug(PSTR("send"), this->addr);
  67. #endif
  68. Wire.beginTransmission(this->addr);
  69. Wire.write(this->buffer, this->buffer_s);
  70. Wire.endTransmission();
  71. this->reset();
  72. }
  73. void TWIBus::echodata(uint8_t bytes, const char prefix[], uint8_t adr) {
  74. SERIAL_ECHO_START;
  75. serialprintPGM(prefix);
  76. SERIAL_ECHOPAIR(": from:", adr);
  77. SERIAL_ECHOPAIR(" bytes:", bytes);
  78. SERIAL_ECHOPGM (" data:");
  79. while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
  80. SERIAL_EOL;
  81. }
  82. void TWIBus::reqbytes(const uint8_t bytes) {
  83. if (!this->addr) return;
  84. #if ENABLED(DEBUG_TWIBUS)
  85. debug(PSTR("reqbytes"), bytes);
  86. #endif
  87. // requestFrom() is a blocking function
  88. Wire.requestFrom(this->addr, bytes);
  89. // Wait until all bytes arrive, or timeout
  90. millis_t t = millis() + this->timeout;
  91. while (Wire.available() < bytes && PENDING(millis(), t)) { /*nada*/ }
  92. // Simply echo the data to the bus
  93. this->echodata(bytes, PSTR("i2c-reply"), this->addr);
  94. }
  95. #if I2C_SLAVE_ADDRESS > 0
  96. void TWIBus::receive(uint8_t bytes) {
  97. #if ENABLED(DEBUG_TWIBUS)
  98. debug(PSTR("receive"), bytes);
  99. #endif
  100. this->echodata(bytes, PSTR("i2c-receive"), 0);
  101. }
  102. void TWIBus::reply(char str[]/*=NULL*/) {
  103. #if ENABLED(DEBUG_TWIBUS)
  104. debug(PSTR("reply"), str);
  105. #endif
  106. if (str) {
  107. this->reset();
  108. this->addstring(str);
  109. }
  110. Wire.write(this->buffer, this->buffer_s);
  111. this->reset();
  112. }
  113. #endif
  114. #if ENABLED(DEBUG_TWIBUS)
  115. void TWIBus::prefix(const char func[]) {
  116. SERIAL_ECHOPGM("TWIBus::");
  117. serialprintPGM(func);
  118. SERIAL_ECHOPGM(": ");
  119. }
  120. void TWIBus::debug(const char func[], uint32_t adr) {
  121. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(adr); }
  122. }
  123. void TWIBus::debug(const char func[], char c) {
  124. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(c); }
  125. }
  126. void TWIBus::debug(const char func[], char str[]) {
  127. if (DEBUGGING(INFO)) { prefix(func); SERIAL_ECHOLN(str); }
  128. }
  129. #endif
  130. #endif //EXPERIMENTAL_I2CBUS