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.

serial.h 14KB

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