]> git.tdb.fi Git - model-railway-devices.git/blobdiff - arducontrol/s88.c
Avoid a race condition with the last bytes of an S88 read
[model-railway-devices.git] / arducontrol / s88.c
index cfe536734be49a78fa7642193148822120304bcb..bd53deb72f55e252e2c88db1ed609e42fa896bae 100644 (file)
@@ -28,15 +28,17 @@ void s88_init(void)
 
 void s88_check(void)
 {
+       /* Check remaining read count before ringbuffer fill.  If it is zero, no
+       more bytes are arriving in the buffer and the contents can be sent out. */
+       uint8_t rc = s88_read_count;
        uint8_t count = ringbuffer_fill(s88_buffer);
-       if(count>=4 || (count>0 && !s88_read_count))
+       if(count>=4 || (count>0 && !rc))
        {
                uint8_t reply[10];
-               uint8_t i;
 
                reply[0] = S88_DATA;
                reply[1] = s88_out_index;
-               for(i=0; i<count; ++i)
+               for(uint8_t i=0; i<count; ++i)
                        reply[2+i] = ringbuffer_pop(s88_buffer);
                interface_send(reply, 2+count);
                s88_out_index += count;