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.

queue.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. /**
  23. * queue.cpp - The G-code command queue
  24. */
  25. #include "queue.h"
  26. GCodeQueue queue;
  27. #include "gcode.h"
  28. #include "../lcd/marlinui.h"
  29. #include "../sd/cardreader.h"
  30. #include "../module/planner.h"
  31. #include "../module/temperature.h"
  32. #include "../MarlinCore.h"
  33. #if ENABLED(PRINTER_EVENT_LEDS)
  34. #include "../feature/leds/printer_event_leds.h"
  35. #endif
  36. #if HAS_ETHERNET
  37. #include "../feature/ethernet.h"
  38. #endif
  39. #if ENABLED(BINARY_FILE_TRANSFER)
  40. #include "../feature/binary_stream.h"
  41. #endif
  42. #if ENABLED(POWER_LOSS_RECOVERY)
  43. #include "../feature/powerloss.h"
  44. #endif
  45. #if ENABLED(GCODE_REPEAT_MARKERS)
  46. #include "../feature/repeat.h"
  47. #endif
  48. /**
  49. * GCode line number handling. Hosts may opt to include line numbers when
  50. * sending commands to Marlin, and lines will be checked for sequentiality.
  51. * M110 N<int> sets the current line number.
  52. */
  53. long GCodeQueue::last_N[NUM_SERIAL];
  54. /**
  55. * GCode Command Queue
  56. * A simple ring buffer of BUFSIZE command strings.
  57. *
  58. * Commands are copied into this buffer by the command injectors
  59. * (immediate, serial, sd card) and they are processed sequentially by
  60. * the main loop. The gcode.process_next_command method parses the next
  61. * command and hands off execution to individual handler functions.
  62. */
  63. uint8_t GCodeQueue::length = 0, // Count of commands in the queue
  64. GCodeQueue::index_r = 0, // Ring buffer read position
  65. GCodeQueue::index_w = 0; // Ring buffer write position
  66. char GCodeQueue::command_buffer[BUFSIZE][MAX_CMD_SIZE];
  67. /*
  68. * The port that the command was received on
  69. */
  70. #if HAS_MULTI_SERIAL
  71. int16_t GCodeQueue::port[BUFSIZE];
  72. #endif
  73. /**
  74. * Serial command injection
  75. */
  76. // Number of characters read in the current line of serial input
  77. static int serial_count[NUM_SERIAL] = { 0 };
  78. bool send_ok[BUFSIZE];
  79. /**
  80. * Next Injected PROGMEM Command pointer. (nullptr == empty)
  81. * Internal commands are enqueued ahead of serial / SD commands.
  82. */
  83. PGM_P GCodeQueue::injected_commands_P; // = nullptr
  84. /**
  85. * Injected SRAM Commands
  86. */
  87. char GCodeQueue::injected_commands[64]; // = { 0 }
  88. GCodeQueue::GCodeQueue() {
  89. // Send "ok" after commands by default
  90. LOOP_L_N(i, COUNT(send_ok)) send_ok[i] = true;
  91. }
  92. /**
  93. * Check whether there are any commands yet to be executed
  94. */
  95. bool GCodeQueue::has_commands_queued() {
  96. return queue.length || injected_commands_P || injected_commands[0];
  97. }
  98. /**
  99. * Clear the Marlin command queue
  100. */
  101. void GCodeQueue::clear() {
  102. index_r = index_w = length = 0;
  103. }
  104. /**
  105. * Once a new command is in the ring buffer, call this to commit it
  106. */
  107. void GCodeQueue::_commit_command(bool say_ok
  108. #if HAS_MULTI_SERIAL
  109. , int16_t p/*=-1*/
  110. #endif
  111. ) {
  112. send_ok[index_w] = say_ok;
  113. TERN_(HAS_MULTI_SERIAL, port[index_w] = p);
  114. TERN_(POWER_LOSS_RECOVERY, recovery.commit_sdpos(index_w));
  115. if (++index_w >= BUFSIZE) index_w = 0;
  116. length++;
  117. }
  118. /**
  119. * Copy a command from RAM into the main command buffer.
  120. * Return true if the command was successfully added.
  121. * Return false for a full buffer, or if the 'command' is a comment.
  122. */
  123. bool GCodeQueue::_enqueue(const char* cmd, bool say_ok/*=false*/
  124. #if HAS_MULTI_SERIAL
  125. , int16_t pn/*=-1*/
  126. #endif
  127. ) {
  128. if (*cmd == ';' || length >= BUFSIZE) return false;
  129. strcpy(command_buffer[index_w], cmd);
  130. _commit_command(say_ok
  131. #if HAS_MULTI_SERIAL
  132. , pn
  133. #endif
  134. );
  135. return true;
  136. }
  137. #define ISEOL(C) ((C) == '\n' || (C) == '\r')
  138. /**
  139. * Enqueue with Serial Echo
  140. * Return true if the command was consumed
  141. */
  142. bool GCodeQueue::enqueue_one(const char* cmd) {
  143. //SERIAL_ECHOPGM("enqueue_one(\"");
  144. //SERIAL_ECHO(cmd);
  145. //SERIAL_ECHOPGM("\") \n");
  146. if (*cmd == 0 || ISEOL(*cmd)) return true;
  147. if (_enqueue(cmd)) {
  148. SERIAL_ECHO_MSG(STR_ENQUEUEING, cmd, "\"");
  149. return true;
  150. }
  151. return false;
  152. }
  153. /**
  154. * Process the next "immediate" command from PROGMEM.
  155. * Return 'true' if any commands were processed.
  156. */
  157. bool GCodeQueue::process_injected_command_P() {
  158. if (!injected_commands_P) return false;
  159. char c;
  160. size_t i = 0;
  161. while ((c = pgm_read_byte(&injected_commands_P[i])) && c != '\n') i++;
  162. // Extract current command and move pointer to next command
  163. char cmd[i + 1];
  164. memcpy_P(cmd, injected_commands_P, i);
  165. cmd[i] = '\0';
  166. injected_commands_P = c ? injected_commands_P + i + 1 : nullptr;
  167. // Execute command if non-blank
  168. if (i) {
  169. parser.parse(cmd);
  170. gcode.process_parsed_command();
  171. }
  172. return true;
  173. }
  174. /**
  175. * Process the next "immediate" command from SRAM.
  176. * Return 'true' if any commands were processed.
  177. */
  178. bool GCodeQueue::process_injected_command() {
  179. if (injected_commands[0] == '\0') return false;
  180. char c;
  181. size_t i = 0;
  182. while ((c = injected_commands[i]) && c != '\n') i++;
  183. // Execute a non-blank command
  184. if (i) {
  185. injected_commands[i] = '\0';
  186. parser.parse(injected_commands);
  187. gcode.process_parsed_command();
  188. }
  189. // Copy the next command into place
  190. for (
  191. uint8_t d = 0, s = i + !!c; // dst, src
  192. (injected_commands[d] = injected_commands[s]); // copy, exit if 0
  193. d++, s++ // next dst, src
  194. );
  195. return true;
  196. }
  197. /**
  198. * Enqueue and return only when commands are actually enqueued.
  199. * Never call this from a G-code handler!
  200. */
  201. void GCodeQueue::enqueue_one_now(const char* cmd) { while (!enqueue_one(cmd)) idle(); }
  202. /**
  203. * Attempt to enqueue a single G-code command
  204. * and return 'true' if successful.
  205. */
  206. bool GCodeQueue::enqueue_one_P(PGM_P const pgcode) {
  207. size_t i = 0;
  208. PGM_P p = pgcode;
  209. char c;
  210. while ((c = pgm_read_byte(&p[i])) && c != '\n') i++;
  211. char cmd[i + 1];
  212. memcpy_P(cmd, p, i);
  213. cmd[i] = '\0';
  214. return _enqueue(cmd);
  215. }
  216. /**
  217. * Enqueue from program memory and return only when commands are actually enqueued
  218. * Never call this from a G-code handler!
  219. */
  220. void GCodeQueue::enqueue_now_P(PGM_P const pgcode) {
  221. size_t i = 0;
  222. PGM_P p = pgcode;
  223. for (;;) {
  224. char c;
  225. while ((c = pgm_read_byte(&p[i])) && c != '\n') i++;
  226. char cmd[i + 1];
  227. memcpy_P(cmd, p, i);
  228. cmd[i] = '\0';
  229. enqueue_one_now(cmd);
  230. if (!c) break;
  231. p += i + 1;
  232. }
  233. }
  234. /**
  235. * Send an "ok" message to the host, indicating
  236. * that a command was successfully processed.
  237. *
  238. * If ADVANCED_OK is enabled also include:
  239. * N<int> Line number of the command, if any
  240. * P<int> Planner space remaining
  241. * B<int> Block queue space remaining
  242. */
  243. void GCodeQueue::ok_to_send() {
  244. #if HAS_MULTI_SERIAL
  245. const int16_t pn = command_port();
  246. if (pn < 0) return;
  247. PORT_REDIRECT(pn); // Reply to the serial port that sent the command
  248. #endif
  249. if (!send_ok[index_r]) return;
  250. SERIAL_ECHOPGM(STR_OK);
  251. #if ENABLED(ADVANCED_OK)
  252. char* p = command_buffer[index_r];
  253. if (*p == 'N') {
  254. SERIAL_ECHO(' ');
  255. SERIAL_ECHO(*p++);
  256. while (NUMERIC_SIGNED(*p))
  257. SERIAL_ECHO(*p++);
  258. }
  259. SERIAL_ECHOPAIR_P(SP_P_STR, int(planner.moves_free()),
  260. SP_B_STR, int(BUFSIZE - length));
  261. #endif
  262. SERIAL_EOL();
  263. }
  264. /**
  265. * Send a "Resend: nnn" message to the host to
  266. * indicate that a command needs to be re-sent.
  267. */
  268. void GCodeQueue::flush_and_request_resend() {
  269. const int16_t pn = command_port();
  270. #if HAS_MULTI_SERIAL
  271. if (pn < 0) return;
  272. PORT_REDIRECT(pn); // Reply to the serial port that sent the command
  273. #endif
  274. SERIAL_FLUSH();
  275. SERIAL_ECHOPGM(STR_RESEND);
  276. SERIAL_ECHOLN(last_N[pn] + 1);
  277. ok_to_send();
  278. }
  279. inline bool serial_data_available() {
  280. byte data_available = 0;
  281. if (MYSERIAL0.available()) data_available++;
  282. #ifdef SERIAL_PORT_2
  283. const bool port2_open = TERN1(HAS_ETHERNET, ethernet.have_telnet_client);
  284. if (port2_open && MYSERIAL1.available()) data_available++;
  285. #endif
  286. return data_available > 0;
  287. }
  288. inline int read_serial(const uint8_t index) {
  289. switch (index) {
  290. case 0: return MYSERIAL0.read();
  291. case 1: {
  292. #if HAS_MULTI_SERIAL
  293. const bool port2_open = TERN1(HAS_ETHERNET, ethernet.have_telnet_client);
  294. if (port2_open) return MYSERIAL1.read();
  295. #endif
  296. }
  297. default: return -1;
  298. }
  299. }
  300. void GCodeQueue::gcode_line_error(PGM_P const err, const int8_t pn) {
  301. PORT_REDIRECT(pn); // Reply to the serial port that sent the command
  302. SERIAL_ERROR_START();
  303. serialprintPGM(err);
  304. SERIAL_ECHOLN(last_N[pn]);
  305. while (read_serial(pn) != -1); // Clear out the RX buffer
  306. flush_and_request_resend();
  307. serial_count[pn] = 0;
  308. }
  309. FORCE_INLINE bool is_M29(const char * const cmd) { // matches "M29" & "M29 ", but not "M290", etc
  310. const char * const m29 = strstr_P(cmd, PSTR("M29"));
  311. return m29 && !NUMERIC(m29[3]);
  312. }
  313. #define PS_NORMAL 0
  314. #define PS_EOL 1
  315. #define PS_QUOTED 2
  316. #define PS_PAREN 3
  317. #define PS_ESC 4
  318. inline void process_stream_char(const char c, uint8_t &sis, char (&buff)[MAX_CMD_SIZE], int &ind) {
  319. if (sis == PS_EOL) return; // EOL comment or overflow
  320. #if ENABLED(PAREN_COMMENTS)
  321. else if (sis == PS_PAREN) { // Inline comment
  322. if (c == ')') sis = PS_NORMAL;
  323. return;
  324. }
  325. #endif
  326. else if (sis >= PS_ESC) // End escaped char
  327. sis -= PS_ESC;
  328. else if (c == '\\') { // Start escaped char
  329. sis += PS_ESC;
  330. if (sis == PS_ESC) return; // Keep if quoting
  331. }
  332. #if ENABLED(GCODE_QUOTED_STRINGS)
  333. else if (sis == PS_QUOTED) {
  334. if (c == '"') sis = PS_NORMAL; // End quoted string
  335. }
  336. else if (c == '"') // Start quoted string
  337. sis = PS_QUOTED;
  338. #endif
  339. else if (c == ';') { // Start end-of-line comment
  340. sis = PS_EOL;
  341. return;
  342. }
  343. #if ENABLED(PAREN_COMMENTS)
  344. else if (c == '(') { // Start inline comment
  345. sis = PS_PAREN;
  346. return;
  347. }
  348. #endif
  349. // Backspace erases previous characters
  350. if (c == 0x08) {
  351. if (ind) buff[--ind] = '\0';
  352. }
  353. else {
  354. buff[ind++] = c;
  355. if (ind >= MAX_CMD_SIZE - 1)
  356. sis = PS_EOL; // Skip the rest on overflow
  357. }
  358. }
  359. /**
  360. * Handle a line being completed. For an empty line
  361. * keep sensor readings going and watchdog alive.
  362. */
  363. inline bool process_line_done(uint8_t &sis, char (&buff)[MAX_CMD_SIZE], int &ind) {
  364. sis = PS_NORMAL; // "Normal" Serial Input State
  365. buff[ind] = '\0'; // Of course, I'm a Terminator.
  366. const bool is_empty = (ind == 0); // An empty line?
  367. if (is_empty)
  368. thermalManager.manage_heater(); // Keep sensors satisfied
  369. else
  370. ind = 0; // Start a new line
  371. return is_empty; // Inform the caller
  372. }
  373. /**
  374. * Get all commands waiting on the serial port and queue them.
  375. * Exit when the buffer is full or when no more characters are
  376. * left on the serial port.
  377. */
  378. void GCodeQueue::get_serial_commands() {
  379. static char serial_line_buffer[NUM_SERIAL][MAX_CMD_SIZE];
  380. static uint8_t serial_input_state[NUM_SERIAL] = { PS_NORMAL };
  381. #if ENABLED(BINARY_FILE_TRANSFER)
  382. if (card.flag.binary_mode) {
  383. /**
  384. * For binary stream file transfer, use serial_line_buffer as the working
  385. * receive buffer (which limits the packet size to MAX_CMD_SIZE).
  386. * The receive buffer also limits the packet size for reliable transmission.
  387. */
  388. binaryStream[card.transfer_port_index].receive(serial_line_buffer[card.transfer_port_index]);
  389. return;
  390. }
  391. #endif
  392. // If the command buffer is empty for too long,
  393. // send "wait" to indicate Marlin is still waiting.
  394. #if NO_TIMEOUTS > 0
  395. static millis_t last_command_time = 0;
  396. const millis_t ms = millis();
  397. if (length == 0 && !serial_data_available() && ELAPSED(ms, last_command_time + NO_TIMEOUTS)) {
  398. SERIAL_ECHOLNPGM(STR_WAIT);
  399. last_command_time = ms;
  400. }
  401. #endif
  402. /**
  403. * Loop while serial characters are incoming and the queue is not full
  404. */
  405. while (length < BUFSIZE && serial_data_available()) {
  406. LOOP_L_N(i, NUM_SERIAL) {
  407. const int c = read_serial(i);
  408. if (c < 0) continue;
  409. const char serial_char = c;
  410. if (ISEOL(serial_char)) {
  411. // Reset our state, continue if the line was empty
  412. if (process_line_done(serial_input_state[i], serial_line_buffer[i], serial_count[i]))
  413. continue;
  414. char* command = serial_line_buffer[i];
  415. while (*command == ' ') command++; // Skip leading spaces
  416. char *npos = (*command == 'N') ? command : nullptr; // Require the N parameter to start the line
  417. if (npos) {
  418. const bool M110 = !!strstr_P(command, PSTR("M110"));
  419. if (M110) {
  420. char* n2pos = strchr(command + 4, 'N');
  421. if (n2pos) npos = n2pos;
  422. }
  423. const long gcode_N = strtol(npos + 1, nullptr, 10);
  424. if (gcode_N != last_N[i] + 1 && !M110)
  425. return gcode_line_error(PSTR(STR_ERR_LINE_NO), i);
  426. char *apos = strrchr(command, '*');
  427. if (apos) {
  428. uint8_t checksum = 0, count = uint8_t(apos - command);
  429. while (count) checksum ^= command[--count];
  430. if (strtol(apos + 1, nullptr, 10) != checksum)
  431. return gcode_line_error(PSTR(STR_ERR_CHECKSUM_MISMATCH), i);
  432. }
  433. else
  434. return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), i);
  435. last_N[i] = gcode_N;
  436. }
  437. #if ENABLED(SDSUPPORT)
  438. // Pronterface "M29" and "M29 " has no line number
  439. else if (card.flag.saving && !is_M29(command))
  440. return gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), i);
  441. #endif
  442. //
  443. // Movement commands give an alert when the machine is stopped
  444. //
  445. if (IsStopped()) {
  446. char* gpos = strchr(command, 'G');
  447. if (gpos) {
  448. switch (strtol(gpos + 1, nullptr, 10)) {
  449. case 0: case 1:
  450. #if ENABLED(ARC_SUPPORT)
  451. case 2: case 3:
  452. #endif
  453. #if ENABLED(BEZIER_CURVE_SUPPORT)
  454. case 5:
  455. #endif
  456. PORT_REDIRECT(i); // Reply to the serial port that sent the command
  457. SERIAL_ECHOLNPGM(STR_ERR_STOPPED);
  458. LCD_MESSAGEPGM(MSG_STOPPED);
  459. break;
  460. }
  461. }
  462. }
  463. #if DISABLED(EMERGENCY_PARSER)
  464. // Process critical commands early
  465. if (command[0] == 'M') switch (command[3]) {
  466. case '8': if (command[2] == '0' && command[1] == '1') { wait_for_heatup = false; TERN_(HAS_LCD_MENU, wait_for_user = false); } break;
  467. case '2': if (command[2] == '1' && command[1] == '1') kill(M112_KILL_STR, nullptr, true); break;
  468. case '0': if (command[1] == '4' && command[2] == '1') quickstop_stepper(); break;
  469. }
  470. #endif
  471. #if defined(NO_TIMEOUTS) && NO_TIMEOUTS > 0
  472. last_command_time = ms;
  473. #endif
  474. // Add the command to the queue
  475. _enqueue(serial_line_buffer[i], true
  476. #if HAS_MULTI_SERIAL
  477. , i
  478. #endif
  479. );
  480. }
  481. else
  482. process_stream_char(serial_char, serial_input_state[i], serial_line_buffer[i], serial_count[i]);
  483. } // for NUM_SERIAL
  484. } // queue has space, serial has data
  485. }
  486. #if ENABLED(SDSUPPORT)
  487. /**
  488. * Get lines from the SD Card until the command buffer is full
  489. * or until the end of the file is reached. Because this method
  490. * always receives complete command-lines, they can go directly
  491. * into the main command queue.
  492. */
  493. inline void GCodeQueue::get_sdcard_commands() {
  494. static uint8_t sd_input_state = PS_NORMAL;
  495. if (!IS_SD_PRINTING()) return;
  496. int sd_count = 0;
  497. while (length < BUFSIZE && !card.eof()) {
  498. const int16_t n = card.get();
  499. const bool card_eof = card.eof();
  500. if (n < 0 && !card_eof) { SERIAL_ERROR_MSG(STR_SD_ERR_READ); continue; }
  501. const char sd_char = (char)n;
  502. const bool is_eol = ISEOL(sd_char);
  503. if (is_eol || card_eof) {
  504. // Reset stream state, terminate the buffer, and commit a non-empty command
  505. if (!is_eol && sd_count) ++sd_count; // End of file with no newline
  506. if (!process_line_done(sd_input_state, command_buffer[index_w], sd_count)) {
  507. // M808 S saves the sdpos of the next line. M808 loops to a new sdpos.
  508. TERN_(GCODE_REPEAT_MARKERS, repeat.early_parse_M808(command_buffer[index_w]));
  509. // Put the new command into the buffer (no "ok" sent)
  510. _commit_command(false);
  511. // Prime Power-Loss Recovery for the NEXT _commit_command
  512. TERN_(POWER_LOSS_RECOVERY, recovery.cmd_sdpos = card.getIndex());
  513. }
  514. if (card.eof()) card.fileHasFinished(); // Handle end of file reached
  515. }
  516. else
  517. process_stream_char(sd_char, sd_input_state, command_buffer[index_w], sd_count);
  518. }
  519. }
  520. #endif // SDSUPPORT
  521. /**
  522. * Add to the circular command queue the next command from:
  523. * - The command-injection queues (injected_commands_P, injected_commands)
  524. * - The active serial input (usually USB)
  525. * - The SD card file being actively printed
  526. */
  527. void GCodeQueue::get_available_commands() {
  528. get_serial_commands();
  529. TERN_(SDSUPPORT, get_sdcard_commands());
  530. }
  531. /**
  532. * Get the next command in the queue, optionally log it to SD, then dispatch it
  533. */
  534. void GCodeQueue::advance() {
  535. // Process immediate commands
  536. if (process_injected_command_P() || process_injected_command()) return;
  537. // Return if the G-code buffer is empty
  538. if (!length) return;
  539. #if ENABLED(SDSUPPORT)
  540. if (card.flag.saving) {
  541. char* command = command_buffer[index_r];
  542. if (is_M29(command)) {
  543. // M29 closes the file
  544. card.closefile();
  545. SERIAL_ECHOLNPGM(STR_FILE_SAVED);
  546. #if !defined(__AVR__) || !defined(USBCON)
  547. #if ENABLED(SERIAL_STATS_DROPPED_RX)
  548. SERIAL_ECHOLNPAIR("Dropped bytes: ", MYSERIAL0.dropped());
  549. #endif
  550. #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
  551. SERIAL_ECHOLNPAIR("Max RX Queue Size: ", MYSERIAL0.rxMaxEnqueued());
  552. #endif
  553. #endif
  554. ok_to_send();
  555. }
  556. else {
  557. // Write the string from the read buffer to SD
  558. card.write_command(command);
  559. if (card.flag.logging)
  560. gcode.process_next_command(); // The card is saving because it's logging
  561. else
  562. ok_to_send();
  563. }
  564. }
  565. else
  566. gcode.process_next_command();
  567. #else
  568. gcode.process_next_command();
  569. #endif // SDSUPPORT
  570. // The queue may be reset by a command handler or by code invoked by idle() within a handler
  571. --length;
  572. if (++index_r >= BUFSIZE) index_r = 0;
  573. }