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

serial.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 __SERIAL_H__
  23. #define __SERIAL_H__
  24. #include "../inc/MarlinConfig.h"
  25. #if HAS_ABL && ENABLED(DEBUG_LEVELING_FEATURE)
  26. #include "../libs/vector_3.h"
  27. #endif
  28. /**
  29. * Define debug bit-masks
  30. */
  31. enum DebugFlags : unsigned char {
  32. DEBUG_NONE = 0,
  33. DEBUG_ECHO = _BV(0), ///< Echo commands in order as they are processed
  34. DEBUG_INFO = _BV(1), ///< Print messages for code that has debug output
  35. DEBUG_ERRORS = _BV(2), ///< Not implemented
  36. DEBUG_DRYRUN = _BV(3), ///< Ignore temperature setting and E movement commands
  37. DEBUG_COMMUNICATION = _BV(4), ///< Not implemented
  38. DEBUG_LEVELING = _BV(5), ///< Print detailed output for homing and leveling
  39. DEBUG_MESH_ADJUST = _BV(6), ///< UBL bed leveling
  40. DEBUG_ALL = 0xFF
  41. };
  42. #if ENABLED(EMERGENCY_PARSER)
  43. enum e_parser_state : char {
  44. state_RESET,
  45. state_N,
  46. state_M,
  47. state_M1,
  48. state_M10,
  49. state_M108,
  50. state_M11,
  51. state_M112,
  52. state_M4,
  53. state_M41,
  54. state_M410,
  55. state_IGNORE // to '\n'
  56. };
  57. #endif
  58. extern uint8_t marlin_debug_flags;
  59. #define DEBUGGING(F) (marlin_debug_flags & (DEBUG_## F))
  60. extern const char echomagic[] PROGMEM;
  61. extern const char errormagic[] PROGMEM;
  62. #if TX_BUFFER_SIZE < 1
  63. #define SERIAL_FLUSHTX_P(p)
  64. #define SERIAL_FLUSHTX()
  65. #endif
  66. #if NUM_SERIAL > 1
  67. #define SERIAL_CHAR_P(p,x) (WITHIN(p, 0, NUM_SERIAL-1) ? (p == 0 ? MYSERIAL0.write(x) : MYSERIAL1.write(x)) : SERIAL_CHAR(x))
  68. #define SERIAL_PROTOCOL_P(p,x) (WITHIN(p, 0, NUM_SERIAL-1) ? (p == 0 ? MYSERIAL0.print(x) : MYSERIAL1.print(x)) : SERIAL_PROTOCOL(x))
  69. #define SERIAL_PROTOCOL_F_P(p,x,y) (WITHIN(p, 0, NUM_SERIAL-1) ? (p == 0 ? MYSERIAL0.print(x,y) : MYSERIAL1.print(x,y)) : SERIAL_PROTOCOL_F(x,y))
  70. #define SERIAL_PROTOCOLLN_P(p,x) (WITHIN(p, 0, NUM_SERIAL-1) ? (p == 0 ? MYSERIAL0.println(x) : MYSERIAL1.println(x)) : SERIAL_PROTOCOLLN(x))
  71. #define SERIAL_PRINT_P(p,x,b) (WITHIN(p, 0, NUM_SERIAL-1) ? (p == 0 ? MYSERIAL0.print(x,b) : MYSERIAL1.print(x,b)) : SERIAL_PRINT(x,b))
  72. #define SERIAL_PRINTLN_P(p,x,b) (WITHIN(p, 0, NUM_SERIAL-1) ? (p == 0 ? MYSERIAL0.println(x,b) : MYSERIAL1.println(x,b)) : SERIAL_PRINTLN(x,b))
  73. #define SERIAL_PRINTF_P(p,args...) (WITHIN(p, 0, NUM_SERIAL-1) ? (p == 0 ? MYSERIAL0.printf(args) : MYSERIAL1.printf(args)) : SERIAL_PRINTF(args))
  74. #define SERIAL_CHAR(x) (MYSERIAL0.write(x), MYSERIAL1.write(x))
  75. #define SERIAL_PROTOCOL(x) (MYSERIAL0.print(x), MYSERIAL1.print(x))
  76. #define SERIAL_PROTOCOL_F(x,y) (MYSERIAL0.print(x,y), MYSERIAL1.print(x,y))
  77. #define SERIAL_PROTOCOLLN(x) (MYSERIAL0.println(x), MYSERIAL1.println(x))
  78. #define SERIAL_PRINT(x,b) (MYSERIAL0.print(x,b), MYSERIAL1.print(x,b))
  79. #define SERIAL_PRINTLN(x,b) (MYSERIAL0.println(x,b), MYSERIAL1.println(x,b))
  80. #define SERIAL_PRINTF(args...) (MYSERIAL0.printf(args), MYSERIAL1.printf(args))
  81. #define SERIAL_FLUSH_P(p) (WITHIN(p, 0, NUM_SERIAL-1) ? (p == 0 ? MYSERIAL0.flush() : MYSERIAL1.flush()) : SERIAL_FLUSH())
  82. #define SERIAL_FLUSH() (MYSERIAL0.flush(), MYSERIAL1.flush())
  83. #if TX_BUFFER_SIZE > 0
  84. #define SERIAL_FLUSHTX_P(p) (WITHIN(p, 0, NUM_SERIAL-1) ? (p == 0 ? MYSERIAL0.flushTX() : MYSERIAL1.flushTX()) : SERIAL_FLUSHTX())
  85. #define SERIAL_FLUSHTX() (MYSERIAL0.flushTX(), MYSERIAL1.flushTX())
  86. #endif
  87. #define SERIAL_EOL_P(p) SERIAL_CHAR_P(p,'\n')
  88. #define SERIAL_PROTOCOLCHAR_P(p,x) SERIAL_CHAR_P(p,x)
  89. #define SERIAL_PROTOCOLPGM_P(p,x) (serialprintPGM_P(p,PSTR(x)))
  90. #define SERIAL_PROTOCOLLNPGM_P(p,x) (serialprintPGM_P(p,PSTR(x "\n")))
  91. #define SERIAL_PROTOCOLPAIR_P(p, pre, value) (serial_echopair_PGM_P(p,PSTR(pre),(value)))
  92. #define SERIAL_PROTOCOLLNPAIR_P(p, pre, value) do { SERIAL_PROTOCOLPAIR_P(p, pre, value); SERIAL_EOL_P(p); } while(0)
  93. #define SERIAL_ECHO_START_P(p) (serialprintPGM_P(p,echomagic))
  94. #define SERIAL_ECHO_P(p,x) SERIAL_PROTOCOL_P(p,x)
  95. #define SERIAL_ECHOPGM_P(p,x) SERIAL_PROTOCOLPGM_P(p,x)
  96. #define SERIAL_ECHOLN_P(p,x) SERIAL_PROTOCOLLN_P(p,x)
  97. #define SERIAL_ECHOLNPGM_P(p,x) SERIAL_PROTOCOLLNPGM_P(p,x)
  98. #define SERIAL_ECHOPAIR_P(p,pre,value) SERIAL_PROTOCOLPAIR_P(p, pre, value)
  99. #define SERIAL_ECHOLNPAIR_P(p,pre, value) SERIAL_PROTOCOLLNPAIR_P(p, pre, value)
  100. #define SERIAL_ECHO_F_P(p,x,y) SERIAL_PROTOCOL_F_P(p,x,y)
  101. #define SERIAL_ERROR_START_P(p) (serialprintPGM_P(p,errormagic))
  102. #define SERIAL_ERROR_P(p,x) SERIAL_PROTOCOL_P(p,x)
  103. #define SERIAL_ERRORPGM_P(p,x) SERIAL_PROTOCOLPGM_P(p,x)
  104. #define SERIAL_ERRORLN_P(p,x) SERIAL_PROTOCOLLN_P(p,x)
  105. #define SERIAL_ERRORLNPGM_P(p,x) SERIAL_PROTOCOLLNPGM_P(p,x)
  106. // These macros compensate for float imprecision
  107. #define SERIAL_PROTOCOLPAIR_F_P(p, pre, value) SERIAL_PROTOCOLPAIR_P(p, pre, FIXFLOAT(value))
  108. #define SERIAL_PROTOCOLLNPAIR_F_P(p, pre, value) SERIAL_PROTOCOLLNPAIR_P(p, pre, FIXFLOAT(value))
  109. #define SERIAL_ECHOPAIR_F_P(p,pre,value) SERIAL_ECHOPAIR_P(p, pre, FIXFLOAT(value))
  110. #define SERIAL_ECHOLNPAIR_F_P(p,pre, value) SERIAL_ECHOLNPAIR_P(p, pre, FIXFLOAT(value))
  111. void serial_echopair_PGM_P(const int8_t p, const char* s_P, const char *v);
  112. void serial_echopair_PGM_P(const int8_t p, const char* s_P, char v);
  113. void serial_echopair_PGM_P(const int8_t p, const char* s_P, int v);
  114. void serial_echopair_PGM_P(const int8_t p, const char* s_P, long v);
  115. void serial_echopair_PGM_P(const int8_t p, const char* s_P, float v);
  116. void serial_echopair_PGM_P(const int8_t p, const char* s_P, double v);
  117. void serial_echopair_PGM_P(const int8_t p, const char* s_P, unsigned int v);
  118. void serial_echopair_PGM_P(const int8_t p, const char* s_P, unsigned long v);
  119. FORCE_INLINE void serial_echopair_PGM_P(const int8_t p, const char* s_P, uint8_t v) { serial_echopair_PGM_P(p, s_P, (int)v); }
  120. FORCE_INLINE void serial_echopair_PGM_P(const int8_t p, const char* s_P, bool v) { serial_echopair_PGM_P(p, s_P, (int)v); }
  121. FORCE_INLINE void serial_echopair_PGM_P(const int8_t p, const char* s_P, void *v) { serial_echopair_PGM_P(p, s_P, (unsigned long)v); }
  122. void serial_spaces_P(const int8_t p, uint8_t count);
  123. #define SERIAL_ECHO_SP_P(p,C) serial_spaces_P(p,C)
  124. #define SERIAL_ERROR_SP_P(p,C) serial_spaces_P(p,C)
  125. #define SERIAL_PROTOCOL_SP_P(p,C) serial_spaces_P(p,C)
  126. void serialprintPGM_P(const int8_t p, const char* str);
  127. #else
  128. #define SERIAL_CHAR_P(p,x) SERIAL_CHAR(x)
  129. #define SERIAL_PROTOCOL_P(p,x) SERIAL_PROTOCOL(x)
  130. #define SERIAL_PROTOCOL_F_P(p,x,y) SERIAL_PROTOCOL_F(x,y)
  131. #define SERIAL_PROTOCOLLN_P(p,x) SERIAL_PROTOCOLLN(x)
  132. #define SERIAL_PRINT_P(p,x,b) SERIAL_PRINT(x,b)
  133. #define SERIAL_PRINTLN_P(p,x,b) SERIAL_PRINTLN(x,b)
  134. #define SERIAL_PRINTF_P(p,args...) SERIAL_PRINTF(args)
  135. #define SERIAL_CHAR(x) MYSERIAL0.write(x)
  136. #define SERIAL_PROTOCOL(x) MYSERIAL0.print(x)
  137. #define SERIAL_PROTOCOL_F(x,y) MYSERIAL0.print(x,y)
  138. #define SERIAL_PROTOCOLLN(x) MYSERIAL0.println(x)
  139. #define SERIAL_PRINT(x,b) MYSERIAL0.print(x,b)
  140. #define SERIAL_PRINTLN(x,b) MYSERIAL0.println(x,b)
  141. #define SERIAL_PRINTF(args...) MYSERIAL0.printf(args)
  142. #define SERIAL_FLUSH_P(p) SERIAL_FLUSH()
  143. #define SERIAL_FLUSH() MYSERIAL0.flush()
  144. #if TX_BUFFER_SIZE > 0
  145. #define SERIAL_FLUSHTX_P(p) SERIAL_FLUSHTX()
  146. #define SERIAL_FLUSHTX() MYSERIAL0.flushTX()
  147. #endif
  148. #define SERIAL_EOL_P(p) SERIAL_EOL()
  149. #define SERIAL_PROTOCOLCHAR_P(p,x) SERIAL_PROTOCOLCHAR(x)
  150. #define SERIAL_PROTOCOLPGM_P(p,x) SERIAL_PROTOCOLPGM(x)
  151. #define SERIAL_PROTOCOLLNPGM_P(p,x) SERIAL_PROTOCOLLNPGM(x)
  152. #define SERIAL_PROTOCOLPAIR_P(p, pre, value) SERIAL_PROTOCOLPAIR(pre, value)
  153. #define SERIAL_PROTOCOLLNPAIR_P(p, pre, value) SERIAL_PROTOCOLLNPAIR(pre, value)
  154. #define SERIAL_ECHO_START_P(p) SERIAL_ECHO_START()
  155. #define SERIAL_ECHO_P(p,x) SERIAL_ECHO(x)
  156. #define SERIAL_ECHOPGM_P(p,x) SERIAL_ECHOPGM(x)
  157. #define SERIAL_ECHOLN_P(p,x) SERIAL_ECHOLN(x)
  158. #define SERIAL_ECHOLNPGM_P(p,x) SERIAL_ECHOLNPGM(x)
  159. #define SERIAL_ECHOPAIR_P(p,pre,value) SERIAL_ECHOPAIR(pre, value)
  160. #define SERIAL_ECHOLNPAIR_P(p,pre, value) SERIAL_ECHOLNPAIR(pre, value)
  161. #define SERIAL_ECHO_F_P(p,x,y) SERIAL_ECHO_F(x,y)
  162. #define SERIAL_ERROR_START_P(p) SERIAL_ERROR_START()
  163. #define SERIAL_ERROR_P(p,x) SERIAL_ERROR(x)
  164. #define SERIAL_ERRORPGM_P(p,x) SERIAL_ERRORPGM(x)
  165. #define SERIAL_ERRORLN_P(p,x) SERIAL_ERRORLN(x)
  166. #define SERIAL_ERRORLNPGM_P(p,x) SERIAL_ERRORLNPGM(x)
  167. // These macros compensate for float imprecision
  168. #define SERIAL_PROTOCOLPAIR_F_P(p, pre, value) SERIAL_PROTOCOLPAIR_F(pre, value)
  169. #define SERIAL_PROTOCOLLNPAIR_F_P(p, pre, value) SERIAL_PROTOCOLLNPAIR_F(pre, value)
  170. #define SERIAL_ECHOPAIR_F_P(p,pre,value) SERIAL_ECHOPAIR_F(pre, value)
  171. #define SERIAL_ECHOLNPAIR_F_P(p,pre, value) SERIAL_ECHOLNPAIR_F(pre, value)
  172. #define serial_echopair_PGM_P(p,s_P,v) serial_echopair_PGM(s_P, v)
  173. #define serial_spaces_P(p,c) serial_spaces(c)
  174. #define SERIAL_ECHO_SP_P(p,C) SERIAL_ECHO_SP(C)
  175. #define SERIAL_ERROR_SP_P(p,C) SERIAL_ERROR_SP(C)
  176. #define SERIAL_PROTOCOL_SP_P(p,C) SERIAL_PROTOCOL_SP(C)
  177. #define serialprintPGM_P(p,s) serialprintPGM(s)
  178. #endif
  179. #define SERIAL_EOL() SERIAL_CHAR('\n')
  180. #define SERIAL_PROTOCOLCHAR(x) SERIAL_CHAR(x)
  181. #define SERIAL_PROTOCOLPGM(x) (serialprintPGM(PSTR(x)))
  182. #define SERIAL_PROTOCOLLNPGM(x) (serialprintPGM(PSTR(x "\n")))
  183. #define SERIAL_PROTOCOLPAIR(pre, value) (serial_echopair_PGM(PSTR(pre), value))
  184. #define SERIAL_PROTOCOLLNPAIR(pre, value) do { SERIAL_PROTOCOLPAIR(pre, value); SERIAL_EOL(); } while(0)
  185. #define SERIAL_ECHO_START() (serialprintPGM(echomagic))
  186. #define SERIAL_ECHO(x) SERIAL_PROTOCOL(x)
  187. #define SERIAL_ECHOPGM(x) SERIAL_PROTOCOLPGM(x)
  188. #define SERIAL_ECHOLN(x) SERIAL_PROTOCOLLN(x)
  189. #define SERIAL_ECHOLNPGM(x) SERIAL_PROTOCOLLNPGM(x)
  190. #define SERIAL_ECHOPAIR(pre,value) SERIAL_PROTOCOLPAIR(pre, value)
  191. #define SERIAL_ECHOLNPAIR(pre, value) SERIAL_PROTOCOLLNPAIR(pre, value)
  192. #define SERIAL_ECHO_F(x,y) SERIAL_PROTOCOL_F(x, y)
  193. #define SERIAL_ERROR_START() (serialprintPGM(errormagic))
  194. #define SERIAL_ERROR(x) SERIAL_PROTOCOL(x)
  195. #define SERIAL_ERRORPGM(x) SERIAL_PROTOCOLPGM(x)
  196. #define SERIAL_ERRORLN(x) SERIAL_PROTOCOLLN(x)
  197. #define SERIAL_ERRORLNPGM(x) SERIAL_PROTOCOLLNPGM(x)
  198. // These macros compensate for float imprecision
  199. #define SERIAL_PROTOCOLPAIR_F(pre, value) SERIAL_PROTOCOLPAIR(pre, FIXFLOAT(value))
  200. #define SERIAL_PROTOCOLLNPAIR_F(pre, value) SERIAL_PROTOCOLLNPAIR(pre, FIXFLOAT(value))
  201. #define SERIAL_ECHOPAIR_F(pre,value) SERIAL_ECHOPAIR(pre, FIXFLOAT(value))
  202. #define SERIAL_ECHOLNPAIR_F(pre, value) SERIAL_ECHOLNPAIR(pre, FIXFLOAT(value))
  203. void serial_echopair_PGM(const char* s_P, const char *v);
  204. void serial_echopair_PGM(const char* s_P, char v);
  205. void serial_echopair_PGM(const char* s_P, int v);
  206. void serial_echopair_PGM(const char* s_P, long v);
  207. void serial_echopair_PGM(const char* s_P, float v);
  208. void serial_echopair_PGM(const char* s_P, double v);
  209. void serial_echopair_PGM(const char* s_P, unsigned int v);
  210. void serial_echopair_PGM(const char* s_P, unsigned long v);
  211. FORCE_INLINE void serial_echopair_PGM(const char* s_P, uint8_t v) { serial_echopair_PGM(s_P, (int)v); }
  212. FORCE_INLINE void serial_echopair_PGM(const char* s_P, bool v) { serial_echopair_PGM(s_P, (int)v); }
  213. FORCE_INLINE void serial_echopair_PGM(const char* s_P, void *v) { serial_echopair_PGM(s_P, (unsigned long)v); }
  214. void serial_spaces(uint8_t count);
  215. #define SERIAL_ECHO_SP(C) serial_spaces(C)
  216. #define SERIAL_ERROR_SP(C) serial_spaces(C)
  217. #define SERIAL_PROTOCOL_SP(C) serial_spaces(C)
  218. //
  219. // Functions for serial printing from PROGMEM. (Saves loads of SRAM.)
  220. //
  221. void serialprintPGM(const char* str);
  222. #if ENABLED(DEBUG_LEVELING_FEATURE)
  223. void print_xyz(const char* prefix, const char* suffix, const float x, const float y, const float z);
  224. void print_xyz(const char* prefix, const char* suffix, const float xyz[]);
  225. #if HAS_ABL
  226. void print_xyz(const char* prefix, const char* suffix, const vector_3 &xyz);
  227. #endif
  228. #define DEBUG_POS(SUFFIX,VAR) do { print_xyz(PSTR(" " STRINGIFY(VAR) "="), PSTR(" : " SUFFIX "\n"), VAR); } while(0)
  229. #endif
  230. #endif // __SERIAL_H__