| LUFA Library 140302 |

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Functions
Generic Byte Ring Buffer - LUFA/Drivers/Misc/RingBuffer.h
Miscellaneous Drivers

Lightweight ring buffer, for fast insertion/deletion of bytes. More...

## Data Structures
struct | RingBuffer_t
| Ring Buffer Management Structure. More...

## Functions
static uint16_t | RingBuffer_GetCount (RingBuffer_t *const Buffer) ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1)
static uint16_t | RingBuffer_GetFreeCount (RingBuffer_t *const Buffer) ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1)
static void | RingBuffer_InitBuffer (RingBuffer_t *Buffer, uint8_t *const DataPtr, const uint16_t Size) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2)
static void | RingBuffer_Insert (RingBuffer_t *Buffer, const uint8_t Data) ATTR_NON_NULL_PTR_ARG(1)
static bool | RingBuffer_IsEmpty (RingBuffer_t *const Buffer) ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1)
static bool | RingBuffer_IsFull (RingBuffer_t *const Buffer) ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1)
static uint8_t | RingBuffer_Peek (RingBuffer_t *const Buffer) ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1)
static uint8_t | RingBuffer_Remove (RingBuffer_t *Buffer) ATTR_NON_NULL_PTR_ARG(1)

## Detailed Description

# Module Source Dependencies

The following files must be built with any user project that uses this module:

- None

# Module Description

Lightweight ring buffer, for fast insertion/deletion. Multiple buffers can be created of different sizes to suit different needs.

Note that for each buffer, insertion and removal operations may occur at the same time (via a multi-threaded ISR based system) however the same kind of operation (two or more insertions or deletions) must not overlap. If there is possibility of two or more of the same kind of operating occurring at the same point in time, atomic (mutex) locking should be used.

# Example Usage

The following snippet is an example of how this module may be used within a typical application.

// Create the buffer structure and its underlying storage array
RingBuffer_t Buffer;
uint8_t BufferData[128];
// Initialize the buffer with the created storage array
RingBuffer_InitBuffer(&Buffer, BufferData, sizeof(BufferData));
// Insert some data into the buffer
RingBuffer_Insert(&Buffer, 'H');
RingBuffer_Insert(&Buffer, 'E');
RingBuffer_Insert(&Buffer, 'L');
RingBuffer_Insert(&Buffer, 'L');
RingBuffer_Insert(&Buffer, 'O');
// Cache the number of stored bytes in the buffer
uint16_t BufferCount = RingBuffer_GetCount(&Buffer);
// Printer stored data length
printf("Buffer Length: %d, Buffer Data: \r\n", BufferCount);
// Print contents of the buffer one character at a time
while (BufferCount--)
putc(RingBuffer_Remove(&Buffer));

## Function Documentation

static uint16_t RingBuffer_GetCount | ( | RingBuffer_t *const | Buffer | ) |
| inline static

Retrieves the current number of bytes stored in a particular buffer. This value is computed by entering an atomic lock on the buffer, so that the buffer cannot be modified while the computation takes place. This value should be cached when reading out the contents of the buffer, so that as small a time as possible is spent in an atomic lock.

Note
  The value returned by this function is guaranteed to only be the minimum number of bytes stored in the given buffer; this value may change as other threads write new data, thus the returned number should be used only to determine how many successive reads may safely be performed on the buffer.

Parameters

  [in] | Buffer | Pointer to a ring buffer structure whose count is to be computed.

Returns
  Number of bytes currently stored in the buffer.

static uint16_t RingBuffer_GetFreeCount | ( | RingBuffer_t *const | Buffer | ) |
| inline static

Retrieves the free space in a particular buffer. This value is computed by entering an atomic lock on the buffer, so that the buffer cannot be modified while the computation takes place.

Note
  The value returned by this function is guaranteed to only be the maximum number of bytes free in the given buffer; this value may change as other threads write new data, thus the returned number should be used only to determine how many successive writes may safely be performed on the buffer when there is a single writer thread.

Parameters

  [in] | Buffer | Pointer to a ring buffer structure whose free count is to be computed.

Returns
  Number of free bytes in the buffer.

static void RingBuffer_InitBuffer | ( | RingBuffer_t * | Buffer,
|  | uint8_t *const | DataPtr,
|  | const uint16_t | Size
| ) |  |
| inline static

Initializes a ring buffer ready for use. Buffers must be initialized via this function before any operations are called upon them. Already initialized buffers may be reset by re-initializing them using this function.

Parameters

  [out] | Buffer | Pointer to a ring buffer structure to initialize.
  [out] | DataPtr | Pointer to a global array that will hold the data stored into the ring buffer.
  [out] | Size | Maximum number of bytes that can be stored in the underlying data array.

static void RingBuffer_Insert | ( | RingBuffer_t * | Buffer,
|  | const uint8_t | Data
| ) |  |
| inline static

Inserts an element into the ring buffer.

Warning
  Only one execution thread (main program thread or an ISR) may insert into a single buffer otherwise data corruption may occur. Insertion and removal may occur from different execution threads.

Parameters

  [in,out] | Buffer | Pointer to a ring buffer structure to insert into.
  [in] | Data | Data element to insert into the buffer.

static bool RingBuffer_IsEmpty | ( | RingBuffer_t *const | Buffer | ) |
| inline static

Atomically determines if the specified ring buffer contains any data. This should be tested before removing data from the buffer, to ensure that the buffer does not underflow.

If the data is to be removed in a loop, store the total number of bytes stored in the buffer (via a call to the RingBuffer_GetCount() function) in a temporary variable to reduce the time spent in atomicity locks.

Parameters

  [in,out] | Buffer | Pointer to a ring buffer structure to insert into.

Returns
  Boolean `true` if the buffer contains no free space, `false` otherwise.

static bool RingBuffer_IsFull | ( | RingBuffer_t *const | Buffer | ) |
| inline static

Atomically determines if the specified ring buffer contains any free space. This should be tested before storing data to the buffer, to ensure that no data is lost due to a buffer overrun.

Parameters

  [in,out] | Buffer | Pointer to a ring buffer structure to insert into.

Returns
  Boolean `true` if the buffer contains no free space, `false` otherwise.

static uint8_t RingBuffer_Peek | ( | RingBuffer_t *const | Buffer | ) |
| inline static

Returns the next element stored in the ring buffer, without removing it.

Parameters

  [in,out] | Buffer | Pointer to a ring buffer structure to retrieve from.

Returns
  Next data element stored in the buffer.

static uint8_t RingBuffer_Remove | ( | RingBuffer_t * | Buffer | ) |
| inline static

Removes an element from the ring buffer.

Warning
  Only one execution thread (main program thread or an ISR) may remove from a single buffer otherwise data corruption may occur. Insertion and removal may occur from different execution threads.

Parameters

  [in,out] | Buffer | Pointer to a ring buffer structure to retrieve from.

Returns
  Next data element stored in the buffer.