Skip to content

Commit

Permalink
Ensure that the rx_buffer does not truncate G-Code and tell the host …
Browse files Browse the repository at this point in the history
…if it does (once)

Conflicts:
	Marlin/MarlinSerial.cpp

Report that the RX buffer was filled once

Ring buffer overflow also checks for \r and :

nonsense that will be squashed

Conflicts:
	Marlin/Marlin.h
	Marlin/MarlinSerial.cpp
	Marlin/Marlin_main.cpp
  • Loading branch information
scalez committed Mar 29, 2016
1 parent 023f085 commit dad9017
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Marlin/Marlin.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ inline void refresh_cmd_timeout() { previous_cmd_ms = millis(); }
#define CRITICAL_SECTION_END SREG = _sreg;
#endif

extern boolean comment_mode;
extern bool axis_relative_modes[];
extern int feedrate_multiplier;
extern bool volumetric_enabled;
Expand All @@ -276,6 +277,7 @@ extern float filament_size[EXTRUDERS]; // cross-sectional area of filament (in m
extern float volumetric_multiplier[EXTRUDERS]; // reciprocal of cross-sectional area of filament (in square millimeters), stored this way to reduce computational burden in planner
extern float current_position[NUM_AXIS];
extern float home_offset[3]; // axis[n].home_offset
extern bool rxbuf_filled;
extern float min_pos[3]; // axis[n].min_pos
extern float max_pos[3]; // axis[n].max_pos
extern bool axis_known_position[3]; // axis[n].is_known
Expand Down
49 changes: 35 additions & 14 deletions Marlin/MarlinSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,41 @@
ring_buffer rx_buffer = { { 0 }, 0, 0 };
#endif

FORCE_INLINE void store_char(unsigned char c) {
CRITICAL_SECTION_START;
uint8_t h = rx_buffer.head;
uint8_t i = (uint8_t)(h + 1) & (RX_BUFFER_SIZE - 1);

// if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer
// and so we don't write the character or advance the head.
if (i != rx_buffer.tail) {
rx_buffer.buffer[h] = c;
rx_buffer.head = i;
}
CRITICAL_SECTION_END;
bool rxbuf_filled = false;

FORCE_INLINE void store_char(unsigned char c)
{
static bool overflow = false;
if(overflow && !(c == '\n' || c == '\r' || (c == ':' && comment_mode == false)))
return;
else if(overflow && (c == '\n' || c == '\r' || (c == ':' && comment_mode == false)))
{
overflow = false;
return;
}

int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;

// if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer
// and so we need to move the head back to the end of the previous line.
if (i != rx_buffer.tail) {
rx_buffer.buffer[rx_buffer.head] = c;
rx_buffer.head = i;
}
else {
overflow = true;
i = (uint8_t)(rx_buffer.head - 1) % RX_BUFFER_SIZE;
for(; !(rx_buffer.buffer[i] == '\n' ||
rx_buffer.buffer[i] == '\r' ||
(rx_buffer.buffer[i] == ':' && comment_mode != true)) &&
rx_buffer.head != rx_buffer.tail; rx_buffer.head = i)
i = (uint8_t)(rx_buffer.head - 1) % RX_BUFFER_SIZE;
rx_buffer.head = (uint8_t)(i + 1) % RX_BUFFER_SIZE;;
if(!rxbuf_filled)
rxbuf_filled = true;
}
}


Expand Down
12 changes: 12 additions & 0 deletions Marlin/Marlin_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ static bool send_ok[BUFSIZE];
#endif

#if ENABLED(HOST_KEEPALIVE_FEATURE)
boolean comment_mode = false;

// States for managing Marlin and host communication
// Marlin sends messages if blocked or busy
Expand Down Expand Up @@ -967,6 +968,7 @@ void get_command() {

// Add the command to the queue
_enqueuecommand(serial_line_buffer, true);
rxbuf_filled = false;
}
else if (serial_count >= MAX_CMD_SIZE - 1) {
// Keep fetching, but ignore normal characters beyond the max length
Expand Down Expand Up @@ -7165,6 +7167,16 @@ void plan_arc(
*/
}

static bool reportrx_once = true;
if(rxbuf_filled && reportrx_once)
{
SERIAL_ERROR_START;
SERIAL_ERRORPGM(MSG_ERR_RXBUF_FULL);
reportrx_once = false;
}
else if(!rxbuf_filled)
reportrx_once = true;

#endif // SCARA

#if ENABLED(TEMP_STAT_LEDS)
Expand Down
1 change: 1 addition & 0 deletions Marlin/language.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
#define MSG_ERR_CHECKSUM_MISMATCH "checksum mismatch, Last Line: "
#define MSG_ERR_NO_CHECKSUM "No Checksum with line number, Last Line: "
#define MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM "No Line Number with checksum, Last Line: "
#define MSG_ERR_RXBUF_FULL "RXbuf full"
#define MSG_FILE_PRINTED "Done printing file"
#define MSG_BEGIN_FILE_LIST "Begin file list"
#define MSG_END_FILE_LIST "End file list"
Expand Down

0 comments on commit dad9017

Please sign in to comment.