My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. #include "../core/macros.h"
  24. #include <Wire.h>
  25. // Print debug messages with M111 S2 (Uses 236 bytes of PROGMEM)
  26. //#define DEBUG_TWIBUS
  27. typedef void (*twiReceiveFunc_t)(int bytes);
  28. typedef void (*twiRequestFunc_t)();
  29. #define TWIBUS_BUFFER_SIZE 32
  30. /**
  31. * TWIBUS class
  32. *
  33. * This class implements a wrapper around the two wire (I2C) bus, allowing
  34. * Marlin to send and request data from any slave device on the bus.
  35. *
  36. * The two main consumers of this class are M260 and M261. M260 provides a way
  37. * to send an I2C packet to a device (no repeated starts) by caching up to 32
  38. * bytes in a buffer and then sending the buffer.
  39. * M261 requests data from a device. The received data is relayed to serial out
  40. * for the host to interpret.
  41. *
  42. * For more information see
  43. * - https://marlinfw.org/docs/gcode/M260.html
  44. * - https://marlinfw.org/docs/gcode/M261.html
  45. */
  46. class TWIBus {
  47. private:
  48. /**
  49. * @brief Number of bytes on buffer
  50. * @description Number of bytes in the buffer waiting to be flushed to the bus
  51. */
  52. uint8_t buffer_s = 0;
  53. /**
  54. * @brief Internal buffer
  55. * @details A fixed buffer. TWI commands can be no longer than this.
  56. */
  57. uint8_t buffer[TWIBUS_BUFFER_SIZE];
  58. public:
  59. /**
  60. * @brief Target device address
  61. * @description The target device address. Persists until changed.
  62. */
  63. uint8_t addr = 0;
  64. /**
  65. * @brief Class constructor
  66. * @details Initialize the TWI bus and clear the buffer
  67. */
  68. TWIBus();
  69. /**
  70. * @brief Reset the buffer
  71. * @details Set the buffer to a known-empty state
  72. */
  73. void reset();
  74. /**
  75. * @brief Send the buffer data to the bus
  76. * @details Flush the buffer to the target address
  77. */
  78. void send();
  79. /**
  80. * @brief Add one byte to the buffer
  81. * @details Add a byte to the end of the buffer.
  82. * Silently fails if the buffer is full.
  83. *
  84. * @param c a data byte
  85. */
  86. void addbyte(const char c);
  87. /**
  88. * @brief Add some bytes to the buffer
  89. * @details Add bytes to the end of the buffer.
  90. * Concatenates at the buffer size.
  91. *
  92. * @param src source data address
  93. * @param bytes the number of bytes to add
  94. */
  95. void addbytes(char src[], uint8_t bytes);
  96. /**
  97. * @brief Add a null-terminated string to the buffer
  98. * @details Add bytes to the end of the buffer up to a nul.
  99. * Concatenates at the buffer size.
  100. *
  101. * @param str source string address
  102. */
  103. void addstring(char str[]);
  104. /**
  105. * @brief Set the target slave address
  106. * @details The target slave address for sending the full packet
  107. *
  108. * @param adr 7-bit integer address
  109. */
  110. void address(const uint8_t adr);
  111. /**
  112. * @brief Prefix for echo to serial
  113. * @details Echo a label, length, address, and "data:"
  114. *
  115. * @param bytes the number of bytes to request
  116. */
  117. static void echoprefix(uint8_t bytes, const char prefix[], uint8_t adr);
  118. /**
  119. * @brief Echo data on the bus to serial
  120. * @details Echo some number of bytes from the bus
  121. * to serial in a parser-friendly format.
  122. *
  123. * @param bytes the number of bytes to request
  124. */
  125. static void echodata(uint8_t bytes, const char prefix[], uint8_t adr);
  126. /**
  127. * @brief Echo data in the buffer to serial
  128. * @details Echo the entire buffer to serial
  129. * to serial in a parser-friendly format.
  130. *
  131. * @param bytes the number of bytes to request
  132. */
  133. void echobuffer(const char prefix[], uint8_t adr);
  134. /**
  135. * @brief Request data from the slave device and wait.
  136. * @details Request a number of bytes from a slave device.
  137. * Wait for the data to arrive, and return true
  138. * on success.
  139. *
  140. * @param bytes the number of bytes to request
  141. * @return status of the request: true=success, false=fail
  142. */
  143. bool request(const uint8_t bytes);
  144. /**
  145. * @brief Capture data from the bus into the buffer.
  146. * @details Capture data after a request has succeeded.
  147. *
  148. * @param bytes the number of bytes to request
  149. * @return the number of bytes captured to the buffer
  150. */
  151. uint8_t capture(char *dst, const uint8_t bytes);
  152. /**
  153. * @brief Flush the i2c bus.
  154. * @details Get all bytes on the bus and throw them away.
  155. */
  156. static void flush();
  157. /**
  158. * @brief Request data from the slave device, echo to serial.
  159. * @details Request a number of bytes from a slave device and output
  160. * the returned data to serial in a parser-friendly format.
  161. *
  162. * @param bytes the number of bytes to request
  163. */
  164. void relay(const uint8_t bytes);
  165. #if I2C_SLAVE_ADDRESS > 0
  166. /**
  167. * @brief Register a slave receive handler
  168. * @details Set a handler to receive data addressed to us
  169. *
  170. * @param handler A function to handle receiving bytes
  171. */
  172. inline void onReceive(const twiReceiveFunc_t handler) { Wire.onReceive(handler); }
  173. /**
  174. * @brief Register a slave request handler
  175. * @details Set a handler to send data requested from us
  176. *
  177. * @param handler A function to handle receiving bytes
  178. */
  179. inline void onRequest(const twiRequestFunc_t handler) { Wire.onRequest(handler); }
  180. /**
  181. * @brief Default handler to receive
  182. * @details Receive bytes sent to our slave address
  183. * and simply echo them to serial.
  184. */
  185. void receive(uint8_t bytes);
  186. /**
  187. * @brief Send a reply to the bus
  188. * @details Send the buffer and clear it.
  189. * If a string is passed, write it into the buffer first.
  190. */
  191. void reply(char str[]=nullptr);
  192. inline void reply(const char str[]) { reply((char*)str); }
  193. #endif
  194. #if ENABLED(DEBUG_TWIBUS)
  195. /**
  196. * @brief Prints a debug message
  197. * @details Prints a simple debug message "TWIBus::function: value"
  198. */
  199. static void prefix(const char func[]);
  200. static void debug(const char func[], uint32_t adr);
  201. static void debug(const char func[], char c);
  202. static void debug(const char func[], char adr[]);
  203. static inline void debug(const char func[], uint8_t v) { debug(func, (uint32_t)v); }
  204. #else
  205. static inline void debug(const char[], uint32_t) {}
  206. static inline void debug(const char[], char) {}
  207. static inline void debug(const char[], char[]) {}
  208. static inline void debug(const char[], uint8_t) {}
  209. #endif
  210. };