defragster
Senior Member+
opps. no problem here - move along
Last edited:
that defense doesn't resonate here!Bug or no bug, I wont argue, but you can see it this way:
Out of the buffer bytes allocated one byte is reserved to keep track of full or empty status of the buffer.
The remaining n-1 bytes are available for buffer data, and that number is reported.
A similar situation exists for C character strings, where the last byte is reserved for a terminating 0 byte. Not respecting this will end you up in lots of fun debugging of weird problems.
it is common for circular buffers to have a capacity of one less than the number of entries.
so does that mean I cannot really send 512 in one shot? it will send as 511 and 1 ?
Since the TX_BUFFER_SIZE is internal to the library, I think the method should return true or false instead to be more intuitive, as it seems as long as there is 1 byte available, you can call the write function.
Look around the 'web for ring buffer code.
Most use pointers, not indices. And many use a counter rather than the difference of head and tail (modulo).
I see absolutely no reason why the serial_write_buffer_free function cannot return the actual free bytes. I think this is a bug.
What I am saying there is absolutely no reason why -1 should be there ..... I used circular buffer and never had the need to lose 1 byte.
that defense doesn't resonate here!
int serial_available(void)
{
uint32_t head, tail;
head = rx_buffer_head;
tail = rx_buffer_tail;
if ([U]head > tail[/U]) return head - tail;
[U]if (RX_BUFFER_SIZE == tail) return 0;[/U]
return RX_BUFFER_SIZE + head - tail;
}
int serial_getchar(void)
{
uint32_t head, tail;
int c;
head = rx_buffer_head;
tail = rx_buffer_tail;
[U]// if (head == tail) return -1;[/U]
if (RX_BUFFER_SIZE == tail) return -1;
if (++tail >= RX_BUFFER_SIZE) tail = 0;
c = rx_buffer[tail];
[U]if (++head >= RX_BUFFER_SIZE) head = 0;[/U]
[U]if (head == tail) tail = RX_BUFFER_SIZE;[/U]
rx_buffer_tail = tail;
return c;
}
#if RX_BUFFER_SIZE > [U]254[/U]
static volatile uint16_t rx_buffer_head = 0;
static volatile uint16_t rx_buffer_tail [U]= RX_BUFFER_SIZE;[/U]
#else
static volatile uint8_t rx_buffer_head = 0;
static volatile uint8_t rx_buffer_tail = [U]RX_BUFFER_SIZE;[/U]
#endif