From aa51a02b8f9439f6244db5e0f4a00f8d469b656c Mon Sep 17 00:00:00 2001 From: Chris Pepper Date: Thu, 5 Oct 2017 23:19:43 +0100 Subject: [PATCH] LPC1768: fix serial buffer underrun (#7854) When the buffer is empty index_write == index_read, but they needed constrained by buffer_mask --- Marlin/src/HAL/HAL_LPC1768/serial.h | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Marlin/src/HAL/HAL_LPC1768/serial.h b/Marlin/src/HAL/HAL_LPC1768/serial.h index c1f1b5249b..c98fe8a90a 100644 --- a/Marlin/src/HAL/HAL_LPC1768/serial.h +++ b/Marlin/src/HAL/HAL_LPC1768/serial.h @@ -56,12 +56,10 @@ public: return buffer_size - available(); } T read() volatile { - if(index_write != index_read) { - T val = buffer[buffer_mask & index_read]; - ++index_read; - return val; - } - return T(0); + if((buffer_mask & index_read) == (buffer_mask & index_write)) return T(-1); + T val = buffer[buffer_mask & index_read]; + ++index_read; + return val; } bool write( T value) volatile { uint32_t next_head = buffer_mask & (index_write + 1); @@ -73,10 +71,10 @@ public: return false; } bool empty() volatile { - return index_read == index_write; + return (buffer_mask & index_read) == (buffer_mask & index_write); } bool full() volatile { - return index_read == index_write + 1; + return index_read == buffer_mask & (index_write + 1); } void clear() volatile { index_read = index_write = 0; @@ -100,7 +98,6 @@ public: } char read() { - if(receive_buffer.empty()) return -1; return (char)receive_buffer.read(); } @@ -110,7 +107,7 @@ public: } operator bool() { - return true; + return host_connected; } uint16_t available() {