My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

twibus.h 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #define TWIBUS_BUFFER_SIZE 32
  31. /**
  32. * TWIBUS class
  33. *
  34. * This class implements a wrapper around the two wire (I2C) bus, it allows
  35. * Marlin to send and request data from any slave device on the bus. This is
  36. * an experimental feature and it's inner workings as well as public facing
  37. * interface are prune to change in the future.
  38. *
  39. * The two main consumers of this class are M155 and M156, where M155 allows
  40. * Marlin to send a I2C packet to a device (please be aware that no repeated
  41. * starts are possible), this can be done in caching method by calling multiple
  42. * times M155 B<byte-1 value in base 10> or a one liner M155, have a look at
  43. * the gcode_M155() function for more information. M156 allows Marlin to
  44. * request data from a device, the received data is then relayed into the serial
  45. * line for host interpretation.
  46. *
  47. */
  48. class TWIBus {
  49. private:
  50. /**
  51. * @brief Timeout value in milliseconds
  52. * @details Maximum amount of time (ms) to wait for a reply.
  53. * Useful if something goes wrong on the bus and the
  54. * SDA/SCL lines are held up by another device.
  55. */
  56. const int timeout = 5;
  57. /**
  58. * @brief Number of bytes on buffer
  59. * @description Number of bytes in the buffer waiting to be flushed to the bus
  60. */
  61. uint8_t buffer_s = 0;
  62. /**
  63. * @brief Internal buffer
  64. * @details A fixed buffer. TWI commands can be no longer than this.
  65. */
  66. char buffer[TWIBUS_BUFFER_SIZE];
  67. public:
  68. /**
  69. * @brief Target device address
  70. * @description The target device address. Persists until changed.
  71. */
  72. uint8_t addr = 0;
  73. /**
  74. * @brief Class constructor
  75. * @details Initialize the TWI bus and clear the buffer
  76. */
  77. TWIBus();
  78. /**
  79. * @brief Reset the buffer
  80. * @details Set the buffer to a known-empty state
  81. */
  82. void reset();
  83. /**
  84. * @brief Send the buffer data to the bus
  85. * @details Flush the buffer to the target address
  86. */
  87. void send();
  88. /**
  89. * @brief Add one byte to the buffer
  90. * @details Add a byte to the end of the buffer.
  91. * Silently fails if the buffer is full.
  92. *
  93. * @param c a data byte
  94. */
  95. void addbyte(const char c);
  96. /**
  97. * @brief Add some bytes to the buffer
  98. * @details Add bytes to the end of the buffer.
  99. * Concatenates at the buffer size.
  100. *
  101. * @param src source data address
  102. * @param bytes the number of bytes to add
  103. */
  104. void addbytes(char src[], uint8_t bytes);
  105. /**
  106. * @brief Add a null-terminated string to the buffer
  107. * @details Add bytes to the end of the buffer up to a nul.
  108. * Concatenates at the buffer size.
  109. *
  110. * @param str source string address
  111. */
  112. void addstring(char str[]);
  113. /**
  114. * @brief Set the target slave address
  115. * @details The target slave address for sending the full packet
  116. *
  117. * @param adr 7-bit integer address
  118. */
  119. void address(const uint8_t adr);
  120. /**
  121. * @brief Prefix for echo to serial
  122. * @details Echo a label, length, address, and "data:"
  123. *
  124. * @param bytes the number of bytes to request
  125. */
  126. static void echoprefix(uint8_t bytes, const char prefix[], uint8_t adr);
  127. /**
  128. * @brief Echo data on the bus to serial
  129. * @details Echo some number of bytes from the bus
  130. * to serial in a parser-friendly format.
  131. *
  132. * @param bytes the number of bytes to request
  133. */
  134. static void echodata(uint8_t bytes, const char prefix[], uint8_t adr);
  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(const char 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 until the timeout
  147. * interval expires. Return true on success.
  148. *
  149. * @param bytes the number of bytes to request
  150. * @return status of the request: true=success, false=timeout
  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. *
  171. * @param bytes the number of bytes to request
  172. */
  173. void relay(const uint8_t bytes);
  174. #if I2C_SLAVE_ADDRESS > 0
  175. /**
  176. * @brief Register a slave receive handler
  177. * @details Set a handler to receive data addressed to us
  178. *
  179. * @param handler A function to handle receiving bytes
  180. */
  181. inline void onReceive(const twiReceiveFunc_t handler) { Wire.onReceive(handler); }
  182. /**
  183. * @brief Register a slave request handler
  184. * @details Set a handler to send data requested from us
  185. *
  186. * @param handler A function to handle receiving bytes
  187. */
  188. inline void onRequest(const twiRequestFunc_t handler) { Wire.onRequest(handler); }
  189. /**
  190. * @brief Default handler to receive
  191. * @details Receive bytes sent to our slave address
  192. * and simply echo them to serial.
  193. */
  194. void receive(uint8_t bytes);
  195. /**
  196. * @brief Send a reply to the bus
  197. * @details Send the buffer and clear it.
  198. * If a string is passed, write it into the buffer first.
  199. */
  200. void reply(char str[]=NULL);
  201. inline void reply(const char str[]) { this->reply((char*)str); }
  202. #endif
  203. #if ENABLED(DEBUG_TWIBUS)
  204. /**
  205. * @brief Prints a debug message
  206. * @details Prints a simple debug message "TWIBus::function: value"
  207. */
  208. static void prefix(const char func[]);
  209. static void debug(const char func[], uint32_t adr);
  210. static void debug(const char func[], char c);
  211. static void debug(const char func[], char adr[]);
  212. static inline void debug(const char func[], uint8_t v) { debug(func, (uint32_t)v); }
  213. #endif
  214. };
  215. #endif //TWIBUS_H