Ring Buffer

  • This is the ring or circular buffer implementation with fixed size of user initialization.

  • The main buffer structure variable and the internal data buffer both should be declared on the application side and the pointers of the variables are used in the module.

Usage

  1. All API functions have the first argument as the buffer structure.

  2. Internal data buffer size and pointer are initialized with module init function.

  3. Module read and module write functions are used to read/write data bytes.

  4. Other API functions to return the status of buffer whether empty or full.

  5. Return codes of API functions are defined in header file.

Ring Buffer calls

Function

Description

rs_ring_buf_init

Function to initialize the ring buffer

rs_ring_buf_push

Function to push the data into the ring buffer

rs_ring_buf_pop

Function to pop the data from the ring buffer

rs_ring_buf_peek

Function to get top of the data

rs_ring_buf_get_used_space

Function to get buffer count

rs_ring_buf_get_free_space

Function to get remaining bytes

rs_ring_buf_is_full

Function to know the buffer is full or not

rs_ring_buf_is_empty

Function to know the buffer is empty or not

rs_ring_buf_flush

Function to reset the elements of the ring buffer structure

Using these ‘rs’ calls to initialize the ring buffer, push and pop from the structure via ring buffer, data is obtained from the structure, and all filter structure elements are flushed.

Error Code

  • Every API’s for the ring buffer module returns some success or failure values. Please refer below section,

Example Demo

Please refer below section,

Ring Buffer Header Details

Documentation from the relevant header as follows:

This file provides the definitions and prototypes for ring_buffer.

This is the header file for Ring buffer module which contains the definitions, prototypes, external variables.

Date

19 Apr 2022

Author

Embien RAPIDSEA Team

Attention

Copyright (c) 2022 Embien Technologies India Pvt. Ltd.. All rights reserved.

Typedefs

typedef struct tag_rs_ring_buf rs_ring_buf_t

Functions

rs_ret_val_t rs_ring_buf_init(rs_ring_buf_t *ptr_ring_buf, uint8_t *ptr_buf_data, uint32_t u32_buffer_size)

Initialize the buffer module with data buffer pointer and indexes.

This function Initializes the buffer module with data buffer pointer and indexes

Parameters:
  • ptr_ring_buf[in] - Pointer to buffer

  • ptr_buf_data[in] - Pointer to data buffer

  • u32_buffer_size[in] - Size of the data buffer

Returns:

0 on success or error code on failure

rs_ret_val_t rs_ring_buf_push(rs_ring_buf_t *ptr_ring_buf, uint8_t *ptr_in_data, uint32_t u32_data_size)

Receive the input data as stream of bytes and write into the data buffer.

This function receives the input data as stream of bytes and write into the data buffer

Parameters:
  • ptr_ring_buf[in] - Pointer to buffer

  • ptr_in_data[in] - Input data stream

  • u32_data_size[in] - size of the data

Returns:

0 on success or error code on failure

rs_ret_val_t rs_ring_buf_pop(rs_ring_buf_t *ptr_ring_buf, uint8_t *ptr_data, uint32_t u32_max_data_size)

Receive the empty container to store output data and the number of bytes to read from the data buffer.

This function receives the empty container to store output data and the number of bytes to read from the data buffer

Parameters:
  • ptr_ring_buf[in] - Pointer to buffer

  • ptr_data[out] - Output data stream

  • u32_max_data_size[in] - Maximum data size

Returns:

0 on success or error code on failure

rs_ret_val_t rs_ring_buf_peek(rs_ring_buf_t *ptr_ring_buf, uint8_t *ptr_data, uint32_t u32_max_data_size)

Peeks the message data from the buffer as per the given size of input.

This function peeks the message data from the buffer as per the given size of input

Parameters:
  • ptr_ring_buf[in] - Pointer to buffer

  • ptr_data[out] - Output data stream

  • u32_max_data_size[in] - Maximum data size

Returns:

0 on success or error code on failure

uint32_t rs_ring_buf_get_used_space(rs_ring_buf_t *ptr_ring_buf)

Returns the current consumed bytes in the data buffer.

This function returns the current consumed bytes in the data buffer

Parameters:

ptr_ring_buf[in] - Pointer to buffer

Returns:

Used space in bytes

uint32_t rs_ring_buf_get_free_space(rs_ring_buf_t *ptr_ring_buf)

Returns remaining bytes in the data buffer.

This function returns remaining bytes in the data buffer

Parameters:

ptr_ring_buf[in] - Pointer to buffer

Returns:

Free space in bytes

rs_ret_val_t rs_ring_buf_is_full(rs_ring_buf_t *ptr_ring_buf)

Checks and return buffer is full or not full.

This function checks and return buffer is full or not full

Parameters:

ptr_ring_buf[in] - Pointer to buffer

Returns:

1 on success or 0 on failure

rs_ret_val_t rs_ring_buf_is_empty(rs_ring_buf_t *ptr_ring_buf)

Checks and return buffer is empty or not empty.

This function checks and return buffer is empty or not empty

Parameters:

ptr_ring_buf[in] - Pointer to buffer

Returns:

1 on success or 0 on failure

rs_ret_val_t rs_ring_buf_flush(rs_ring_buf_t *ptr_ring_buf)

Clears the data buffer and reset the read/write indexes.

This function clears the data buffer and reset the read/write indexes

Parameters:

ptr_ring_buf[in] - Pointer to buffer

Returns:

0 on success or error code on failure

struct tag_rs_ring_buf

Public Members

uint8_t *ptr_buff

Pointer to the storage area

uint32_t read_index

Next index to read from

uint32_t write_index

Next index to write to

uint32_t buff_count

Buffer count

uint32_t buff_size

Size of the storage area