My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

twibus.cpp 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. Wire.begin(); // We use no address so we will join the BUS as the master
  28. this->reset();
  29. }
  30. void TWIBus::reset() {
  31. this->addr = 0;
  32. this->buffer_s = 0;
  33. this->buffer[0] = 0x00;
  34. }
  35. void TWIBus::address(uint8_t addr) {
  36. this->addr = addr;
  37. #if ENABLED(DEBUG_TWIBUS)
  38. debug(PSTR("sendto"), this->addr);
  39. #endif
  40. }
  41. void TWIBus::addbyte(char c) {
  42. if (buffer_s >= sizeof(this->buffer)) return;
  43. this->buffer[this->buffer_s++] = c;
  44. #if ENABLED(DEBUG_TWIBUS)
  45. debug(PSTR("addbyte"), this->buffer[this->buffer_s - 1]);
  46. #endif
  47. }
  48. void TWIBus::send() {
  49. if (!this->addr) return;
  50. #if ENABLED(DEBUG_TWIBUS)
  51. debug(PSTR("send()"));
  52. #endif
  53. Wire.beginTransmission(this->addr);
  54. Wire.write(this->buffer, this->buffer_s);
  55. Wire.endTransmission();
  56. // Reset the buffer after sending the data
  57. this->reset();
  58. }
  59. void TWIBus::reqbytes(uint8_t bytes) {
  60. if (!this->addr) return;
  61. #if ENABLED(DEBUG_TWIBUS)
  62. debug(PSTR("reqbytes"), bytes);
  63. #endif
  64. millis_t t = millis() + this->timeout;
  65. Wire.requestFrom(this->addr, bytes);
  66. // requestFrom() is a blocking function
  67. while (Wire.available() < bytes) {
  68. if (ELAPSED(millis(), t)) break;
  69. else continue;
  70. }
  71. SERIAL_ECHO_START;
  72. SERIAL_ECHOPAIR("i2c-reply: from:", this->addr);
  73. SERIAL_ECHOPAIR(" bytes:", Wire.available());
  74. SERIAL_ECHOPGM (" data:");
  75. // Protect against buffer overflows if the number of received bytes
  76. // is less than the number of requested bytes
  77. uint8_t wba = Wire.available();
  78. for (int i = 0; i < wba; i++) SERIAL_CHAR(Wire.read());
  79. SERIAL_EOL;
  80. // Reset the buffer after sending the data
  81. this->reset();
  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