LPC1768: fix serial buffer underrun (#7854)

When the buffer is empty index_write == index_read, but they needed constrained by buffer_mask
This commit is contained in:
Chris Pepper 2017-10-05 23:19:43 +01:00 committed by Roxy-3D
parent 46b2773e13
commit aa51a02b8f

View file

@ -56,12 +56,10 @@ public:
return buffer_size - available(); return buffer_size - available();
} }
T read() volatile { T read() volatile {
if(index_write != index_read) { if((buffer_mask & index_read) == (buffer_mask & index_write)) return T(-1);
T val = buffer[buffer_mask & index_read]; T val = buffer[buffer_mask & index_read];
++index_read; ++index_read;
return val; return val;
}
return T(0);
} }
bool write( T value) volatile { bool write( T value) volatile {
uint32_t next_head = buffer_mask & (index_write + 1); uint32_t next_head = buffer_mask & (index_write + 1);
@ -73,10 +71,10 @@ public:
return false; return false;
} }
bool empty() volatile { bool empty() volatile {
return index_read == index_write; return (buffer_mask & index_read) == (buffer_mask & index_write);
} }
bool full() volatile { bool full() volatile {
return index_read == index_write + 1; return index_read == buffer_mask & (index_write + 1);
} }
void clear() volatile { void clear() volatile {
index_read = index_write = 0; index_read = index_write = 0;
@ -100,7 +98,6 @@ public:
} }
char read() { char read() {
if(receive_buffer.empty()) return -1;
return (char)receive_buffer.read(); return (char)receive_buffer.read();
} }
@ -110,7 +107,7 @@ public:
} }
operator bool() { operator bool() {
return true; return host_connected;
} }
uint16_t available() { uint16_t available() {