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.h 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #ifndef TWIBUS_H
  23. #define TWIBUS_H
  24. #include "macros.h"
  25. #include <Wire.h>
  26. // Print debug messages with M111 S2 (Uses 236 bytes of PROGMEM)
  27. //#define DEBUG_TWIBUS
  28. typedef void (*twiReceiveFunc_t)(int bytes);
  29. typedef void (*twiRequestFunc_t)();
  30. /**
  31. * TWIBUS class
  32. *
  33. * This class implements a wrapper around the two wire (I2C) bus, it allows
  34. * Marlin to send and request data from any slave device on the bus. This is
  35. * an experimental feature and it's inner workings as well as public facing
  36. * interface are prune to change in the future.
  37. *
  38. * The two main consumers of this class are M155 and M156, where M155 allows
  39. * Marlin to send a I2C packet to a device (please be aware that no repeated
  40. * starts are possible), this can be done in caching method by calling multiple
  41. * times M155 B<byte-1 value in base 10> or a one liner M155, have a look at
  42. * the gcode_M155() function for more information. M156 allows Marlin to
  43. * request data from a device, the received data is then relayed into the serial
  44. * line for host interpretation.
  45. *
  46. */
  47. class TWIBus {
  48. private:
  49. /**
  50. * @brief Timeout value in milliseconds
  51. * @details Maximum amount of time (ms) to wait for a reply.
  52. * Useful if something goes wrong on the bus and the
  53. * SDA/SCL lines are held up by another device.
  54. */
  55. const int timeout = 5;
  56. /**
  57. * @brief Number of bytes on buffer
  58. * @description Number of bytes in the buffer waiting to be flushed to the bus.
  59. */
  60. uint8_t buffer_s = 0;
  61. /**
  62. * @brief Internal buffer
  63. * @details A fixed buffer. TWI commands can be no longer than this.
  64. */
  65. char buffer[32];
  66. public:
  67. /**
  68. * @brief Target device address
  69. * @description The target device address. Persists until changed.
  70. */
  71. uint8_t addr = 0;
  72. /**
  73. * @brief Class constructor
  74. * @details Initialize the TWI bus and clear the buffer
  75. */
  76. TWIBus();
  77. /**
  78. * @brief Reset the buffer
  79. * @details Set the buffer to a known-empty state
  80. */
  81. void reset();
  82. /**
  83. * @brief Send the buffer data to the bus
  84. * @details Flush the buffer to the bus at the target address.
  85. */
  86. void send();
  87. /**
  88. * @brief Add one byte to the buffer
  89. * @details Add a byte to the end of the buffer.
  90. * Silently fails if the buffer is full.
  91. *
  92. * @param c a data byte
  93. */
  94. void addbyte(const char c);
  95. /**
  96. * @brief Set the target slave address
  97. * @details The target slave address for sending the full packet.
  98. *
  99. * @param addr 7-bit integer address
  100. */
  101. void address(const uint8_t addr);
  102. /**
  103. * @brief Request data from the slave device
  104. * @details Request a number of bytes from a slave device.
  105. * This implementation simply sends the data to serial
  106. * in a parser-friendly format.
  107. *
  108. * @param bytes the number of bytes to request
  109. */
  110. void reqbytes(const uint8_t bytes);
  111. /**
  112. * @brief Relay data from the slave device to serial
  113. * @details Relay a number of bytes from the bus to
  114. * serial in a parser-friendly format.
  115. *
  116. * @param bytes the number of bytes to request
  117. */
  118. void relaydata(uint8_t bytes);
  119. #if I2C_SLAVE_ADDRESS > 0
  120. /**
  121. * @brief Receive bytes (passively)
  122. * @details Receive bytes sent to our slave address.
  123. * and simply echo them to serial.
  124. */
  125. inline void receive(uint8_t bytes) { relaydata(bytes); }
  126. /**
  127. * @brief Register a slave receive handler
  128. * @details Set a handler to receive data addressed to us.
  129. *
  130. * @param handler A function to handle receiving bytes
  131. */
  132. inline void onReceive(const twiReceiveFunc_t handler) { Wire.onReceive(handler); }
  133. /**
  134. * @brief Register a slave request handler
  135. * @details Set a handler to send data requested from us.
  136. *
  137. * @param handler A function to handle receiving bytes
  138. */
  139. inline void onRequest(const twiRequestFunc_t handler) { Wire.onRequest(handler); }
  140. #endif
  141. #if ENABLED(DEBUG_TWIBUS)
  142. /**
  143. * @brief Prints a debug message
  144. * @details Prints a simple debug message "TWIBus::function: value"
  145. */
  146. static void debug(const char func[], int32_t val = -1);
  147. #endif
  148. };
  149. #endif //TWIBUS_H