My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

twibus.cpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 (DEBUGGING(INFO)) {
  38. SERIAL_ECHOPAIR("TWIBus::sendto: ", this->addr);
  39. SERIAL_EOL;
  40. }
  41. }
  42. void TWIBus::addbyte(char c) {
  43. if (buffer_s >= sizeof(this->buffer)) return;
  44. this->buffer[this->buffer_s++] = c;
  45. if (DEBUGGING(INFO)) {
  46. SERIAL_ECHOPAIR("TWIBus::addbyte: ", this->buffer[this->buffer_s -1]);
  47. SERIAL_EOL;
  48. }
  49. }
  50. void TWIBus::send() {
  51. if (!this->addr) return;
  52. if (DEBUGGING(INFO)) SERIAL_ECHOLNPGM("TWIBus::send()");
  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 (DEBUGGING(INFO)) {
  62. SERIAL_ECHOPAIR("TWIBus::reqbytes(): ", bytes);
  63. SERIAL_EOL;
  64. }
  65. millis_t t = millis() + this->timeout;
  66. Wire.requestFrom(this->addr, bytes);
  67. // requestFrom() is a blocking function
  68. while (Wire.available() < bytes) {
  69. if (ELAPSED(millis(), t)) break;
  70. else continue;
  71. }
  72. SERIAL_ECHO_START;
  73. SERIAL_ECHOPAIR("i2c-reply: from:", this->addr);
  74. SERIAL_ECHOPAIR(" bytes:", Wire.available());
  75. SERIAL_ECHOPGM (" data:");
  76. // Protect against buffer overflows if the number of received bytes
  77. // is less than the number of requested bytes
  78. uint8_t wba = Wire.available();
  79. for (int i = 0; i < wba; i++) SERIAL_CHAR(Wire.read());
  80. SERIAL_EOL;
  81. // Reset the buffer after sending the data
  82. this->reset();
  83. }
  84. #endif //EXPERIMENTAL_I2CBUS