My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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