Ring buffer

group LWGSM_BUFF

Generic ring buffer.

Defines

BUF_PREF(x)

Buffer function/typedef prefix string.

It is used to change function names in zero time to easily re-use same library between applications. Use #define BUF_PREF(x) my_prefix_ ## x to change all function names to (for example) my_prefix_buff_init

Note

Modification of this macro must be done in header and source file aswell

Functions

uint8_t lwgsm_buff_init(lwgsm_buff_t *buff, size_t size)

Initialize buffer.

Parameters
  • buff[in] Pointer to buffer structure

  • size[in] Size of buffer in units of bytes

Returns

1 on success, 0 otherwise

void lwgsm_buff_free(lwgsm_buff_t *buff)

Free dynamic allocation if used on memory.

Parameters

buff[in] Pointer to buffer structure

void lwgsm_buff_reset(lwgsm_buff_t *buff)

Resets buffer to default values. Buffer size is not modified.

Parameters

buff[in] Buffer handle

size_t lwgsm_buff_write(lwgsm_buff_t *buff, const void *data, size_t btw)

Write data to buffer Copies data from data array to buffer and marks buffer as full for maximum count number of bytes.

Parameters
  • buff[in] Buffer handle

  • data[in] Pointer to data to write into buffer

  • btw[in] Number of bytes to write

Returns

Number of bytes written to buffer. When returned value is less than btw, there was no enough memory available to copy full data array

size_t lwgsm_buff_read(lwgsm_buff_t *buff, void *data, size_t btr)

Read data from buffer Copies data from buffer to data array and marks buffer as free for maximum btr number of bytes.

Parameters
  • buff[in] Buffer handle

  • data[out] Pointer to output memory to copy buffer data to

  • btr[in] Number of bytes to read

Returns

Number of bytes read and copied to data array

size_t lwgsm_buff_peek(lwgsm_buff_t *buff, size_t skip_count, void *data, size_t btp)

Read from buffer without changing read pointer (peek only)

Parameters
  • buff[in] Buffer handle

  • skip_count[in] Number of bytes to skip before reading data

  • data[out] Pointer to output memory to copy buffer data to

  • btp[in] Number of bytes to peek

Returns

Number of bytes peeked and written to output array

size_t lwgsm_buff_get_free(lwgsm_buff_t *buff)

Get number of bytes in buffer available to write.

Parameters

buff[in] Buffer handle

Returns

Number of free bytes in memory

size_t lwgsm_buff_get_full(lwgsm_buff_t *buff)

Get number of bytes in buffer available to read.

Parameters

buff[in] Buffer handle

Returns

Number of bytes ready to be read

void *lwgsm_buff_get_linear_block_read_address(lwgsm_buff_t *buff)

Get linear address for buffer for fast read.

Parameters

buff[in] Buffer handle

Returns

Linear buffer start address

size_t lwgsm_buff_get_linear_block_read_length(lwgsm_buff_t *buff)

Get length of linear block address before it overflows for read operation.

Parameters

buff[in] Buffer handle

Returns

Linear buffer size in units of bytes for read operation

size_t lwgsm_buff_skip(lwgsm_buff_t *buff, size_t len)

Skip (ignore; advance read pointer) buffer data Marks data as read in the buffer and increases free memory for up to len bytes.

Note

Useful at the end of streaming transfer such as DMA

Parameters
  • buff[in] Buffer handle

  • len[in] Number of bytes to skip and mark as read

Returns

Number of bytes skipped

void *lwgsm_buff_get_linear_block_write_address(lwgsm_buff_t *buff)

Get linear address for buffer for fast read.

Parameters

buff[in] Buffer handle

Returns

Linear buffer start address

size_t lwgsm_buff_get_linear_block_write_length(lwgsm_buff_t *buff)

Get length of linear block address before it overflows for write operation.

Parameters

buff[in] Buffer handle

Returns

Linear buffer size in units of bytes for write operation

size_t lwgsm_buff_advance(lwgsm_buff_t *buff, size_t len)

Advance write pointer in the buffer. Similar to skip function but modifies write pointer instead of read.

Note

Useful when hardware is writing to buffer and application needs to increase number of bytes written to buffer by hardware

Parameters
  • buff[in] Buffer handle

  • len[in] Number of bytes to advance

Returns

Number of bytes advanced for write operation

struct lwgsm_buff_t
#include <lwgsm_types.h>

Buffer structure.

Public Members

uint8_t *buff

Pointer to buffer data. Buffer is considered initialized when buff != NULL

size_t size

Size of buffer data. Size of actual buffer is 1 byte less than this value

size_t r

Next read pointer. Buffer is considered empty when r == w and full when w == r - 1

size_t w

Next write pointer. Buffer is considered empty when r == w and full when w == r - 1