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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 addr) {
  39. this->addr = addr;
  40. #if ENABLED(DEBUG_TWIBUS)
  41. debug(PSTR("address"), this->addr);
  42. #endif
  43. }
  44. void TWIBus::addbyte(const char c) {
  45. if (buffer_s >= sizeof(this->buffer)) return;
  46. this->buffer[this->buffer_s++] = c;
  47. #if ENABLED(DEBUG_TWIBUS)
  48. debug(PSTR("addbyte"), this->buffer[this->buffer_s - 1]);
  49. #endif
  50. }
  51. void TWIBus::send() {
  52. if (!this->addr) return;
  53. #if ENABLED(DEBUG_TWIBUS)
  54. debug(PSTR("send()"));
  55. #endif
  56. Wire.beginTransmission(this->addr);
  57. Wire.write(this->buffer, this->buffer_s);
  58. Wire.endTransmission();
  59. // Reset the buffer after sending the data
  60. this->reset();
  61. }
  62. void TWIBus::reqbytes(const uint8_t bytes) {
  63. if (!this->addr) return;
  64. #if ENABLED(DEBUG_TWIBUS)
  65. debug(PSTR("reqbytes"), bytes);
  66. #endif
  67. // requestFrom() is a blocking function
  68. millis_t t = millis() + this->timeout;
  69. Wire.requestFrom(this->addr, bytes);
  70. while (Wire.available() < bytes && PENDING(millis(), t)) { /*nada*/ }
  71. this->relaydata(bytes);
  72. // Reset the buffer after sending the data
  73. this->reset();
  74. }
  75. void TWIBus::relaydata(uint8_t bytes) {
  76. SERIAL_ECHO_START;
  77. SERIAL_ECHOPAIR("i2c-reply: from:", this->addr);
  78. SERIAL_ECHOPAIR(" bytes:", bytes);
  79. SERIAL_ECHOPGM (" data:");
  80. while (bytes-- && Wire.available()) SERIAL_CHAR(Wire.read());
  81. SERIAL_EOL;
  82. }
  83. #if ENABLED(DEBUG_TWIBUS)
  84. void TWIBus::debug(const char func[], int32_t val/*=-1*/) {
  85. if (DEBUGGING(INFO)) {
  86. SERIAL_ECHOPGM("TWIBus::");
  87. serialprintPGM(func);
  88. if (val >= 0) SERIAL_ECHOPAIR(": ", val);
  89. SERIAL_EOL;
  90. }
  91. }
  92. #endif
  93. #endif //EXPERIMENTAL_I2CBUS