My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

queue.cpp 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. /**
  23. * queue.cpp - The G-code command queue
  24. */
  25. #include "queue.h"
  26. #include "gcode.h"
  27. #include "../lcd/ultralcd.h"
  28. #include "../sd/cardreader.h"
  29. #include "../module/planner.h"
  30. #include "../module/temperature.h"
  31. #include "../Marlin.h"
  32. #if HAS_COLOR_LEDS
  33. #include "../feature/leds/leds.h"
  34. #endif
  35. #if ENABLED(POWER_LOSS_RECOVERY)
  36. #include "../feature/power_loss_recovery.h"
  37. #endif
  38. /**
  39. * GCode line number handling. Hosts may opt to include line numbers when
  40. * sending commands to Marlin, and lines will be checked for sequentiality.
  41. * M110 N<int> sets the current line number.
  42. */
  43. long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
  44. /**
  45. * GCode Command Queue
  46. * A simple ring buffer of BUFSIZE command strings.
  47. *
  48. * Commands are copied into this buffer by the command injectors
  49. * (immediate, serial, sd card) and they are processed sequentially by
  50. * the main loop. The gcode.process_next_command method parses the next
  51. * command and hands off execution to individual handler functions.
  52. */
  53. uint8_t commands_in_queue = 0, // Count of commands in the queue
  54. cmd_queue_index_r = 0, // Ring buffer read position
  55. cmd_queue_index_w = 0; // Ring buffer write position
  56. char command_queue[BUFSIZE][MAX_CMD_SIZE];
  57. /*
  58. * The port that the command was received on
  59. */
  60. #if NUM_SERIAL > 1
  61. int16_t command_queue_port[BUFSIZE];
  62. #endif
  63. /**
  64. * Serial command injection
  65. */
  66. // Number of characters read in the current line of serial input
  67. static int serial_count[NUM_SERIAL] = { 0 };
  68. bool send_ok[BUFSIZE];
  69. /**
  70. * Next Injected Command pointer. NULL if no commands are being injected.
  71. * Used by Marlin internally to ensure that commands initiated from within
  72. * are enqueued ahead of any pending serial or sd card commands.
  73. */
  74. static const char *injected_commands_P = NULL;
  75. void queue_setup() {
  76. // Send "ok" after commands by default
  77. for (uint8_t i = 0; i < COUNT(send_ok); i++) send_ok[i] = true;
  78. }
  79. /**
  80. * Clear the Marlin command queue
  81. */
  82. void clear_command_queue() {
  83. cmd_queue_index_r = cmd_queue_index_w = commands_in_queue = 0;
  84. }
  85. /**
  86. * Once a new command is in the ring buffer, call this to commit it
  87. */
  88. inline void _commit_command(bool say_ok
  89. #if NUM_SERIAL > 1
  90. , int16_t port = -1
  91. #endif
  92. ) {
  93. send_ok[cmd_queue_index_w] = say_ok;
  94. #if NUM_SERIAL > 1
  95. command_queue_port[cmd_queue_index_w] = port;
  96. #endif
  97. if (++cmd_queue_index_w >= BUFSIZE) cmd_queue_index_w = 0;
  98. commands_in_queue++;
  99. }
  100. /**
  101. * Copy a command from RAM into the main command buffer.
  102. * Return true if the command was successfully added.
  103. * Return false for a full buffer, or if the 'command' is a comment.
  104. */
  105. inline bool _enqueuecommand(const char* cmd, bool say_ok=false
  106. #if NUM_SERIAL > 1
  107. , int16_t port = -1
  108. #endif
  109. ) {
  110. if (*cmd == ';' || commands_in_queue >= BUFSIZE) return false;
  111. strcpy(command_queue[cmd_queue_index_w], cmd);
  112. _commit_command(say_ok
  113. #if NUM_SERIAL > 1
  114. , port
  115. #endif
  116. );
  117. return true;
  118. }
  119. /**
  120. * Enqueue with Serial Echo
  121. */
  122. bool enqueue_and_echo_command(const char* cmd) {
  123. //SERIAL_ECHO("enqueue_and_echo_command(\"");
  124. //SERIAL_ECHO(cmd);
  125. //SERIAL_ECHO("\") \n");
  126. if (*cmd == 0 || *cmd == '\n' || *cmd == '\r') {
  127. //SERIAL_ECHO("Null command found... Did not queue!\n");
  128. return true;
  129. }
  130. if (_enqueuecommand(cmd)) {
  131. SERIAL_ECHO_START();
  132. SERIAL_ECHOPAIR(MSG_ENQUEUEING, cmd);
  133. SERIAL_CHAR('"');
  134. SERIAL_EOL();
  135. return true;
  136. }
  137. return false;
  138. }
  139. /**
  140. * Inject the next "immediate" command, when possible, onto the front of the queue.
  141. * Return true if any immediate commands remain to inject.
  142. */
  143. static bool drain_injected_commands_P() {
  144. if (injected_commands_P != NULL) {
  145. size_t i = 0;
  146. char c, cmd[60];
  147. strncpy_P(cmd, injected_commands_P, sizeof(cmd) - 1);
  148. cmd[sizeof(cmd) - 1] = '\0';
  149. while ((c = cmd[i]) && c != '\n') i++; // find the end of this gcode command
  150. cmd[i] = '\0';
  151. if (enqueue_and_echo_command(cmd)) // success?
  152. injected_commands_P = c ? injected_commands_P + i + 1 : NULL; // next command or done
  153. }
  154. return (injected_commands_P != NULL); // return whether any more remain
  155. }
  156. /**
  157. * Record one or many commands to run from program memory.
  158. * Aborts the current queue, if any.
  159. * Note: drain_injected_commands_P() must be called repeatedly to drain the commands afterwards
  160. */
  161. void enqueue_and_echo_commands_P(const char * const pgcode) {
  162. injected_commands_P = pgcode;
  163. (void)drain_injected_commands_P(); // first command executed asap (when possible)
  164. }
  165. #if HAS_QUEUE_NOW
  166. /**
  167. * Enqueue and return only when commands are actually enqueued
  168. */
  169. void enqueue_and_echo_command_now(const char* cmd) {
  170. while (!enqueue_and_echo_command(cmd)) idle();
  171. }
  172. #if HAS_LCD_QUEUE_NOW
  173. /**
  174. * Enqueue from program memory and return only when commands are actually enqueued
  175. */
  176. void enqueue_and_echo_commands_now_P(const char * const pgcode) {
  177. enqueue_and_echo_commands_P(pgcode);
  178. while (drain_injected_commands_P()) idle();
  179. }
  180. #endif
  181. #endif
  182. /**
  183. * Send an "ok" message to the host, indicating
  184. * that a command was successfully processed.
  185. *
  186. * If ADVANCED_OK is enabled also include:
  187. * N<int> Line number of the command, if any
  188. * P<int> Planner space remaining
  189. * B<int> Block queue space remaining
  190. */
  191. void ok_to_send() {
  192. #if NUM_SERIAL > 1
  193. const int16_t port = command_queue_port[cmd_queue_index_r];
  194. if (port < 0) return;
  195. #endif
  196. if (!send_ok[cmd_queue_index_r]) return;
  197. SERIAL_PROTOCOLPGM_P(port, MSG_OK);
  198. #if ENABLED(ADVANCED_OK)
  199. char* p = command_queue[cmd_queue_index_r];
  200. if (*p == 'N') {
  201. SERIAL_PROTOCOL_P(port, ' ');
  202. SERIAL_ECHO_P(port, *p++);
  203. while (NUMERIC_SIGNED(*p))
  204. SERIAL_ECHO_P(port, *p++);
  205. }
  206. SERIAL_PROTOCOLPGM_P(port, " P"); SERIAL_PROTOCOL_P(port, int(BLOCK_BUFFER_SIZE - planner.movesplanned() - 1));
  207. SERIAL_PROTOCOLPGM_P(port, " B"); SERIAL_PROTOCOL_P(port, BUFSIZE - commands_in_queue);
  208. #endif
  209. SERIAL_EOL_P(port);
  210. }
  211. /**
  212. * Send a "Resend: nnn" message to the host to
  213. * indicate that a command needs to be re-sent.
  214. */
  215. void flush_and_request_resend() {
  216. #if NUM_SERIAL > 1
  217. const int16_t port = command_queue_port[cmd_queue_index_r];
  218. if (port < 0) return;
  219. #endif
  220. SERIAL_FLUSH_P(port);
  221. SERIAL_PROTOCOLPGM_P(port, MSG_RESEND);
  222. SERIAL_PROTOCOLLN_P(port, gcode_LastN + 1);
  223. ok_to_send();
  224. }
  225. void gcode_line_error(const char* err, uint8_t port) {
  226. SERIAL_ERROR_START_P(port);
  227. serialprintPGM_P(port, err);
  228. SERIAL_ERRORLN_P(port, gcode_LastN);
  229. flush_and_request_resend();
  230. serial_count[port] = 0;
  231. }
  232. static bool serial_data_available() {
  233. return (MYSERIAL0.available() ? true :
  234. #if NUM_SERIAL > 1
  235. MYSERIAL1.available() ? true :
  236. #endif
  237. false);
  238. }
  239. static int read_serial(const int index) {
  240. switch (index) {
  241. case 0: return MYSERIAL0.read();
  242. #if NUM_SERIAL > 1
  243. case 1: return MYSERIAL1.read();
  244. #endif
  245. default: return -1;
  246. }
  247. }
  248. /**
  249. * Get all commands waiting on the serial port and queue them.
  250. * Exit when the buffer is full or when no more characters are
  251. * left on the serial port.
  252. */
  253. inline void get_serial_commands() {
  254. static char serial_line_buffer[NUM_SERIAL][MAX_CMD_SIZE];
  255. static bool serial_comment_mode[NUM_SERIAL] = { false };
  256. // If the command buffer is empty for too long,
  257. // send "wait" to indicate Marlin is still waiting.
  258. #if NO_TIMEOUTS > 0
  259. static millis_t last_command_time = 0;
  260. const millis_t ms = millis();
  261. if (commands_in_queue == 0 && !serial_data_available() && ELAPSED(ms, last_command_time + NO_TIMEOUTS)) {
  262. SERIAL_ECHOLNPGM(MSG_WAIT);
  263. last_command_time = ms;
  264. }
  265. #endif
  266. /**
  267. * Loop while serial characters are incoming and the queue is not full
  268. */
  269. while (commands_in_queue < BUFSIZE && serial_data_available()) {
  270. for (uint8_t i = 0; i < NUM_SERIAL; ++i) {
  271. int c;
  272. if ((c = read_serial(i)) < 0) continue;
  273. char serial_char = c;
  274. /**
  275. * If the character ends the line
  276. */
  277. if (serial_char == '\n' || serial_char == '\r') {
  278. serial_comment_mode[i] = false; // end of line == end of comment
  279. // Skip empty lines and comments
  280. if (!serial_count[i]) { thermalManager.manage_heater(); continue; }
  281. serial_line_buffer[i][serial_count[i]] = 0; // Terminate string
  282. serial_count[i] = 0; // Reset buffer
  283. char* command = serial_line_buffer[i];
  284. while (*command == ' ') command++; // Skip leading spaces
  285. char *npos = (*command == 'N') ? command : NULL; // Require the N parameter to start the line
  286. if (npos) {
  287. bool M110 = strstr_P(command, PSTR("M110")) != NULL;
  288. if (M110) {
  289. char* n2pos = strchr(command + 4, 'N');
  290. if (n2pos) npos = n2pos;
  291. }
  292. gcode_N = strtol(npos + 1, NULL, 10);
  293. if (gcode_N != gcode_LastN + 1 && !M110)
  294. return gcode_line_error(PSTR(MSG_ERR_LINE_NO), i);
  295. char *apos = strrchr(command, '*');
  296. if (apos) {
  297. uint8_t checksum = 0, count = uint8_t(apos - command);
  298. while (count) checksum ^= command[--count];
  299. if (strtol(apos + 1, NULL, 10) != checksum)
  300. return gcode_line_error(PSTR(MSG_ERR_CHECKSUM_MISMATCH), i);
  301. }
  302. else
  303. return gcode_line_error(PSTR(MSG_ERR_NO_CHECKSUM), i);
  304. gcode_LastN = gcode_N;
  305. }
  306. #if ENABLED(SDSUPPORT)
  307. else if (card.saving)
  308. return gcode_line_error(PSTR(MSG_ERR_NO_CHECKSUM), i);
  309. #endif
  310. // Movement commands alert when stopped
  311. if (IsStopped()) {
  312. char* gpos = strchr(command, 'G');
  313. if (gpos) {
  314. const int codenum = strtol(gpos + 1, NULL, 10);
  315. switch (codenum) {
  316. case 0:
  317. case 1:
  318. case 2:
  319. case 3:
  320. SERIAL_ERRORLNPGM_P(i, MSG_ERR_STOPPED);
  321. LCD_MESSAGEPGM(MSG_STOPPED);
  322. break;
  323. }
  324. }
  325. }
  326. #if DISABLED(EMERGENCY_PARSER)
  327. // Process critical commands early
  328. if (strcmp(command, "M108") == 0) {
  329. wait_for_heatup = false;
  330. #if ENABLED(ULTIPANEL)
  331. wait_for_user = false;
  332. #endif
  333. }
  334. if (strcmp(command, "M112") == 0) kill(PSTR(MSG_KILLED));
  335. if (strcmp(command, "M410") == 0) quickstop_stepper();
  336. #endif
  337. #if defined(NO_TIMEOUTS) && NO_TIMEOUTS > 0
  338. last_command_time = ms;
  339. #endif
  340. // Add the command to the queue
  341. _enqueuecommand(serial_line_buffer[i], true
  342. #if NUM_SERIAL > 1
  343. , i
  344. #endif
  345. );
  346. }
  347. else if (serial_count[i] >= MAX_CMD_SIZE - 1) {
  348. // Keep fetching, but ignore normal characters beyond the max length
  349. // The command will be injected when EOL is reached
  350. }
  351. else if (serial_char == '\\') { // Handle escapes
  352. // if we have one more character, copy it over
  353. if ((c = read_serial(i)) >= 0 && !serial_comment_mode[i])
  354. serial_line_buffer[i][serial_count[i]++] = (char)c;
  355. }
  356. else { // it's not a newline, carriage return or escape char
  357. if (serial_char == ';') serial_comment_mode[i] = true;
  358. if (!serial_comment_mode[i]) serial_line_buffer[i][serial_count[i]++] = serial_char;
  359. }
  360. } // for NUM_SERIAL
  361. } // queue has space, serial has data
  362. }
  363. #if ENABLED(SDSUPPORT)
  364. /**
  365. * Get commands from the SD Card until the command buffer is full
  366. * or until the end of the file is reached. The special character '#'
  367. * can also interrupt buffering.
  368. */
  369. inline void get_sdcard_commands() {
  370. static bool stop_buffering = false,
  371. sd_comment_mode = false;
  372. if (!IS_SD_PRINTING) return;
  373. /**
  374. * '#' stops reading from SD to the buffer prematurely, so procedural
  375. * macro calls are possible. If it occurs, stop_buffering is triggered
  376. * and the buffer is run dry; this character _can_ occur in serial com
  377. * due to checksums, however, no checksums are used in SD printing.
  378. */
  379. if (commands_in_queue == 0) stop_buffering = false;
  380. uint16_t sd_count = 0;
  381. bool card_eof = card.eof();
  382. while (commands_in_queue < BUFSIZE && !card_eof && !stop_buffering) {
  383. const int16_t n = card.get();
  384. char sd_char = (char)n;
  385. card_eof = card.eof();
  386. if (card_eof || n == -1
  387. || sd_char == '\n' || sd_char == '\r'
  388. || ((sd_char == '#' || sd_char == ':') && !sd_comment_mode)
  389. ) {
  390. if (card_eof) {
  391. card.printingHasFinished();
  392. if (card.sdprinting)
  393. sd_count = 0; // If a sub-file was printing, continue from call point
  394. else {
  395. SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
  396. #if ENABLED(PRINTER_EVENT_LEDS)
  397. LCD_MESSAGEPGM(MSG_INFO_COMPLETED_PRINTS);
  398. leds.set_green();
  399. #if HAS_RESUME_CONTINUE
  400. gcode.lights_off_after_print = true;
  401. enqueue_and_echo_commands_P(PSTR("M0 S"
  402. #if ENABLED(NEWPANEL)
  403. "1800"
  404. #else
  405. "60"
  406. #endif
  407. ));
  408. #else
  409. safe_delay(2000);
  410. leds.set_off();
  411. #endif
  412. #endif // PRINTER_EVENT_LEDS
  413. }
  414. }
  415. else if (n == -1) {
  416. SERIAL_ERROR_START();
  417. SERIAL_ECHOLNPGM(MSG_SD_ERR_READ);
  418. }
  419. if (sd_char == '#') stop_buffering = true;
  420. sd_comment_mode = false; // for new command
  421. // Skip empty lines and comments
  422. if (!sd_count) { thermalManager.manage_heater(); continue; }
  423. command_queue[cmd_queue_index_w][sd_count] = '\0'; // terminate string
  424. sd_count = 0; // clear sd line buffer
  425. _commit_command(false);
  426. }
  427. else if (sd_count >= MAX_CMD_SIZE - 1) {
  428. /**
  429. * Keep fetching, but ignore normal characters beyond the max length
  430. * The command will be injected when EOL is reached
  431. */
  432. }
  433. else {
  434. if (sd_char == ';') sd_comment_mode = true;
  435. if (!sd_comment_mode) command_queue[cmd_queue_index_w][sd_count++] = sd_char;
  436. }
  437. }
  438. }
  439. #if ENABLED(POWER_LOSS_RECOVERY)
  440. inline bool drain_job_recovery_commands() {
  441. static uint8_t job_recovery_commands_index = 0; // Resets on reboot
  442. if (job_recovery_commands_count) {
  443. if (_enqueuecommand(job_recovery_commands[job_recovery_commands_index])) {
  444. ++job_recovery_commands_index;
  445. if (!--job_recovery_commands_count) job_recovery_phase = JOB_RECOVERY_DONE;
  446. }
  447. return true;
  448. }
  449. return false;
  450. }
  451. #endif
  452. #endif // SDSUPPORT
  453. /**
  454. * Add to the circular command queue the next command from:
  455. * - The command-injection queue (injected_commands_P)
  456. * - The active serial input (usually USB)
  457. * - The SD card file being actively printed
  458. */
  459. void get_available_commands() {
  460. // if any immediate commands remain, don't get other commands yet
  461. if (drain_injected_commands_P()) return;
  462. get_serial_commands();
  463. #if ENABLED(POWER_LOSS_RECOVERY)
  464. // Commands for power-loss recovery take precedence
  465. if (job_recovery_phase == JOB_RECOVERY_YES && drain_job_recovery_commands()) return;
  466. #endif
  467. #if ENABLED(SDSUPPORT)
  468. get_sdcard_commands();
  469. #endif
  470. }
  471. /**
  472. * Get the next command in the queue, optionally log it to SD, then dispatch it
  473. */
  474. void advance_command_queue() {
  475. if (!commands_in_queue) return;
  476. #if ENABLED(SDSUPPORT)
  477. if (card.saving) {
  478. char* command = command_queue[cmd_queue_index_r];
  479. if (strstr_P(command, PSTR("M29"))) {
  480. // M29 closes the file
  481. card.closefile();
  482. SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
  483. #if !defined(__AVR__) || !defined(USBCON)
  484. #if ENABLED(SERIAL_STATS_DROPPED_RX)
  485. SERIAL_ECHOLNPAIR("Dropped bytes: ", customizedSerial.dropped());
  486. #endif
  487. #if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
  488. SERIAL_ECHOLNPAIR("Max RX Queue Size: ", customizedSerial.rxMaxEnqueued());
  489. #endif
  490. #endif // !defined(__AVR__) || !defined(USBCON)
  491. ok_to_send();
  492. }
  493. else {
  494. // Write the string from the read buffer to SD
  495. card.write_command(command);
  496. if (card.logging)
  497. gcode.process_next_command(); // The card is saving because it's logging
  498. else
  499. ok_to_send();
  500. }
  501. }
  502. else {
  503. gcode.process_next_command();
  504. #if ENABLED(POWER_LOSS_RECOVERY)
  505. if (card.cardOK && card.sdprinting) save_job_recovery_info();
  506. #endif
  507. }
  508. #else
  509. gcode.process_next_command();
  510. #endif // SDSUPPORT
  511. // The queue may be reset by a command handler or by code invoked by idle() within a handler
  512. if (commands_in_queue) {
  513. --commands_in_queue;
  514. if (++cmd_queue_index_r >= BUFSIZE) cmd_queue_index_r = 0;
  515. }
  516. }