2017-09-07 22:33:16 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-09-07 22:33:16 -05:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2017-09-07 22:33:16 -05:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-07-22 22:20:14 -05:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-09-07 22:33:16 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* queue.cpp - The G-code command queue
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "queue.h"
|
2019-06-19 00:00:19 -05:00
|
|
|
GCodeQueue queue;
|
|
|
|
|
2017-09-07 22:33:16 -05:00
|
|
|
#include "gcode.h"
|
|
|
|
|
2020-10-16 19:36:25 -05:00
|
|
|
#include "../lcd/marlinui.h"
|
2017-09-07 22:33:16 -05:00
|
|
|
#include "../sd/cardreader.h"
|
2021-01-21 03:40:07 -06:00
|
|
|
#include "../module/motion.h"
|
2017-09-07 22:33:16 -05:00
|
|
|
#include "../module/planner.h"
|
2018-02-09 01:50:14 -06:00
|
|
|
#include "../module/temperature.h"
|
2020-01-02 19:01:38 -06:00
|
|
|
#include "../MarlinCore.h"
|
2021-02-26 16:54:46 -06:00
|
|
|
#include "../core/bug_on.h"
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2018-10-10 21:25:43 -05:00
|
|
|
#if ENABLED(PRINTER_EVENT_LEDS)
|
|
|
|
#include "../feature/leds/printer_event_leds.h"
|
2017-09-08 14:47:47 -05:00
|
|
|
#endif
|
|
|
|
|
2020-10-20 14:35:29 -05:00
|
|
|
#if HAS_ETHERNET
|
|
|
|
#include "../feature/ethernet.h"
|
|
|
|
#endif
|
|
|
|
|
2019-08-05 20:41:53 -05:00
|
|
|
#if ENABLED(BINARY_FILE_TRANSFER)
|
2020-08-06 08:14:00 -05:00
|
|
|
#include "../feature/binary_stream.h"
|
2019-08-05 20:41:53 -05:00
|
|
|
#endif
|
|
|
|
|
2019-09-10 18:52:41 -05:00
|
|
|
#if ENABLED(POWER_LOSS_RECOVERY)
|
2020-03-13 16:29:29 -05:00
|
|
|
#include "../feature/powerloss.h"
|
2019-09-10 18:52:41 -05:00
|
|
|
#endif
|
2019-08-05 20:41:53 -05:00
|
|
|
|
2020-11-26 21:18:40 -06:00
|
|
|
#if ENABLED(GCODE_REPEAT_MARKERS)
|
|
|
|
#include "../feature/repeat.h"
|
|
|
|
#endif
|
|
|
|
|
2021-01-22 15:01:19 -06:00
|
|
|
// Frequently used G-code strings
|
|
|
|
PGMSTR(G28_STR, "G28");
|
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
GCodeQueue::SerialState GCodeQueue::serial_state[NUM_SERIAL] = { 0 };
|
|
|
|
GCodeQueue::RingBuffer GCodeQueue::ring_buffer = { 0 };
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
#if NO_TIMEOUTS > 0
|
|
|
|
static millis_t last_command_time = 0;
|
2017-11-05 08:49:38 -06:00
|
|
|
#endif
|
|
|
|
|
2017-09-07 22:33:16 -05:00
|
|
|
/**
|
|
|
|
* Serial command injection
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2020-04-09 20:05:58 -05:00
|
|
|
* Next Injected PROGMEM Command pointer. (nullptr == empty)
|
|
|
|
* Internal commands are enqueued ahead of serial / SD commands.
|
2017-09-07 22:33:16 -05:00
|
|
|
*/
|
2020-04-09 20:05:58 -05:00
|
|
|
PGM_P GCodeQueue::injected_commands_P; // = nullptr
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Injected SRAM Commands
|
|
|
|
*/
|
|
|
|
char GCodeQueue::injected_commands[64]; // = { 0 }
|
2017-09-07 22:33:16 -05:00
|
|
|
|
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
void GCodeQueue::RingBuffer::commit_command(bool skip_ok
|
2020-05-12 05:50:28 -05:00
|
|
|
#if HAS_MULTI_SERIAL
|
2021-01-28 01:47:12 -06:00
|
|
|
, serial_index_t serial_ind/*=-1*/
|
2017-11-05 08:49:38 -06:00
|
|
|
#endif
|
|
|
|
) {
|
2021-02-26 16:54:46 -06:00
|
|
|
commands[index_w].skip_ok = skip_ok;
|
|
|
|
TERN_(HAS_MULTI_SERIAL, commands[index_w].port = serial_ind);
|
2020-04-22 16:35:03 -05:00
|
|
|
TERN_(POWER_LOSS_RECOVERY, recovery.commit_sdpos(index_w));
|
2021-02-26 16:54:46 -06:00
|
|
|
advance_pos(index_w, 1);
|
2017-09-07 22:33:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy a command from RAM into the main command buffer.
|
|
|
|
* Return true if the command was successfully added.
|
|
|
|
* Return false for a full buffer, or if the 'command' is a comment.
|
|
|
|
*/
|
2021-03-29 20:36:37 -05:00
|
|
|
bool GCodeQueue::RingBuffer::enqueue(const char *cmd, bool skip_ok/*=true*/
|
2020-05-12 05:50:28 -05:00
|
|
|
#if HAS_MULTI_SERIAL
|
2021-01-28 01:47:12 -06:00
|
|
|
, serial_index_t serial_ind/*=-1*/
|
2017-11-05 08:49:38 -06:00
|
|
|
#endif
|
|
|
|
) {
|
2019-06-19 00:00:19 -05:00
|
|
|
if (*cmd == ';' || length >= BUFSIZE) return false;
|
2021-02-26 16:54:46 -06:00
|
|
|
strcpy(commands[index_w].buffer, cmd);
|
|
|
|
commit_command(skip_ok
|
2020-05-12 05:50:28 -05:00
|
|
|
#if HAS_MULTI_SERIAL
|
2021-01-28 01:47:12 -06:00
|
|
|
, serial_ind
|
2017-11-05 08:49:38 -06:00
|
|
|
#endif
|
|
|
|
);
|
2017-09-07 22:33:16 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueue with Serial Echo
|
2019-06-08 05:23:53 -05:00
|
|
|
* Return true if the command was consumed
|
2017-09-07 22:33:16 -05:00
|
|
|
*/
|
2021-03-29 20:36:37 -05:00
|
|
|
bool GCodeQueue::enqueue_one(const char *cmd) {
|
2021-02-26 16:54:46 -06:00
|
|
|
//SERIAL_ECHOLNPAIR("enqueue_one(\"", cmd, "\")");
|
2018-09-03 23:15:31 -05:00
|
|
|
|
2020-03-13 16:29:29 -05:00
|
|
|
if (*cmd == 0 || ISEOL(*cmd)) return true;
|
2018-09-02 10:18:59 -05:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
if (ring_buffer.enqueue(cmd)) {
|
2020-02-26 03:02:03 -06:00
|
|
|
SERIAL_ECHO_MSG(STR_ENQUEUEING, cmd, "\"");
|
2017-09-07 22:33:16 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-19 00:00:19 -05:00
|
|
|
/**
|
2020-04-09 20:05:58 -05:00
|
|
|
* Process the next "immediate" command from PROGMEM.
|
|
|
|
* Return 'true' if any commands were processed.
|
2019-06-19 00:00:19 -05:00
|
|
|
*/
|
2020-04-09 20:05:58 -05:00
|
|
|
bool GCodeQueue::process_injected_command_P() {
|
2020-10-24 17:13:10 -05:00
|
|
|
if (!injected_commands_P) return false;
|
2019-06-08 05:23:53 -05:00
|
|
|
|
2019-06-19 00:00:19 -05:00
|
|
|
char c;
|
|
|
|
size_t i = 0;
|
|
|
|
while ((c = pgm_read_byte(&injected_commands_P[i])) && c != '\n') i++;
|
2019-06-08 05:23:53 -05:00
|
|
|
|
2019-07-14 10:12:48 -05:00
|
|
|
// Extract current command and move pointer to next command
|
|
|
|
char cmd[i + 1];
|
|
|
|
memcpy_P(cmd, injected_commands_P, i);
|
|
|
|
cmd[i] = '\0';
|
|
|
|
injected_commands_P = c ? injected_commands_P + i + 1 : nullptr;
|
|
|
|
|
|
|
|
// Execute command if non-blank
|
|
|
|
if (i) {
|
2019-06-29 00:23:57 -05:00
|
|
|
parser.parse(cmd);
|
|
|
|
gcode.process_parsed_command();
|
|
|
|
}
|
2019-06-19 00:00:19 -05:00
|
|
|
return true;
|
2017-09-07 22:33:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-09 20:05:58 -05:00
|
|
|
* Process the next "immediate" command from SRAM.
|
|
|
|
* Return 'true' if any commands were processed.
|
2017-09-07 22:33:16 -05:00
|
|
|
*/
|
2020-04-09 20:05:58 -05:00
|
|
|
bool GCodeQueue::process_injected_command() {
|
|
|
|
if (injected_commands[0] == '\0') return false;
|
|
|
|
|
|
|
|
char c;
|
|
|
|
size_t i = 0;
|
|
|
|
while ((c = injected_commands[i]) && c != '\n') i++;
|
|
|
|
|
|
|
|
// Execute a non-blank command
|
|
|
|
if (i) {
|
|
|
|
injected_commands[i] = '\0';
|
|
|
|
parser.parse(injected_commands);
|
|
|
|
gcode.process_parsed_command();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the next command into place
|
2020-05-12 01:45:28 -05:00
|
|
|
for (
|
|
|
|
uint8_t d = 0, s = i + !!c; // dst, src
|
|
|
|
(injected_commands[d] = injected_commands[s]); // copy, exit if 0
|
|
|
|
d++, s++ // next dst, src
|
|
|
|
);
|
2020-04-28 08:30:52 -05:00
|
|
|
|
2020-04-09 20:05:58 -05:00
|
|
|
return true;
|
|
|
|
}
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2019-06-19 00:00:19 -05:00
|
|
|
/**
|
|
|
|
* Enqueue and return only when commands are actually enqueued.
|
|
|
|
* Never call this from a G-code handler!
|
|
|
|
*/
|
2021-03-29 20:36:37 -05:00
|
|
|
void GCodeQueue::enqueue_one_now(const char *cmd) { while (!enqueue_one(cmd)) idle(); }
|
2019-06-19 00:00:19 -05:00
|
|
|
|
2020-03-10 18:04:54 -05:00
|
|
|
/**
|
|
|
|
* Attempt to enqueue a single G-code command
|
|
|
|
* and return 'true' if successful.
|
|
|
|
*/
|
|
|
|
bool GCodeQueue::enqueue_one_P(PGM_P const pgcode) {
|
|
|
|
size_t i = 0;
|
|
|
|
PGM_P p = pgcode;
|
|
|
|
char c;
|
|
|
|
while ((c = pgm_read_byte(&p[i])) && c != '\n') i++;
|
|
|
|
char cmd[i + 1];
|
|
|
|
memcpy_P(cmd, p, i);
|
|
|
|
cmd[i] = '\0';
|
2021-02-26 16:54:46 -06:00
|
|
|
return ring_buffer.enqueue(cmd);
|
2020-03-10 18:04:54 -05:00
|
|
|
}
|
|
|
|
|
2019-06-19 00:00:19 -05:00
|
|
|
/**
|
|
|
|
* Enqueue from program memory and return only when commands are actually enqueued
|
|
|
|
* Never call this from a G-code handler!
|
|
|
|
*/
|
|
|
|
void GCodeQueue::enqueue_now_P(PGM_P const pgcode) {
|
|
|
|
size_t i = 0;
|
|
|
|
PGM_P p = pgcode;
|
|
|
|
for (;;) {
|
|
|
|
char c;
|
2019-06-23 22:21:33 -05:00
|
|
|
while ((c = pgm_read_byte(&p[i])) && c != '\n') i++;
|
2019-06-19 00:00:19 -05:00
|
|
|
char cmd[i + 1];
|
|
|
|
memcpy_P(cmd, p, i);
|
|
|
|
cmd[i] = '\0';
|
|
|
|
enqueue_one_now(cmd);
|
|
|
|
if (!c) break;
|
|
|
|
p += i + 1;
|
2018-01-02 21:00:06 -06:00
|
|
|
}
|
2019-06-19 00:00:19 -05:00
|
|
|
}
|
2018-01-02 21:00:06 -06:00
|
|
|
|
2017-09-07 22:33:16 -05:00
|
|
|
/**
|
|
|
|
* Send an "ok" message to the host, indicating
|
|
|
|
* that a command was successfully processed.
|
|
|
|
*
|
|
|
|
* If ADVANCED_OK is enabled also include:
|
|
|
|
* N<int> Line number of the command, if any
|
|
|
|
* P<int> Planner space remaining
|
|
|
|
* B<int> Block queue space remaining
|
|
|
|
*/
|
2021-02-26 16:54:46 -06:00
|
|
|
void GCodeQueue::RingBuffer::ok_to_send() {
|
2021-02-25 01:23:29 -06:00
|
|
|
#if NO_TIMEOUTS > 0
|
|
|
|
// Start counting from the last command's execution
|
|
|
|
last_command_time = millis();
|
|
|
|
#endif
|
2021-02-26 16:54:46 -06:00
|
|
|
CommandLine &command = commands[index_r];
|
2020-05-12 05:50:28 -05:00
|
|
|
#if HAS_MULTI_SERIAL
|
2021-02-26 16:54:46 -06:00
|
|
|
const serial_index_t serial_ind = command.port;
|
2021-03-09 03:20:37 -06:00
|
|
|
if (!serial_ind.valid()) return; // Optimization here, skip processing if it's not going anywhere
|
2021-01-28 02:02:06 -06:00
|
|
|
PORT_REDIRECT(SERIAL_PORTMASK(serial_ind)); // Reply to the serial port that sent the command
|
2017-11-05 08:49:38 -06:00
|
|
|
#endif
|
2021-02-26 16:54:46 -06:00
|
|
|
if (command.skip_ok) return;
|
2020-02-26 03:02:03 -06:00
|
|
|
SERIAL_ECHOPGM(STR_OK);
|
2017-09-07 22:33:16 -05:00
|
|
|
#if ENABLED(ADVANCED_OK)
|
2021-02-26 16:54:46 -06:00
|
|
|
char* p = command.buffer;
|
2017-09-07 22:33:16 -05:00
|
|
|
if (*p == 'N') {
|
2021-02-04 19:18:31 -06:00
|
|
|
SERIAL_CHAR(' ', *p++);
|
2017-09-07 22:33:16 -05:00
|
|
|
while (NUMERIC_SIGNED(*p))
|
2021-02-04 19:18:31 -06:00
|
|
|
SERIAL_CHAR(*p++);
|
2017-09-07 22:33:16 -05:00
|
|
|
}
|
2021-02-08 00:37:24 -06:00
|
|
|
SERIAL_ECHOPAIR_P(SP_P_STR, planner.moves_free(),
|
|
|
|
SP_B_STR, BUFSIZE - length);
|
2017-09-07 22:33:16 -05:00
|
|
|
#endif
|
2019-02-23 22:53:01 -06:00
|
|
|
SERIAL_EOL();
|
2017-09-07 22:33:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a "Resend: nnn" message to the host to
|
|
|
|
* indicate that a command needs to be re-sent.
|
|
|
|
*/
|
2021-03-04 17:39:08 -06:00
|
|
|
void GCodeQueue::flush_and_request_resend(const serial_index_t serial_ind) {
|
2020-05-12 05:50:28 -05:00
|
|
|
#if HAS_MULTI_SERIAL
|
2021-03-09 03:20:37 -06:00
|
|
|
if (!serial_ind.valid()) return; // Optimization here, skip if the command came from SD or Flash Drive
|
2021-01-28 02:02:06 -06:00
|
|
|
PORT_REDIRECT(SERIAL_PORTMASK(serial_ind)); // Reply to the serial port that sent the command
|
2017-11-05 08:49:38 -06:00
|
|
|
#endif
|
2019-02-23 22:53:01 -06:00
|
|
|
SERIAL_FLUSH();
|
2021-05-06 01:51:19 -05:00
|
|
|
SERIAL_ECHOLNPAIR(STR_RESEND, serial_state[serial_ind.index].last_N + 1);
|
2021-05-04 01:47:42 -05:00
|
|
|
SERIAL_ECHOLNPGM(STR_OK);
|
2017-11-05 08:49:38 -06:00
|
|
|
}
|
|
|
|
|
2021-03-10 11:35:19 -06:00
|
|
|
static bool serial_data_available(serial_index_t index) {
|
2021-02-26 16:54:46 -06:00
|
|
|
const int a = SERIAL_IMPL.available(index);
|
|
|
|
#if BOTH(RX_BUFFER_MONITOR, RX_BUFFER_SIZE)
|
|
|
|
if (a > RX_BUFFER_SIZE - 2) {
|
|
|
|
PORT_REDIRECT(SERIAL_PORTMASK(index));
|
|
|
|
SERIAL_ERROR_MSG("RX BUF overflow, increase RX_BUFFER_SIZE: ", a);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return a > 0;
|
2017-09-07 22:33:16 -05:00
|
|
|
}
|
|
|
|
|
2021-03-10 11:35:19 -06:00
|
|
|
#if NO_TIMEOUTS > 0
|
|
|
|
// Multiserial already handles dispatch to/from multiple ports
|
|
|
|
static bool any_serial_data_available() {
|
|
|
|
LOOP_L_N(p, NUM_SERIAL)
|
|
|
|
if (serial_data_available(p))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2021-03-08 01:11:37 -06:00
|
|
|
|
2021-03-09 03:20:37 -06:00
|
|
|
inline int read_serial(const serial_index_t index) { return SERIAL_IMPL.read(index); }
|
2021-02-25 01:23:29 -06:00
|
|
|
|
2021-01-28 01:47:12 -06:00
|
|
|
void GCodeQueue::gcode_line_error(PGM_P const err, const serial_index_t serial_ind) {
|
2021-01-28 02:02:06 -06:00
|
|
|
PORT_REDIRECT(SERIAL_PORTMASK(serial_ind)); // Reply to the serial port that sent the command
|
2019-06-19 00:00:19 -05:00
|
|
|
SERIAL_ERROR_START();
|
2021-03-09 03:20:37 -06:00
|
|
|
SERIAL_ECHOLNPAIR_P(err, serial_state[serial_ind.index].last_N);
|
2021-02-26 16:54:46 -06:00
|
|
|
while (read_serial(serial_ind) != -1) { /* nada */ } // Clear out the RX buffer. Why don't use flush here ?
|
2021-03-04 17:39:08 -06:00
|
|
|
flush_and_request_resend(serial_ind);
|
2021-03-09 03:20:37 -06:00
|
|
|
serial_state[serial_ind.index].count = 0;
|
2019-06-19 00:00:19 -05:00
|
|
|
}
|
|
|
|
|
2019-04-15 23:46:36 -05:00
|
|
|
FORCE_INLINE bool is_M29(const char * const cmd) { // matches "M29" & "M29 ", but not "M290", etc
|
|
|
|
const char * const m29 = strstr_P(cmd, PSTR("M29"));
|
|
|
|
return m29 && !NUMERIC(m29[3]);
|
2019-02-04 07:17:40 -06:00
|
|
|
}
|
|
|
|
|
2020-02-10 14:52:15 -06:00
|
|
|
#define PS_NORMAL 0
|
|
|
|
#define PS_EOL 1
|
|
|
|
#define PS_QUOTED 2
|
|
|
|
#define PS_PAREN 3
|
|
|
|
#define PS_ESC 4
|
|
|
|
|
|
|
|
inline void process_stream_char(const char c, uint8_t &sis, char (&buff)[MAX_CMD_SIZE], int &ind) {
|
|
|
|
|
|
|
|
if (sis == PS_EOL) return; // EOL comment or overflow
|
|
|
|
|
|
|
|
#if ENABLED(PAREN_COMMENTS)
|
|
|
|
else if (sis == PS_PAREN) { // Inline comment
|
|
|
|
if (c == ')') sis = PS_NORMAL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
else if (sis >= PS_ESC) // End escaped char
|
|
|
|
sis -= PS_ESC;
|
|
|
|
|
|
|
|
else if (c == '\\') { // Start escaped char
|
|
|
|
sis += PS_ESC;
|
|
|
|
if (sis == PS_ESC) return; // Keep if quoting
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLED(GCODE_QUOTED_STRINGS)
|
|
|
|
|
|
|
|
else if (sis == PS_QUOTED) {
|
|
|
|
if (c == '"') sis = PS_NORMAL; // End quoted string
|
|
|
|
}
|
|
|
|
else if (c == '"') // Start quoted string
|
|
|
|
sis = PS_QUOTED;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
else if (c == ';') { // Start end-of-line comment
|
|
|
|
sis = PS_EOL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLED(PAREN_COMMENTS)
|
|
|
|
else if (c == '(') { // Start inline comment
|
|
|
|
sis = PS_PAREN;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-08 17:28:25 -05:00
|
|
|
// Backspace erases previous characters
|
|
|
|
if (c == 0x08) {
|
|
|
|
if (ind) buff[--ind] = '\0';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buff[ind++] = c;
|
|
|
|
if (ind >= MAX_CMD_SIZE - 1)
|
|
|
|
sis = PS_EOL; // Skip the rest on overflow
|
|
|
|
}
|
2020-02-10 14:52:15 -06:00
|
|
|
}
|
|
|
|
|
2020-03-01 21:08:01 -06:00
|
|
|
/**
|
|
|
|
* Handle a line being completed. For an empty line
|
|
|
|
* keep sensor readings going and watchdog alive.
|
|
|
|
*/
|
2020-02-10 14:52:15 -06:00
|
|
|
inline bool process_line_done(uint8_t &sis, char (&buff)[MAX_CMD_SIZE], int &ind) {
|
2020-11-14 18:09:17 -06:00
|
|
|
sis = PS_NORMAL; // "Normal" Serial Input State
|
|
|
|
buff[ind] = '\0'; // Of course, I'm a Terminator.
|
|
|
|
const bool is_empty = (ind == 0); // An empty line?
|
|
|
|
if (is_empty)
|
|
|
|
thermalManager.manage_heater(); // Keep sensors satisfied
|
|
|
|
else
|
|
|
|
ind = 0; // Start a new line
|
|
|
|
return is_empty; // Inform the caller
|
2020-02-10 14:52:15 -06:00
|
|
|
}
|
|
|
|
|
2017-09-07 22:33:16 -05:00
|
|
|
/**
|
|
|
|
* Get all commands waiting on the serial port and queue them.
|
|
|
|
* Exit when the buffer is full or when no more characters are
|
|
|
|
* left on the serial port.
|
|
|
|
*/
|
2019-06-19 00:00:19 -05:00
|
|
|
void GCodeQueue::get_serial_commands() {
|
2019-02-27 19:57:48 -06:00
|
|
|
#if ENABLED(BINARY_FILE_TRANSFER)
|
2019-08-05 20:41:53 -05:00
|
|
|
if (card.flag.binary_mode) {
|
2018-10-30 19:44:12 -05:00
|
|
|
/**
|
|
|
|
* For binary stream file transfer, use serial_line_buffer as the working
|
|
|
|
* receive buffer (which limits the packet size to MAX_CMD_SIZE).
|
|
|
|
* The receive buffer also limits the packet size for reliable transmission.
|
|
|
|
*/
|
2021-03-10 11:33:29 -06:00
|
|
|
binaryStream[card.transfer_port_index.index].receive(serial_state[card.transfer_port_index.index].line_buffer);
|
2018-10-30 19:44:12 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-09-07 22:33:16 -05:00
|
|
|
// If the command buffer is empty for too long,
|
|
|
|
// send "wait" to indicate Marlin is still waiting.
|
2018-02-09 01:52:28 -06:00
|
|
|
#if NO_TIMEOUTS > 0
|
2017-09-07 22:33:16 -05:00
|
|
|
const millis_t ms = millis();
|
2021-03-08 01:11:37 -06:00
|
|
|
if (ring_buffer.empty() && !any_serial_data_available() && ELAPSED(ms, last_command_time + NO_TIMEOUTS)) {
|
2020-02-26 03:02:03 -06:00
|
|
|
SERIAL_ECHOLNPGM(STR_WAIT);
|
2017-09-07 22:33:16 -05:00
|
|
|
last_command_time = ms;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
// Loop while serial characters are incoming and the queue is not full
|
|
|
|
for (bool hadData = true; hadData;) {
|
|
|
|
// Unless a serial port has data, this will exit on next iteration
|
|
|
|
hadData = false;
|
|
|
|
|
2021-01-28 01:47:12 -06:00
|
|
|
LOOP_L_N(p, NUM_SERIAL) {
|
2021-02-26 16:54:46 -06:00
|
|
|
// Check if the queue is full and exit if it is.
|
|
|
|
if (ring_buffer.full()) return;
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
// No data for this port ? Skip it
|
|
|
|
if (!serial_data_available(p)) continue;
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
// Ok, we have some data to process, let's make progress here
|
|
|
|
hadData = true;
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
const int c = read_serial(p);
|
|
|
|
if (c < 0) {
|
|
|
|
// This should never happen, let's log it
|
|
|
|
PORT_REDIRECT(SERIAL_PORTMASK(p)); // Reply to the serial port that sent the command
|
|
|
|
// Crash here to get more information why it failed
|
|
|
|
BUG_ON("SP available but read -1");
|
|
|
|
SERIAL_ERROR_MSG(STR_ERR_SERIAL_MISMATCH);
|
|
|
|
SERIAL_FLUSH();
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
const char serial_char = (char)c;
|
|
|
|
SerialState &serial = serial_state[p];
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
if (ISEOL(serial_char)) {
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
// Reset our state, continue if the line was empty
|
|
|
|
if (process_line_done(serial.input_state, serial.line_buffer, serial.count))
|
|
|
|
continue;
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2021-04-04 17:35:58 -05:00
|
|
|
char* command = serial.line_buffer;
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
while (*command == ' ') command++; // Skip leading spaces
|
|
|
|
char *npos = (*command == 'N') ? command : nullptr; // Require the N parameter to start the line
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
if (npos) {
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
const bool M110 = !!strstr_P(command, PSTR("M110"));
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
if (M110) {
|
|
|
|
char* n2pos = strchr(command + 4, 'N');
|
|
|
|
if (n2pos) npos = n2pos;
|
|
|
|
}
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2021-04-04 17:35:58 -05:00
|
|
|
const long gcode_N = strtol(npos + 1, nullptr, 10);
|
2021-01-24 00:43:23 -06:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
if (gcode_N != serial.last_N + 1 && !M110) {
|
|
|
|
// In case of error on a serial port, don't prevent other serial port from making progress
|
|
|
|
gcode_line_error(PSTR(STR_ERR_LINE_NO), p);
|
|
|
|
break;
|
2017-11-05 08:49:38 -06:00
|
|
|
}
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
char *apos = strrchr(command, '*');
|
|
|
|
if (apos) {
|
|
|
|
uint8_t checksum = 0, count = uint8_t(apos - command);
|
|
|
|
while (count) checksum ^= command[--count];
|
2021-04-04 17:35:58 -05:00
|
|
|
if (strtol(apos + 1, nullptr, 10) != checksum) {
|
2021-02-26 16:54:46 -06:00
|
|
|
// In case of error on a serial port, don't prevent other serial port from making progress
|
|
|
|
gcode_line_error(PSTR(STR_ERR_CHECKSUM_MISMATCH), p);
|
|
|
|
break;
|
2021-01-24 00:43:23 -06:00
|
|
|
}
|
2017-11-05 08:49:38 -06:00
|
|
|
}
|
2021-02-26 16:54:46 -06:00
|
|
|
else {
|
|
|
|
// In case of error on a serial port, don't prevent other serial port from making progress
|
|
|
|
gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
serial.last_N = gcode_N;
|
|
|
|
}
|
|
|
|
#if ENABLED(SDSUPPORT)
|
|
|
|
// Pronterface "M29" and "M29 " has no line number
|
|
|
|
else if (card.flag.saving && !is_M29(command)) {
|
|
|
|
gcode_line_error(PSTR(STR_ERR_NO_CHECKSUM), p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2017-11-05 08:49:38 -06:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
//
|
|
|
|
// Movement commands give an alert when the machine is stopped
|
|
|
|
//
|
|
|
|
|
|
|
|
if (IsStopped()) {
|
|
|
|
char* gpos = strchr(command, 'G');
|
|
|
|
if (gpos) {
|
2021-04-04 17:35:58 -05:00
|
|
|
switch (strtol(gpos + 1, nullptr, 10)) {
|
2021-05-07 22:39:34 -05:00
|
|
|
case 0 ... 1:
|
|
|
|
TERN_(ARC_SUPPORT, case 2 ... 3:)
|
|
|
|
TERN_(BEZIER_CURVE_SUPPORT, case 5:)
|
2021-02-26 16:54:46 -06:00
|
|
|
PORT_REDIRECT(SERIAL_PORTMASK(p)); // Reply to the serial port that sent the command
|
|
|
|
SERIAL_ECHOLNPGM(STR_ERR_STOPPED);
|
|
|
|
LCD_MESSAGEPGM(MSG_STOPPED);
|
|
|
|
break;
|
2021-01-24 00:43:23 -06:00
|
|
|
}
|
2021-02-26 16:54:46 -06:00
|
|
|
}
|
|
|
|
}
|
2017-11-05 08:49:38 -06:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
#if DISABLED(EMERGENCY_PARSER)
|
|
|
|
// Process critical commands early
|
|
|
|
if (command[0] == 'M') switch (command[3]) {
|
|
|
|
case '8': if (command[2] == '0' && command[1] == '1') { wait_for_heatup = false; TERN_(HAS_LCD_MENU, wait_for_user = false); } break;
|
|
|
|
case '2': if (command[2] == '1' && command[1] == '1') kill(M112_KILL_STR, nullptr, true); break;
|
|
|
|
case '0': if (command[1] == '4' && command[2] == '1') quickstop_stepper(); break;
|
|
|
|
}
|
|
|
|
#endif
|
2020-02-10 14:52:15 -06:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
#if NO_TIMEOUTS > 0
|
|
|
|
last_command_time = ms;
|
|
|
|
#endif
|
2021-01-24 00:43:23 -06:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
// Add the command to the queue
|
|
|
|
ring_buffer.enqueue(serial.line_buffer, false
|
|
|
|
#if HAS_MULTI_SERIAL
|
|
|
|
, p
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
process_stream_char(serial_char, serial.input_state, serial.line_buffer, serial.count);
|
2021-01-24 00:43:23 -06:00
|
|
|
|
|
|
|
} // NUM_SERIAL loop
|
2017-09-07 22:33:16 -05:00
|
|
|
} // queue has space, serial has data
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLED(SDSUPPORT)
|
|
|
|
|
|
|
|
/**
|
2020-02-25 22:18:14 -06:00
|
|
|
* Get lines from the SD Card until the command buffer is full
|
|
|
|
* or until the end of the file is reached. Because this method
|
|
|
|
* always receives complete command-lines, they can go directly
|
|
|
|
* into the main command queue.
|
2017-09-07 22:33:16 -05:00
|
|
|
*/
|
2019-06-19 00:00:19 -05:00
|
|
|
inline void GCodeQueue::get_sdcard_commands() {
|
2020-02-10 14:52:15 -06:00
|
|
|
static uint8_t sd_input_state = PS_NORMAL;
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2018-10-19 13:52:44 -05:00
|
|
|
if (!IS_SD_PRINTING()) return;
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2020-02-10 14:52:15 -06:00
|
|
|
int sd_count = 0;
|
2021-02-26 16:54:46 -06:00
|
|
|
while (!ring_buffer.full() && !card.eof()) {
|
2017-09-07 22:33:16 -05:00
|
|
|
const int16_t n = card.get();
|
2020-11-26 21:18:40 -06:00
|
|
|
const bool card_eof = card.eof();
|
2020-02-26 03:02:03 -06:00
|
|
|
if (n < 0 && !card_eof) { SERIAL_ERROR_MSG(STR_SD_ERR_READ); continue; }
|
2020-02-25 22:18:14 -06:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
CommandLine &command = ring_buffer.commands[ring_buffer.index_w];
|
2020-02-10 14:52:15 -06:00
|
|
|
const char sd_char = (char)n;
|
2020-03-13 16:29:29 -05:00
|
|
|
const bool is_eol = ISEOL(sd_char);
|
2020-02-25 22:18:14 -06:00
|
|
|
if (is_eol || card_eof) {
|
2020-02-22 20:45:59 -06:00
|
|
|
|
|
|
|
// Reset stream state, terminate the buffer, and commit a non-empty command
|
2020-02-25 22:18:14 -06:00
|
|
|
if (!is_eol && sd_count) ++sd_count; // End of file with no newline
|
2021-02-26 16:54:46 -06:00
|
|
|
if (!process_line_done(sd_input_state, command.buffer, sd_count)) {
|
2020-11-26 21:18:40 -06:00
|
|
|
|
2021-02-07 18:16:43 -06:00
|
|
|
// M808 L saves the sdpos of the next line. M808 loops to a new sdpos.
|
2021-02-26 16:54:46 -06:00
|
|
|
TERN_(GCODE_REPEAT_MARKERS, repeat.early_parse_M808(command.buffer));
|
2020-11-26 21:18:40 -06:00
|
|
|
|
2021-03-12 08:34:22 -06:00
|
|
|
#if DISABLED(PARK_HEAD_ON_PAUSE)
|
|
|
|
// When M25 is non-blocking it can still suspend SD commands
|
|
|
|
// Otherwise the M125 handler needs to know SD printing is active
|
|
|
|
if (command.buffer[0] == 'M' && command.buffer[1] == '2' && command.buffer[2] == '5' && !NUMERIC(command.buffer[3]))
|
|
|
|
card.pauseSDPrint();
|
|
|
|
#endif
|
|
|
|
|
2020-11-26 21:18:40 -06:00
|
|
|
// Put the new command into the buffer (no "ok" sent)
|
2021-02-26 16:54:46 -06:00
|
|
|
ring_buffer.commit_command(true);
|
2020-11-26 21:18:40 -06:00
|
|
|
|
2021-02-26 16:54:46 -06:00
|
|
|
// Prime Power-Loss Recovery for the NEXT commit_command
|
2020-11-26 21:18:40 -06:00
|
|
|
TERN_(POWER_LOSS_RECOVERY, recovery.cmd_sdpos = card.getIndex());
|
2020-02-22 20:45:59 -06:00
|
|
|
}
|
|
|
|
|
2020-11-26 21:18:40 -06:00
|
|
|
if (card.eof()) card.fileHasFinished(); // Handle end of file reached
|
2017-09-07 22:33:16 -05:00
|
|
|
}
|
2020-02-10 14:52:15 -06:00
|
|
|
else
|
2021-02-26 16:54:46 -06:00
|
|
|
process_stream_char(sd_char, sd_input_state, command.buffer, sd_count);
|
2017-09-07 22:33:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // SDSUPPORT
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add to the circular command queue the next command from:
|
2020-04-09 20:05:58 -05:00
|
|
|
* - The command-injection queues (injected_commands_P, injected_commands)
|
2017-09-07 22:33:16 -05:00
|
|
|
* - The active serial input (usually USB)
|
|
|
|
* - The SD card file being actively printed
|
|
|
|
*/
|
2019-06-19 00:00:19 -05:00
|
|
|
void GCodeQueue::get_available_commands() {
|
2021-02-26 16:54:46 -06:00
|
|
|
if (ring_buffer.full()) return;
|
2017-09-07 22:33:16 -05:00
|
|
|
|
|
|
|
get_serial_commands();
|
|
|
|
|
2020-04-22 16:35:03 -05:00
|
|
|
TERN_(SDSUPPORT, get_sdcard_commands());
|
2017-09-07 22:33:16 -05:00
|
|
|
}
|
|
|
|
|
2021-03-06 09:04:05 -06:00
|
|
|
/**
|
|
|
|
* Run the entire queue in-place. Blocks SD completion/abort until complete.
|
|
|
|
*/
|
|
|
|
void GCodeQueue::exhaust() {
|
|
|
|
while (ring_buffer.occupied()) advance();
|
|
|
|
planner.synchronize();
|
|
|
|
}
|
|
|
|
|
2017-09-07 22:33:16 -05:00
|
|
|
/**
|
|
|
|
* Get the next command in the queue, optionally log it to SD, then dispatch it
|
|
|
|
*/
|
2019-06-19 00:00:19 -05:00
|
|
|
void GCodeQueue::advance() {
|
|
|
|
|
|
|
|
// Process immediate commands
|
2020-04-09 20:05:58 -05:00
|
|
|
if (process_injected_command_P() || process_injected_command()) return;
|
2017-09-07 22:33:16 -05:00
|
|
|
|
2019-06-19 00:00:19 -05:00
|
|
|
// Return if the G-code buffer is empty
|
2021-02-26 16:54:46 -06:00
|
|
|
if (ring_buffer.empty()) return;
|
2017-09-07 22:33:16 -05:00
|
|
|
|
|
|
|
#if ENABLED(SDSUPPORT)
|
|
|
|
|
2018-11-16 22:39:16 -06:00
|
|
|
if (card.flag.saving) {
|
2021-02-26 16:54:46 -06:00
|
|
|
char * const cmd = ring_buffer.peek_next_command_string();
|
|
|
|
if (is_M29(cmd)) {
|
2017-09-07 22:33:16 -05:00
|
|
|
// M29 closes the file
|
|
|
|
card.closefile();
|
2020-02-26 03:02:03 -06:00
|
|
|
SERIAL_ECHOLNPGM(STR_FILE_SAVED);
|
2017-10-02 02:47:30 -05:00
|
|
|
|
2020-10-14 13:48:46 -05:00
|
|
|
#if !defined(__AVR__) || !defined(USBCON)
|
2018-01-09 18:42:55 -06:00
|
|
|
#if ENABLED(SERIAL_STATS_DROPPED_RX)
|
2021-03-10 12:05:05 -06:00
|
|
|
SERIAL_ECHOLNPAIR("Dropped bytes: ", MYSERIAL1.dropped());
|
2018-01-09 18:42:55 -06:00
|
|
|
#endif
|
|
|
|
#if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
|
2021-03-10 12:05:05 -06:00
|
|
|
SERIAL_ECHOLNPAIR("Max RX Queue Size: ", MYSERIAL1.rxMaxEnqueued());
|
2018-01-09 18:42:55 -06:00
|
|
|
#endif
|
2020-08-06 08:14:00 -05:00
|
|
|
#endif
|
2017-10-02 02:47:30 -05:00
|
|
|
|
2017-09-07 22:33:16 -05:00
|
|
|
ok_to_send();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Write the string from the read buffer to SD
|
2021-02-26 16:54:46 -06:00
|
|
|
card.write_command(cmd);
|
2018-11-16 22:39:16 -06:00
|
|
|
if (card.flag.logging)
|
2017-09-07 22:33:16 -05:00
|
|
|
gcode.process_next_command(); // The card is saving because it's logging
|
|
|
|
else
|
|
|
|
ok_to_send();
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 01:47:06 -06:00
|
|
|
else
|
2017-09-07 22:33:16 -05:00
|
|
|
gcode.process_next_command();
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
gcode.process_next_command();
|
|
|
|
|
|
|
|
#endif // SDSUPPORT
|
|
|
|
|
|
|
|
// The queue may be reset by a command handler or by code invoked by idle() within a handler
|
2021-02-26 16:54:46 -06:00
|
|
|
ring_buffer.advance_pos(ring_buffer.index_r, -1);
|
2017-09-07 22:33:16 -05:00
|
|
|
}
|