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

twibus.h 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. /**
  30. * For a light i2c protocol that runs on two boards running Marlin see:
  31. * See https://github.com/MarlinFirmware/Marlin/issues/4776#issuecomment-246262879
  32. */
  33. #if I2C_SLAVE_ADDRESS > 0
  34. void i2c_on_receive(int bytes); // Demo i2c onReceive handler
  35. void i2c_on_request(); // Demo i2c onRequest handler
  36. #endif
  37. #define TWIBUS_BUFFER_SIZE 32
  38. /**
  39. * TWIBUS class
  40. *
  41. * This class implements a wrapper around the two wire (I2C) bus, allowing
  42. * Marlin to send and request data from any slave device on the bus.
  43. *
  44. * The two main consumers of this class are M260 and M261. M260 provides a way
  45. * to send an I2C packet to a device (no repeated starts) by caching up to 32
  46. * bytes in a buffer and then sending the buffer.
  47. * M261 requests data from a device. The received data is relayed to serial out
  48. * for the host to interpret.
  49. *
  50. * For more information see
  51. * - https://marlinfw.org/docs/gcode/M260.html
  52. * - https://marlinfw.org/docs/gcode/M261.html
  53. */
  54. class TWIBus {
  55. private:
  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. uint8_t buffer[TWIBUS_BUFFER_SIZE];
  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 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 Add some bytes to the buffer
  97. * @details Add bytes to the end of the buffer.
  98. * Concatenates at the buffer size.
  99. *
  100. * @param src source data address
  101. * @param bytes the number of bytes to add
  102. */
  103. void addbytes(char src[], uint8_t bytes);
  104. /**
  105. * @brief Add a null-terminated string to the buffer
  106. * @details Add bytes to the end of the buffer up to a nul.
  107. * Concatenates at the buffer size.
  108. *
  109. * @param str source string address
  110. */
  111. void addstring(char str[]);
  112. /**
  113. * @brief Set the target slave address
  114. * @details The target slave address for sending the full packet
  115. *
  116. * @param adr 7-bit integer address
  117. */
  118. void address(const uint8_t adr);
  119. /**
  120. * @brief Prefix for echo to serial
  121. * @details Echo a label, length, address, and "data:"
  122. *
  123. * @param bytes the number of bytes to request
  124. */
  125. static void echoprefix(uint8_t bytes, FSTR_P const prefix, uint8_t adr);
  126. /**
  127. * @brief Echo data on the bus to serial
  128. * @details Echo some number of bytes from the bus
  129. * to serial in a parser-friendly format.
  130. *
  131. * @param bytes the number of bytes to request
  132. * @param style Output format for the bytes, 0 = Raw byte [default], 1 = Hex characters, 2 = uint16_t
  133. */
  134. static void echodata(uint8_t bytes, FSTR_P const prefix, uint8_t adr, const uint8_t style=0);
  135. /**
  136. * @brief Echo data in the buffer to serial
  137. * @details Echo the entire buffer to serial
  138. * to serial in a parser-friendly format.
  139. *
  140. * @param bytes the number of bytes to request
  141. */
  142. void echobuffer(FSTR_P const prefix, uint8_t adr);
  143. /**
  144. * @brief Request data from the slave device and wait.
  145. * @details Request a number of bytes from a slave device.
  146. * Wait for the data to arrive, and return true
  147. * on success.
  148. *
  149. * @param bytes the number of bytes to request
  150. * @return status of the request: true=success, false=fail
  151. */
  152. bool request(const uint8_t bytes);
  153. /**
  154. * @brief Capture data from the bus into the buffer.
  155. * @details Capture data after a request has succeeded.
  156. *
  157. * @param bytes the number of bytes to request
  158. * @return the number of bytes captured to the buffer
  159. */
  160. uint8_t capture(char *dst, const uint8_t bytes);
  161. /**
  162. * @brief Flush the i2c bus.
  163. * @details Get all bytes on the bus and throw them away.
  164. */
  165. static void flush();
  166. /**
  167. * @brief Request data from the slave device, echo to serial.
  168. * @details Request a number of bytes from a slave device and output
  169. * the returned data to serial in a parser-friendly format.
  170. * @style Output format for the bytes, 0 = raw byte [default], 1 = Hex characters, 2 = uint16_t
  171. *
  172. * @param bytes the number of bytes to request
  173. */
  174. void relay(const uint8_t bytes, const uint8_t style=0);
  175. #if I2C_SLAVE_ADDRESS > 0
  176. /**
  177. * @brief Register a slave receive handler
  178. * @details Set a handler to receive data addressed to us
  179. *
  180. * @param handler A function to handle receiving bytes
  181. */
  182. inline void onReceive(const twiReceiveFunc_t handler) { Wire.onReceive(handler); }
  183. /**
  184. * @brief Register a slave request handler
  185. * @details Set a handler to send data requested from us
  186. *
  187. * @param handler A function to handle receiving bytes
  188. */
  189. inline void onRequest(const twiRequestFunc_t handler) { Wire.onRequest(handler); }
  190. /**
  191. * @brief Default handler to receive
  192. * @details Receive bytes sent to our slave address
  193. * and simply echo them to serial.
  194. */
  195. void receive(uint8_t bytes);
  196. /**
  197. * @brief Send a reply to the bus
  198. * @details Send the buffer and clear it.
  199. * If a string is passed, write it into the buffer first.
  200. */
  201. void reply(char str[]=nullptr);
  202. inline void reply(const char str[]) { reply((char*)str); }
  203. #endif
  204. #if ENABLED(DEBUG_TWIBUS)
  205. /**
  206. * @brief Prints a debug message
  207. * @details Prints a simple debug message "TWIBus::function: value"
  208. */
  209. static void prefix(FSTR_P const func);
  210. static void debug(FSTR_P const func, uint32_t adr);
  211. static void debug(FSTR_P const func, char c);
  212. static void debug(FSTR_P const func, char adr[]);
  213. #else
  214. static void debug(FSTR_P const, uint32_t) {}
  215. static void debug(FSTR_P const, char) {}
  216. static void debug(FSTR_P const, char[]) {}
  217. #endif
  218. static void debug(FSTR_P const func, uint8_t v) { debug(func, (uint32_t)v); }
  219. };
  220. extern TWIBus i2c;