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

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