My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

serial.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 "../inc/MarlinConfig.h"
  24. #include "serial_hook.h"
  25. #if HAS_MEATPACK
  26. #include "../feature/meatpack.h"
  27. #endif
  28. // Commonly-used strings in serial output
  29. extern const char NUL_STR[],
  30. SP_X_STR[], SP_Y_STR[], SP_Z_STR[],
  31. SP_A_STR[], SP_B_STR[], SP_C_STR[], SP_E_STR[],
  32. SP_X_LBL[], SP_Y_LBL[], SP_Z_LBL[], SP_E_LBL[],
  33. SP_I_STR[], SP_J_STR[], SP_K_STR[],
  34. SP_I_LBL[], SP_J_LBL[], SP_K_LBL[],
  35. SP_P_STR[], SP_T_STR[],
  36. X_STR[], Y_STR[], Z_STR[], E_STR[],
  37. I_STR[], J_STR[], K_STR[],
  38. X_LBL[], Y_LBL[], Z_LBL[], E_LBL[],
  39. I_LBL[], J_LBL[], K_LBL[];
  40. //
  41. // Debugging flags for use by M111
  42. //
  43. enum MarlinDebugFlags : uint8_t {
  44. MARLIN_DEBUG_NONE = 0,
  45. MARLIN_DEBUG_ECHO = _BV(0), ///< Echo commands in order as they are processed
  46. MARLIN_DEBUG_INFO = _BV(1), ///< Print messages for code that has debug output
  47. MARLIN_DEBUG_ERRORS = _BV(2), ///< Not implemented
  48. MARLIN_DEBUG_DRYRUN = _BV(3), ///< Ignore temperature setting and E movement commands
  49. MARLIN_DEBUG_COMMUNICATION = _BV(4), ///< Not implemented
  50. #if ENABLED(DEBUG_LEVELING_FEATURE)
  51. MARLIN_DEBUG_LEVELING = _BV(5), ///< Print detailed output for homing and leveling
  52. MARLIN_DEBUG_MESH_ADJUST = _BV(6), ///< UBL bed leveling
  53. #else
  54. MARLIN_DEBUG_LEVELING = 0,
  55. MARLIN_DEBUG_MESH_ADJUST = 0,
  56. #endif
  57. MARLIN_DEBUG_ALL = 0xFF
  58. };
  59. extern uint8_t marlin_debug_flags;
  60. #define DEBUGGING(F) (marlin_debug_flags & (MARLIN_DEBUG_## F))
  61. //
  62. // Serial redirection
  63. //
  64. // Step 1: Find out what the first serial leaf is
  65. #if HAS_MULTI_SERIAL && defined(SERIAL_CATCHALL)
  66. #define _SERIAL_LEAF_1 MYSERIAL
  67. #else
  68. #define _SERIAL_LEAF_1 MYSERIAL1
  69. #endif
  70. // Hook Meatpack if it's enabled on the first leaf
  71. #if ENABLED(MEATPACK_ON_SERIAL_PORT_1)
  72. typedef MeatpackSerial<decltype(_SERIAL_LEAF_1)> SerialLeafT1;
  73. extern SerialLeafT1 mpSerial1;
  74. #define SERIAL_LEAF_1 mpSerial1
  75. #else
  76. #define SERIAL_LEAF_1 _SERIAL_LEAF_1
  77. #endif
  78. // Step 2: For multiserial wrap all serial ports in a single
  79. // interface with the ability to output to multiple serial ports.
  80. #if HAS_MULTI_SERIAL
  81. #define _PORT_REDIRECT(n,p) REMEMBER(n,multiSerial.portMask,p)
  82. #define _PORT_RESTORE(n,p) RESTORE(n)
  83. #define SERIAL_ASSERT(P) if (multiSerial.portMask!=(P)) { debugger(); }
  84. // If we have a catchall, use that directly
  85. #ifdef SERIAL_CATCHALL
  86. #define _SERIAL_LEAF_2 SERIAL_CATCHALL
  87. #elif HAS_ETHERNET
  88. typedef ConditionalSerial<decltype(MYSERIAL2)> SerialLeafT2; // We need to create an instance here
  89. extern SerialLeafT2 msSerial2;
  90. #define _SERIAL_LEAF_2 msSerial2
  91. #else
  92. #define _SERIAL_LEAF_2 MYSERIAL2 // Don't create a useless instance here, directly use the existing instance
  93. #endif
  94. // Nothing complicated here
  95. #define _SERIAL_LEAF_3 MYSERIAL3
  96. // Hook Meatpack if it's enabled on the second leaf
  97. #if ENABLED(MEATPACK_ON_SERIAL_PORT_2)
  98. typedef MeatpackSerial<decltype(_SERIAL_LEAF_2)> SerialLeafT2;
  99. extern SerialLeafT2 mpSerial2;
  100. #define SERIAL_LEAF_2 mpSerial2
  101. #else
  102. #define SERIAL_LEAF_2 _SERIAL_LEAF_2
  103. #endif
  104. // Hook Meatpack if it's enabled on the third leaf
  105. #if ENABLED(MEATPACK_ON_SERIAL_PORT_3)
  106. typedef MeatpackSerial<decltype(_SERIAL_LEAF_3)> SerialLeafT3;
  107. extern SerialLeafT3 mpSerial3;
  108. #define SERIAL_LEAF_3 mpSerial3
  109. #else
  110. #define SERIAL_LEAF_3 _SERIAL_LEAF_3
  111. #endif
  112. #define __S_MULTI(N) decltype(SERIAL_LEAF_##N),
  113. #define _S_MULTI(N) __S_MULTI(N)
  114. typedef MultiSerial< REPEAT_1(NUM_SERIAL, _S_MULTI) 0> SerialOutputT;
  115. #undef __S_MULTI
  116. #undef _S_MULTI
  117. extern SerialOutputT multiSerial;
  118. #define SERIAL_IMPL multiSerial
  119. #else
  120. #define _PORT_REDIRECT(n,p) NOOP
  121. #define _PORT_RESTORE(n) NOOP
  122. #define SERIAL_ASSERT(P) NOOP
  123. #define SERIAL_IMPL SERIAL_LEAF_1
  124. #endif
  125. #define SERIAL_OUT(WHAT, V...) (void)SERIAL_IMPL.WHAT(V)
  126. #define PORT_REDIRECT(p) _PORT_REDIRECT(1,p)
  127. #define PORT_RESTORE() _PORT_RESTORE(1)
  128. #define SERIAL_PORTMASK(P) SerialMask::from(P)
  129. //
  130. // SERIAL_CHAR - Print one or more individual chars
  131. //
  132. inline void SERIAL_CHAR(char a) { SERIAL_IMPL.write(a); }
  133. template <typename ... Args>
  134. void SERIAL_CHAR(char a, Args ... args) { SERIAL_IMPL.write(a); SERIAL_CHAR(args ...); }
  135. /**
  136. * SERIAL_ECHO - Print a single string or value.
  137. * Any numeric parameter (including char) is printed as a base-10 number.
  138. * A string pointer or literal will be output as a string.
  139. *
  140. * NOTE: Use SERIAL_CHAR to print char as a single character.
  141. */
  142. template <typename T>
  143. void SERIAL_ECHO(T x) { SERIAL_IMPL.print(x); }
  144. // Wrapper for ECHO commands to interpret a char
  145. typedef struct SerialChar { char c; SerialChar(char n) : c(n) { } } serial_char_t;
  146. inline void SERIAL_ECHO(serial_char_t x) { SERIAL_IMPL.write(x.c); }
  147. #define AS_CHAR(C) serial_char_t(C)
  148. #define AS_DIGIT(C) AS_CHAR('0' + (C))
  149. template <typename T>
  150. void SERIAL_ECHOLN(T x) { SERIAL_IMPL.println(x); }
  151. // SERIAL_PRINT works like SERIAL_ECHO but also takes the numeric base
  152. template <typename T, typename U>
  153. void SERIAL_PRINT(T x, U y) { SERIAL_IMPL.print(x, y); }
  154. template <typename T>
  155. void SERIAL_PRINTLN(T x, PrintBase y) { SERIAL_IMPL.println(x, y); }
  156. // Flush the serial port
  157. inline void SERIAL_FLUSH() { SERIAL_IMPL.flush(); }
  158. inline void SERIAL_FLUSHTX() { SERIAL_IMPL.flushTX(); }
  159. // Serial echo and error prefixes
  160. #define SERIAL_ECHO_START() serial_echo_start()
  161. #define SERIAL_ERROR_START() serial_error_start()
  162. // Serial end-of-line
  163. #define SERIAL_EOL() SERIAL_CHAR('\n')
  164. // Print a single PROGMEM, PGM_P, or PSTR() string.
  165. void serial_print_P(PGM_P str);
  166. inline void serial_println_P(PGM_P str) { serial_print_P(str); SERIAL_EOL(); }
  167. // Print a single FSTR_P, F(), or FPSTR() string.
  168. inline void serial_print(FSTR_P const fstr) { serial_print_P(FTOP(fstr)); }
  169. inline void serial_println(FSTR_P const fstr) { serial_println_P(FTOP(fstr)); }
  170. //
  171. // SERIAL_ECHOPGM... macros are used to output string-value pairs.
  172. //
  173. // Print up to 20 pairs of values. Odd elements must be literal strings.
  174. #define __SEP_N(N,V...) _SEP_##N(V)
  175. #define _SEP_N(N,V...) __SEP_N(N,V)
  176. #define _SEP_N_REF() _SEP_N
  177. #define _SEP_1(s) serial_print(F(s));
  178. #define _SEP_2(s,v) serial_echopair(F(s),v);
  179. #define _SEP_3(s,v,V...) _SEP_2(s,v); DEFER2(_SEP_N_REF)()(TWO_ARGS(V),V);
  180. #define SERIAL_ECHOPGM(V...) do{ EVAL(_SEP_N(TWO_ARGS(V),V)); }while(0)
  181. // Print up to 20 pairs of values followed by newline. Odd elements must be literal strings.
  182. #define __SELP_N(N,V...) _SELP_##N(V)
  183. #define _SELP_N(N,V...) __SELP_N(N,V)
  184. #define _SELP_N_REF() _SELP_N
  185. #define _SELP_1(s) serial_print(F(s "\n"));
  186. #define _SELP_2(s,v) serial_echolnpair(F(s),v);
  187. #define _SELP_3(s,v,V...) _SEP_2(s,v); DEFER2(_SELP_N_REF)()(TWO_ARGS(V),V);
  188. #define SERIAL_ECHOLNPGM(V...) do{ EVAL(_SELP_N(TWO_ARGS(V),V)); }while(0)
  189. // Print up to 20 pairs of values. Odd elements must be PSTR pointers.
  190. #define __SEP_N_P(N,V...) _SEP_##N##_P(V)
  191. #define _SEP_N_P(N,V...) __SEP_N_P(N,V)
  192. #define _SEP_N_P_REF() _SEP_N_P
  193. #define _SEP_1_P(p) serial_print_P(p);
  194. #define _SEP_2_P(p,v) serial_echopair_P(p,v);
  195. #define _SEP_3_P(p,v,V...) _SEP_2_P(p,v); DEFER2(_SEP_N_P_REF)()(TWO_ARGS(V),V);
  196. #define SERIAL_ECHOPGM_P(V...) do{ EVAL(_SEP_N_P(TWO_ARGS(V),V)); }while(0)
  197. // Print up to 20 pairs of values followed by newline. Odd elements must be PSTR pointers.
  198. #define __SELP_N_P(N,V...) _SELP_##N##_P(V)
  199. #define _SELP_N_P(N,V...) __SELP_N_P(N,V)
  200. #define _SELP_N_P_REF() _SELP_N_P
  201. #define _SELP_1_P(p) serial_println_P(p)
  202. #define _SELP_2_P(p,v) serial_echolnpair_P(p,v)
  203. #define _SELP_3_P(p,v,V...) { _SEP_2_P(p,v); DEFER2(_SELP_N_P_REF)()(TWO_ARGS(V),V); }
  204. #define SERIAL_ECHOLNPGM_P(V...) do{ EVAL(_SELP_N_P(TWO_ARGS(V),V)); }while(0)
  205. // Print up to 20 pairs of values. Odd elements must be FSTR_P, F(), or FPSTR().
  206. #define __SEP_N_F(N,V...) _SEP_##N##_F(V)
  207. #define _SEP_N_F(N,V...) __SEP_N_F(N,V)
  208. #define _SEP_N_F_REF() _SEP_N_F
  209. #define _SEP_1_F(p) serial_print(p);
  210. #define _SEP_2_F(p,v) serial_echopair(p,v);
  211. #define _SEP_3_F(p,v,V...) _SEP_2_F(p,v); DEFER2(_SEP_N_F_REF)()(TWO_ARGS(V),V);
  212. #define SERIAL_ECHOF(V...) do{ EVAL(_SEP_N_F(TWO_ARGS(V),V)); }while(0)
  213. // Print up to 20 pairs of values followed by newline. Odd elements must be FSTR_P, F(), or FPSTR().
  214. #define __SELP_N_F(N,V...) _SELP_##N##_F(V)
  215. #define _SELP_N_F(N,V...) __SELP_N_F(N,V)
  216. #define _SELP_N_F_REF() _SELP_N_F
  217. #define _SELP_1_F(p) serial_println(p)
  218. #define _SELP_2_F(p,v) serial_echolnpair(p,v)
  219. #define _SELP_3_F(p,v,V...) { _SEP_2_F(p,v); DEFER2(_SELP_N_F_REF)()(TWO_ARGS(V),V); }
  220. #define SERIAL_ECHOLNF(V...) do{ EVAL(_SELP_N_F(TWO_ARGS(V),V)); }while(0)
  221. #ifdef AllowDifferentTypeInList
  222. inline void SERIAL_ECHOLIST_IMPL() {}
  223. template <typename T>
  224. void SERIAL_ECHOLIST_IMPL(T && t) { SERIAL_IMPL.print(t); }
  225. template <typename T, typename ... Args>
  226. void SERIAL_ECHOLIST_IMPL(T && t, Args && ... args) {
  227. SERIAL_IMPL.print(t);
  228. serial_print(F(", "));
  229. SERIAL_ECHOLIST_IMPL(args...);
  230. }
  231. template <typename ... Args>
  232. void SERIAL_ECHOLIST(FSTR_P const str, Args && ... args) {
  233. SERIAL_IMPL.print(FTOP(str));
  234. SERIAL_ECHOLIST_IMPL(args...);
  235. }
  236. #else // Optimization if the listed type are all the same (seems to be the case in the codebase so use that instead)
  237. template <typename ... Args>
  238. void SERIAL_ECHOLIST(FSTR_P const fstr, Args && ... args) {
  239. serial_print(fstr);
  240. typename Private::first_type_of<Args...>::type values[] = { args... };
  241. constexpr size_t argsSize = sizeof...(args);
  242. for (size_t i = 0; i < argsSize; i++) {
  243. if (i) serial_print(F(", "));
  244. SERIAL_IMPL.print(values[i]);
  245. }
  246. }
  247. #endif
  248. // SERIAL_ECHO_F prints a floating point value with optional precision
  249. inline void SERIAL_ECHO_F(EnsureDouble x, int digit=2) { SERIAL_IMPL.print(x, digit); }
  250. #define SERIAL_ECHOPAIR_F_P(P,V...) do{ serial_print_P(P); SERIAL_ECHO_F(V); }while(0)
  251. #define SERIAL_ECHOLNPAIR_F_P(P,V...) do{ SERIAL_ECHOPAIR_F_P(P,V); SERIAL_EOL(); }while(0)
  252. #define SERIAL_ECHOPAIR_F_F(S,V...) do{ serial_print(S); SERIAL_ECHO_F(V); }while(0)
  253. #define SERIAL_ECHOLNPAIR_F_F(S,V...) do{ SERIAL_ECHOPAIR_F_F(S,V); SERIAL_EOL(); }while(0)
  254. #define SERIAL_ECHOPAIR_F(S,V...) SERIAL_ECHOPAIR_F_F(F(S),V)
  255. #define SERIAL_ECHOLNPAIR_F(V...) do{ SERIAL_ECHOPAIR_F(V); SERIAL_EOL(); }while(0)
  256. #define SERIAL_ECHO_MSG(V...) do{ SERIAL_ECHO_START(); SERIAL_ECHOLNPGM(V); }while(0)
  257. #define SERIAL_ERROR_MSG(V...) do{ SERIAL_ERROR_START(); SERIAL_ECHOLNPGM(V); }while(0)
  258. #define SERIAL_ECHO_SP(C) serial_spaces(C)
  259. #define SERIAL_ECHO_TERNARY(TF, PRE, ON, OFF, POST) serial_ternary(TF, F(PRE), F(ON), F(OFF), F(POST))
  260. #if SERIAL_FLOAT_PRECISION
  261. #define SERIAL_DECIMAL(V) SERIAL_PRINT(V, SERIAL_FLOAT_PRECISION)
  262. #else
  263. #define SERIAL_DECIMAL(V) SERIAL_ECHO(V)
  264. #endif
  265. //
  266. // Functions for serial printing from PROGMEM. (Saves loads of SRAM.)
  267. //
  268. inline void serial_echopair_P(PGM_P const pstr, serial_char_t v) { serial_print_P(pstr); SERIAL_CHAR(v.c); }
  269. inline void serial_echopair_P(PGM_P const pstr, float v) { serial_print_P(pstr); SERIAL_DECIMAL(v); }
  270. inline void serial_echopair_P(PGM_P const pstr, double v) { serial_print_P(pstr); SERIAL_DECIMAL(v); }
  271. //inline void serial_echopair_P(PGM_P const pstr, const char *v) { serial_print_P(pstr); SERIAL_ECHO(v); }
  272. inline void serial_echopair_P(PGM_P const pstr, FSTR_P v) { serial_print_P(pstr); SERIAL_ECHOF(v); }
  273. // Default implementation for types without a specialization. Handles integers.
  274. template <typename T>
  275. inline void serial_echopair_P(PGM_P const pstr, T v) { serial_print_P(pstr); SERIAL_ECHO(v); }
  276. // Add a newline.
  277. template <typename T>
  278. inline void serial_echolnpair_P(PGM_P const pstr, T v) { serial_echopair_P(pstr, v); SERIAL_EOL(); }
  279. // Catch-all for __FlashStringHelper *
  280. template <typename T>
  281. inline void serial_echopair(FSTR_P const fstr, T v) { serial_echopair_P(FTOP(fstr), v); }
  282. // Add a newline to the serial output
  283. template <typename T>
  284. inline void serial_echolnpair(FSTR_P const fstr, T v) { serial_echolnpair_P(FTOP(fstr), v); }
  285. void serial_echo_start();
  286. void serial_error_start();
  287. void serial_ternary(const bool onoff, FSTR_P const pre, FSTR_P const on, FSTR_P const off, FSTR_P const post=nullptr);
  288. void serialprint_onoff(const bool onoff);
  289. void serialprintln_onoff(const bool onoff);
  290. void serialprint_truefalse(const bool tf);
  291. void serial_spaces(uint8_t count);
  292. void print_bin(const uint16_t val);
  293. void print_pos(LINEAR_AXIS_ARGS(const_float_t), FSTR_P const prefix=nullptr, FSTR_P const suffix=nullptr);
  294. inline void print_pos(const xyz_pos_t &xyz, FSTR_P const prefix=nullptr, FSTR_P const suffix=nullptr) {
  295. print_pos(LINEAR_AXIS_ELEM(xyz), prefix, suffix);
  296. }
  297. #define SERIAL_POS(SUFFIX,VAR) do { print_pos(VAR, F(" " STRINGIFY(VAR) "="), F(" : " SUFFIX "\n")); }while(0)
  298. #define SERIAL_XYZ(PREFIX,V...) do { print_pos(V, F(PREFIX)); }while(0)