Simplify serial port redirect (#13234)

This commit is contained in:
Scott Lahteine
2019-02-23 22:53:01 -06:00
committed by GitHub
parent 88cc1d1a31
commit e15354e387
22 changed files with 575 additions and 876 deletions

View File

@ -219,21 +219,22 @@ void ok_to_send() {
#if NUM_SERIAL > 1
const int16_t port = command_queue_port[cmd_queue_index_r];
if (port < 0) return;
PORT_REDIRECT(port);
#endif
if (!send_ok[cmd_queue_index_r]) return;
SERIAL_ECHOPGM_P(port, MSG_OK);
SERIAL_ECHOPGM(MSG_OK);
#if ENABLED(ADVANCED_OK)
char* p = command_queue[cmd_queue_index_r];
if (*p == 'N') {
SERIAL_ECHO_P(port, ' ');
SERIAL_ECHO_P(port, *p++);
SERIAL_ECHO(' ');
SERIAL_ECHO(*p++);
while (NUMERIC_SIGNED(*p))
SERIAL_ECHO_P(port, *p++);
SERIAL_ECHO(*p++);
}
SERIAL_ECHOPGM_P(port, " P"); SERIAL_ECHO_P(port, int(BLOCK_BUFFER_SIZE - planner.movesplanned() - 1));
SERIAL_ECHOPGM_P(port, " B"); SERIAL_ECHO_P(port, BUFSIZE - commands_in_queue);
SERIAL_ECHOPGM(" P"); SERIAL_ECHO(int(BLOCK_BUFFER_SIZE - planner.movesplanned() - 1));
SERIAL_ECHOPGM(" B"); SERIAL_ECHO(BUFSIZE - commands_in_queue);
#endif
SERIAL_EOL_P(port);
SERIAL_EOL();
}
/**
@ -244,10 +245,11 @@ void flush_and_request_resend() {
#if NUM_SERIAL > 1
const int16_t port = command_queue_port[cmd_queue_index_r];
if (port < 0) return;
PORT_REDIRECT(port);
#endif
SERIAL_FLUSH_P(port);
SERIAL_ECHOPGM_P(port, MSG_RESEND);
SERIAL_ECHOLN_P(port, gcode_LastN + 1);
SERIAL_FLUSH();
SERIAL_ECHOPGM(MSG_RESEND);
SERIAL_ECHOLN(gcode_LastN + 1);
ok_to_send();
}
@ -270,10 +272,11 @@ inline int read_serial(const uint8_t index) {
}
}
void gcode_line_error(PGM_P err, uint8_t port) {
SERIAL_ERROR_START_P(port);
serialprintPGM_P(port, err);
SERIAL_ECHOLN_P(port, gcode_LastN);
void gcode_line_error(PGM_P const err, const int8_t port) {
PORT_REDIRECT(port);
SERIAL_ERROR_START();
serialprintPGM(err);
SERIAL_ECHOLN(gcode_LastN);
while (read_serial(port) != -1); // clear out the RX buffer
flush_and_request_resend();
serial_count[port] = 0;
@ -281,12 +284,6 @@ void gcode_line_error(PGM_P err, uint8_t port) {
#if ENABLED(FAST_FILE_TRANSFER)
#if ENABLED(SDSUPPORT)
#define CARD_CHAR_P(C) SERIAL_CHAR_P(card.transfer_port, C)
#define CARD_ECHO_P(V) SERIAL_ECHO_P(card.transfer_port, V)
#define CARD_ECHOLN_P(V) SERIAL_ECHOLN_P(card.transfer_port, V)
#endif
inline bool serial_data_available(const uint8_t index) {
switch (index) {
case 0: return MYSERIAL0.available();
@ -380,6 +377,11 @@ void gcode_line_error(PGM_P err, uint8_t port) {
void receive(char (&buffer)[buffer_size]) {
uint8_t data = 0;
millis_t transfer_timeout = millis() + RX_TIMESLICE;
#if ENABLED(SDSUPPORT)
PORT_REDIRECT(card.transfer_port);
#endif
while (PENDING(millis(), transfer_timeout)) {
switch (stream_state) {
case StreamState::STREAM_RESET:
@ -388,24 +390,24 @@ void gcode_line_error(PGM_P err, uint8_t port) {
packet_reset();
stream_state = StreamState::PACKET_HEADER;
break;
case StreamState::STREAM_HEADER: // we could also transfer the filename in this packet, rather than handling it in the gcode
for (size_t i = 0; i < sizeof(stream_header); ++i) {
case StreamState::STREAM_HEADER: // The filename could also be in this packet, rather than handling it in the gcode
for (size_t i = 0; i < sizeof(stream_header); ++i)
stream_header_bytes[i] = buffer[i];
}
if (stream_header.token == 0x1234) {
stream_state = StreamState::PACKET_RESET;
bytes_received = 0;
time_stream_start = millis();
CARD_ECHO_P("echo: Datastream initialized (");
CARD_ECHO_P(stream_header.filesize);
CARD_ECHOLN_P("Bytes expected)");
CARD_ECHO_P("so"); // confirm active stream and the maximum block size supported
CARD_CHAR_P(static_cast<uint8_t>(buffer_size & 0xFF));
CARD_CHAR_P(static_cast<uint8_t>((buffer_size >> 8) & 0xFF));
CARD_CHAR_P('\n');
SERIAL_ECHO("echo: Datastream initialized (");
SERIAL_ECHO(stream_header.filesize);
SERIAL_ECHOLN("Bytes expected)");
SERIAL_ECHO("so"); // confirm active stream and the maximum block size supported
SERIAL_CHAR(static_cast<uint8_t>(buffer_size & 0xFF));
SERIAL_CHAR(static_cast<uint8_t>((buffer_size >> 8) & 0xFF));
SERIAL_CHAR('\n');
}
else {
CARD_ECHOLN_P("echo: Datastream initialization error (invalid token)");
SERIAL_ECHOLN("echo: Datastream initialization error (invalid token)");
stream_state = StreamState::STREAM_FAILED;
}
buffer_next_index = 0;
@ -421,7 +423,7 @@ void gcode_line_error(PGM_P err, uint8_t port) {
stream_state = StreamState::PACKET_DATA;
}
else {
CARD_ECHO_P("echo: Datastream packet out of order");
SERIAL_ECHO("echo: Datastream packet out of order");
stream_state = StreamState::PACKET_FLUSHRX;
}
}
@ -433,7 +435,7 @@ void gcode_line_error(PGM_P err, uint8_t port) {
buffer[buffer_next_index] = data;
}
else {
CARD_ECHO_P("echo: Datastream packet data buffer overrun");
SERIAL_ECHO("echo: Datastream packet data buffer overrun");
stream_state = StreamState::STREAM_FAILED;
break;
}
@ -458,22 +460,22 @@ void gcode_line_error(PGM_P err, uint8_t port) {
else {
if (bytes_received < stream_header.filesize) {
stream_state = StreamState::PACKET_RESET; // reset and receive next packet
CARD_ECHOLN_P("ok"); // transmit confirm packet received and valid token
SERIAL_ECHOLN("ok"); // transmit confirm packet received and valid token
}
else {
stream_state = StreamState::STREAM_COMPLETE; // no more data required
}
if (card.write(buffer, buffer_next_index) < 0) {
stream_state = StreamState::STREAM_FAILED;
CARD_ECHO_P("echo: IO ERROR");
SERIAL_ECHO("echo: IO ERROR");
break;
};
}
}
else {
CARD_ECHO_P("echo: Block(");
CARD_ECHO_P(packet.header.id);
CARD_ECHOLN_P(") Corrupt");
SERIAL_ECHO("echo: Block(");
SERIAL_ECHO(packet.header.id);
SERIAL_ECHOLN(") Corrupt");
stream_state = StreamState::PACKET_FLUSHRX;
}
break;
@ -481,9 +483,9 @@ void gcode_line_error(PGM_P err, uint8_t port) {
if (packet_retries < MAX_RETRIES) {
packet_retries ++;
stream_state = StreamState::PACKET_RESET;
CARD_ECHO_P("echo: Resend request ");
CARD_ECHOLN_P(packet_retries);
CARD_ECHOLN_P("rs"); // transmit resend packet token
SERIAL_ECHO("echo: Resend request ");
SERIAL_ECHOLN(packet_retries);
SERIAL_ECHOLN("rs"); // transmit resend packet token
}
else {
stream_state = StreamState::STREAM_FAILED;
@ -499,27 +501,27 @@ void gcode_line_error(PGM_P err, uint8_t port) {
packet.timeout = millis() + STREAM_MAX_WAIT;
break;
case StreamState::PACKET_TIMEOUT:
CARD_ECHOLN_P("echo: Datastream timeout");
SERIAL_ECHOLN("echo: Datastream timeout");
stream_state = StreamState::PACKET_RESEND;
break;
case StreamState::STREAM_COMPLETE:
stream_state = StreamState::STREAM_RESET;
card.flag.binary_mode = false;
card.closefile();
CARD_ECHO_P("echo: ");
CARD_ECHO_P(card.filename);
CARD_ECHO_P(" transfer completed @ ");
CARD_ECHO_P(((bytes_received / (millis() - time_stream_start) * 1000) / 1024 ));
CARD_ECHOLN_P("KiB/s");
CARD_ECHOLN_P("sc"); // transmit stream complete token
SERIAL_ECHO("echo: ");
SERIAL_ECHO(card.filename);
SERIAL_ECHO(" transfer completed @ ");
SERIAL_ECHO(((bytes_received / (millis() - time_stream_start) * 1000) / 1024 ));
SERIAL_ECHOLN("KiB/s");
SERIAL_ECHOLN("sc"); // transmit stream complete token
return;
case StreamState::STREAM_FAILED:
stream_state = StreamState::STREAM_RESET;
card.flag.binary_mode = false;
card.closefile();
card.removeFile(card.filename);
CARD_ECHOLN_P("echo: File transfer failed");
CARD_ECHOLN_P("sf"); // transmit stream failed token
SERIAL_ECHOLN("echo: File transfer failed");
SERIAL_ECHOLN("sf"); // transmit stream failed token
return;
}
}