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 13KB

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