Browse Source

LPC1768: fix serial buffer underrun (#7854)

When the buffer is empty index_write == index_read, but they needed constrained by buffer_mask
Chris Pepper 7 years ago
parent
commit
aa51a02b8f
1 changed files with 7 additions and 10 deletions
  1. 7
    10
      Marlin/src/HAL/HAL_LPC1768/serial.h

+ 7
- 10
Marlin/src/HAL/HAL_LPC1768/serial.h View File

56
     return buffer_size - available();
56
     return buffer_size - available();
57
   }
57
   }
58
   T read() volatile {
58
   T read() volatile {
59
-    if(index_write != index_read) {
60
-      T val = buffer[buffer_mask & index_read];
61
-      ++index_read;
62
-      return val;
63
-    }
64
-    return T(0);
59
+    if((buffer_mask & index_read) == (buffer_mask & index_write)) return T(-1);
60
+    T val = buffer[buffer_mask & index_read];
61
+    ++index_read;
62
+    return val;
65
   }
63
   }
66
   bool write( T value) volatile {
64
   bool write( T value) volatile {
67
     uint32_t next_head = buffer_mask & (index_write + 1);
65
     uint32_t next_head = buffer_mask & (index_write + 1);
73
     return false;
71
     return false;
74
   }
72
   }
75
   bool empty() volatile {
73
   bool empty() volatile {
76
-    return index_read == index_write;
74
+    return (buffer_mask & index_read) == (buffer_mask & index_write);
77
   }
75
   }
78
   bool full() volatile {
76
   bool full() volatile {
79
-    return index_read == index_write + 1;
77
+    return index_read == buffer_mask & (index_write + 1);
80
   }
78
   }
81
   void clear() volatile {
79
   void clear() volatile {
82
     index_read = index_write = 0;
80
     index_read = index_write = 0;
100
   }
98
   }
101
 
99
 
102
   char read() {
100
   char read() {
103
-    if(receive_buffer.empty()) return -1;
104
     return (char)receive_buffer.read();
101
     return (char)receive_buffer.read();
105
   }
102
   }
106
 
103
 
110
   }
107
   }
111
 
108
 
112
   operator bool() {
109
   operator bool() {
113
-    return true;
110
+    return host_connected;
114
   }
111
   }
115
 
112
 
116
   uint16_t available() {
113
   uint16_t available() {

Loading…
Cancel
Save