Zephyr API Documentation 4.0.0-rc2
A Scalable Open Source RTOS
|
Simple ring buffer implementation. More...
Data Structures | |
struct | ring_buf |
A structure to represent a ring buffer. More... | |
Macros | |
#define | RING_BUF_DECLARE(name, size8) |
Define and initialize a ring buffer for byte data. | |
#define | RING_BUF_ITEM_DECLARE(name, size32) |
Define and initialize an "item based" ring buffer. | |
#define | RING_BUF_ITEM_DECLARE_SIZE(name, size32) |
Define and initialize an "item based" ring buffer. | |
#define | RING_BUF_ITEM_DECLARE_POW2(name, pow) |
Define and initialize a power-of-2 sized "item based" ring buffer. | |
#define | RING_BUF_ITEM_SIZEOF(expr) |
Compute the ring buffer size in 32-bit needed to store an element. | |
Functions | |
static void | ring_buf_internal_reset (struct ring_buf *buf, int32_t value) |
Function to force ring_buf internal states to given value. | |
static void | ring_buf_init (struct ring_buf *buf, uint32_t size, uint8_t *data) |
Initialize a ring buffer for byte data. | |
static void | ring_buf_item_init (struct ring_buf *buf, uint32_t size, uint32_t *data) |
Initialize an "item based" ring buffer. | |
static bool | ring_buf_is_empty (struct ring_buf *buf) |
Determine if a ring buffer is empty. | |
static void | ring_buf_reset (struct ring_buf *buf) |
Reset ring buffer state. | |
static uint32_t | ring_buf_space_get (struct ring_buf *buf) |
Determine free space in a ring buffer. | |
static uint32_t | ring_buf_item_space_get (struct ring_buf *buf) |
Determine free space in an "item based" ring buffer. | |
static uint32_t | ring_buf_capacity_get (struct ring_buf *buf) |
Return ring buffer capacity. | |
static uint32_t | ring_buf_size_get (struct ring_buf *buf) |
Determine used space in a ring buffer. | |
uint32_t | ring_buf_put_claim (struct ring_buf *buf, uint8_t **data, uint32_t size) |
Allocate buffer for writing data to a ring buffer. | |
int | ring_buf_put_finish (struct ring_buf *buf, uint32_t size) |
Indicate number of bytes written to allocated buffers. | |
uint32_t | ring_buf_put (struct ring_buf *buf, const uint8_t *data, uint32_t size) |
Write (copy) data to a ring buffer. | |
uint32_t | ring_buf_get_claim (struct ring_buf *buf, uint8_t **data, uint32_t size) |
Get address of a valid data in a ring buffer. | |
int | ring_buf_get_finish (struct ring_buf *buf, uint32_t size) |
Indicate number of bytes read from claimed buffer. | |
uint32_t | ring_buf_get (struct ring_buf *buf, uint8_t *data, uint32_t size) |
Read data from a ring buffer. | |
uint32_t | ring_buf_peek (struct ring_buf *buf, uint8_t *data, uint32_t size) |
Peek at data from a ring buffer. | |
int | ring_buf_item_put (struct ring_buf *buf, uint16_t type, uint8_t value, uint32_t *data, uint8_t size32) |
Write a data item to a ring buffer. | |
int | ring_buf_item_get (struct ring_buf *buf, uint16_t *type, uint8_t *value, uint32_t *data, uint8_t *size32) |
Read a data item from a ring buffer. | |
Simple ring buffer implementation.
#define RING_BUF_DECLARE | ( | name, | |
size8 ) |
#include <zephyr/sys/ring_buffer.h>
Define and initialize a ring buffer for byte data.
This macro establishes a ring buffer of an arbitrary size. The basic storage unit is a byte.
The ring buffer can be accessed outside the module where it is defined using:
name | Name of the ring buffer. |
size8 | Size of ring buffer (in bytes). |
#define RING_BUF_ITEM_DECLARE | ( | name, | |
size32 ) |
#include <zephyr/sys/ring_buffer.h>
Define and initialize an "item based" ring buffer.
This macro establishes an "item based" ring buffer. Each data item is an array of 32-bit words (from zero to 1020 bytes in length), coupled with a 16-bit type identifier and an 8-bit integer value.
The ring buffer can be accessed outside the module where it is defined using:
name | Name of the ring buffer. |
size32 | Size of ring buffer (in 32-bit words). |
#define RING_BUF_ITEM_DECLARE_POW2 | ( | name, | |
pow ) |
#include <zephyr/sys/ring_buffer.h>
Define and initialize a power-of-2 sized "item based" ring buffer.
This macro establishes an "item based" ring buffer by specifying its size using a power of 2. This exists mainly for backward compatibility reasons. RING_BUF_ITEM_DECLARE should be used instead.
name | Name of the ring buffer. |
pow | Ring buffer size exponent. |
#define RING_BUF_ITEM_DECLARE_SIZE | ( | name, | |
size32 ) |
#include <zephyr/sys/ring_buffer.h>
Define and initialize an "item based" ring buffer.
This exists for backward compatibility reasons. RING_BUF_ITEM_DECLARE should be used instead.
name | Name of the ring buffer. |
size32 | Size of ring buffer (in 32-bit words). |
#define RING_BUF_ITEM_SIZEOF | ( | expr | ) |
#include <zephyr/sys/ring_buffer.h>
Compute the ring buffer size in 32-bit needed to store an element.
The argument can be a type or an expression. Note: rounds up if the size is not a multiple of 32 bits.
expr | Expression or type to compute the size of |
#include <zephyr/sys/ring_buffer.h>
Return ring buffer capacity.
buf | Address of ring buffer. |
#include <zephyr/sys/ring_buffer.h>
Read data from a ring buffer.
This routine reads data from a ring buffer buf.
buf | Address of ring buffer. |
data | Address of the output buffer. Can be NULL to discard data. |
size | Data size (in bytes). |
Number | of bytes written to the output buffer. |
#include <zephyr/sys/ring_buffer.h>
Get address of a valid data in a ring buffer.
With this routine, memory copying can be reduced since internal ring buffer can be used directly by the user. Once data is processed it must be freed using ring_buf_get_finish.
[in] | buf | Address of ring buffer. |
[out] | data | Pointer to the address. It is set to a location within ring buffer. |
[in] | size | Requested size (in bytes). |
#include <zephyr/sys/ring_buffer.h>
Indicate number of bytes read from claimed buffer.
The number of bytes must be equal or lower than the sum corresponding to all preceding ring_buf_get_claim invocations (or even 0). Surplus bytes will remain available in the buffer.
buf | Address of ring buffer. |
size | Number of bytes that can be freed. |
0 | Successful operation. |
-EINVAL | Provided size exceeds valid bytes in the ring buffer. |
#include <zephyr/sys/ring_buffer.h>
Initialize a ring buffer for byte data.
This routine initializes a ring buffer, prior to its first use. It is only used for ring buffers not defined using RING_BUF_DECLARE.
buf | Address of ring buffer. |
size | Ring buffer size (in bytes). |
data | Ring buffer data area (uint8_t data[size]). |
#include <zephyr/sys/ring_buffer.h>
Function to force ring_buf internal states to given value.
Any value other than 0 makes sense only in validation testing context.
#include <zephyr/sys/ring_buffer.h>
Determine if a ring buffer is empty.
buf | Address of ring buffer. |
int ring_buf_item_get | ( | struct ring_buf * | buf, |
uint16_t * | type, | ||
uint8_t * | value, | ||
uint32_t * | data, | ||
uint8_t * | size32 ) |
#include <zephyr/sys/ring_buffer.h>
Read a data item from a ring buffer.
This routine reads a data item from ring buffer buf. The data item is an array of 32-bit words (up to 1020 bytes in length), coupled with a 16-bit type identifier and an 8-bit integer value.
buf | Address of ring buffer. |
type | Area to store the data item's type identifier. |
value | Area to store the data item's integer value. |
data | Area to store the data item. Can be NULL to discard data. |
size32 | Size of the data item storage area (number of 32-bit chunks). |
0 | Data item was fetched; size32 now contains the number of 32-bit words read into data area data. |
-EAGAIN | Ring buffer is empty. |
-EMSGSIZE | Data area data is too small; size32 now contains the number of 32-bit words needed. |
|
inlinestatic |
#include <zephyr/sys/ring_buffer.h>
Initialize an "item based" ring buffer.
This routine initializes a ring buffer, prior to its first use. It is only used for ring buffers not defined using RING_BUF_ITEM_DECLARE.
Each data item is an array of 32-bit words (from zero to 1020 bytes in length), coupled with a 16-bit type identifier and an 8-bit integer value.
buf | Address of ring buffer. |
size | Ring buffer size (in 32-bit words) |
data | Ring buffer data area (uint32_t data[size]). |
int ring_buf_item_put | ( | struct ring_buf * | buf, |
uint16_t | type, | ||
uint8_t | value, | ||
uint32_t * | data, | ||
uint8_t | size32 ) |
#include <zephyr/sys/ring_buffer.h>
Write a data item to a ring buffer.
This routine writes a data item to ring buffer buf. The data item is an array of 32-bit words (from zero to 1020 bytes in length), coupled with a 16-bit type identifier and an 8-bit integer value.
buf | Address of ring buffer. |
type | Data item's type identifier (application specific). |
value | Data item's integer value (application specific). |
data | Address of data item. |
size32 | Data item size (number of 32-bit words). |
0 | Data item was written. |
-EMSGSIZE | Ring buffer has insufficient free space. |
#include <zephyr/sys/ring_buffer.h>
Determine free space in an "item based" ring buffer.
buf | Address of ring buffer. |
#include <zephyr/sys/ring_buffer.h>
Peek at data from a ring buffer.
This routine reads data from a ring buffer buf without removal.
size
.buf | Address of ring buffer. |
data | Address of the output buffer. Cannot be NULL. |
size | Data size (in bytes). |
Number | of bytes written to the output buffer. |
#include <zephyr/sys/ring_buffer.h>
Write (copy) data to a ring buffer.
This routine writes data to a ring buffer buf.
buf | Address of ring buffer. |
data | Address of data. |
size | Data size (in bytes). |
Number | of bytes written. |
#include <zephyr/sys/ring_buffer.h>
Allocate buffer for writing data to a ring buffer.
With this routine, memory copying can be reduced since internal ring buffer can be used directly by the user. Once data is written to allocated area number of bytes written must be confirmed (see ring_buf_put_finish).
[in] | buf | Address of ring buffer. |
[out] | data | Pointer to the address. It is set to a location within ring buffer. |
[in] | size | Requested allocation size (in bytes). |
#include <zephyr/sys/ring_buffer.h>
Indicate number of bytes written to allocated buffers.
The number of bytes must be equal to or lower than the sum corresponding to all preceding ring_buf_put_claim invocations (or even 0). Surplus bytes will be returned to the available free buffer space.
buf | Address of ring buffer. |
size | Number of valid bytes in the allocated buffers. |
0 | Successful operation. |
-EINVAL | Provided size exceeds free space in the ring buffer. |
|
inlinestatic |
#include <zephyr/sys/ring_buffer.h>
Determine used space in a ring buffer.
buf | Address of ring buffer. |
#include <zephyr/sys/ring_buffer.h>
Determine free space in a ring buffer.
buf | Address of ring buffer. |