PDA

View Full Version : Bug in teensy3 HardwareSerial peek helper functions



anakkii
09-19-2013, 05:29 AM
An eensy teensy bug with serial_peek():

I'm looking at the serial[1,2,3].c files in the teensy3 core library. I understand that RX and TX data handling is provided by a pair of ring buffers, where head points to the newest element, and tail points to the byte before the oldest element. However, the peek function is implemented as:



int serial_peek(void)
{
...
if (head == tail) return -1;
return rx_buffer[tail];
}


But it looks like it ought to be:



int serial_peek(void)
{
...
if (head == tail) return -1;
if (++tail >= RX_BUFFER_SIZE) tail = 0;
return rx_buffer[tail];
}

mlu
09-19-2013, 06:08 AM
You cannot change the buffer pointers in the peek function, so no ++tail.

PaulStoffregen
09-19-2013, 01:36 PM
Yes, you're right, it's a bug. I'll fix this in Teensyduino 1.17.

@mlu - in this case, "tail" is a local copy of the actual tail variable. It does need to be incremented.

anakkii
09-24-2013, 04:02 PM
You cannot change the buffer pointers in the peek function, so no ++tail.

Actually, tail is a copy, so it's okay.