Prusa MINI Firmware overview
serial.h
Go to the documentation of this file.
1 /**
2  * Marlin 3D Printer Firmware
3  * Copyright (c) 2019 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 #pragma once
23 
24 #include "../inc/MarlinConfig.h"
25 
26 /**
27  * Define debug bit-masks
28  */
31  MARLIN_DEBUG_ECHO = _BV(0), ///< Echo commands in order as they are processed
32  MARLIN_DEBUG_INFO = _BV(1), ///< Print messages for code that has debug output
33  MARLIN_DEBUG_ERRORS = _BV(2), ///< Not implemented
34  MARLIN_DEBUG_DRYRUN = _BV(3), ///< Ignore temperature setting and E movement commands
35  MARLIN_DEBUG_COMMUNICATION = _BV(4), ///< Not implemented
36  #if ENABLED(DEBUG_LEVELING_FEATURE)
37  MARLIN_DEBUG_LEVELING = _BV(5), ///< Print detailed output for homing and leveling
38  MARLIN_DEBUG_MESH_ADJUST = _BV(6), ///< UBL bed leveling
39  #else
42  #endif
44 };
45 
47 #define DEBUGGING(F) (marlin_debug_flags & (MARLIN_DEBUG_## F))
48 
49 #define SERIAL_BOTH 0x7F
50 #if NUM_SERIAL > 1
51  extern int8_t serial_port_index;
52  #define _PORT_REDIRECT(n,p) REMEMBER(n,serial_port_index,p)
53  #define _PORT_RESTORE(n) RESTORE(n)
54  #define SERIAL_OUT(WHAT, V...) do{ \
55  if (!serial_port_index || serial_port_index == SERIAL_BOTH) (void)MYSERIAL0.WHAT(V); \
56  if ( serial_port_index) (void)MYSERIAL1.WHAT(V); \
57  }while(0)
58  #define SERIAL_ASSERT(P) if(serial_port_index!=(P)){ debugger(); }
59 #else
60  #define _PORT_REDIRECT(n,p) NOOP
61  #define _PORT_RESTORE(n) NOOP
62  #define SERIAL_OUT(WHAT, V...) (void)MYSERIAL0.WHAT(V)
63  #define SERIAL_ASSERT(P) NOOP
64 #endif
65 
66 #define PORT_REDIRECT(p) _PORT_REDIRECT(1,p)
67 #define PORT_RESTORE() _PORT_RESTORE(1)
68 
69 #define SERIAL_CHAR(x) SERIAL_OUT(write, x)
70 #define SERIAL_ECHO(x) SERIAL_OUT(print, x)
71 #define SERIAL_ECHO_F(V...) SERIAL_OUT(print, V)
72 #define SERIAL_ECHOLN(x) SERIAL_OUT(println, x)
73 #define SERIAL_PRINT(x,b) SERIAL_OUT(print, x, b)
74 #define SERIAL_PRINTLN(x,b) SERIAL_OUT(println, x, b)
75 #define SERIAL_PRINTF(V...) SERIAL_OUT(printf, V)
76 #define SERIAL_FLUSH() SERIAL_OUT(flush)
77 
78 #ifdef __STM32F1__
79  #define SERIAL_FLUSHTX() SERIAL_OUT(flush)
80 #elif TX_BUFFER_SIZE > 0
81  #define SERIAL_FLUSHTX() SERIAL_OUT(flushTX)
82 #else
83  #define SERIAL_FLUSHTX()
84 #endif
85 
86 // Print up to 12 pairs of values
87 #define __SEP_N(N,V...) _SEP_##N(V)
88 #define _SEP_N(N,V...) __SEP_N(N,V)
89 #define _SEP_1(PRE) SERIAL_ECHOPGM(PRE)
90 #define _SEP_2(PRE,V) serial_echopair_PGM(PSTR(PRE),V)
91 #define _SEP_3(a,b,c) do{ _SEP_2(a,b); SERIAL_ECHOPGM(c); }while(0)
92 #define _SEP_4(a,b,V...) do{ _SEP_2(a,b); _SEP_2(V); }while(0)
93 #define _SEP_5(a,b,V...) do{ _SEP_2(a,b); _SEP_3(V); }while(0)
94 #define _SEP_6(a,b,V...) do{ _SEP_2(a,b); _SEP_4(V); }while(0)
95 #define _SEP_7(a,b,V...) do{ _SEP_2(a,b); _SEP_5(V); }while(0)
96 #define _SEP_8(a,b,V...) do{ _SEP_2(a,b); _SEP_6(V); }while(0)
97 #define _SEP_9(a,b,V...) do{ _SEP_2(a,b); _SEP_7(V); }while(0)
98 #define _SEP_10(a,b,V...) do{ _SEP_2(a,b); _SEP_8(V); }while(0)
99 #define _SEP_11(a,b,V...) do{ _SEP_2(a,b); _SEP_9(V); }while(0)
100 #define _SEP_12(a,b,V...) do{ _SEP_2(a,b); _SEP_10(V); }while(0)
101 #define _SEP_13(a,b,V...) do{ _SEP_2(a,b); _SEP_11(V); }while(0)
102 #define _SEP_14(a,b,V...) do{ _SEP_2(a,b); _SEP_12(V); }while(0)
103 #define _SEP_15(a,b,V...) do{ _SEP_2(a,b); _SEP_13(V); }while(0)
104 #define _SEP_16(a,b,V...) do{ _SEP_2(a,b); _SEP_14(V); }while(0)
105 #define _SEP_17(a,b,V...) do{ _SEP_2(a,b); _SEP_15(V); }while(0)
106 #define _SEP_18(a,b,V...) do{ _SEP_2(a,b); _SEP_16(V); }while(0)
107 #define _SEP_19(a,b,V...) do{ _SEP_2(a,b); _SEP_17(V); }while(0)
108 #define _SEP_20(a,b,V...) do{ _SEP_2(a,b); _SEP_18(V); }while(0)
109 #define _SEP_21(a,b,V...) do{ _SEP_2(a,b); _SEP_19(V); }while(0)
110 #define _SEP_22(a,b,V...) do{ _SEP_2(a,b); _SEP_20(V); }while(0)
111 #define _SEP_23(a,b,V...) do{ _SEP_2(a,b); _SEP_21(V); }while(0)
112 #define _SEP_24(a,b,V...) do{ _SEP_2(a,b); _SEP_22(V); }while(0)
113 
114 #define SERIAL_ECHOPAIR(V...) _SEP_N(NUM_ARGS(V),V)
115 
116 // Print up to 12 pairs of values followed by newline
117 #define __SELP_N(N,V...) _SELP_##N(V)
118 #define _SELP_N(N,V...) __SELP_N(N,V)
119 #define _SELP_1(PRE) SERIAL_ECHOLNPGM(PRE)
120 #define _SELP_2(PRE,V) do{ serial_echopair_PGM(PSTR(PRE),V); SERIAL_EOL(); }while(0)
121 #define _SELP_3(a,b,c) do{ _SEP_2(a,b); SERIAL_ECHOLNPGM(c); }while(0)
122 #define _SELP_4(a,b,V...) do{ _SEP_2(a,b); _SELP_2(V); }while(0)
123 #define _SELP_5(a,b,V...) do{ _SEP_2(a,b); _SELP_3(V); }while(0)
124 #define _SELP_6(a,b,V...) do{ _SEP_2(a,b); _SELP_4(V); }while(0)
125 #define _SELP_7(a,b,V...) do{ _SEP_2(a,b); _SELP_5(V); }while(0)
126 #define _SELP_8(a,b,V...) do{ _SEP_2(a,b); _SELP_6(V); }while(0)
127 #define _SELP_9(a,b,V...) do{ _SEP_2(a,b); _SELP_7(V); }while(0)
128 #define _SELP_10(a,b,V...) do{ _SEP_2(a,b); _SELP_8(V); }while(0)
129 #define _SELP_11(a,b,V...) do{ _SEP_2(a,b); _SELP_9(V); }while(0)
130 #define _SELP_12(a,b,V...) do{ _SEP_2(a,b); _SELP_10(V); }while(0)
131 #define _SELP_13(a,b,V...) do{ _SEP_2(a,b); _SELP_11(V); }while(0)
132 #define _SELP_14(a,b,V...) do{ _SEP_2(a,b); _SELP_12(V); }while(0)
133 #define _SELP_15(a,b,V...) do{ _SEP_2(a,b); _SELP_13(V); }while(0)
134 #define _SELP_16(a,b,V...) do{ _SEP_2(a,b); _SELP_14(V); }while(0)
135 #define _SELP_17(a,b,V...) do{ _SEP_2(a,b); _SELP_15(V); }while(0)
136 #define _SELP_18(a,b,V...) do{ _SEP_2(a,b); _SELP_16(V); }while(0)
137 #define _SELP_19(a,b,V...) do{ _SEP_2(a,b); _SELP_17(V); }while(0)
138 #define _SELP_20(a,b,V...) do{ _SEP_2(a,b); _SELP_18(V); }while(0)
139 #define _SELP_21(a,b,V...) do{ _SEP_2(a,b); _SELP_19(V); }while(0)
140 #define _SELP_22(a,b,V...) do{ _SEP_2(a,b); _SELP_20(V); }while(0)
141 #define _SELP_23(a,b,V...) do{ _SEP_2(a,b); _SELP_21(V); }while(0)
142 #define _SELP_24(a,b,V...) do{ _SEP_2(a,b); _SELP_22(V); }while(0) // Use up two, pass the rest up
143 
144 #define SERIAL_ECHOLNPAIR(V...) _SELP_N(NUM_ARGS(V),V)
145 
146 // Print up to 20 comma-separated pairs of values
147 #define __SLST_N(N,V...) _SLST_##N(V)
148 #define _SLST_N(N,V...) __SLST_N(N,V)
149 #define _SLST_1(a) SERIAL_ECHO(a)
150 #define _SLST_2(a,b) do{ SERIAL_ECHO(a); SERIAL_ECHOPAIR(", ",b); }while(0)
151 #define _SLST_3(a,b,c) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_1(c); }while(0)
152 #define _SLST_4(a,b,V...) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_2(V); }while(0)
153 #define _SLST_5(a,b,V...) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_3(V); }while(0)
154 #define _SLST_6(a,b,V...) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_4(V); }while(0)
155 #define _SLST_7(a,b,V...) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_5(V); }while(0)
156 #define _SLST_8(a,b,V...) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_6(V); }while(0)
157 #define _SLST_9(a,b,V...) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_7(V); }while(0)
158 #define _SLST_10(a,b,V...) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_8(V); }while(0)
159 #define _SLST_11(a,b,V...) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_9(V); }while(0)
160 #define _SLST_12(a,b,V...) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_10(V); }while(0)
161 #define _SLST_13(a,b,V...) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_11(V); }while(0)
162 #define _SLST_14(a,b,V...) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_12(V); }while(0)
163 #define _SLST_15(a,b,V...) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_13(V); }while(0)
164 #define _SLST_16(a,b,V...) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_14(V); }while(0)
165 #define _SLST_17(a,b,V...) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_15(V); }while(0)
166 #define _SLST_18(a,b,V...) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_16(V); }while(0)
167 #define _SLST_19(a,b,V...) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_17(V); }while(0)
168 #define _SLST_20(a,b,V...) do{ SERIAL_ECHO(a); _SEP_2(", ",b); _SLST_18(V); }while(0) // Use up two, pass the rest up
169 
170 #define SERIAL_ECHOLIST(pre,V...) do{ SERIAL_ECHOPGM(pre); _SLST_N(NUM_ARGS(V),V); }while(0)
171 #define SERIAL_ECHOLIST_N(N,V...) _SLST_N(N,LIST_N(N,V))
172 
173 #define SERIAL_ECHOPGM(S) (serialprintPGM(PSTR(S)))
174 #define SERIAL_ECHOLNPGM(S) (serialprintPGM(PSTR(S "\n")))
175 
176 #define SERIAL_ECHOPAIR_F(S,V...) do{ SERIAL_ECHOPGM(S); SERIAL_ECHO_F(V); }while(0)
177 #define SERIAL_ECHOLNPAIR_F(V...) do{ SERIAL_ECHOPAIR_F(V); SERIAL_EOL(); }while(0)
178 
179 #define SERIAL_ECHO_START() serial_echo_start()
180 #define SERIAL_ERROR_START() serial_error_start()
181 #define SERIAL_EOL() SERIAL_CHAR('\n')
182 
183 #define SERIAL_ECHO_MSG(S) do{ SERIAL_ECHO_START(); SERIAL_ECHOLNPGM(S); }while(0)
184 #define SERIAL_ERROR_MSG(S) do{ SERIAL_ERROR_START(); SERIAL_ECHOLNPGM(S); }while(0)
185 
186 #define SERIAL_ECHO_SP(C) serial_spaces(C)
187 
188 #define SERIAL_ECHO_TERNARY(TF, PRE, ON, OFF, POST) serial_ternary(TF, PSTR(PRE), PSTR(ON), PSTR(OFF), PSTR(POST))
189 
190 //
191 // Functions for serial printing from PROGMEM. (Saves loads of SRAM.)
192 //
193 void serial_echopair_PGM(PGM_P const s_P, const char *v);
194 void serial_echopair_PGM(PGM_P const s_P, char v);
195 void serial_echopair_PGM(PGM_P const s_P, int v);
196 void serial_echopair_PGM(PGM_P const s_P, long v);
197 void serial_echopair_PGM(PGM_P const s_P, float v);
198 void serial_echopair_PGM(PGM_P const s_P, double v);
199 void serial_echopair_PGM(PGM_P const s_P, unsigned int v);
200 void serial_echopair_PGM(PGM_P const s_P, unsigned long v);
201 inline void serial_echopair_PGM(PGM_P const s_P, uint8_t v) { serial_echopair_PGM(s_P, (int)v); }
202 inline void serial_echopair_PGM(PGM_P const s_P, bool v) { serial_echopair_PGM(s_P, (int)v); }
203 inline void serial_echopair_PGM(PGM_P const s_P, void *v) { serial_echopair_PGM(s_P, (unsigned long)v); }
204 
205 void serialprintPGM(PGM_P str);
206 void serial_echo_start();
207 void serial_error_start();
208 void serial_ternary(const bool onoff, PGM_P const pre, PGM_P const on, PGM_P const off, PGM_P const post=nullptr);
209 void serialprint_onoff(const bool onoff);
210 void serialprintln_onoff(const bool onoff);
211 void serialprint_truefalse(const bool tf);
212 void serial_spaces(uint8_t count);
213 
214 void print_bin(const uint16_t val);
215 
216 void print_xyz(const float &x, const float &y, const float &z, PGM_P const prefix=nullptr, PGM_P const suffix=nullptr);
217 
218 inline void print_xyz(const xyz_pos_t &xyz, PGM_P const prefix=nullptr, PGM_P const suffix=nullptr) {
219  print_xyz(xyz.x, xyz.y, xyz.z, prefix, suffix);
220 }
221 
222 #define SERIAL_POS(SUFFIX,VAR) do { print_xyz(VAR, PSTR(" " STRINGIFY(VAR) "="), PSTR(" : " SUFFIX "\n")); }while(0)
223 #define SERIAL_XYZ(PREFIX,V...) do { print_xyz(V, PSTR(PREFIX), nullptr); }while(0)
XYZval::z
T z
Definition: types.h:286
SERIAL_CHAR
#define SERIAL_CHAR(x)
Definition: serial.h:69
serial_echo_start
void serial_echo_start()
Definition: serial.cpp:38
MARLIN_DEBUG_ALL
Definition: serial.h:43
serialprint_onoff
void serialprint_onoff(const bool onoff)
Definition: serial.cpp:57
serial_echopair_PGM
void serial_echopair_PGM(PGM_P const s_P, const char *v)
Definition: serial.cpp:41
SERIAL_ECHO
#define SERIAL_ECHO(x)
Definition: serial.h:70
marlin_debug_flags
uint8_t marlin_debug_flags
Definition: serial.cpp:26
print_xyz
void print_xyz(const float &x, const float &y, const float &z, PGM_P const prefix, PGM_P const suffix)
Definition: serial.cpp:70
MarlinDebugFlags
MarlinDebugFlags
Definition: serial.h:29
XYZval::x
T x
Definition: types.h:286
MSG_X
#define MSG_X
Definition: language.h:319
serial.h
serial_echo_start
void serial_echo_start()
Definition: serial.cpp:38
serialprintln_onoff
void serialprintln_onoff(const bool onoff)
Definition: serial.cpp:58
SERIAL_ECHOPAIR
#define SERIAL_ECHOPAIR(V...)
Definition: serial.h:114
MARLIN_DEBUG_NONE
Definition: serial.h:30
MARLIN_DEBUG_LEVELING
Definition: serial.h:40
PGM_P
#define PGM_P
Definition: pgmspace.h:30
SERIAL_PORT
#define SERIAL_PORT
Definition: Configuration_A3ides_2209_MINI.h:105
i
uint8_t i
Definition: screen_test_graph.c:72
MARLIN_DEBUG_COMMUNICATION
Not implemented.
Definition: serial.h:35
serial_error_start
void serial_error_start()
Definition: serial.cpp:39
pgm_read_byte
#define pgm_read_byte(addr)
Definition: pgmspace.h:95
PSTR
#define PSTR(str)
Definition: pgmspace.h:31
MARLIN_DEBUG_INFO
Print messages for code that has debug output.
Definition: serial.h:32
serialprintln_onoff
void serialprintln_onoff(const bool onoff)
Definition: serial.cpp:58
MSG_Y
#define MSG_Y
Definition: language.h:320
language.h
MARLIN_DEBUG_DRYRUN
Ignore temperature setting and E movement commands.
Definition: serial.h:34
print_bin
void print_bin(const uint16_t val)
Definition: serial.cpp:61
MSG_Z
#define MSG_Z
Definition: language.h:321
MSG_ON
#define MSG_ON
Definition: language.h:213
marlin_debug_flags
uint8_t marlin_debug_flags
Definition: serial.cpp:26
serialprint_truefalse
void serialprint_truefalse(const bool tf)
Definition: serial.cpp:59
uint8_t
const uint8_t[]
Definition: 404_html.c:3
MARLIN_DEBUG_ERRORS
Not implemented.
Definition: serial.h:33
serial_ternary
void serial_ternary(const bool onoff, PGM_P const pre, PGM_P const on, PGM_P const off, PGM_P const post)
Definition: serial.cpp:52
_BV
#define _BV(bit)
Definition: wiring_constants.h:99
PROPORTIONAL_FONT_RATIO
#define PROPORTIONAL_FONT_RATIO
Definition: Configuration_A3ides_2209_MINI_adv.h:2044
XYZval::y
T y
Definition: types.h:286
print_bin
void print_bin(const uint16_t val)
Definition: serial.cpp:61
serial_spaces
void serial_spaces(uint8_t count)
Definition: serial.cpp:50
MARLIN_DEBUG_ECHO
Echo commands in order as they are processed.
Definition: serial.h:31
print_xyz
void print_xyz(const float &x, const float &y, const float &z, PGM_P const prefix=nullptr, PGM_P const suffix=nullptr)
Definition: serial.cpp:70
serialprintPGM
void serialprintPGM(PGM_P str)
Definition: serial.cpp:35
serial_error_start
void serial_error_start()
Definition: serial.cpp:39
SERIAL_EOL
#define SERIAL_EOL()
Definition: serial.h:181
serialprint_onoff
void serialprint_onoff(const bool onoff)
Definition: serial.cpp:57
MSG_OFF
#define MSG_OFF
Definition: language.h:214
serialprint_truefalse
void serialprint_truefalse(const bool tf)
Definition: serial.cpp:59
XYZval< float >
serial_ternary
void serial_ternary(const bool onoff, PGM_P const pre, PGM_P const on, PGM_P const off, PGM_P const post=nullptr)
Definition: serial.cpp:52
MARLIN_DEBUG_MESH_ADJUST
Definition: serial.h:41
serialprintPGM
void serialprintPGM(PGM_P str)
Definition: serial.cpp:35
serial_spaces
void serial_spaces(uint8_t count)
Definition: serial.cpp:50
PROGMEM
static const char errormagic[] PROGMEM
Definition: serial.cpp:28
serial_echopair_PGM
void serial_echopair_PGM(PGM_P const s_P, const char *v)
Definition: serial.cpp:41