Pete Fluery's UART library - spare buffer space

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi.

The Peter Fleury's library ( for UART usage ) seems to be excellent.

                 http://www.peterfleury.epizy.com...

The UART library permits buffering in both directions.  Also, I suspect that data can be sent to the PC (from Arduino) while the Arduino mainline performs other TWI/I2C comms with an EEPROM (this is my interpretation of Oscilloscope readings).  That implies overlaying communication operations (to different devices) occur at the same time.

 

In transmitting data with this UART library, I would like to be able to determine the space available in the circular buffer before committing to a transmit. 

Can this be done ??

 

Regards JC.......

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You should be able to easily test for available buffer space.

 

There are two indices for the buffer. One is typically called "head" and the other is typically called "tail". When they are next to each other (tail in front of head), then there is no space. You need to be careful about the point where the buffer wraps into a circle. 

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

On the tx side, if the tx buffer is full, the Fleury uart_putc will sit in a loop waiting until space becomes available as the tx interrupt empties the buffer.

This works as long as you don't call it with interrupts blocked, and don't call it from an interrupt handler.

 

Both tx and rx circular buffers use the method that head == tail means the buffer is empty, and you always have to leave one space free in the buffer so the maximum capacity is (BUFFER_SIZE - 1). If you didn't leave one space free, you would have head == tail also when the buffer was full and so wouldn't be able to tell the difference between empty/full.

 

To determine how much space is available, you can first calculate how much is used, then subtract that from the maximum capacity (BUFFER_SIZE -1).

Untested

 

uint8_t used;
if (head >= tail)
    used = head - tail;
else
    used = BUFFER_SIZE + head - tail;

uint8_t free_space = (BUFFER_SIZE - 1) - used;