diff --git a/MAINTAINERS.yml b/MAINTAINERS.yml index 577fbeb85f59a..a01c14c528ecb 100644 --- a/MAINTAINERS.yml +++ b/MAINTAINERS.yml @@ -5133,7 +5133,7 @@ Utilities: - tests/lib/onoff/ - tests/lib/sys_util/ - tests/lib/sprintf/ - - tests/lib/ringbuffer/ + - tests/lib/ring_buffer/ - tests/lib/notify/ - tests/lib/linear_range/ labels: diff --git a/doc/kernel/data_structures/ring_buffers.rst b/doc/kernel/data_structures/ring_buffers.rst index 409b1c2bdc624..16c7a7edc88c6 100644 --- a/doc/kernel/data_structures/ring_buffers.rst +++ b/doc/kernel/data_structures/ring_buffers.rst @@ -3,30 +3,7 @@ Ring Buffers ############ -A :dfn:`ring buffer` is a circular buffer, whose contents are stored in -first-in-first-out order. - -For circumstances where an application needs to implement asynchronous -"streaming" copying of data, Zephyr provides a ``struct ring_buf`` -abstraction to manage copies of such data in and out of a shared -buffer of memory. - -Two content data modes are supported: - -* **Byte mode**: raw bytes can be enqueued and dequeued. - -* **Data item mode**: Multiple 32-bit word data items with metadata - can be enqueued and dequeued from the ring buffer in - chunks of up to 1020 bytes. Each data item also has two - associated metadata values: a type identifier and a 16-bit - integer value, both of which are application-specific. - -While the underlying data structure is the same, it is not -legal to mix these two modes on a single ring buffer instance. A ring -buffer initialized with a byte count must be used only with the -"bytes" API, one initialized with a word count must use the "items" -calls. - +A :dfn:`ring buffer` is a circular buffer that stores bytes in a first-in, first-out (FIFO) order. .. contents:: :local: @@ -35,353 +12,179 @@ calls. Concepts ******** -Any number of ring buffers can be defined (limited only by available RAM). Each -ring buffer is referenced by its memory address. - -A ring buffer has the following key properties: - -* A **data buffer** of bytes or 32-bit words. The data buffer contains the raw - bytes or 32-bit words that have been added to the ring buffer but not yet - removed. - -* A **data buffer size**, measured in bytes or 32-bit words. This governs - the maximum amount of data (including possible metadata values) the ring - buffer can hold. - -A ring buffer must be initialized before it can be used. This sets its -data buffer to empty. - -A ``struct ring_buf`` may be placed anywhere in user-accessible -memory, and must be initialized with :c:func:`ring_buf_init` or -:c:func:`ring_buf_item_init` before use. This must be provided a region -of user-controlled memory for use as the buffer itself. Note carefully that the units of the size of the -buffer passed change (either bytes or words) depending on how the ring -buffer will be used later. Macros for combining these steps in a -single static declaration exist for convenience. -:c:macro:`RING_BUF_DECLARE` will declare and statically initialize a ring -buffer with a specified byte count, where -:c:macro:`RING_BUF_ITEM_DECLARE` will declare and statically -initialize a buffer with a given count of 32 bit words. -:c:macro:`RING_BUF_ITEM_SIZEOF` will compute the size in 32-bit words -corresponding to a type or an expression. Note: rounds up if the size is -not a multiple of 32 bits. - -"Bytes" data may be copied into the ring buffer using -:c:func:`ring_buf_put`, passing a data pointer and byte count. These -bytes will be copied into the buffer in order, as many as will fit in -the allocated buffer. The total number of bytes copied (which may be -fewer than provided) will be returned. Likewise :c:func:`ring_buf_get` -will copy bytes out of the ring buffer in the order that they were -written, into a user-provided buffer, returning the number of bytes -that were transferred. - -To avoid multiply-copied-data situations, a "claim" API exists for -byte mode. :c:func:`ring_buf_put_claim` takes a byte size value from the -user and returns a pointer to memory internal to the ring buffer that -can be used to receive those bytes, along with a size of the -contiguous internal region (which may be smaller than requested). The -user can then copy data into that region at a later time without -assembling all the bytes in a single region first. When complete, -:c:func:`ring_buf_put_finish` can be used to signal the buffer that the -transfer is complete, passing the number of bytes actually -transferred. At this point a new transfer can be initiated. -Similarly, :c:func:`ring_buf_get_claim` returns a pointer to internal ring -buffer data from which the user can read without making a verbatim -copy, and :c:func:`ring_buf_get_finish` signals the buffer with how many -bytes have been consumed and allows for a new transfer to begin. - -"Items" mode works similarly to bytes mode, except that all transfers -are in units of 32 bit words and all memory is assumed to be aligned -on 32 bit boundaries. The write and read operations are -:c:func:`ring_buf_item_put` and :c:func:`ring_buf_item_get`, and work -otherwise identically to the bytes mode APIs. There no "claim" API -provided for items mode. One important difference is that unlike -:c:func:`ring_buf_put`, :c:func:`ring_buf_item_put` will not do a partial -transfer; it will return an error in the case where the provided data -does not fit in its entirety. - -The user can manage the capacity of a ring buffer without modifying it -using either :c:func:`ring_buf_space_get` or :c:func:`ring_buf_item_space_get` -which returns the number of free bytes or free 32-bit item words respectively, -or by testing the :c:func:`ring_buf_is_empty` predicate. - -Finally, a :c:func:`ring_buf_reset` call exists to immediately empty a -ring buffer, discarding the tracking of any bytes or items already -written to the buffer. It does not modify the memory contents of the -buffer itself, however. - - -Byte mode -========= - -A **byte mode** ring buffer instance is declared using -:c:macro:`RING_BUF_DECLARE()` and accessed using: -:c:func:`ring_buf_put_claim`, :c:func:`ring_buf_put_finish`, -:c:func:`ring_buf_get_claim`, :c:func:`ring_buf_get_finish`, -:c:func:`ring_buf_put` and :c:func:`ring_buf_get`. - -Data can be copied into the ring buffer (see -:c:func:`ring_buf_put`) or ring buffer memory can be used -directly by the user. In the latter case, the operation is split into three stages: - -1. allocating the buffer (:c:func:`ring_buf_put_claim`) when - user requests the destination location where data can be written. -#. writing the data by the user (e.g. buffer written by DMA). -#. indicating the amount of data written to the provided buffer - (:c:func:`ring_buf_put_finish`). The amount - can be less than or equal to the allocated amount. +An arbitrary number of ring buffers can be defined, limited only by the available RAM. Each ring +buffer is identified by its memory address. -Data can be retrieved from a ring buffer through copying -(see :c:func:`ring_buf_get`) or accessed directly by address. In the latter -case, the operation is split into three stages: +A :c:type:`ring_buffer` can reside anywhere in user-accessible memory but must be initialized using +:c:func:`ring_buffer_init` before it can be used. -1. retrieving source location with valid data written to a ring buffer - (see :c:func:`ring_buf_get_claim`). -#. processing data -#. freeing processed data (see :c:func:`ring_buf_get_finish`). - The amount freed can be less than or equal or to the retrieved amount. - -Data item mode -============== +To simplify setup, macros are provided for defining both the ring buffer and its associated memory. +The :c:macro:`RING_BUFFER_DECLARE` macro declares and statically initializes a ring buffer with the +specified size. -A **data item mode** ring buffer instance is declared using -:c:macro:`RING_BUF_ITEM_DECLARE()` and accessed using -:c:func:`ring_buf_item_put` and :c:func:`ring_buf_item_get`. +Data can be written to the ring buffer using :c:func:`ring_buffer_write`. Bytes are copied into the +buffer in order, up to the amount that will fit. The function returns the number of bytes actually +written, which may be less than requested. -A ring buffer **data item** is an array of 32-bit words from 0 to 1020 bytes -in length. When a data item is **enqueued** (:c:func:`ring_buf_item_put`) -its contents are copied to the data buffer, along with its associated metadata -values (which occupy one additional 32-bit word). If the ring buffer has -insufficient space to hold the new data item the enqueue operation fails. +Similarly, :c:func:`ring_buffer_read` copies bytes out of the buffer in the order they were written +and returns the number of bytes successfully read. +:c:func:`ring_buffer_peek` allows data to be read from the buffer without consuming it, so that it +can be read again later. -A data item is **dequeued** (:c:func:`ring_buf_item_get`) from a ring -buffer by removing the oldest enqueued item. The contents of the dequeued data -item, as well as its two metadata values, are copied to areas supplied by the -retriever. If the ring buffer is empty, or if the data array supplied by the -retriever is not large enough to hold the data item's data, the dequeue -operation fails. +To avoid unnecessary data copying, users can manage the ring buffer directly. The +:c:func:`ring_buffer_write_ptr` function returns a pointer to a contiguous region of internal buffer +memory, along with its size. This allows data to be written directly into the buffer without first +assembling it in a separate memory region. After writing, the user must call +:c:func:`ring_buffer_commit` to indicate how many bytes were actually written. At that point, a new +transfer can begin using either :c:func:`ring_buffer_write` or another call to +:c:func:`ring_buffer_write_ptr`. -Concurrency -=========== +Likewise, :c:func:`ring_buffer_read_ptr` provides a pointer to data stored in the buffer, enabling the +user to read directly without making a full copy. Once the data has been consumed, +:c:func:`ring_buffer_consume` should be called to notify the buffer of how many bytes were read. -The ring buffer APIs do not provide any concurrency control. -Depending on usage (particularly with respect to number of concurrent -readers/writers) applications may need to protect the ring buffer with -mutexes and/or use semaphores to notify consumers that there is data to -read. +The user can querry the capacity of a ring buffer without modifying it using the following functions: +* :c:func:`ring_buffer_capacity` - returns the size of the ring buffer in bytes +* :c:func:`ring_buffer_size` - returns the number of bytes currently stored in the ring buffer +* :c:func:`ring_buf_space` - returns the number of bytes available for writing +* :c:func:`ring_buffer_full` - indicates whether the ring buffer is full +* :c:func:`ring_buffer_empty` - indicates whether the ring buffer is empty -For the trivial case of one producer and one consumer, concurrency -control shouldn't be needed. +Finally, a :c:func:`ring_buffer_reset` call exists to immediately empty a +ring buffer, discarding any bytes already written to the buffer. -Internal Operation -================== +A ring buffer instance is declared using :c:macro:`RING_BUFFER_DECLARE()`. -Data streamed through a ring buffer is always written to the next byte -within the buffer, wrapping around to the first element after reaching -the end, thus the "ring" structure. Internally, the ``struct -ring_buf`` contains its own buffer pointer and its size, and also a -set of "head" and "tail" indices representing where the next read and write -operations may occur. - -This boundary is invisible to the user using the normal put/get APIs, -but becomes a barrier to the "claim" API, because obviously no -contiguous region can be returned that crosses the end of the buffer. -This can be surprising to application code, and produce performance -artifacts when transfers need to happen close to the end of the -buffer, as the number of calls to claim/finish needs to double for such -transfers. +Data can be copied into the ring buffer (see :c:func:`ring_buffer_write`) or accessing the ring +buffer memory directly. In the latter case, the operation is broken into three steps: +1. Retrieving the pointer and size of contiguous free space within the ring buffer (see + :c:func:`ring_buffer_write_ptr`). +#. writing the data by the user (e.g. buffer written by DMA). +#. indicating the amount of data written to the ring buffer + (:c:func:`ring_buffer_commit`). The amount can be less than or equal to the allocated amount. -Implementation -************** +Similarly, data can be read from the ring buffer by copying it out with :c:func:`ring_buffer_read`, +or by accessing the buffer memory directly. Direct access also involves three steps: -Defining a Ring Buffer -====================== - -A ring buffer is defined using a variable of type :c:struct:`ring_buf`. -It must then be initialized by calling :c:func:`ring_buf_init` or -:c:func:`ring_buf_item_init`. +1. Retrieving the pointer and size of contiguous allocated data within the ring buffer + (see :c:func:`ring_buffer_read_ptr`). +#. processing data +#. freeing processed data (see :c:func:`ring_buffer_consume`). + The amount freed can be less than or equal or to the retrieved amount. -The following code defines and initializes an empty **data item mode** ring -buffer (which is part of a larger data structure). The ring buffer's data buffer -is capable of holding 64 words of data and metadata information. +Concurrency +=========== -.. code-block:: c +The ring buffer API do not provide any concurrency control, if multiple threads +access the same ring buffer, the application should provide concurrency control. - #define MY_RING_BUF_WORDS 64 +Example usage +************* - struct my_struct { - struct ring_buf rb; - uint32_t buffer[MY_RING_BUF_WORDS]; - ... - }; - struct my_struct ms; +Defining a Ring Buffer +====================== - void init_my_struct { - ring_buf_item_init(&ms.rb, MY_RING_BUF_WORDS, ms.buffer); - ... - } +A ring buffer is defined using a variable of type :c:type:`ring_buffer`. +It must then be initialized by calling :c:func:`ring_buffer_init`; -Alternatively, a ring buffer can be defined and initialized at compile time -using one of two macros at file scope. Each macro defines both the ring +A ring buffer can be defined and initialized at compile time +using the following macro at file scope. The macro defines both the ring buffer itself and its data buffer. -The following code defines a **data item mode** ring buffer: - -.. code-block:: c - - #define MY_RING_BUF_WORDS 93 - RING_BUF_ITEM_DECLARE(my_ring_buf, MY_RING_BUF_WORDS); - -The following code defines a ring buffer intended to be used for raw bytes: - .. code-block:: c #define MY_RING_BUF_BYTES 93 - RING_BUF_DECLARE(my_ring_buf, MY_RING_BUF_BYTES); + RING_BUFFER_DECLARE(my_ring_buf, MY_RING_BUF_BYTES); -Enqueuing Data -============== +Writing Data to the Ring Buffer +=============================== -Bytes are copied to a **byte mode** ring buffer by calling -:c:func:`ring_buf_put`. +:c:func:`ring_buffer_write`. .. code-block:: c uint8_t my_data[MY_RING_BUF_BYTES]; - uint32_t ret; + size_t written; - ret = ring_buf_put(&ring_buf, my_data, MY_RING_BUF_BYTES); - if (ret != MY_RING_BUF_BYTES) { + written = ring_buf_write(&ring_buf, my_data, MY_RING_BUF_BYTES); + if (written != MY_RING_BUF_BYTES) { /* not enough room, partial copy. */ ... } -Data can be added to a **byte mode** ring buffer by directly accessing the -ring buffer's memory. For example: +Data can be added to a ring buffer by directly accessing its internal memory: .. code-block:: c - uint32_t size; + uint32_t writable_size; uint32_t rx_size; uint8_t *data; - int err; - /* Allocate buffer within a ring buffer memory. */ - size = ring_buf_put_claim(&ring_buf, &data, MY_RING_BUF_BYTES); + /* Retrives a pointer to writable space within the ring buffer and returns the size of the + * writable space. + */ + writable_size = ring_buffer_write_ptr(&ring_buf, &data); /* Work directly on a ring buffer memory. */ - rx_size = uart_rx(data, size); - - /* Indicate amount of valid data. rx_size can be equal or less than size. */ - err = ring_buf_put_finish(&ring_buf, rx_size); - if (err != 0) { - /* This shouldn't happen unless rx_size > size */ - ... - } - -A data item is added to a ring buffer by calling -:c:func:`ring_buf_item_put`. - -.. code-block:: c - - uint32_t data[MY_DATA_WORDS]; - int ret; - - ret = ring_buf_item_put(&ring_buf, TYPE_FOO, 0, data, MY_DATA_WORDS); - if (ret == -EMSGSIZE) { - /* not enough room for the data item */ - ... - } + rx_size = uart_rx(data, writable_size); -If the data item requires only the type or application-specific integer value -(i.e. it has no data array), a size of 0 and data pointer of :c:macro:`NULL` -can be specified. - -.. code-block:: c - - int ret; - - ret = ring_buf_item_put(&ring_buf, TYPE_BAR, 17, NULL, 0); - if (ret == -EMSGSIZE) { - /* not enough room for the data item */ + /* Indicate amount of data that has been committed into the ring_buffer. + * rx_size can be equal or less than size. + */ + ring_buffer_commit(&ring_buf, rx_size); ... } -Retrieving Data -=============== -Data bytes are copied out from a **byte mode** ring buffer by calling -:c:func:`ring_buf_get`. For example: +Reading Data from the Ring Buffer +================================= .. code-block:: c uint8_t my_data[MY_DATA_BYTES]; - size_t ret; + size_t bytes_read; - ret = ring_buf_get(&ring_buf, my_data, sizeof(my_data)); - if (ret != sizeof(my_data)) { + bytes_read = ring_buffer_read(&ring_buf, my_data, sizeof(my_data)); + if (bytes_read != sizeof(my_data)) { /* Fewer bytes copied. */ } else { /* Requested amount of bytes retrieved. */ ... } -Data can be retrieved from a **byte mode** ring buffer by direct -operations on the ring buffer's memory. For example: +Data can be retrieved from a ring buffer by directly accessing its internal memory: .. code-block:: c - uint32_t size; - uint32_t proc_size; + size_t readable_size; + size_t consumed; uint8_t *data; - int err; - /* Get buffer within a ring buffer memory. */ - size = ring_buf_get_claim(&ring_buf, &data, MY_RING_BUF_BYTES); + /* Get a reference to readable space within a ring buffer memory and the readable spaces size.*/ + readable_size = ring_buf_read_ptr(&ring_buf, &data); /* Work directly on a ring buffer memory. */ - proc_size = process(data, size); + consumed = uart_tx(data, readable_size); - /* Indicate amount of data that can be freed. proc_size can be equal or less - * than size. - */ - err = ring_buf_get_finish(&ring_buf, proc_size); - if (err != 0) { - /* proc_size exceeds amount of valid data in a ring buffer. */ - ... - } + /* Indicate amount of data that can be freed. proc_size can be equal or less than size. */ + ring_buffer_consume(&ring_buf, consumed); -A data item is removed from a ring buffer by calling -:c:func:`ring_buf_item_get`. +Alternatively, data could be observed without consuming it by using :c:func:`ring_buffer_peek`: .. code-block:: c - uint32_t my_data[MY_DATA_WORDS]; - uint16_t my_type; - uint8_t my_value; - uint8_t my_size; - int ret; - - my_size = MY_DATA_WORDS; - ret = ring_buf_item_get(&ring_buf, &my_type, &my_value, my_data, &my_size); - if (ret == -EMSGSIZE) { - printk("Buffer is too small, need %d uint32_t\n", my_size); - } else if (ret == -EAGAIN) { - printk("Ring buffer is empty\n"); + uint8_t my_data[MY_DATA_BYTES]; + size_t bytes_peeked; + + bytes_peeked = ring_buffer_peek(&ring_buf, my_data, sizeof(my_data)); + if (bytes_peeked != sizeof(my_data)) { + /* Fewer bytes available than requested */ } else { - printk("Got item of type %u value &u of size %u dwords\n", - my_type, my_value, my_size); + /* Requested amount of bytes retrieved. */ ... } -Configuration Options -********************* - -Related configuration options: - -* :kconfig:option:`CONFIG_RING_BUFFER`: Enable ring buffer. - API Reference ************* diff --git a/drivers/console/Kconfig b/drivers/console/Kconfig index 17e45604b43ec..02d6007c78ff8 100644 --- a/drivers/console/Kconfig +++ b/drivers/console/Kconfig @@ -173,7 +173,6 @@ config IPM_CONSOLE_SENDER config IPM_CONSOLE_RECEIVER bool "Inter-processor Mailbox console receiver" - select RING_BUFFER help Enable the receiving side of IPM console diff --git a/drivers/console/ipm_console_receiver.c b/drivers/console/ipm_console_receiver.c index d1ccf1c48531c..c88e5b4d18a27 100644 --- a/drivers/console/ipm_console_receiver.c +++ b/drivers/console/ipm_console_receiver.c @@ -19,7 +19,6 @@ static void ipm_console_thread(void *arg1, void *arg2, void *arg3) { uint8_t size32; - uint16_t type; int ret, key; const struct ipm_console_receiver_config_info *config_info; struct ipm_console_receiver_runtime_data *driver_data; @@ -28,19 +27,17 @@ static void ipm_console_thread(void *arg1, void *arg2, void *arg3) driver_data = (struct ipm_console_receiver_runtime_data *)arg1; config_info = (const struct ipm_console_receiver_config_info *)arg2; ARG_UNUSED(arg3); - size32 = 0U; pos = 0; while (1) { k_sem_take(&driver_data->sem, K_FOREVER); - ret = ring_buf_item_get(&driver_data->rb, &type, - (uint8_t *)&config_info->line_buf[pos], - NULL, &size32); - if (ret) { + ret = ring_buffer_read(&driver_data->rb, (uint8_t *)&config_info->line_buf[pos], + sizeof(uint32_t)); + + if (ret < sizeof(uint32_t)) { /* Shouldn't ever happen... */ printk("ipm console ring buffer error: %d\n", ret); - size32 = 0U; continue; } @@ -73,7 +70,7 @@ static void ipm_console_thread(void *arg1, void *arg2, void *arg3) * clearing the channel_disabled flag. */ if (driver_data->channel_disabled && - ring_buf_item_space_get(&driver_data->rb)) { + sizeof(uint32_t) <= ring_buffer_space(&driver_data->rb)) { key = irq_lock(); ipm_set_enabled(driver_data->ipm_device, 1); driver_data->channel_disabled = 0; @@ -92,8 +89,8 @@ static void ipm_console_receive_callback(const struct device *ipm_dev, ARG_UNUSED(data); /* Should always be at least one free buffer slot */ - ret = ring_buf_item_put(&driver_data->rb, 0, id, NULL, 0); - __ASSERT(ret == 0, "Failed to insert data into ring buffer"); + ret = ring_buffer_write(&driver_data->rb, &id, sizeof(id)); + __ASSERT(ret != sizeof(id), "Failed to insert data into ring buffer"); k_sem_give(&driver_data->sem); /* If the buffer is now full, disable future interrupts for this channel @@ -104,7 +101,7 @@ static void ipm_console_receive_callback(const struct device *ipm_dev, * call with the wait flag enabled. It blocks until the receiver side * re-enables the channel and consumes the data. */ - if (ring_buf_item_space_get(&driver_data->rb) == 0) { + if (ring_buffer_space(&driver_data->rb) < sizeof(uint32_t)) { ipm_set_enabled(ipm_dev, 0); driver_data->channel_disabled = 1; } @@ -135,8 +132,8 @@ int ipm_console_receiver_init(const struct device *d) driver_data->ipm_device = ipm; driver_data->channel_disabled = 0; k_sem_init(&driver_data->sem, 0, K_SEM_MAX_LIMIT); - ring_buf_item_init(&driver_data->rb, config_info->rb_size32, - config_info->ring_buf_data); + ring_buffer_init(&driver_data->rb, config_info->ring_buf_data, + config_info->rb_size32 * sizeof(uint32_t)); ipm_register_callback(ipm, ipm_console_receive_callback, driver_data); diff --git a/drivers/entropy/Kconfig.cc13xx_cc26xx b/drivers/entropy/Kconfig.cc13xx_cc26xx index d2cd32ef72fe1..a64c9fa764d2c 100644 --- a/drivers/entropy/Kconfig.cc13xx_cc26xx +++ b/drivers/entropy/Kconfig.cc13xx_cc26xx @@ -8,7 +8,6 @@ config ENTROPY_CC13XX_CC26XX_RNG default y depends on DT_HAS_TI_CC13XX_CC26XX_TRNG_ENABLED select ENTROPY_HAS_DRIVER - select RING_BUFFER help This option enables the driver for the True Random Number Generator (TRNG) for TI SimpleLink CC13xx / CC26xx SoCs. diff --git a/drivers/entropy/entropy_cc13xx_cc26xx.c b/drivers/entropy/entropy_cc13xx_cc26xx.c index 6a12f0a9fd181..a6934353a1c96 100644 --- a/drivers/entropy/entropy_cc13xx_cc26xx.c +++ b/drivers/entropy/entropy_cc13xx_cc26xx.c @@ -30,7 +30,7 @@ struct entropy_cc13xx_cc26xx_data { struct k_sem lock; struct k_sem sync; - struct ring_buf pool; + struct ring_buffer pool; uint8_t data[CONFIG_ENTROPY_CC13XX_CC26XX_POOL_SIZE]; #ifdef CONFIG_PM Power_NotifyObj post_notify; @@ -111,7 +111,7 @@ static int entropy_cc13xx_cc26xx_get_entropy(const struct device *dev, while (len) { k_sem_take(&data->lock, K_FOREVER); - cnt = ring_buf_get(&data->pool, buf, len); + cnt = ring_buffer_read(&data->pool, buf, len); k_sem_give(&data->lock); if (cnt) { @@ -140,7 +140,7 @@ static void entropy_cc13xx_cc26xx_isr(const struct device *dev) num[1] = TRNGNumberGet(TRNG_HI_WORD); num[0] = TRNGNumberGet(TRNG_LOW_WORD); - cnt = ring_buf_put(&data->pool, (uint8_t *)num, sizeof(num)); + cnt = ring_buffer_write(&data->pool, (uint8_t *)num, sizeof(num)); /* When pool is full disable interrupt and stop reading numbers */ if (cnt != sizeof(num)) { @@ -178,7 +178,7 @@ static int entropy_cc13xx_cc26xx_get_entropy_isr(const struct device *dev, unsigned int key; key = irq_lock(); - cnt = ring_buf_get(&data->pool, buf, len); + cnt = ring_buffer_read(&data->pool, buf, len); irq_unlock(key); if ((cnt == len) || ((flags & ENTROPY_BUSYWAIT) == 0U)) { @@ -200,7 +200,7 @@ static int entropy_cc13xx_cc26xx_get_entropy_isr(const struct device *dev, num[1] = TRNGNumberGet(TRNG_HI_WORD); num[0] = TRNGNumberGet(TRNG_LOW_WORD); - ring_buf_put(&data->pool, (uint8_t *)num, + ring_buffer_write(&data->pool, (uint8_t *)num, sizeof(num)); } @@ -209,7 +209,7 @@ static int entropy_cc13xx_cc26xx_get_entropy_isr(const struct device *dev, * would allow us to pick up anything that has been put * in by the ISR as well. */ - cnt = ring_buf_get(&data->pool, buf, len); + cnt = ring_buffer_read(&data->pool, buf, len); if (src & TRNG_FRO_SHUTDOWN) { handle_shutdown_ovf(); @@ -286,7 +286,7 @@ static int entropy_cc13xx_cc26xx_init(const struct device *dev) struct entropy_cc13xx_cc26xx_data *data = dev->data; /* Initialize driver data */ - ring_buf_init(&data->pool, sizeof(data->data), data->data); + ring_buffer_init(&data->pool, data->data, sizeof(data->data)); #if defined(CONFIG_PM) Power_setDependency(PowerCC26XX_PERIPH_TRNG); diff --git a/drivers/espi/Kconfig.npcx b/drivers/espi/Kconfig.npcx index fbd6ee643ca76..e05c3fe39743c 100644 --- a/drivers/espi/Kconfig.npcx +++ b/drivers/espi/Kconfig.npcx @@ -47,7 +47,6 @@ config ESPI_NPCX_BYPASS_CH_ENABLE_FATAL_ERROR config ESPI_NPCX_PERIPHERAL_DEBUG_PORT_80_MULTI_BYTE bool "Host can write 1/2/4 bytes of Port80 data in a eSPI transaction" depends on (SOC_SERIES_NPCX9 || SOC_SERIES_NPCX4) && ESPI_PERIPHERAL_DEBUG_PORT_80 - select RING_BUFFER help EC can accept 1/2/4 bytes of Port 80 data written from the Host in an eSPI transaction. diff --git a/drivers/espi/host_subs_npcx.c b/drivers/espi/host_subs_npcx.c index 33dbeab3ccf27..b9870aa9a7b68 100644 --- a/drivers/espi/host_subs_npcx.c +++ b/drivers/espi/host_subs_npcx.c @@ -150,7 +150,7 @@ struct host_sub_npcx_data { uint8_t espi_rst_level; /* current ESPI_RST# status */ const struct device *host_bus_dev; /* device for eSPI/LPC bus */ #ifdef CONFIG_ESPI_NPCX_PERIPHERAL_DEBUG_PORT_80_MULTI_BYTE - struct ring_buf port80_ring_buf; + struct ring_buffer port80_ring_buf; uint8_t port80_data[CONFIG_ESPI_NPCX_PERIPHERAL_DEBUG_PORT_80_RING_BUF_SIZE]; struct k_work work; #endif @@ -505,26 +505,26 @@ static void host_port80_work_handler(struct k_work *item) { uint32_t code = 0; struct host_sub_npcx_data *data = CONTAINER_OF(item, struct host_sub_npcx_data, work); - struct ring_buf *rbuf = &data->port80_ring_buf; + struct ring_buffer *rbuf = &data->port80_ring_buf; struct espi_event evt = {ESPI_BUS_PERIPHERAL_NOTIFICATION, (ESPI_PERIPHERAL_INDEX_0 << 16) | ESPI_PERIPHERAL_DEBUG_PORT80, ESPI_PERIPHERAL_NODATA}; - while (!ring_buf_is_empty(rbuf)) { + while (!ring_buffer_empty(rbuf)) { struct npcx_dp80_buf dp80_buf; uint8_t offset; - ring_buf_get(rbuf, &dp80_buf.offset_code[0], sizeof(dp80_buf.offset_code)); + ring_buffer_read(rbuf, &dp80_buf.offset_code[0], sizeof(dp80_buf.offset_code)); offset = dp80_buf.offset_code[1]; code |= dp80_buf.offset_code[0] << (8 * offset); - if (ring_buf_is_empty(rbuf)) { + if (ring_buffer_empty(rbuf)) { evt.evt_data = code; espi_send_callbacks(host_sub_data.callbacks, host_sub_data.host_bus_dev, evt); break; } /* peek the offset of the next byte */ - ring_buf_peek(rbuf, &dp80_buf.offset_code[0], sizeof(dp80_buf.offset_code)); + ring_buffer_peek(rbuf, &dp80_buf.offset_code[0], sizeof(dp80_buf.offset_code)); offset = dp80_buf.offset_code[1]; /* * If the peeked next byte's offset is 0, it is the start of the new code. @@ -547,13 +547,13 @@ static void host_port80_isr(const void *arg) uint8_t status = inst_shm->DP80STS; #ifdef CONFIG_ESPI_NPCX_PERIPHERAL_DEBUG_PORT_80_MULTI_BYTE - struct ring_buf *rbuf = &host_sub_data.port80_ring_buf; + struct ring_buffer *rbuf = &host_sub_data.port80_ring_buf; while (IS_BIT_SET(inst_shm->DP80STS, NPCX_DP80STS_FNE)) { struct npcx_dp80_buf dp80_buf; dp80_buf.offset_code_16 = inst_shm->DP80BUF; - ring_buf_put(rbuf, &dp80_buf.offset_code[0], sizeof(dp80_buf.offset_code)); + ring_buffer_write(rbuf, &dp80_buf.offset_code[0], sizeof(dp80_buf.offset_code)); } k_work_submit(&host_sub_data.work); #else @@ -1156,8 +1156,9 @@ int npcx_host_init_subs_core_domain(const struct device *host_bus_dev, #if defined(CONFIG_ESPI_PERIPHERAL_DEBUG_PORT_80) host_port80_init(); #if defined(CONFIG_ESPI_NPCX_PERIPHERAL_DEBUG_PORT_80_MULTI_BYTE) - ring_buf_init(&host_sub_data.port80_ring_buf, sizeof(host_sub_data.port80_data), - host_sub_data.port80_data); + ring_buffer_init(&host_sub_data.port80_ring_buf, + host_sub_data.port80_data, + sizeof(host_sub_data.port80_data)); k_work_init(&host_sub_data.work, host_port80_work_handler); #endif #endif diff --git a/drivers/hdlc_rcp_if/hdlc_rcp_if_uart.c b/drivers/hdlc_rcp_if/hdlc_rcp_if_uart.c index 2b0b3c7c10af8..26b22f3aec419 100644 --- a/drivers/hdlc_rcp_if/hdlc_rcp_if_uart.c +++ b/drivers/hdlc_rcp_if/hdlc_rcp_if_uart.c @@ -35,8 +35,8 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME); struct openthread_uart { struct k_work work; - struct ring_buf *rx_ringbuf; - struct ring_buf *tx_ringbuf; + struct ring_buffer *rx_ringbuf; + struct ring_buffer *tx_ringbuf; const struct device *dev; atomic_t tx_busy; @@ -45,8 +45,8 @@ struct openthread_uart { }; #define OT_UART_DEFINE(_name, _ringbuf_rx_size, _ringbuf_tx_size) \ - RING_BUF_DECLARE(_name##_rx_ringbuf, _ringbuf_rx_size); \ - RING_BUF_DECLARE(_name##_tx_ringbuf, _ringbuf_tx_size); \ + RING_BUFFER_DECLARE(_name##_rx_ringbuf, _ringbuf_rx_size); \ + RING_BUFFER_DECLARE(_name##_tx_ringbuf, _ringbuf_tx_size); \ static struct openthread_uart _name = { \ .rx_ringbuf = &_name##_rx_ringbuf, \ .tx_ringbuf = &_name##_tx_ringbuf, \ @@ -70,11 +70,10 @@ static void ot_uart_rx_cb(struct k_work *item) uint8_t *data; uint32_t len; - len = ring_buf_get_claim(otuart->rx_ringbuf, &data, - otuart->rx_ringbuf->size); + len = ring_buffer_read_ptr(otuart->rx_ringbuf, &data); if (len > 0) { otuart->cb(data, len, otuart->param); - ring_buf_get_finish(otuart->rx_ringbuf, len); + ring_buffer_consume(otuart->rx_ringbuf, len); } } @@ -83,14 +82,10 @@ static void uart_tx_handle(const struct device *dev) uint32_t tx_len = 0, len; uint8_t *data; - len = ring_buf_get_claim( - ot_uart.tx_ringbuf, &data, - ot_uart.tx_ringbuf->size); + len = ring_buffer_read_ptr(ot_uart.tx_ringbuf, &data); if (len > 0) { tx_len = uart_fifo_fill(dev, data, len); - int err = ring_buf_get_finish(ot_uart.tx_ringbuf, tx_len); - (void)err; - __ASSERT_NO_MSG(err == 0); + ring_buffer_consume(ot_uart.tx_ringbuf, tx_len); } else { uart_irq_tx_disable(dev); } @@ -101,15 +96,10 @@ static void uart_rx_handle(const struct device *dev) uint32_t rd_len = 0, len; uint8_t *data; - len = ring_buf_put_claim( - ot_uart.rx_ringbuf, &data, - ot_uart.rx_ringbuf->size); + len = ring_buffer_write_ptr(ot_uart.rx_ringbuf, &data); if (len > 0) { rd_len = uart_fifo_read(dev, data, len); - - int err = ring_buf_put_finish(ot_uart.rx_ringbuf, rd_len); - (void)err; - __ASSERT_NO_MSG(err == 0); + ring_buffer_commit(ot_uart.rx_ringbuf, rd_len); } } @@ -127,7 +117,7 @@ static void uart_callback(const struct device *dev, void *user_data) } } - if (ring_buf_size_get(ot_uart.rx_ringbuf) > 0) { + if (ring_buffer_size(ot_uart.rx_ringbuf) > 0) { k_work_submit(&ot_uart.work); } } @@ -175,7 +165,7 @@ static int hdlc_send(const uint8_t *frame, uint16_t length) return -EIO; } - ret = ring_buf_put(ot_uart.tx_ringbuf, frame, length); + ret = ring_buffer_write(ot_uart.tx_ringbuf, frame, length); uart_irq_tx_enable(ot_uart.dev); if (ret < length) { @@ -191,8 +181,8 @@ static int hdlc_deinit(void) uart_irq_tx_disable(ot_uart.dev); uart_irq_rx_disable(ot_uart.dev); - ring_buf_reset(ot_uart.rx_ringbuf); - ring_buf_reset(ot_uart.tx_ringbuf); + ring_buffer_reset(ot_uart.rx_ringbuf); + ring_buffer_reset(ot_uart.tx_ringbuf); return 0; } diff --git a/drivers/input/Kconfig.tsc_keys b/drivers/input/Kconfig.tsc_keys index 57b05321a6731..f5638caddf3f1 100644 --- a/drivers/input/Kconfig.tsc_keys +++ b/drivers/input/Kconfig.tsc_keys @@ -5,7 +5,6 @@ config INPUT_STM32_TSC_KEYS bool "STM32 TSC touch library" default y depends on DT_HAS_ST_STM32_TSC_ENABLED - select RING_BUFFER help Enable support for STM32 TSC touch library. diff --git a/drivers/input/input_tsc_keys.c b/drivers/input/input_tsc_keys.c index a09afc53782b4..86ef1212b3039 100644 --- a/drivers/input/input_tsc_keys.c +++ b/drivers/input/input_tsc_keys.c @@ -345,7 +345,7 @@ DT_INST_FOREACH_STATUS_OKAY(STM32_TSC_INIT) struct input_tsc_keys_data { uint32_t buffer[CONFIG_INPUT_STM32_TSC_KEYS_BUFFER_WORD_SIZE]; - struct ring_buf rb; + struct ring_buffer rb; bool expect_release; struct k_timer sampling_timer; }; @@ -372,11 +372,11 @@ static void input_tsc_callback_handler(uint32_t count_value, void *user_data) (const struct input_tsc_keys_config *)dev->config; struct input_tsc_keys_data *data = (struct input_tsc_keys_data *)dev->data; - if (ring_buf_item_space_get(&data->rb) == 0) { + if (ring_buffer_space(&data->rb) < sizeof(int32_t)) { uint32_t oldest_point; int32_t slope; - (void)ring_buf_get(&data->rb, (uint8_t *)&oldest_point, sizeof(oldest_point)); + (void)ring_buffer_read(&data->rb, (uint8_t *)&oldest_point, sizeof(oldest_point)); slope = count_value - oldest_point; if (slope < -config->noise_threshold && !data->expect_release) { @@ -388,7 +388,7 @@ static void input_tsc_callback_handler(uint32_t count_value, void *user_data) } } - (void)ring_buf_put(&data->rb, (uint8_t *)&count_value, sizeof(count_value)); + (void)ring_buffer_write(&data->rb, (uint8_t *)&count_value, sizeof(count_value)); } static int input_tsc_keys_init(const struct device *dev) @@ -401,7 +401,8 @@ static int input_tsc_keys_init(const struct device *dev) return -ENODEV; } - ring_buf_item_init(&data->rb, CONFIG_INPUT_STM32_TSC_KEYS_BUFFER_WORD_SIZE, data->buffer); + ring_buffer_init(&data->rb, data->buffer, + CONFIG_INPUT_STM32_TSC_KEYS_BUFFER_WORD_SIZE * sizeof(uint32_t)); uint8_t group_index = 0; diff --git a/drivers/modem/Kconfig b/drivers/modem/Kconfig index b8e66ef427482..952437122a09d 100644 --- a/drivers/modem/Kconfig +++ b/drivers/modem/Kconfig @@ -18,7 +18,6 @@ config MODEM_RECEIVER bool "Modem receiver helper driver" depends on SERIAL_SUPPORT_INTERRUPT select UART_INTERRUPT_DRIVEN - select RING_BUFFER help This driver allows modem drivers to communicate over UART with custom defined protocols. Driver doesn't inspect received data and all @@ -64,7 +63,6 @@ config MODEM_CONTEXT_VERBOSE_DEBUG config MODEM_IFACE_UART bool "UART-based modem interface" - select RING_BUFFER help To configure this layer for use, create a modem_iface_uart_data object and pass it's reference to modem_iface_uart_init() diff --git a/drivers/modem/Kconfig.cellular b/drivers/modem/Kconfig.cellular index cc746ff9770e5..a28cd97f5b40d 100644 --- a/drivers/modem/Kconfig.cellular +++ b/drivers/modem/Kconfig.cellular @@ -11,7 +11,6 @@ config MODEM_CELLULAR select MODEM_PIPELINK select MODEM_BACKEND_UART select UART_USE_RUNTIME_CONFIGURE - select RING_BUFFER select NET_L2_PPP_OPTION_MRU select NET_L2_PPP_PAP select NET_L2_PPP_MGMT diff --git a/drivers/modem/modem_cellular.c b/drivers/modem/modem_cellular.c index 23ea498f8220f..95c0d12c63849 100644 --- a/drivers/modem/modem_cellular.c +++ b/drivers/modem/modem_cellular.c @@ -165,7 +165,7 @@ struct modem_cellular_data { /* Event dispatcher */ struct k_work event_dispatch_work; uint8_t event_buf[8]; - struct ring_buf event_rb; + struct ring_buffer event_rb; struct k_mutex event_rb_lock; struct k_mutex api_lock; @@ -723,7 +723,7 @@ static void modem_cellular_event_dispatch_handler(struct k_work *item) k_mutex_lock(&data->event_rb_lock, K_FOREVER); - events_cnt = (uint8_t)ring_buf_get(&data->event_rb, events, sizeof(data->event_buf)); + events_cnt = (uint8_t)ring_buffer_read(&data->event_rb, events, sizeof(data->event_buf)); k_mutex_unlock(&data->event_rb_lock); @@ -736,7 +736,7 @@ static void modem_cellular_delegate_event(struct modem_cellular_data *data, enum modem_cellular_event evt) { k_mutex_lock(&data->event_rb_lock, K_FOREVER); - ring_buf_put(&data->event_rb, (uint8_t *)&evt, 1); + ring_buffer_write(&data->event_rb, (uint8_t *)&evt, 1); k_mutex_unlock(&data->event_rb_lock); k_work_submit(&data->event_dispatch_work); } @@ -2192,7 +2192,7 @@ static int modem_cellular_init(const struct device *dev) k_work_init_delayable(&data->timeout_work, modem_cellular_timeout_handler); k_work_init(&data->event_dispatch_work, modem_cellular_event_dispatch_handler); - ring_buf_init(&data->event_rb, sizeof(data->event_buf), data->event_buf); + ring_buffer_init(&data->event_rb, data->event_buf, sizeof(data->event_buf)); k_sem_init(&data->suspended_sem, 0, 1); diff --git a/drivers/modem/modem_iface_uart.h b/drivers/modem/modem_iface_uart.h index 91b94da33750f..cddcff99d5de4 100644 --- a/drivers/modem/modem_iface_uart.h +++ b/drivers/modem/modem_iface_uart.h @@ -24,7 +24,7 @@ struct modem_iface_uart_data { bool hw_flow_control; /* ring buffer */ - struct ring_buf rx_rb; + struct ring_buffer rx_rb; /* rx semaphore */ struct k_sem rx_sem; diff --git a/drivers/modem/modem_iface_uart_async.c b/drivers/modem/modem_iface_uart_async.c index 47e74dc4f3724..0068755bb0e29 100644 --- a/drivers/modem/modem_iface_uart_async.c +++ b/drivers/modem/modem_iface_uart_async.c @@ -52,7 +52,7 @@ static void iface_uart_async_callback(const struct device *dev, break; case UART_RX_RDY: /* Place received data on the ring buffer */ - written = ring_buf_put(&data->rx_rb, + written = ring_buffer_write(&data->rx_rb, evt->data.rx.buf + evt->data.rx.offset, evt->data.rx.len); if (written != evt->data.rx.len) { @@ -97,7 +97,7 @@ static int modem_iface_uart_async_read(struct modem_iface *iface, /* Pull data off the ring buffer */ data = iface->iface_data; - *bytes_read = ring_buf_get(&data->rx_rb, buf, size); + *bytes_read = ring_buffer_read(&data->rx_rb, buf, size); return 0; } @@ -176,7 +176,7 @@ int modem_iface_uart_init(struct modem_iface *iface, struct modem_iface_uart_dat iface->read = modem_iface_uart_async_read; iface->write = modem_iface_uart_async_write; - ring_buf_init(&data->rx_rb, config->rx_rb_buf_len, config->rx_rb_buf); + ring_buffer_init(&data->rx_rb, config->rx_rb_buf_len, config->rx_rb_buf); k_sem_init(&data->rx_sem, 0, 1); k_sem_init(&data->tx_sem, 0, 1); diff --git a/drivers/modem/modem_iface_uart_interrupt.c b/drivers/modem/modem_iface_uart_interrupt.c index 5bc06f5985884..53e6e0c776892 100644 --- a/drivers/modem/modem_iface_uart_interrupt.c +++ b/drivers/modem/modem_iface_uart_interrupt.c @@ -52,7 +52,7 @@ static void modem_iface_uart_isr(const struct device *uart_dev, { struct modem_context *ctx; struct modem_iface_uart_data *data; - int rx = 0, ret; + int rx = 0; uint8_t *dst; uint32_t partial_size = 0; uint32_t total_size = 0; @@ -70,8 +70,7 @@ static void modem_iface_uart_isr(const struct device *uart_dev, while (uart_irq_update(ctx->iface.dev) && uart_irq_rx_ready(ctx->iface.dev)) { if (!partial_size) { - partial_size = ring_buf_put_claim(&data->rx_rb, &dst, - UINT32_MAX); + partial_size = ring_buffer_write_ptr(&data->rx_rb, &dst); } if (!partial_size) { if (data->hw_flow_control) { @@ -93,8 +92,7 @@ static void modem_iface_uart_isr(const struct device *uart_dev, partial_size -= rx; } - ret = ring_buf_put_finish(&data->rx_rb, total_size); - __ASSERT_NO_MSG(ret == 0); + ring_buffer_commit(&data->rx_rb, total_size); if (total_size > 0) { k_sem_give(&data->rx_sem); @@ -116,7 +114,7 @@ static int modem_iface_uart_read(struct modem_iface *iface, } data = (struct modem_iface_uart_data *)(iface->iface_data); - *bytes_read = ring_buf_get(&data->rx_rb, buf, size); + *bytes_read = ring_buffer_read(&data->rx_rb, buf, size); if (data->hw_flow_control && *bytes_read == 0) { uart_irq_rx_enable(iface->dev); @@ -190,7 +188,7 @@ int modem_iface_uart_init(struct modem_iface *iface, struct modem_iface_uart_dat iface->read = modem_iface_uart_read; iface->write = modem_iface_uart_write; - ring_buf_init(&data->rx_rb, config->rx_rb_buf_len, config->rx_rb_buf); + ring_buffer_init(&data->rx_rb, config->rx_rb_buf, config->rx_rb_buf_len); k_sem_init(&data->rx_sem, 0, 1); /* Configure hardware flow control */ diff --git a/drivers/modem/modem_receiver.c b/drivers/modem/modem_receiver.c index 0d2cb627c067b..ad317fc034d0c 100644 --- a/drivers/modem/modem_receiver.c +++ b/drivers/modem/modem_receiver.c @@ -121,7 +121,7 @@ static void mdm_receiver_isr(const struct device *uart_dev, void *user_data) uart_irq_rx_ready(ctx->uart_dev)) { rx = uart_fifo_read(ctx->uart_dev, read_buf, sizeof(read_buf)); if (rx > 0) { - ret = ring_buf_put(&ctx->rx_rb, read_buf, rx); + ret = ring_buffer_write(&ctx->rx_rb, read_buf, rx); if (ret != rx) { LOG_ERR("Rx buffer doesn't have enough space. " "Bytes pending: %d, written: %d", @@ -174,7 +174,7 @@ int mdm_receiver_recv(struct mdm_receiver_context *ctx, return 0; } - *bytes_read = ring_buf_get(&ctx->rx_rb, buf, size); + *bytes_read = ring_buffer_read(&ctx->rx_rb, buf, size); return 0; } @@ -232,7 +232,7 @@ int mdm_receiver_register(struct mdm_receiver_context *ctx, } ctx->uart_dev = uart_dev; - ring_buf_init(&ctx->rx_rb, size, buf); + ring_buffer_init(&ctx->rx_rb, buf, size); k_sem_init(&ctx->rx_sem, 0, 1); ret = mdm_receiver_get(ctx); diff --git a/drivers/modem/modem_receiver.h b/drivers/modem/modem_receiver.h index e08839e88f9c1..96a0bc8ae0f3a 100644 --- a/drivers/modem/modem_receiver.h +++ b/drivers/modem/modem_receiver.h @@ -25,7 +25,7 @@ struct mdm_receiver_context { const struct device *uart_dev; /* rx data */ - struct ring_buf rx_rb; + struct ring_buffer rx_rb; struct k_sem rx_sem; /* modem data */ diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig index c9672be5f77d1..02124b5ff91d7 100644 --- a/drivers/net/Kconfig +++ b/drivers/net/Kconfig @@ -13,7 +13,6 @@ menuconfig NET_PPP bool "Point-to-point (PPP) UART based driver" depends on NET_L2_PPP depends on NET_NATIVE - select RING_BUFFER select CRC if NET_PPP diff --git a/drivers/net/ppp.c b/drivers/net/ppp.c index e6682a76b43fb..83eaad6057bea 100644 --- a/drivers/net/ppp.c +++ b/drivers/net/ppp.c @@ -101,7 +101,7 @@ struct ppp_driver_context { atomic_t modem_init_done; /* Incoming data is routed via ring buffer */ - struct ring_buf rx_ringbuf; + struct ring_buffer rx_ringbuf; uint8_t rx_buf[CONFIG_NET_PPP_RINGBUF_SIZE]; /* ISR function callback worker */ @@ -176,7 +176,7 @@ static void uart_callback(const struct device *dev, LOG_DBG("Received data %d bytes", len); - ret = ring_buf_put(&context->rx_ringbuf, p, len); + ret = ring_buffer_write(&context->rx_ringbuf, p, len); if (ret < evt->data.rx.len) { LOG_WRN("Rx buffer doesn't have enough space. " "Bytes pending: %d, written only: %d. " @@ -193,7 +193,7 @@ static void uart_callback(const struct device *dev, } } - space_left = ring_buf_space_get(&context->rx_ringbuf); + space_left = ring_buffer_space(&context->rx_ringbuf); if (!rx_retry_pending && space_left < (sizeof(context->rx_buf) / 8)) { /* Not much room left in buffer after a write to ring buffer. * We submit a work, but enable flow ctrl also @@ -277,7 +277,7 @@ static void uart_recovery(struct k_work *work) CONTAINER_OF(dwork, struct ppp_driver_context, uart_recovery_work); int ret; - ret = ring_buf_space_get(&ppp->rx_ringbuf); + ret = ring_buffer_space(&ppp->rx_ringbuf); if (ret >= (sizeof(ppp->rx_buf) / 2)) { ret = ppp_async_uart_rx_enable(ppp); if (ret) { @@ -902,10 +902,8 @@ static int ppp_consume_ringbuf(struct ppp_driver_context *ppp) { uint8_t *data; size_t len, tmp; - int ret; - len = ring_buf_get_claim(&ppp->rx_ringbuf, &data, - CONFIG_NET_PPP_RINGBUF_SIZE); + len = ring_buffer_read_ptr(&ppp->rx_ringbuf, &data); if (len == 0) { LOG_DBG("Ringbuf %p is empty!", &ppp->rx_ringbuf); return 0; @@ -927,10 +925,7 @@ static int ppp_consume_ringbuf(struct ppp_driver_context *ppp) } } while (--tmp); - ret = ring_buf_get_finish(&ppp->rx_ringbuf, len); - if (ret < 0) { - LOG_DBG("Cannot flush ring buffer (%d)", ret); - } + ring_buffer_consume(&ppp->rx_ringbuf, len); return -EAGAIN; } @@ -954,7 +949,7 @@ static int ppp_driver_init(const struct device *dev) LOG_DBG("[%p] dev %p", ppp, dev); #if !defined(CONFIG_NET_TEST) - ring_buf_init(&ppp->rx_ringbuf, sizeof(ppp->rx_buf), ppp->rx_buf); + ring_buffer_init(&ppp->rx_ringbuf, ppp->rx_buf, sizeof(ppp->rx_buf)); k_work_init(&ppp->cb_work, ppp_isr_cb_work); k_work_queue_start(&ppp->cb_workq, ppp_workq, @@ -1077,7 +1072,7 @@ static void ppp_uart_isr(const struct device *uart, void *user_data) continue; } - ret = ring_buf_put(&context->rx_ringbuf, context->buf, rx); + ret = ring_buffer_write(&context->rx_ringbuf, context->buf, rx); if (ret < rx) { LOG_ERR("Rx buffer doesn't have enough space. " "Bytes pending: %d, written: %d", diff --git a/drivers/serial/Kconfig.bitbang b/drivers/serial/Kconfig.bitbang index 38e3aaaae0884..e69503bfbf913 100644 --- a/drivers/serial/Kconfig.bitbang +++ b/drivers/serial/Kconfig.bitbang @@ -8,7 +8,6 @@ config UART_BITBANG select SERIAL_HAS_DRIVER select SERIAL_SUPPORT_INTERRUPT select COUNTER - select RING_BUFFER help Enable the Bitbang UART controller. The driver relies on counter interrupts to generate/sample UART diff --git a/drivers/serial/Kconfig.bridge b/drivers/serial/Kconfig.bridge index a8f9c0e56496e..6d5bc67032080 100644 --- a/drivers/serial/Kconfig.bridge +++ b/drivers/serial/Kconfig.bridge @@ -6,7 +6,6 @@ config UART_BRIDGE default y depends on DT_HAS_ZEPHYR_UART_BRIDGE_ENABLED select UART_INTERRUPT_DRIVEN - select RING_BUFFER help Enable the UART bridge driver. diff --git a/drivers/serial/Kconfig.bt b/drivers/serial/Kconfig.bt index 4fa842d7e9cb4..61bf222bf5150 100644 --- a/drivers/serial/Kconfig.bt +++ b/drivers/serial/Kconfig.bt @@ -6,7 +6,6 @@ config UART_BT depends on BT_ZEPHYR_NUS depends on DT_HAS_ZEPHYR_NUS_UART_ENABLED select UART_INTERRUPT_DRIVEN - select RING_BUFFER select EXPERIMENTAL select SERIAL_SUPPORT_INTERRUPT help diff --git a/drivers/serial/Kconfig.emul b/drivers/serial/Kconfig.emul index 8427f41a2c0fc..13e8202f2c120 100644 --- a/drivers/serial/Kconfig.emul +++ b/drivers/serial/Kconfig.emul @@ -11,7 +11,6 @@ config UART_EMUL select SERIAL_HAS_DRIVER select SERIAL_SUPPORT_INTERRUPT select SERIAL_SUPPORT_ASYNC - select RING_BUFFER select EXPERIMENTAL help Enable the emulated UART driver. diff --git a/drivers/serial/Kconfig.test b/drivers/serial/Kconfig.test index e7f38edc59184..5e7353ba65a46 100644 --- a/drivers/serial/Kconfig.test +++ b/drivers/serial/Kconfig.test @@ -8,4 +8,3 @@ config SERIAL_TEST select SERIAL_HAS_DRIVER select SERIAL_SUPPORT_INTERRUPT select SERIAL_SUPPORT_ASYNC - select RING_BUFFER if (UART_INTERRUPT_DRIVEN || UART_ASYNC_API) diff --git a/drivers/serial/serial_test.c b/drivers/serial/serial_test.c index 532fffcc91f07..f6179ac0aab5c 100644 --- a/drivers/serial/serial_test.c +++ b/drivers/serial/serial_test.c @@ -23,10 +23,8 @@ LOG_MODULE_REGISTER(mock_serial, CONFIG_LOG_DEFAULT_LEVEL); #define DT_DRV_COMPAT vnd_serial struct serial_vnd_data { -#ifdef CONFIG_RING_BUFFER - struct ring_buf *written; - struct ring_buf *read_queue; -#endif + struct ring_buffer *written; + struct ring_buffer *read_queue; serial_vnd_write_cb_t callback; void *callback_data; #ifdef CONFIG_UART_INTERRUPT_DRIVEN @@ -49,14 +47,14 @@ static bool is_irq_rx_pending(const struct device *dev) { struct serial_vnd_data *data = dev->data; - return !ring_buf_is_empty(data->read_queue); + return !ring_buffer_empty(data->read_queue); } static bool is_irq_tx_pending(const struct device *dev) { struct serial_vnd_data *data = dev->data; - return ring_buf_space_get(data->written) != 0; + return ring_buffer_space(data->written) != 0; } static void irq_process(const struct device *dev) @@ -105,7 +103,7 @@ static void irq_rx_disable(const struct device *dev) static int irq_rx_ready(const struct device *dev) { struct serial_vnd_data *data = dev->data; - bool ready = !ring_buf_is_empty(data->read_queue); + bool ready = !ring_buffer_empty(data->read_queue); LOG_DBG("rx ready: %d", ready); return ready; @@ -131,7 +129,7 @@ static void irq_tx_disable(const struct device *dev) static int irq_tx_ready(const struct device *dev) { struct serial_vnd_data *data = dev->data; - int available = ring_buf_space_get(data->written); + int available = ring_buffer_space(data->written); LOG_DBG("tx ready: %d", available); return available; @@ -163,7 +161,7 @@ static int irq_update(const struct device *dev) static int fifo_fill(const struct device *dev, const uint8_t *tx_data, int size) { struct serial_vnd_data *data = dev->data; - uint32_t write_len = ring_buf_put(data->written, tx_data, size); + uint32_t write_len = ring_buffer_write(data->written, tx_data, size); if (data->callback) { data->callback(dev, data->callback_data); @@ -174,7 +172,7 @@ static int fifo_fill(const struct device *dev, const uint8_t *tx_data, int size) static int fifo_read(const struct device *dev, uint8_t *rx_data, const int size) { struct serial_vnd_data *data = dev->data; - int read_len = ring_buf_get(data->read_queue, rx_data, size); + int read_len = ring_buffer_read(data->read_queue, rx_data, size); LOG_HEXDUMP_DBG(rx_data, read_len, ""); return read_len; @@ -183,33 +181,29 @@ static int fifo_read(const struct device *dev, uint8_t *rx_data, const int size) static int serial_vnd_poll_in(const struct device *dev, unsigned char *c) { -#ifdef CONFIG_RING_BUFFER + struct serial_vnd_data *data = dev->data; uint32_t bytes_read; if (data == NULL || data->read_queue == NULL) { return -ENOTSUP; } - bytes_read = ring_buf_get(data->read_queue, c, 1); + bytes_read = ring_buffer_read(data->read_queue, c, 1); if (bytes_read == 1) { return 0; } return -1; -#else - return -ENOTSUP; -#endif } static void serial_vnd_poll_out(const struct device *dev, unsigned char c) { struct serial_vnd_data *data = dev->data; -#ifdef CONFIG_RING_BUFFER if (data == NULL || data->written == NULL) { return; } - ring_buf_put(data->written, &c, 1); -#endif + ring_buffer_write(data->written, &c, 1); + if (data->callback) { data->callback(dev, data->callback_data); } @@ -219,7 +213,6 @@ static void serial_vnd_poll_out(const struct device *dev, unsigned char c) static void async_rx_run(const struct device *dev); #endif -#ifdef CONFIG_RING_BUFFER int serial_vnd_queue_in_data(const struct device *dev, const unsigned char *c, uint32_t size) { struct serial_vnd_data *data = dev->data; @@ -228,7 +221,7 @@ int serial_vnd_queue_in_data(const struct device *dev, const unsigned char *c, u if (data == NULL || data->read_queue == NULL) { return -ENOTSUP; } - write_size = ring_buf_put(data->read_queue, c, size); + write_size = ring_buffer_write(data->read_queue, c, size); LOG_DBG("size %u write_size %u", size, write_size); LOG_HEXDUMP_DBG(c, write_size, ""); @@ -253,7 +246,7 @@ uint32_t serial_vnd_out_data_size_get(const struct device *dev) if (data == NULL || data->written == NULL) { return -ENOTSUP; } - return ring_buf_size_get(data->written); + return ring_buffer_size(data->written); } uint32_t serial_vnd_read_out_data(const struct device *dev, unsigned char *out_data, uint32_t size) @@ -263,7 +256,7 @@ uint32_t serial_vnd_read_out_data(const struct device *dev, unsigned char *out_d if (data == NULL || data->written == NULL) { return -ENOTSUP; } - return ring_buf_get(data->written, out_data, size); + return ring_buffer_read(data->written, out_data, size); } uint32_t serial_vnd_peek_out_data(const struct device *dev, unsigned char *out_data, uint32_t size) @@ -273,9 +266,9 @@ uint32_t serial_vnd_peek_out_data(const struct device *dev, unsigned char *out_d if (data == NULL || data->written == NULL) { return -ENOTSUP; } - return ring_buf_peek(data->written, out_data, size); + return ring_buffer_peek(data->written, out_data, size); } -#endif + void serial_vnd_set_callback(const struct device *dev, serial_vnd_write_cb_t callback, void *user_data) @@ -345,7 +338,7 @@ static int serial_vnd_api_tx(const struct device *dev, const uint8_t *tx_data, s return -EINVAL; } - write_len = ring_buf_put(data->written, tx_data, len); + write_len = ring_buffer_write(data->written, tx_data, len); if (data->callback) { data->callback(dev, data->callback_data); } @@ -377,7 +370,7 @@ static void async_rx_run(const struct device *dev) read_remaining = data->read_size - data->read_position; - read_len = ring_buf_get(data->read_queue, &data->read_buf[data->read_position], + read_len = ring_buffer_read(data->read_queue, &data->read_buf[data->read_position], read_remaining); if (read_len != 0) { @@ -456,8 +449,8 @@ static DEVICE_API(uart, serial_vnd_api) = { }; #define VND_SERIAL_DATA_BUFFER(n) \ - RING_BUF_DECLARE(written_data_##n, DT_INST_PROP(n, buffer_size)); \ - RING_BUF_DECLARE(read_queue_##n, DT_INST_PROP(n, buffer_size)); \ + RING_BUFFER_DECLARE(written_data_##n, DT_INST_PROP(n, buffer_size)); \ + RING_BUFFER_DECLARE(read_queue_##n, DT_INST_PROP(n, buffer_size)); \ static struct serial_vnd_data serial_vnd_data_##n = { \ .written = &written_data_##n, \ .read_queue = &read_queue_##n, \ diff --git a/drivers/serial/uart_bitbang.c b/drivers/serial/uart_bitbang.c index 9908e5362babc..1e344ec3253e2 100644 --- a/drivers/serial/uart_bitbang.c +++ b/drivers/serial/uart_bitbang.c @@ -56,7 +56,7 @@ struct uart_bitbang_data { /* Tx counter config */ struct counter_top_cfg tx_counter_cfg; /* Tx ring buffer */ - struct ring_buf *tx_ringbuf; + struct ring_buffer *tx_ringbuf; /* Rx state */ enum uart_bitbang_state rx_state; /* Rx bit index */ @@ -70,7 +70,7 @@ struct uart_bitbang_data { /* Rx callback data */ struct gpio_callback rx_gpio_cb_data; /* Rx ring buffer */ - struct ring_buf *rx_ringbuf; + struct ring_buffer *rx_ringbuf; #ifdef CONFIG_UART_INTERRUPT_DRIVEN /* Interrupt flags */ #define UART_BITBANG_IRQ_TC (1 << 0) @@ -196,7 +196,8 @@ static void uart_bitbang_rx_counter_top_interrupt(const struct device *dev, void } /* Push data received to rx ring buffer */ - ring_buf_put(data->rx_ringbuf, (const uint8_t *)&data->rx_data, sizeof(uint16_t)); + ring_buffer_write(data->rx_ringbuf, (const uint8_t *)&data->rx_data, + sizeof(uint16_t)); #ifdef CONFIG_UART_INTERRUPT_DRIVEN if ((data->user_cb) && (data->irq & UART_BITBANG_IRQ_RXNE)) { @@ -233,7 +234,7 @@ static int uart_bitbang_poll_in_u16(const struct device *dev, uint16_t *in_u16) { struct uart_bitbang_data *data = dev->data; - uint32_t s = ring_buf_get(data->rx_ringbuf, (uint8_t *)in_u16, sizeof(uint16_t)); + uint32_t s = ring_buffer_read(data->rx_ringbuf, (uint8_t *)in_u16, sizeof(uint16_t)); return (s == sizeof(uint16_t)) ? 0 : -1; } @@ -265,7 +266,7 @@ static void uart_bitbang_tx_counter_top_interrupt(const struct device *dev, void switch (data->tx_state) { case UART_BITBANG_IDLE: /* Claim the next data */ - size = ring_buf_get_claim(data->tx_ringbuf, (uint8_t **)&data->tx_data, + size = MIN(ring_buffer_read_ptr(data->tx_ringbuf, (uint8_t **)&data->tx_data), sizeof(uint16_t)); if (size == sizeof(uint16_t)) { /* Start next transmission */ @@ -333,7 +334,7 @@ static void uart_bitbang_tx_counter_top_interrupt(const struct device *dev, void break; case UART_BITBANG_COMPLETE: /* Terminate current transfer */ - ring_buf_get_finish(data->tx_ringbuf, sizeof(uint16_t)); + ring_buffer_consume(data->tx_ringbuf, sizeof(uint16_t)); data->tx_state = UART_BITBANG_IDLE; break; } @@ -348,7 +349,7 @@ static void uart_bitbang_poll_out_u16(const struct device *dev, uint16_t out_u16 if (config->tx_gpio.port != NULL) { /* Push data to send to tx ring buffer */ - ring_buf_put(data->tx_ringbuf, (const uint8_t *)&out_u16, sizeof(uint16_t)); + ring_buffer_write(data->tx_ringbuf, (const uint8_t *)&out_u16, sizeof(uint16_t)); /* Start tx counter if not already started */ counter_reset(config->tx_counter); @@ -497,7 +498,7 @@ static int uart_bitbang_irq_tx_ready(const struct device *dev) { struct uart_bitbang_data *data = dev->data; - return (int)(ring_buf_space_get(data->tx_ringbuf) / sizeof(uint16_t)); + return (int)(ring_buffer_space(data->tx_ringbuf) / sizeof(uint16_t)); } static void uart_bitbang_irq_rx_enable(const struct device *dev) @@ -520,14 +521,14 @@ static int uart_bitbang_irq_tx_complete(const struct device *dev) { struct uart_bitbang_data *data = dev->data; - return ring_buf_is_empty(data->tx_ringbuf) ? 1 : 0; + return ring_buffer_empty(data->tx_ringbuf) ? 1 : 0; } static int uart_bitbang_irq_rx_ready(const struct device *dev) { struct uart_bitbang_data *data = dev->data; - return (ring_buf_size_get(data->rx_ringbuf) > 0) ? 1 : 0; + return (ring_buffer_size(data->rx_ringbuf) > 0) ? 1 : 0; } static void uart_bitbang_irq_err_enable(const struct device *dev) @@ -777,8 +778,8 @@ static int uart_bitbang_init(const struct device *dev) .uart_cfg = &uart_cfg_##index, \ .msb = DT_INST_PROP_OR(index, msb, false), \ }; \ - RING_BUF_DECLARE(uart_bitbang_tx_ringbuf##index, DT_INST_PROP(index, tx_fifo_size)); \ - RING_BUF_DECLARE(uart_bitbang_rx_ringbuf##index, DT_INST_PROP(index, rx_fifo_size)); \ + RING_BUFFER_DECLARE(uart_bitbang_tx_ringbuf##index, DT_INST_PROP(index, tx_fifo_size)); \ + RING_BUFFER_DECLARE(uart_bitbang_rx_ringbuf##index, DT_INST_PROP(index, rx_fifo_size)); \ static struct uart_bitbang_data uart_bitbang_data_##index = { \ .config = &uart_bitbang_config_##index, \ .tx_ringbuf = &uart_bitbang_tx_ringbuf##index, \ diff --git a/drivers/serial/uart_bridge.c b/drivers/serial/uart_bridge.c index 7428700f3be3f..b7efa40fd5350 100644 --- a/drivers/serial/uart_bridge.c +++ b/drivers/serial/uart_bridge.c @@ -25,7 +25,7 @@ struct uart_bridge_config { struct uart_bridge_peer_data { uint8_t buf[RING_BUF_SIZE]; - struct ring_buf rb; + struct ring_buffer rb; bool paused; }; @@ -105,33 +105,27 @@ static void uart_bridge_handle_rx(const struct device *dev, uint8_t *recv_buf; int rb_len, recv_len; - int ret; - if (ring_buf_space_get(&own_data->rb) < RING_BUF_FULL_THRESHOLD) { + if (ring_buffer_space(&own_data->rb) < RING_BUF_FULL_THRESHOLD) { LOG_DBG("%s: buffer full: pause", dev->name); uart_irq_rx_disable(dev); own_data->paused = true; return; } - rb_len = ring_buf_put_claim(&own_data->rb, &recv_buf, RING_BUF_SIZE); + rb_len = ring_buffer_write_ptr(&own_data->rb, &recv_buf); if (rb_len == 0) { - LOG_WRN("%s: ring_buf full", dev->name); + LOG_WRN("%s: ring_buffer full", dev->name); return; } recv_len = uart_fifo_read(dev, recv_buf, rb_len); if (recv_len < 0) { - ring_buf_put_finish(&own_data->rb, 0); LOG_ERR("%s: rx error: %d", dev->name, recv_len); return; } - ret = ring_buf_put_finish(&own_data->rb, recv_len); - if (ret < 0) { - LOG_ERR("%s: ring_buf_put_finish error: %d", dev->name, rb_len); - return; - } + ring_buffer_commit(&own_data->rb, recv_len); uart_irq_tx_enable(peer_dev); } @@ -149,9 +143,8 @@ static void uart_bridge_handle_tx(const struct device *dev, uint8_t *send_buf; int rb_len, sent_len; - int ret; - rb_len = ring_buf_get_claim(&peer_data->rb, &send_buf, RING_BUF_SIZE); + rb_len = ring_buffer_read_ptr(&peer_data->rb, &send_buf); if (rb_len == 0) { LOG_DBG("%s: buffer empty, disable tx irq", dev->name); uart_irq_tx_disable(dev); @@ -160,19 +153,14 @@ static void uart_bridge_handle_tx(const struct device *dev, sent_len = uart_fifo_fill(dev, send_buf, rb_len); if (sent_len < 0) { - ring_buf_get_finish(&peer_data->rb, 0); LOG_ERR("%s: tx error: %d", dev->name, sent_len); return; } - ret = ring_buf_get_finish(&peer_data->rb, sent_len); - if (ret < 0) { - LOG_ERR("ring_buf_get_finish error: %d", ret); - return; - } + ring_buffer_consume(&peer_data->rb, sent_len); if (peer_data->paused && - ring_buf_space_get(&peer_data->rb) > RING_BUF_FULL_THRESHOLD) { + ring_buffer_space(&peer_data->rb) > RING_BUF_FULL_THRESHOLD) { LOG_DBG("%s: buffer free: resume", dev->name); uart_irq_rx_enable(peer_dev); peer_data->paused = false; @@ -225,8 +213,8 @@ static int uart_bridge_init(const struct device *dev) { struct uart_bridge_data *data = dev->data; - ring_buf_init(&data->peer[0].rb, RING_BUF_SIZE, data->peer[0].buf); - ring_buf_init(&data->peer[1].rb, RING_BUF_SIZE, data->peer[1].buf); + ring_buffer_init(&data->peer[0].rb, data->peer[0].buf, RING_BUF_SIZE); + ring_buffer_init(&data->peer[1].rb, data->peer[1].buf, RING_BUF_SIZE); return pm_device_driver_init(dev, uart_bridge_pm_action); } diff --git a/drivers/serial/uart_bt.c b/drivers/serial/uart_bt.c index 3ae70465a2eec..c0dee0326445a 100644 --- a/drivers/serial/uart_bt.c +++ b/drivers/serial/uart_bt.c @@ -27,8 +27,8 @@ struct uart_bt_data { atomic_t enabled; } bt; struct { - struct ring_buf *rx_ringbuf; - struct ring_buf *tx_ringbuf; + struct ring_buffer *rx_ringbuf; + struct ring_buffer *tx_ringbuf; struct k_work cb_work; struct k_work_delayable tx_work; bool rx_irq_ena; @@ -52,7 +52,7 @@ static void bt_notif_enabled(bool enabled, void *ctx) LOG_DBG("%s() - %s", __func__, enabled ? "enabled" : "disabled"); - if (!ring_buf_is_empty(dev_data->uart.tx_ringbuf)) { + if (!ring_buffer_empty(dev_data->uart.tx_ringbuf)) { k_work_reschedule_for_queue(&nus_work_queue, &dev_data->uart.tx_work, K_NO_WAIT); } } @@ -66,13 +66,13 @@ static void bt_received(struct bt_conn *conn, const void *data, uint16_t len, vo const struct device *dev = (const struct device *)ctx; struct uart_bt_data *dev_data = (struct uart_bt_data *)dev->data; - struct ring_buf *ringbuf = dev_data->uart.rx_ringbuf; + struct ring_buffer *ringbuf = dev_data->uart.rx_ringbuf; uint32_t put_len; - LOG_DBG("%s() - len: %d, rx_ringbuf space %d", __func__, len, ring_buf_space_get(ringbuf)); + LOG_DBG("%s() - len: %d, rx_ringbuf space %d", __func__, len, ring_buffer_space(ringbuf)); LOG_HEXDUMP_DBG(data, len, "data"); - put_len = ring_buf_put(ringbuf, (const uint8_t *)data, len); + put_len = ring_buffer_write(ringbuf, (const uint8_t *)data, len); if (put_len < len) { LOG_ERR("RX Ring buffer full. received: %d, added to queue: %d", len, put_len); } @@ -139,7 +139,7 @@ static void tx_work_handler(struct k_work *work) * peers, and the same chunk is sent to everyone. This avoids * managing separate read pointers: one per connection. */ - len = ring_buf_get_claim(dev_data->uart.tx_ringbuf, &data, chunk_size); + len = MIN(ring_buffer_read_ptr(dev_data->uart.tx_ringbuf, &data), chunk_size); if (len > 0) { err = bt_nus_inst_send(NULL, dev_data->bt.inst, data, len); if (err) { @@ -147,10 +147,10 @@ static void tx_work_handler(struct k_work *work) } } - ring_buf_get_finish(dev_data->uart.tx_ringbuf, len); + ring_buffer_consume(dev_data->uart.tx_ringbuf, len); } while (len > 0 && !err); - if ((ring_buf_space_get(dev_data->uart.tx_ringbuf) > 0) && dev_data->uart.tx_irq_ena) { + if ((ring_buffer_space(dev_data->uart.tx_ringbuf) > 0) && dev_data->uart.tx_irq_ena) { k_work_submit_to_queue(&nus_work_queue, &dev_data->uart.cb_work); } } @@ -160,7 +160,7 @@ static int uart_bt_fifo_fill(const struct device *dev, const uint8_t *tx_data, i struct uart_bt_data *dev_data = (struct uart_bt_data *)dev->data; size_t wrote; - wrote = ring_buf_put(dev_data->uart.tx_ringbuf, tx_data, len); + wrote = ring_buffer_write(dev_data->uart.tx_ringbuf, tx_data, len); if (wrote < len) { LOG_WRN("Ring buffer full, drop %zd bytes", len - wrote); } @@ -176,7 +176,7 @@ static int uart_bt_fifo_read(const struct device *dev, uint8_t *rx_data, const i { struct uart_bt_data *dev_data = (struct uart_bt_data *)dev->data; - return ring_buf_get(dev_data->uart.rx_ringbuf, rx_data, size); + return ring_buffer_read(dev_data->uart.rx_ringbuf, rx_data, size); } static int uart_bt_poll_in(const struct device *dev, unsigned char *c) @@ -189,10 +189,10 @@ static int uart_bt_poll_in(const struct device *dev, unsigned char *c) static void uart_bt_poll_out(const struct device *dev, unsigned char c) { struct uart_bt_data *dev_data = (struct uart_bt_data *)dev->data; - struct ring_buf *ringbuf = dev_data->uart.tx_ringbuf; + struct ring_buffer *ringbuf = dev_data->uart.tx_ringbuf; /** Right now we're discarding data if ring-buf is full. */ - while (!ring_buf_put(ringbuf, &c, 1)) { + while (!ring_buffer_write(ringbuf, &c, 1)) { if (k_is_in_isr() || !atomic_get(&dev_data->bt.enabled)) { LOG_WRN_ONCE("Ring buffer full, discard %c", c); break; @@ -215,7 +215,7 @@ static int uart_bt_irq_tx_ready(const struct device *dev) { struct uart_bt_data *dev_data = (struct uart_bt_data *)dev->data; - if ((ring_buf_space_get(dev_data->uart.tx_ringbuf) > 0) && dev_data->uart.tx_irq_ena) { + if ((ring_buffer_space(dev_data->uart.tx_ringbuf) > 0) && dev_data->uart.tx_irq_ena) { return 1; } @@ -244,7 +244,7 @@ static int uart_bt_irq_rx_ready(const struct device *dev) { struct uart_bt_data *dev_data = (struct uart_bt_data *)dev->data; - if (!ring_buf_is_empty(dev_data->uart.rx_ringbuf) && dev_data->uart.rx_irq_ena) { + if (!ring_buffer_empty(dev_data->uart.rx_ringbuf) && dev_data->uart.rx_irq_ena) { return 1; } @@ -347,8 +347,8 @@ static int uart_bt_init(const struct device *dev) \ BT_NUS_INST_DEFINE(bt_nus_inst_##n); \ \ - RING_BUF_DECLARE(bt_nus_rx_rb_##n, UART_BT_RX_FIFO_SIZE(n)); \ - RING_BUF_DECLARE(bt_nus_tx_rb_##n, UART_BT_TX_FIFO_SIZE(n)); \ + RING_BUFFER_DECLARE(bt_nus_rx_rb_##n, UART_BT_RX_FIFO_SIZE(n)); \ + RING_BUFFER_DECLARE(bt_nus_tx_rb_##n, UART_BT_TX_FIFO_SIZE(n)); \ \ static struct uart_bt_data uart_bt_data_##n = { \ .bt = { \ diff --git a/drivers/serial/uart_emul.c b/drivers/serial/uart_emul.c index 86a1c66a6f51c..b2d31afa5f280 100644 --- a/drivers/serial/uart_emul.c +++ b/drivers/serial/uart_emul.c @@ -39,13 +39,13 @@ struct uart_emul_data { struct uart_config cfg; int errors; - struct ring_buf *rx_rb; + struct ring_buffer *rx_rb; struct k_spinlock rx_lock; uart_emul_callback_tx_data_ready_t tx_data_ready_cb; void *user_data; - struct ring_buf *tx_rb; + struct ring_buffer *tx_rb; struct k_spinlock tx_lock; #ifdef CONFIG_UART_INTERRUPT_DRIVEN @@ -118,7 +118,7 @@ static void uart_emul_tx_data_ready(const struct device *dev) sys_snode_t *node; if (data->tx_data_ready_cb) { - (data->tx_data_ready_cb)(dev, ring_buf_size_get(data->tx_rb), data->user_data); + (data->tx_data_ready_cb)(dev, ring_buffer_size(data->tx_rb), data->user_data); } SYS_SLIST_FOR_EACH_NODE(&data->emuls, node) { struct uart_emul *emul = CONTAINER_OF(node, struct uart_emul, node); @@ -126,7 +126,7 @@ static void uart_emul_tx_data_ready(const struct device *dev) __ASSERT_NO_MSG(emul->api != NULL); __ASSERT_NO_MSG(emul->api->tx_data_ready != NULL); - emul->api->tx_data_ready(dev, ring_buf_size_get(data->tx_rb), emul->target); + emul->api->tx_data_ready(dev, ring_buffer_size(data->tx_rb), emul->target); } } @@ -137,7 +137,7 @@ static int uart_emul_poll_in(const struct device *dev, unsigned char *p_char) uint32_t read; key = k_spin_lock(&drv_data->rx_lock); - read = ring_buf_get(drv_data->rx_rb, p_char, 1); + read = ring_buffer_read(drv_data->rx_rb, p_char, 1); k_spin_unlock(&drv_data->rx_lock, key); if (!read) { @@ -156,7 +156,7 @@ static void uart_emul_poll_out(const struct device *dev, unsigned char out_char) uint32_t written; key = k_spin_lock(&drv_data->tx_lock); - written = ring_buf_put(drv_data->tx_rb, &out_char, 1); + written = ring_buffer_write(drv_data->tx_rb, &out_char, 1); k_spin_unlock(&drv_data->tx_lock, key); if (!written) { @@ -207,7 +207,7 @@ static int uart_emul_fifo_fill(const struct device *dev, const uint8_t *tx_data, uint32_t put_size = MIN(config->latch_buffer_size, size); K_SPINLOCK(&data->tx_lock) { - ret = ring_buf_put(data->tx_rb, tx_data, put_size); + ret = ring_buffer_write(data->tx_rb, tx_data, put_size); } if (config->loopback) { @@ -226,9 +226,9 @@ static int uart_emul_fifo_read(const struct device *dev, uint8_t *rx_data, int s uint32_t bytes_to_read = 0; K_SPINLOCK(&data->rx_lock) { - bytes_to_read = MIN(config->latch_buffer_size, ring_buf_size_get(data->rx_rb)); + bytes_to_read = MIN(config->latch_buffer_size, ring_buffer_size(data->rx_rb)); bytes_to_read = MIN(bytes_to_read, size); - ring_buf_get(data->rx_rb, rx_data, bytes_to_read); + ring_buffer_read(data->rx_rb, rx_data, bytes_to_read); } return bytes_to_read; @@ -244,7 +244,7 @@ static int uart_emul_irq_tx_ready(const struct device *dev) K_SPINLOCK_BREAK; } - available = ring_buf_space_get(data->tx_rb); + available = ring_buffer_space(data->tx_rb); } return available; @@ -260,7 +260,7 @@ static int uart_emul_irq_rx_ready(const struct device *dev) K_SPINLOCK_BREAK; } - ready = !ring_buf_is_empty(data->rx_rb); + ready = !ring_buffer_empty(data->rx_rb); } return ready; @@ -286,7 +286,7 @@ static void uart_emul_irq_handler(struct k_work *work) K_SPINLOCK_BREAK; } - have_work = have_work || ring_buf_space_get(data->tx_rb) > 0; + have_work = have_work || ring_buffer_space(data->tx_rb) > 0; } K_SPINLOCK(&data->rx_lock) { @@ -294,7 +294,7 @@ static void uart_emul_irq_handler(struct k_work *work) K_SPINLOCK_BREAK; } - have_work = have_work || !ring_buf_is_empty(data->rx_rb); + have_work = have_work || !ring_buffer_empty(data->rx_rb); } if (!have_work) { @@ -317,7 +317,7 @@ static void uart_emul_irq_tx_enable(const struct device *dev) K_SPINLOCK(&data->tx_lock) { data->tx_irq_en = true; - submit_irq_work = ring_buf_space_get(data->tx_rb) > 0; + submit_irq_work = ring_buffer_space(data->tx_rb) > 0; } if (submit_irq_work) { @@ -332,7 +332,7 @@ static void uart_emul_irq_rx_enable(const struct device *dev) K_SPINLOCK(&data->rx_lock) { data->rx_irq_en = true; - submit_irq_work = !ring_buf_is_empty(data->rx_rb); + submit_irq_work = !ring_buffer_empty(data->rx_rb); } if (submit_irq_work) { @@ -364,7 +364,7 @@ static int uart_emul_irq_tx_complete(const struct device *dev) struct uart_emul_data *const data = dev->data; K_SPINLOCK(&data->tx_lock) { - tx_complete = ring_buf_is_empty(data->tx_rb); + tx_complete = ring_buffer_empty(data->tx_rb); } return tx_complete; @@ -499,7 +499,7 @@ static void uart_emul_async_rx_handler(struct k_work *work) buf_len = data->rx_buf_len; offset = data->rx_buf_offset; data_len = data->rx_buf_data_len; - empty = ring_buf_is_empty(data->rx_rb); + empty = ring_buffer_empty(data->rx_rb); if (!rx_en) { K_SPINLOCK_BREAK; @@ -527,7 +527,7 @@ static void uart_emul_async_rx_handler(struct k_work *work) buf_request = data_len == 0 && data->rx_buf_next == NULL; - uint32_t read = ring_buf_get(data->rx_rb, &rx_buf[offset + data_len], + uint32_t read = ring_buffer_read(data->rx_rb, &rx_buf[offset + data_len], buf_len - (offset + data_len)); data_len += read; data->rx_buf_data_len = data_len; @@ -603,7 +603,7 @@ static void uart_emul_async_tx_handler(struct k_work *work) K_SPINLOCK_BREAK; } - written = ring_buf_put(data->tx_rb, &data->tx_buf[tx_buf_offset], + written = ring_buffer_write(data->tx_rb, &data->tx_buf[tx_buf_offset], tx_buf_len - tx_buf_offset); tx_done = written == (tx_buf_len - tx_buf_offset); if (!tx_done) { @@ -849,7 +849,7 @@ static int uart_emul_rx_enable(const struct device *dev, uint8_t *buf, size_t le data->rx_buf_next = NULL; data->rx_buf_next_len = 0; - if (!ring_buf_is_empty(data->rx_rb)) { + if (!ring_buffer_empty(data->rx_rb)) { (void)k_work_submit_to_queue(&uart_emul_work_q, &data->rx_work); } } @@ -925,8 +925,8 @@ uint32_t uart_emul_put_rx_data(const struct device *dev, const uint8_t *data, si __unused bool rx_en = false; K_SPINLOCK(&drv_data->rx_lock) { - count = ring_buf_put(drv_data->rx_rb, data, size); - empty = ring_buf_is_empty(drv_data->rx_rb); + count = ring_buffer_write(drv_data->rx_rb, data, size); + empty = ring_buffer_empty(drv_data->rx_rb); IF_ENABLED(CONFIG_UART_INTERRUPT_DRIVEN, (irq_en = drv_data->rx_irq_en;)); IF_ENABLED(CONFIG_UART_ASYNC_API, (rx_en = drv_data->rx_async_en;)); } @@ -956,7 +956,7 @@ uint32_t uart_emul_get_tx_data(const struct device *dev, uint8_t *data, size_t s uint32_t count; key = k_spin_lock(&drv_data->tx_lock); - count = ring_buf_get(drv_data->tx_rb, data, size); + count = ring_buffer_read(drv_data->tx_rb, data, size); k_spin_unlock(&drv_data->tx_lock, key); return count; } @@ -968,8 +968,8 @@ uint32_t uart_emul_flush_rx_data(const struct device *dev) uint32_t count; key = k_spin_lock(&drv_data->rx_lock); - count = ring_buf_size_get(drv_data->rx_rb); - ring_buf_reset(drv_data->rx_rb); + count = ring_buffer_size(drv_data->rx_rb); + ring_buffer_reset(drv_data->rx_rb); k_spin_unlock(&drv_data->rx_lock, key); return count; } @@ -981,8 +981,8 @@ uint32_t uart_emul_flush_tx_data(const struct device *dev) uint32_t count; key = k_spin_lock(&drv_data->tx_lock); - count = ring_buf_size_get(drv_data->tx_rb); - ring_buf_reset(drv_data->tx_rb); + count = ring_buffer_size(drv_data->tx_rb); + ring_buffer_reset(drv_data->tx_rb); k_spin_unlock(&drv_data->tx_lock, key); return count; } @@ -1022,8 +1022,8 @@ int uart_emul_register(const struct device *dev, struct uart_emul *emul) static const struct emul_link_for_bus emuls_##inst[] = { \ DT_FOREACH_CHILD_STATUS_OKAY(DT_DRV_INST(inst), EMUL_LINK_AND_COMMA)}; \ \ - RING_BUF_DECLARE(uart_emul_##inst##_rx_rb, UART_EMUL_RX_FIFO_SIZE(inst)); \ - RING_BUF_DECLARE(uart_emul_##inst##_tx_rb, UART_EMUL_TX_FIFO_SIZE(inst)); \ + RING_BUFFER_DECLARE(uart_emul_##inst##_rx_rb, UART_EMUL_RX_FIFO_SIZE(inst)); \ + RING_BUFFER_DECLARE(uart_emul_##inst##_tx_rb, UART_EMUL_TX_FIFO_SIZE(inst)); \ \ static const struct uart_emul_config uart_emul_cfg_##inst = { \ .loopback = DT_INST_PROP(inst, loopback), \ diff --git a/drivers/wifi/eswifi/Kconfig.eswifi b/drivers/wifi/eswifi/Kconfig.eswifi index e82eba51e50b4..76baee01da363 100644 --- a/drivers/wifi/eswifi/Kconfig.eswifi +++ b/drivers/wifi/eswifi/Kconfig.eswifi @@ -27,7 +27,6 @@ config WIFI_ESWIFI_BUS_SPI config WIFI_ESWIFI_BUS_UART bool "UART Bus interface" select SERIAL - select RING_BUFFER endchoice diff --git a/drivers/wifi/eswifi/eswifi_bus_uart.c b/drivers/wifi/eswifi/eswifi_bus_uart.c index f82e34a6b490d..fd2ea4704bc9f 100644 --- a/drivers/wifi/eswifi/eswifi_bus_uart.c +++ b/drivers/wifi/eswifi/eswifi_bus_uart.c @@ -39,7 +39,7 @@ struct eswifi_uart_data { /* RX Ring Buf */ uint8_t iface_rb_buf[ESWIFI_RING_BUF_SIZE]; - struct ring_buf rx_rb; + struct ring_buffer rx_rb; }; static struct eswifi_uart_data eswifi_uart0; /* Static instance */ @@ -67,8 +67,7 @@ static void eswifi_iface_uart_isr(const struct device *uart_dev, while (uart_irq_update(uart->dev) && uart_irq_rx_ready(uart->dev)) { if (!partial_size) { - partial_size = ring_buf_put_claim(&uart->rx_rb, &dst, - UINT32_MAX); + partial_size = ring_buffer_write_ptr(&uart->rx_rb, &dst); } if (!partial_size) { LOG_ERR("Rx buffer doesn't have enough space"); @@ -86,7 +85,7 @@ static void eswifi_iface_uart_isr(const struct device *uart_dev, partial_size -= rx; } - ring_buf_put_finish(&uart->rx_rb, total_size); + ring_buffer_commit(&uart->rx_rb, total_size); } static char get_fsm_char(int fsm) @@ -111,7 +110,7 @@ static int eswifi_uart_get_resp(struct eswifi_uart_data *uart) { uint8_t c; - while (ring_buf_get(&uart->rx_rb, &c, 1) > 0) { + while (ring_buffer_read(&uart->rx_rb, &c, 1) > 0) { LOG_DBG("FSM: %c, RX: 0x%02x : %c", get_fsm_char(uart->fsm), c, c); @@ -233,8 +232,8 @@ int eswifi_uart_init(struct eswifi_dev *eswifi) uart_irq_callback_set(uart->dev, eswifi_iface_uart_isr); uart_irq_rx_enable(uart->dev); - ring_buf_init(&uart->rx_rb, sizeof(uart->iface_rb_buf), - uart->iface_rb_buf); + ring_buffer_init(&uart->rx_rb, uart->iface_rb_buf, + sizeof(uart->iface_rb_buf)); LOG_DBG("success"); diff --git a/include/zephyr/drivers/console/ipm_console.h b/include/zephyr/drivers/console/ipm_console.h index d2d2167968f5b..17015694e6ed1 100644 --- a/include/zephyr/drivers/console/ipm_console.h +++ b/include/zephyr/drivers/console/ipm_console.h @@ -65,7 +65,7 @@ struct ipm_console_receiver_config_info { struct ipm_console_receiver_runtime_data { /** Buffer for received bytes from the low-level IPM device */ - struct ring_buf rb; + struct ring_buffer rb; /** Semaphore to wake up the thread to print out messages */ struct k_sem sem; diff --git a/include/zephyr/kernel.h b/include/zephyr/kernel.h index 687d88368935e..03d10ef8ee6ed 100644 --- a/include/zephyr/kernel.h +++ b/include/zephyr/kernel.h @@ -5346,7 +5346,7 @@ enum pipe_flags { struct k_pipe { size_t waiting; - struct ring_buf buf; + struct ring_buffer buf; struct k_spinlock lock; _wait_q_t data; _wait_q_t space; @@ -5365,7 +5365,7 @@ struct k_pipe { #define Z_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \ { \ .waiting = 0, \ - .buf = RING_BUF_INIT(pipe_buffer, pipe_buffer_size), \ + .buf = RING_BUFFER_INIT(pipe_buffer, pipe_buffer_size), \ .data = Z_WAIT_Q_INIT(&obj.data), \ .space = Z_WAIT_Q_INIT(&obj.space), \ .flags = PIPE_FLAG_OPEN, \ diff --git a/include/zephyr/modem/backend/uart.h b/include/zephyr/modem/backend/uart.h index 73054b792d8e4..a45d114986e36 100644 --- a/include/zephyr/modem/backend/uart.h +++ b/include/zephyr/modem/backend/uart.h @@ -22,8 +22,8 @@ extern "C" { #endif struct modem_backend_uart_isr { - struct ring_buf receive_rdb[2]; - struct ring_buf transmit_rb; + struct ring_buffer receive_rdb[2]; + struct ring_buffer transmit_rb; atomic_t transmit_buf_len; atomic_t receive_buf_len; uint8_t receive_rdb_used; @@ -60,7 +60,7 @@ struct modem_backend_uart_async { struct modem_backend_uart_async_common common; uint8_t *receive_bufs[2]; uint32_t receive_buf_size; - struct ring_buf receive_rb; + struct ring_buffer receive_rb; struct k_spinlock receive_rb_lock; }; diff --git a/include/zephyr/modem/cmux.h b/include/zephyr/modem/cmux.h index 913f4187d7292..f4645c8b7e55e 100644 --- a/include/zephyr/modem/cmux.h +++ b/include/zephyr/modem/cmux.h @@ -99,7 +99,7 @@ struct modem_cmux_dlci { struct modem_cmux *cmux; /* Receive buffer */ - struct ring_buf receive_rb; + struct ring_buffer receive_rb; struct k_mutex receive_rb_lock; /* Work */ @@ -159,7 +159,7 @@ struct modem_cmux { uint8_t work_buf[MODEM_CMUX_WORK_BUFFER_SIZE]; /* Transmit buffer */ - struct ring_buf transmit_rb; + struct ring_buffer transmit_rb; struct k_mutex transmit_rb_lock; /* Received frame */ diff --git a/include/zephyr/modem/ppp.h b/include/zephyr/modem/ppp.h index b3dbcfdeb7fb3..56257f2b0196c 100644 --- a/include/zephyr/modem/ppp.h +++ b/include/zephyr/modem/ppp.h @@ -105,7 +105,7 @@ struct modem_ppp { uint16_t tx_pkt_fcs; /* Ring buffer used for transmitting partial PPP frame */ - struct ring_buf transmit_rb; + struct ring_buffer transmit_rb; struct k_fifo tx_pkt_fifo; diff --git a/include/zephyr/shell/shell_mqtt.h b/include/zephyr/shell/shell_mqtt.h index 4a96f9f0eac5d..61b00a780dea3 100644 --- a/include/zephyr/shell/shell_mqtt.h +++ b/include/zephyr/shell/shell_mqtt.h @@ -47,7 +47,7 @@ struct shell_mqtt { /** Handler function registered by shell. */ shell_transport_handler_t shell_handler; - struct ring_buf rx_rb; + struct ring_buffer rx_rb; uint8_t rx_rb_buf[RX_RB_SIZE]; uint8_t *rx_rb_ptr; diff --git a/include/zephyr/shell/shell_uart.h b/include/zephyr/shell/shell_uart.h index d1a8a4df97ffb..e95fb72758f17 100644 --- a/include/zephyr/shell/shell_uart.h +++ b/include/zephyr/shell/shell_uart.h @@ -49,8 +49,8 @@ struct shell_uart_common { struct shell_uart_int_driven { struct shell_uart_common common; - struct ring_buf tx_ringbuf; - struct ring_buf rx_ringbuf; + struct ring_buffer tx_ringbuf; + struct ring_buffer rx_ringbuf; uint8_t tx_buf[CONFIG_SHELL_BACKEND_SERIAL_TX_RING_BUFFER_SIZE]; uint8_t rx_buf[CONFIG_SHELL_BACKEND_SERIAL_RX_RING_BUFFER_SIZE]; struct k_timer dtr_timer; @@ -68,7 +68,7 @@ struct shell_uart_async { struct shell_uart_polling { struct shell_uart_common common; - struct ring_buf rx_ringbuf; + struct ring_buffer rx_ringbuf; uint8_t rx_buf[CONFIG_SHELL_BACKEND_SERIAL_RX_RING_BUFFER_SIZE]; struct k_timer rx_timer; }; diff --git a/include/zephyr/sys/ring_buffer.h b/include/zephyr/sys/ring_buffer.h index 76c9710761008..957636b92560b 100644 --- a/include/zephyr/sys/ring_buffer.h +++ b/include/zephyr/sys/ring_buffer.h @@ -1,13 +1,66 @@ /* - * Copyright (c) 2015 Intel Corporation + * Copyright (c) 2025 Måns Ansgariusson * * SPDX-License-Identifier: Apache-2.0 */ +#ifdef CONFIG_RING_BUFFER +/* TODO: remove everything encapsulated by CONFIG_RING_BUFFER when deprecated ring buffer is + * removed + */ +#include + +#ifndef _RING_BUFFER_SHIM_ +#define _RING_BUFFER_SHIM_ + +struct ring_buffer { struct ring_buf rb; }; +#define RING_BUFFER_INIT(buff, size) { .rb = RING_BUF_INIT(buff, size) } +#define RING_BUFFER_DECLARE(buff, size) RING_BUF_DECLARE(buff, size) +static inline void ring_buffer_init(struct ring_buffer *rb, uint8_t *buff, size_t size) +{ + ring_buf_init((struct ring_buf *)rb, size, buff); +} +static inline size_t ring_buffer_write(struct ring_buffer *rb, const uint8_t *data, size_t size) +{ + return ring_buf_put((struct ring_buf *)rb, data, size); +} + +static inline size_t ring_buffer_read(struct ring_buffer *rb, uint8_t *data, size_t size) +{ + return ring_buf_get((struct ring_buf *)rb, data, size); +} + +static inline size_t ring_buffer_size(const struct ring_buffer *rb) +{ + return ring_buf_size_get((const struct ring_buf *)rb); +} + +static inline size_t ring_buffer_capacity(const struct ring_buffer *rb) +{ + return ring_buf_capacity_get((const struct ring_buf *)rb); +} +static inline bool ring_buffer_empty(const struct ring_buffer *rb) +{ + return ring_buf_is_empty((const struct ring_buf *)rb); +} +static inline void ring_buffer_reset(struct ring_buffer *rb) +{ + ring_buf_reset((struct ring_buf *)rb); +} +static inline size_t ring_buffer_space(struct ring_buffer *rb) +{ + return ring_buf_space_get((struct ring_buf *)rb); +} +#endif /* _RING_BUFFER_SHIM_ */ + +#else + #ifndef ZEPHYR_INCLUDE_SYS_RING_BUFFER_H_ #define ZEPHYR_INCLUDE_SYS_RING_BUFFER_H_ -#include +#include +#include +#include #include #ifdef __cplusplus @@ -26,59 +79,40 @@ extern "C" { /** @cond INTERNAL_HIDDEN */ -/* The limit is used by algorithm for distinguishing between empty and full - * state. - */ #ifdef CONFIG_RING_BUFFER_LARGE -typedef uint32_t ring_buf_idx_t; -#define RING_BUFFER_MAX_SIZE (UINT32_MAX / 2) +typedef size_t ring_buffer_index_t; +typedef size_t ring_buffer_size_t; #else -typedef uint16_t ring_buf_idx_t; -#define RING_BUFFER_MAX_SIZE (UINT16_MAX / 2) +typedef uint16_t ring_buffer_index_t; +typedef uint16_t ring_buffer_size_t; #endif - -#define RING_BUFFER_SIZE_ASSERT_MSG "Size too big" - -struct ring_buf_index { ring_buf_idx_t head, tail, base; }; - +#define RING_BUFFER_MAX_SIZE ((((size_t)1) << (8 * sizeof(ring_buffer_index_t))) - 1) +#define RING_BUFFER_SIZE_ERROR_MSG \ + "ring buffer size exceeds maximum for indexing type" /** @endcond */ /** * @brief A structure to represent a ring buffer */ -struct ring_buf { +struct ring_buffer { /** @cond INTERNAL_HIDDEN */ uint8_t *buffer; - struct ring_buf_index put; - struct ring_buf_index get; - uint32_t size; + ring_buffer_size_t size; + ring_buffer_index_t read_ptr; + ring_buffer_index_t write_ptr; /** @endcond */ }; /** @cond INTERNAL_HIDDEN */ -uint32_t ring_buf_area_claim(struct ring_buf *buf, struct ring_buf_index *ring, - uint8_t **data, uint32_t size); -int ring_buf_area_finish(struct ring_buf *buf, struct ring_buf_index *ring, - uint32_t size); - -/** - * @brief Function to force ring_buf internal states to given value - * - * Any value other than 0 makes sense only in validation testing context. - */ -static inline void ring_buf_internal_reset(struct ring_buf *buf, ring_buf_idx_t value) -{ - buf->put.head = buf->put.tail = buf->put.base = value; - buf->get.head = buf->get.tail = buf->get.base = value; -} - /** @endcond */ -#define RING_BUF_INIT(buf, size8) \ -{ \ - .buffer = buf, \ - .size = size8, \ +#define RING_BUFFER_INIT(buf, sz) \ +{ \ + .buffer = buf, \ + .size = sz, \ + .read_ptr = 0, \ + .write_ptr = 0, \ } /** @@ -90,75 +124,15 @@ static inline void ring_buf_internal_reset(struct ring_buf *buf, ring_buf_idx_t * The ring buffer can be accessed outside the module where it is defined * using: * - * @code extern struct ring_buf ; @endcode + * @code extern struct ring_buffer ; @endcode * * @param name Name of the ring buffer. - * @param size8 Size of ring buffer (in bytes). - */ -#define RING_BUF_DECLARE(name, size8) \ - BUILD_ASSERT(size8 <= RING_BUFFER_MAX_SIZE,\ - RING_BUFFER_SIZE_ASSERT_MSG); \ - static uint8_t __noinit _ring_buffer_data_##name[size8]; \ - struct ring_buf name = RING_BUF_INIT(_ring_buffer_data_##name, size8) - -/** - * @brief 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: - * - * @code extern struct ring_buf ; @endcode - * - * @param name Name of the ring buffer. - * @param size32 Size of ring buffer (in 32-bit words). + * @param size Size of ring buffer (in bytes). */ -#define RING_BUF_ITEM_DECLARE(name, size32) \ - BUILD_ASSERT((size32) <= RING_BUFFER_MAX_SIZE / 4, \ - RING_BUFFER_SIZE_ASSERT_MSG); \ - static uint32_t __noinit _ring_buffer_data_##name[size32]; \ - struct ring_buf name = { \ - .buffer = (uint8_t *) _ring_buffer_data_##name, \ - .size = 4 * (size32) \ - } - -/** - * @brief Define and initialize an "item based" ring buffer. - * - * This exists for backward compatibility reasons. @ref RING_BUF_ITEM_DECLARE - * should be used instead. - * - * @param name Name of the ring buffer. - * @param size32 Size of ring buffer (in 32-bit words). - */ -#define RING_BUF_ITEM_DECLARE_SIZE(name, size32) \ - RING_BUF_ITEM_DECLARE(name, size32) - -/** - * @brief 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. @ref RING_BUF_ITEM_DECLARE should be used instead. - * - * @param name Name of the ring buffer. - * @param pow Ring buffer size exponent. - */ -#define RING_BUF_ITEM_DECLARE_POW2(name, pow) \ - RING_BUF_ITEM_DECLARE(name, BIT(pow)) - -/** - * @brief 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. - * - * @param expr Expression or type to compute the size of - */ -#define RING_BUF_ITEM_SIZEOF(expr) DIV_ROUND_UP(sizeof(expr), sizeof(uint32_t)) +#define RING_BUFFER_DECLARE(name, size) \ + static uint8_t __noinit _ring_buffer_data_##name[size]; \ + struct ring_buffer name = RING_BUFFER_INIT(_ring_buffer_data_##name, size); \ + BUILD_ASSERT(size < RING_BUFFER_MAX_SIZE, RING_BUFFER_SIZE_ERROR_MSG) /** * @brief Initialize a ring buffer for byte data. @@ -166,358 +140,153 @@ static inline void ring_buf_internal_reset(struct ring_buf *buf, ring_buf_idx_t * This routine initializes a ring buffer, prior to its first use. It is only * used for ring buffers not defined using RING_BUF_DECLARE. * - * @param buf Address of ring buffer. - * @param size Ring buffer size (in bytes). + * @param rb Address of ring buffer. * @param data Ring buffer data area (uint8_t data[size]). + * @param size Ring buffer size (in bytes). */ -static inline void ring_buf_init(struct ring_buf *buf, - uint32_t size, - uint8_t *data) -{ - __ASSERT(size <= RING_BUFFER_MAX_SIZE, RING_BUFFER_SIZE_ASSERT_MSG); - - buf->size = size; - buf->buffer = data; - ring_buf_internal_reset(buf, 0); -} +void ring_buffer_init(struct ring_buffer *rb, uint8_t *data, size_t size); /** - * @brief 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. + * @brief Determine if a ring buffer is empty. * - * 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. + * @param rb Address of ring buffer. * - * @param buf Address of ring buffer. - * @param size Ring buffer size (in 32-bit words) - * @param data Ring buffer data area (uint32_t data[size]). + * @return true if the ring buffer is empty, else false. */ -static inline void ring_buf_item_init(struct ring_buf *buf, - uint32_t size, - uint32_t *data) -{ - __ASSERT(size <= RING_BUFFER_MAX_SIZE / 4, RING_BUFFER_SIZE_ASSERT_MSG); - ring_buf_init(buf, 4 * size, (uint8_t *)data); -} +bool ring_buffer_empty(const struct ring_buffer *rb); /** - * @brief Determine if a ring buffer is empty. + * @brief Determine if a ring buffer is full. * - * @param buf Address of ring buffer. + * @param rb Address of ring buffer. * - * @return true if the ring buffer is empty, or false if not. + * @return true if the ring buffer is full, else false. */ -static inline bool ring_buf_is_empty(const struct ring_buf *buf) -{ - return buf->get.head == buf->put.tail; -} +bool ring_buffer_full(const struct ring_buffer *rb); + /** * @brief Reset ring buffer state. * - * @param buf Address of ring buffer. + * @param rb Address of ring buffer. */ -static inline void ring_buf_reset(struct ring_buf *buf) -{ - ring_buf_internal_reset(buf, 0); -} +void ring_buffer_reset(struct ring_buffer *rb); /** * @brief Determine free space in a ring buffer. * - * @param buf Address of ring buffer. + * @param rb Address of ring buffer. * * @return Ring buffer free space (in bytes). */ -static inline uint32_t ring_buf_space_get(const struct ring_buf *buf) -{ - ring_buf_idx_t allocated = buf->put.head - buf->get.tail; - - return buf->size - allocated; -} - -/** - * @brief Determine free space in an "item based" ring buffer. - * - * @param buf Address of ring buffer. - * - * @return Ring buffer free space (in 32-bit words). - */ -static inline uint32_t ring_buf_item_space_get(const struct ring_buf *buf) -{ - return ring_buf_space_get(buf) / 4; -} +size_t ring_buffer_space(const struct ring_buffer *rb); /** * @brief Return ring buffer capacity. * - * @param buf Address of ring buffer. + * @param rb Address of ring buffer. * * @return Ring buffer capacity (in bytes). */ -static inline uint32_t ring_buf_capacity_get(const struct ring_buf *buf) -{ - return buf->size; -} +size_t ring_buffer_capacity(const struct ring_buffer *rb); /** * @brief Determine size of available data in a ring buffer. * - * @param buf Address of ring buffer. + * @param rb Address of ring buffer. * * @return Ring buffer data size (in bytes). */ -static inline uint32_t ring_buf_size_get(const struct ring_buf *buf) -{ - ring_buf_idx_t available = buf->put.tail - buf->get.head; - - return available; -} +size_t ring_buffer_size(const struct ring_buffer *rb); /** - * @brief Allocate buffer for writing data to a ring buffer. + * @brief Request buffer for writing data to a ring buffer directly. * * 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 @ref ring_buf_put_finish). + * can be used directly by the user. Once data is written to allotted area + * number of bytes written must be confirmed (see @ref ring_buffer_commit). * - * @warning - * Use cases involving multiple writers to the ring buffer must prevent - * concurrent write operations, either by preventing all writers from - * being preempted or by using a mutex to govern writes to the ring buffer. + * @param rb Address of ring buffer. + * @param data Pointer to the write address set within the ring buffer. * - * @warning - * Ring buffer instance should not mix byte access and item access - * (calls prefixed with ring_buf_item_). - * - * @param[in] buf Address of ring buffer. - * @param[out] data Pointer to the address. It is set to a location within - * ring buffer. - * @param[in] size Requested allocation size (in bytes). - * - * @return Size of allocated buffer which can be smaller than requested if - * there is not enough free space or buffer wraps. + * @return Size of the allotted buffer. */ -static inline uint32_t ring_buf_put_claim(struct ring_buf *buf, - uint8_t **data, - uint32_t size) -{ - uint32_t space = ring_buf_space_get(buf); - return ring_buf_area_claim(buf, &buf->put, data, - MIN(size, space)); -} +size_t ring_buffer_write_ptr(const struct ring_buffer *rb, uint8_t **data); /** - * @brief Indicate number of bytes written to allocated buffers. + * @brief confirm number of bytes written to claimed buffer. * - * The number of bytes must be equal to or lower than the sum corresponding - * to all preceding @ref ring_buf_put_claim invocations (or even 0). Surplus - * bytes will be returned to the available free buffer space. + * The number of bytes must be equal to or lower than the preceding @ref ring_buffer_write_ptr. * - * @warning - * Use cases involving multiple writers to the ring buffer must prevent - * concurrent write operations, either by preventing all writers from - * being preempted or by using a mutex to govern writes to the ring buffer. - * - * @warning - * Ring buffer instance should not mix byte access and item access - * (calls prefixed with ring_buf_item_). - * - * @param buf Address of ring buffer. + * @param rb Address of ring buffer. * @param size Number of valid bytes in the allocated buffers. - * - * @retval 0 Successful operation. - * @retval -EINVAL Provided @a size exceeds free space in the ring buffer. */ -static inline int ring_buf_put_finish(struct ring_buf *buf, uint32_t size) -{ - return ring_buf_area_finish(buf, &buf->put, size); -} +void ring_buffer_commit(struct ring_buffer *rb, size_t size); /** * @brief Write (copy) data to a ring buffer. * - * This routine writes data to a ring buffer @a buf. - * - * @warning - * Use cases involving multiple writers to the ring buffer must prevent - * concurrent write operations, either by preventing all writers from - * being preempted or by using a mutex to govern writes to the ring buffer. + * This routine writes data to a ring buffer @a rb. * - * @warning - * Ring buffer instance should not mix byte access and item access - * (calls prefixed with ring_buf_item_). - * - * @param buf Address of ring buffer. + * @param rb Address of ring buffer. * @param data Address of data. * @param size Data size (in bytes). * * @retval Number of bytes written. */ -uint32_t ring_buf_put(struct ring_buf *buf, const uint8_t *data, uint32_t size); +size_t ring_buffer_write(struct ring_buffer *rb, const uint8_t *data, size_t size); /** * @brief 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 @ref ring_buf_get_finish. - * - * @warning - * Use cases involving multiple reads of the ring buffer must prevent - * concurrent read operations, either by preventing all readers from - * being preempted or by using a mutex to govern reads to the ring buffer. + * can be used directly by the user. Once data is processed it must be confirmed + * using @ref ring_buffer_consume. * - * @warning - * Ring buffer instance should not mix byte access and item access - * (calls prefixed with ring_buf_item_). + * @param rb Address of ring buffer. + * @param data Pointer to the address. It is set to a location within ring buffer. * - * @param[in] buf Address of ring buffer. - * @param[out] data Pointer to the address. It is set to a location within - * ring buffer. - * @param[in] size Requested size (in bytes). - * - * @return Number of valid bytes in the provided buffer which can be smaller - * than requested if there is not enough free space or buffer wraps. + * @return Number of bytes available in the buffer. */ -static inline uint32_t ring_buf_get_claim(struct ring_buf *buf, - uint8_t **data, - uint32_t size) -{ - uint32_t buf_size = ring_buf_size_get(buf); - return ring_buf_area_claim(buf, &buf->get, data, - MIN(size, buf_size)); -} +size_t ring_buffer_read_ptr(const struct ring_buffer *rb, uint8_t **data); /** * @brief Indicate number of bytes read from claimed buffer. * - * The number of bytes must be equal or lower than the sum corresponding to - * all preceding @ref ring_buf_get_claim invocations (or even 0). Surplus - * bytes will remain available in the buffer. - * - * @warning - * Use cases involving multiple reads of the ring buffer must prevent - * concurrent read operations, either by preventing all readers from - * being preempted or by using a mutex to govern reads to the ring buffer. + * The number of bytes must be less or equal to the preceding @ref ring_buffer_read_ptr. + * Surplus bytes will remain available in the buffer. * - * @warning - * Ring buffer instance should not mix byte access and item mode - * (calls prefixed with ring_buf_item_). + * @param rb Address of ring buffer. + * @param size Number of bytes that have been consumed from the buffer. * - * @param buf Address of ring buffer. - * @param size Number of bytes that can be freed. - * - * @retval 0 Successful operation. - * @retval -EINVAL Provided @a size exceeds valid bytes in the ring buffer. */ -static inline int ring_buf_get_finish(struct ring_buf *buf, uint32_t size) -{ - return ring_buf_area_finish(buf, &buf->get, size); -} +void ring_buffer_consume(struct ring_buffer *rb, size_t size); /** * @brief Read data from a ring buffer. * * This routine reads data from a ring buffer @a buf. * - * @warning - * Use cases involving multiple reads of the ring buffer must prevent - * concurrent read operations, either by preventing all readers from - * being preempted or by using a mutex to govern reads to the ring buffer. - * - * @warning - * Ring buffer instance should not mix byte access and item mode - * (calls prefixed with ring_buf_item_). - * - * @param buf Address of ring buffer. - * @param data Address of the output buffer. Can be NULL to discard data. + * @param rb Address of ring buffer. + * @param data Address of the output buffer. * @param size Data size (in bytes). * - * @retval Number of bytes written to the output buffer. + * @retval Number of bytes available in the data buffer. */ -uint32_t ring_buf_get(struct ring_buf *buf, uint8_t *data, uint32_t size); +size_t ring_buffer_read(struct ring_buffer *rb, uint8_t *data, size_t size); /** * @brief Peek at data from a ring buffer. * - * This routine reads data from a ring buffer @a buf without removal. - * - * @warning - * Use cases involving multiple reads of the ring buffer must prevent - * concurrent read operations, either by preventing all readers from - * being preempted or by using a mutex to govern reads to the ring buffer. - * - * @warning - * Ring buffer instance should not mix byte access and item mode - * (calls prefixed with ring_buf_item_). + * This routine reads data from a ring buffer @a buf without consuming. * - * @warning - * Multiple calls to peek will result in the same data being 'peeked' - * multiple times. To remove data, use either @ref ring_buf_get or - * @ref ring_buf_get_claim followed by @ref ring_buf_get_finish with a - * non-zero `size`. - * - * @param buf Address of ring buffer. - * @param data Address of the output buffer. Cannot be NULL. + * @param rb Address of ring buffer. + * @param data Address of the output buffer. * @param size Data size (in bytes). * * @retval Number of bytes written to the output buffer. */ -uint32_t ring_buf_peek(struct ring_buf *buf, uint8_t *data, uint32_t size); - -/** - * @brief Write a data item to a ring buffer. - * - * This routine writes a data item to ring buffer @a 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. - * - * @warning - * Use cases involving multiple writers to the ring buffer must prevent - * concurrent write operations, either by preventing all writers from - * being preempted or by using a mutex to govern writes to the ring buffer. - * - * @param buf Address of ring buffer. - * @param type Data item's type identifier (application specific). - * @param value Data item's integer value (application specific). - * @param data Address of data item. - * @param size32 Data item size (number of 32-bit words). - * - * @retval 0 Data item was written. - * @retval -EMSGSIZE Ring buffer has insufficient free space. - */ -int ring_buf_item_put(struct ring_buf *buf, uint16_t type, uint8_t value, - uint32_t *data, uint8_t size32); - -/** - * @brief Read a data item from a ring buffer. - * - * This routine reads a data item from ring buffer @a 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. - * - * @warning - * Use cases involving multiple reads of the ring buffer must prevent - * concurrent read operations, either by preventing all readers from - * being preempted or by using a mutex to govern reads to the ring buffer. - * - * @param buf Address of ring buffer. - * @param type Area to store the data item's type identifier. - * @param value Area to store the data item's integer value. - * @param data Area to store the data item. Can be NULL to discard data. - * @param size32 Size of the data item storage area (number of 32-bit chunks). - * - * @retval 0 Data item was fetched; @a size32 now contains the number of - * 32-bit words read into data area @a data. - * @retval -EAGAIN Ring buffer is empty. - * @retval -EMSGSIZE Data area @a data is too small; @a size32 now contains - * the number of 32-bit words needed. - */ -int ring_buf_item_get(struct ring_buf *buf, uint16_t *type, uint8_t *value, - uint32_t *data, uint8_t *size32); +size_t ring_buffer_peek(struct ring_buffer *rb, uint8_t *data, size_t size); /** * @} @@ -528,3 +297,4 @@ int ring_buf_item_get(struct ring_buf *buf, uint16_t *type, uint8_t *value, #endif #endif /* ZEPHYR_INCLUDE_SYS_RING_BUFFER_H_ */ +#endif /* CONFIG_RING_BUFFER */ diff --git a/include/zephyr/sys/ring_buffer_deprecated.h b/include/zephyr/sys/ring_buffer_deprecated.h new file mode 100644 index 0000000000000..2fcb48c670ba7 --- /dev/null +++ b/include/zephyr/sys/ring_buffer_deprecated.h @@ -0,0 +1,532 @@ +/* + * Copyright (c) 2015 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_INCLUDE_SYS_RING_BUFFER_H_ +#define ZEPHYR_INCLUDE_SYS_RING_BUFFER_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @file + * @defgroup ring_buffer_apis Ring Buffer APIs + * @ingroup datastructure_apis + * + * @brief Simple ring buffer implementation. + * + * @{ + */ + +/** @cond INTERNAL_HIDDEN */ + +/* The limit is used by algorithm for distinguishing between empty and full + * state. + */ +#ifdef CONFIG_RING_BUFFER_LARGE +typedef uint32_t ring_buf_idx_t; +#define RING_BUFFER_MAX_SIZE (UINT32_MAX / 2) +#else +typedef uint16_t ring_buf_idx_t; +#define RING_BUFFER_MAX_SIZE (UINT16_MAX / 2) +#endif + +#define RING_BUFFER_SIZE_ASSERT_MSG "Size too big" + +struct ring_buf_index { ring_buf_idx_t head, tail, base; }; + +/** @endcond */ + +/** + * @brief A structure to represent a ring buffer + */ +struct ring_buf { + /** @cond INTERNAL_HIDDEN */ + uint8_t *buffer; + struct ring_buf_index put; + struct ring_buf_index get; + uint32_t size; + /** @endcond */ +}; + +/** @cond INTERNAL_HIDDEN */ + +uint32_t ring_buf_area_claim(struct ring_buf *buf, struct ring_buf_index *ring, + uint8_t **data, uint32_t size); +int ring_buf_area_finish(struct ring_buf *buf, struct ring_buf_index *ring, + uint32_t size); + +/** + * @brief Function to force ring_buf internal states to given value + * + * Any value other than 0 makes sense only in validation testing context. + */ +static inline void ring_buf_internal_reset(struct ring_buf *buf, ring_buf_idx_t value) +{ + buf->put.head = buf->put.tail = buf->put.base = value; + buf->get.head = buf->get.tail = buf->get.base = value; +} + +/** @endcond */ + +#define RING_BUF_INIT(buf, size8) \ +{ \ + .buffer = buf, \ + .size = size8, \ +} + +/** + * @brief 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: + * + * @code extern struct ring_buf ; @endcode + * + * @param name Name of the ring buffer. + * @param size8 Size of ring buffer (in bytes). + */ +#define RING_BUF_DECLARE(name, size8) \ + BUILD_ASSERT(size8 <= RING_BUFFER_MAX_SIZE,\ + RING_BUFFER_SIZE_ASSERT_MSG); \ + static uint8_t __noinit _ring_buffer_data_##name[size8]; \ + struct ring_buf name = RING_BUF_INIT(_ring_buffer_data_##name, size8) + +/** + * @brief 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: + * + * @code extern struct ring_buf ; @endcode + * + * @param name Name of the ring buffer. + * @param size32 Size of ring buffer (in 32-bit words). + */ +#define RING_BUF_ITEM_DECLARE(name, size32) \ + BUILD_ASSERT((size32) <= RING_BUFFER_MAX_SIZE / 4, \ + RING_BUFFER_SIZE_ASSERT_MSG); \ + static uint32_t __noinit _ring_buffer_data_##name[size32]; \ + struct ring_buf name = { \ + .buffer = (uint8_t *) _ring_buffer_data_##name, \ + .size = 4 * (size32) \ + } + +/** + * @brief Define and initialize an "item based" ring buffer. + * + * This exists for backward compatibility reasons. @ref RING_BUF_ITEM_DECLARE + * should be used instead. + * + * @param name Name of the ring buffer. + * @param size32 Size of ring buffer (in 32-bit words). + */ +#define RING_BUF_ITEM_DECLARE_SIZE(name, size32) \ + RING_BUF_ITEM_DECLARE(name, size32) + +/** + * @brief 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. @ref RING_BUF_ITEM_DECLARE should be used instead. + * + * @param name Name of the ring buffer. + * @param pow Ring buffer size exponent. + */ +#define RING_BUF_ITEM_DECLARE_POW2(name, pow) \ + RING_BUF_ITEM_DECLARE(name, BIT(pow)) + +/** + * @brief 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. + * + * @param expr Expression or type to compute the size of + */ +#define RING_BUF_ITEM_SIZEOF(expr) DIV_ROUND_UP(sizeof(expr), sizeof(uint32_t)) + +/** + * @brief 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. + * + * @param buf Address of ring buffer. + * @param size Ring buffer size (in bytes). + * @param data Ring buffer data area (uint8_t data[size]). + */ +static inline void ring_buf_init(struct ring_buf *buf, + uint32_t size, + uint8_t *data) +{ + __ASSERT(size <= RING_BUFFER_MAX_SIZE, RING_BUFFER_SIZE_ASSERT_MSG); + + buf->size = size; + buf->buffer = data; + ring_buf_internal_reset(buf, 0); +} + +/** + * @brief 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. + * + * @param buf Address of ring buffer. + * @param size Ring buffer size (in 32-bit words) + * @param data Ring buffer data area (uint32_t data[size]). + */ +static inline void ring_buf_item_init(struct ring_buf *buf, + uint32_t size, + uint32_t *data) +{ + __ASSERT(size <= RING_BUFFER_MAX_SIZE / 4, RING_BUFFER_SIZE_ASSERT_MSG); + ring_buf_init(buf, 4 * size, (uint8_t *)data); +} + +/** + * @brief Determine if a ring buffer is empty. + * + * @param buf Address of ring buffer. + * + * @return true if the ring buffer is empty, or false if not. + */ +static inline bool ring_buf_is_empty(const struct ring_buf *buf) +{ + return buf->get.head == buf->put.tail; +} + +/** + * @brief Reset ring buffer state. + * + * @param buf Address of ring buffer. + */ +static inline void ring_buf_reset(struct ring_buf *buf) +{ + ring_buf_internal_reset(buf, 0); +} + +/** + * @brief Determine free space in a ring buffer. + * + * @param buf Address of ring buffer. + * + * @return Ring buffer free space (in bytes). + */ +static inline uint32_t ring_buf_space_get(const struct ring_buf *buf) +{ + ring_buf_idx_t allocated = buf->put.head - buf->get.tail; + + return buf->size - allocated; +} + +/** + * @brief Determine free space in an "item based" ring buffer. + * + * @param buf Address of ring buffer. + * + * @return Ring buffer free space (in 32-bit words). + */ +static inline uint32_t ring_buf_item_space_get(const struct ring_buf *buf) +{ + return ring_buf_space_get(buf) / 4; +} + +/** + * @brief Return ring buffer capacity. + * + * @param buf Address of ring buffer. + * + * @return Ring buffer capacity (in bytes). + */ +static inline uint32_t ring_buf_capacity_get(const struct ring_buf *buf) +{ + return buf->size; +} + +/** + * @brief Determine size of available data in a ring buffer. + * + * @param buf Address of ring buffer. + * + * @return Ring buffer data size (in bytes). + */ +static inline uint32_t ring_buf_size_get(const struct ring_buf *buf) +{ + ring_buf_idx_t available = buf->put.tail - buf->get.head; + + return available; +} + +/** + * @brief 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 @ref ring_buf_put_finish). + * + * @warning + * Use cases involving multiple writers to the ring buffer must prevent + * concurrent write operations, either by preventing all writers from + * being preempted or by using a mutex to govern writes to the ring buffer. + * + * @warning + * Ring buffer instance should not mix byte access and item access + * (calls prefixed with ring_buf_item_). + * + * @param[in] buf Address of ring buffer. + * @param[out] data Pointer to the address. It is set to a location within + * ring buffer. + * @param[in] size Requested allocation size (in bytes). + * + * @return Size of allocated buffer which can be smaller than requested if + * there is not enough free space or buffer wraps. + */ +static inline uint32_t ring_buf_put_claim(struct ring_buf *buf, + uint8_t **data, + uint32_t size) +{ + uint32_t space = ring_buf_space_get(buf); + + return ring_buf_area_claim(buf, &buf->put, data, + MIN(size, space)); +} + +/** + * @brief 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 @ref ring_buf_put_claim invocations (or even 0). Surplus + * bytes will be returned to the available free buffer space. + * + * @warning + * Use cases involving multiple writers to the ring buffer must prevent + * concurrent write operations, either by preventing all writers from + * being preempted or by using a mutex to govern writes to the ring buffer. + * + * @warning + * Ring buffer instance should not mix byte access and item access + * (calls prefixed with ring_buf_item_). + * + * @param buf Address of ring buffer. + * @param size Number of valid bytes in the allocated buffers. + * + * @retval 0 Successful operation. + * @retval -EINVAL Provided @a size exceeds free space in the ring buffer. + */ +static inline int ring_buf_put_finish(struct ring_buf *buf, uint32_t size) +{ + return ring_buf_area_finish(buf, &buf->put, size); +} + +/** + * @brief Write (copy) data to a ring buffer. + * + * This routine writes data to a ring buffer @a buf. + * + * @warning + * Use cases involving multiple writers to the ring buffer must prevent + * concurrent write operations, either by preventing all writers from + * being preempted or by using a mutex to govern writes to the ring buffer. + * + * @warning + * Ring buffer instance should not mix byte access and item access + * (calls prefixed with ring_buf_item_). + * + * @param buf Address of ring buffer. + * @param data Address of data. + * @param size Data size (in bytes). + * + * @retval Number of bytes written. + */ +uint32_t ring_buf_put(struct ring_buf *buf, const uint8_t *data, uint32_t size); + +/** + * @brief 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 @ref ring_buf_get_finish. + * + * @warning + * Use cases involving multiple reads of the ring buffer must prevent + * concurrent read operations, either by preventing all readers from + * being preempted or by using a mutex to govern reads to the ring buffer. + * + * @warning + * Ring buffer instance should not mix byte access and item access + * (calls prefixed with ring_buf_item_). + * + * @param[in] buf Address of ring buffer. + * @param[out] data Pointer to the address. It is set to a location within + * ring buffer. + * @param[in] size Requested size (in bytes). + * + * @return Number of valid bytes in the provided buffer which can be smaller + * than requested if there is not enough free space or buffer wraps. + */ +static inline uint32_t ring_buf_get_claim(struct ring_buf *buf, + uint8_t **data, + uint32_t size) +{ + uint32_t buf_size = ring_buf_size_get(buf); + + return ring_buf_area_claim(buf, &buf->get, data, + MIN(size, buf_size)); +} + +/** + * @brief Indicate number of bytes read from claimed buffer. + * + * The number of bytes must be equal or lower than the sum corresponding to + * all preceding @ref ring_buf_get_claim invocations (or even 0). Surplus + * bytes will remain available in the buffer. + * + * @warning + * Use cases involving multiple reads of the ring buffer must prevent + * concurrent read operations, either by preventing all readers from + * being preempted or by using a mutex to govern reads to the ring buffer. + * + * @warning + * Ring buffer instance should not mix byte access and item mode + * (calls prefixed with ring_buf_item_). + * + * @param buf Address of ring buffer. + * @param size Number of bytes that can be freed. + * + * @retval 0 Successful operation. + * @retval -EINVAL Provided @a size exceeds valid bytes in the ring buffer. + */ +static inline int ring_buf_get_finish(struct ring_buf *buf, uint32_t size) +{ + return ring_buf_area_finish(buf, &buf->get, size); +} + +/** + * @brief Read data from a ring buffer. + * + * This routine reads data from a ring buffer @a buf. + * + * @warning + * Use cases involving multiple reads of the ring buffer must prevent + * concurrent read operations, either by preventing all readers from + * being preempted or by using a mutex to govern reads to the ring buffer. + * + * @warning + * Ring buffer instance should not mix byte access and item mode + * (calls prefixed with ring_buf_item_). + * + * @param buf Address of ring buffer. + * @param data Address of the output buffer. Can be NULL to discard data. + * @param size Data size (in bytes). + * + * @retval Number of bytes written to the output buffer. + */ +uint32_t ring_buf_get(struct ring_buf *buf, uint8_t *data, uint32_t size); + +/** + * @brief Peek at data from a ring buffer. + * + * This routine reads data from a ring buffer @a buf without removal. + * + * @warning + * Use cases involving multiple reads of the ring buffer must prevent + * concurrent read operations, either by preventing all readers from + * being preempted or by using a mutex to govern reads to the ring buffer. + * + * @warning + * Ring buffer instance should not mix byte access and item mode + * (calls prefixed with ring_buf_item_). + * + * @warning + * Multiple calls to peek will result in the same data being 'peeked' + * multiple times. To remove data, use either @ref ring_buf_get or + * @ref ring_buf_get_claim followed by @ref ring_buf_get_finish with a + * non-zero `size`. + * + * @param buf Address of ring buffer. + * @param data Address of the output buffer. Cannot be NULL. + * @param size Data size (in bytes). + * + * @retval Number of bytes written to the output buffer. + */ +uint32_t ring_buf_peek(struct ring_buf *buf, uint8_t *data, uint32_t size); + +/** + * @brief Write a data item to a ring buffer. + * + * This routine writes a data item to ring buffer @a 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. + * + * @warning + * Use cases involving multiple writers to the ring buffer must prevent + * concurrent write operations, either by preventing all writers from + * being preempted or by using a mutex to govern writes to the ring buffer. + * + * @param buf Address of ring buffer. + * @param type Data item's type identifier (application specific). + * @param value Data item's integer value (application specific). + * @param data Address of data item. + * @param size32 Data item size (number of 32-bit words). + * + * @retval 0 Data item was written. + * @retval -EMSGSIZE Ring buffer has insufficient free space. + */ +int ring_buf_item_put(struct ring_buf *buf, uint16_t type, uint8_t value, + uint32_t *data, uint8_t size32); + +/** + * @brief Read a data item from a ring buffer. + * + * This routine reads a data item from ring buffer @a 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. + * + * @warning + * Use cases involving multiple reads of the ring buffer must prevent + * concurrent read operations, either by preventing all readers from + * being preempted or by using a mutex to govern reads to the ring buffer. + * + * @param buf Address of ring buffer. + * @param type Area to store the data item's type identifier. + * @param value Area to store the data item's integer value. + * @param data Area to store the data item. Can be NULL to discard data. + * @param size32 Size of the data item storage area (number of 32-bit chunks). + * + * @retval 0 Data item was fetched; @a size32 now contains the number of + * 32-bit words read into data area @a data. + * @retval -EAGAIN Ring buffer is empty. + * @retval -EMSGSIZE Data area @a data is too small; @a size32 now contains + * the number of 32-bit words needed. + */ +int ring_buf_item_get(struct ring_buf *buf, uint16_t *type, uint8_t *value, + uint32_t *data, uint8_t *size32); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_SYS_RING_BUFFER_H_ */ diff --git a/kernel/Kconfig b/kernel/Kconfig index bb2c5bade905b..50d8ae9f100a1 100644 --- a/kernel/Kconfig +++ b/kernel/Kconfig @@ -12,7 +12,6 @@ source "subsys/logging/Kconfig.template.log_config" config MULTITHREADING bool "Multi-threading" if ARCH_HAS_SINGLE_THREAD_SUPPORT default y - select RING_BUFFER help If disabled, only the main thread is available, so a main() function must be provided. Interrupts are available. Kernel objects will most diff --git a/kernel/pipe.c b/kernel/pipe.c index c5157ea6921e6..130d136743066 100644 --- a/kernel/pipe.c +++ b/kernel/pipe.c @@ -26,12 +26,12 @@ static inline bool pipe_resetting(struct k_pipe *pipe) static inline bool pipe_full(struct k_pipe *pipe) { - return ring_buf_space_get(&pipe->buf) == 0; + return ring_buffer_space(&pipe->buf) == 0; } static inline bool pipe_empty(struct k_pipe *pipe) { - return ring_buf_is_empty(&pipe->buf); + return ring_buffer_empty(&pipe->buf); } static int wait_for(_wait_q_t *waitq, struct k_pipe *pipe, k_spinlock_key_t *key, @@ -66,7 +66,7 @@ static int wait_for(_wait_q_t *waitq, struct k_pipe *pipe, k_spinlock_key_t *key void z_impl_k_pipe_init(struct k_pipe *pipe, uint8_t *buffer, size_t buffer_size) { - ring_buf_init(&pipe->buf, buffer_size, buffer); + ring_buffer_init(&pipe->buf, buffer, buffer_size); pipe->flags = PIPE_FLAG_OPEN; pipe->waiting = 0; @@ -193,7 +193,7 @@ int z_impl_k_pipe_write(struct k_pipe *pipe, const uint8_t *data, size_t len, k_ K_POLL_STATE_PIPE_DATA_AVAILABLE); #endif /* CONFIG_POLL */ - written += ring_buf_put(&pipe->buf, &data[written], len - written); + written += ring_buffer_write(&pipe->buf, &data[written], len - written); if (likely(written == len)) { rc = written; break; @@ -238,7 +238,7 @@ int z_impl_k_pipe_read(struct k_pipe *pipe, uint8_t *data, size_t len, k_timeout need_resched = z_sched_wake_all(&pipe->space, 0, NULL); } - buf.used += ring_buf_get(&pipe->buf, &data[buf.used], len - buf.used); + buf.used += ring_buffer_read(&pipe->buf, &data[buf.used], len - buf.used); if (likely(buf.used == len)) { rc = buf.used; break; @@ -274,7 +274,7 @@ void z_impl_k_pipe_reset(struct k_pipe *pipe) { SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_pipe, reset, pipe); K_SPINLOCK(&pipe->lock) { - ring_buf_reset(&pipe->buf); + ring_buffer_reset(&pipe->buf); if (likely(pipe->waiting != 0)) { pipe->flags |= PIPE_FLAG_RESET; z_sched_wake_all(&pipe->data, 0, NULL); diff --git a/kernel/poll.c b/kernel/poll.c index da956a268cc81..4000caa9dd1c5 100644 --- a/kernel/poll.c +++ b/kernel/poll.c @@ -88,7 +88,7 @@ static inline bool is_condition_met(struct k_poll_event *event, uint32_t *state) } break; case K_POLL_TYPE_PIPE_DATA_AVAILABLE: - if (!ring_buf_is_empty(&event->pipe->buf)) { + if (!ring_buffer_empty(&event->pipe->buf)) { *state = K_POLL_STATE_PIPE_DATA_AVAILABLE; return true; } diff --git a/lib/utils/CMakeLists.txt b/lib/utils/CMakeLists.txt index f6ab864d4640e..e15b4fc8f4829 100644 --- a/lib/utils/CMakeLists.txt +++ b/lib/utils/CMakeLists.txt @@ -11,13 +11,18 @@ zephyr_sources( bitmask.c ) +if (NOT CONFIG_RING_BUFFER) + #TODO: Move to zephyr_sources when old ring buffer is removed + zephyr_sources(ring_buffer.c) +endif() + zephyr_sources_ifdef(CONFIG_ONOFF onoff.c) zephyr_sources_ifdef(CONFIG_NOTIFY notify.c) zephyr_sources_ifdef(CONFIG_JSON_LIBRARY json.c) -zephyr_sources_ifdef(CONFIG_RING_BUFFER ring_buffer.c) +zephyr_sources_ifdef(CONFIG_RING_BUFFER ring_buf.c) zephyr_sources_ifdef(CONFIG_UTF8 utf8.c) diff --git a/lib/utils/Kconfig b/lib/utils/Kconfig index 4db79272bbafe..237332d6209e6 100644 --- a/lib/utils/Kconfig +++ b/lib/utils/Kconfig @@ -22,6 +22,7 @@ config JSON_LIBRARY_FP_SUPPORT config RING_BUFFER bool "Ring buffers" + select DEPRECATED help Provide highly efficient ring buffer management for arbitrary data. Some facilities such as kernel pipes are built on top of this. @@ -29,10 +30,9 @@ config RING_BUFFER config RING_BUFFER_LARGE bool "Allow large ring buffer sizes" - depends on RING_BUFFER help Increase maximum buffer size from 32KB to 2GB. When this is enabled, - all struct ring_buf instances become 12 bytes bigger. + increases the ring_buffer struct size slightly. config NOTIFY bool "Asynchronous Notifications" diff --git a/lib/utils/ring_buf.c b/lib/utils/ring_buf.c new file mode 100644 index 0000000000000..652b53b758f8b --- /dev/null +++ b/lib/utils/ring_buf.c @@ -0,0 +1,234 @@ +/* ring_buffer.c: Simple ring buffer API */ + +/* + * Copyright (c) 2015 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +uint32_t ring_buf_area_claim(struct ring_buf *buf, struct ring_buf_index *ring, + uint8_t **data, uint32_t size) +{ + ring_buf_idx_t head_offset, wrap_size; + + head_offset = ring->head - ring->base; + if (unlikely(head_offset >= buf->size)) { + /* ring->base is not yet adjusted */ + head_offset -= buf->size; + } + wrap_size = buf->size - head_offset; + size = MIN(size, wrap_size); + + *data = &buf->buffer[head_offset]; + ring->head += size; + + return size; +} + +int ring_buf_area_finish(struct ring_buf *buf, struct ring_buf_index *ring, + uint32_t size) +{ + ring_buf_idx_t claimed_size, tail_offset; + + claimed_size = ring->head - ring->tail; + if (unlikely(size > claimed_size)) { + return -EINVAL; + } + + ring->tail += size; + ring->head = ring->tail; + + tail_offset = ring->tail - ring->base; + if (unlikely(tail_offset >= buf->size)) { + /* we wrapped: adjust ring->base */ + ring->base += buf->size; + } + + return 0; +} + +uint32_t ring_buf_put(struct ring_buf *buf, const uint8_t *data, uint32_t size) +{ + uint8_t *dst; + uint32_t partial_size; + uint32_t total_size = 0U; + int err; + + do { + partial_size = ring_buf_put_claim(buf, &dst, size); + if (partial_size == 0) { + break; + } + memcpy(dst, data, partial_size); + total_size += partial_size; + size -= partial_size; + data += partial_size; + } while (size != 0); + + err = ring_buf_put_finish(buf, total_size); + __ASSERT_NO_MSG(err == 0); + ARG_UNUSED(err); + + return total_size; +} + +uint32_t ring_buf_get(struct ring_buf *buf, uint8_t *data, uint32_t size) +{ + uint8_t *src; + uint32_t partial_size; + uint32_t total_size = 0U; + int err; + + do { + partial_size = ring_buf_get_claim(buf, &src, size); + if (partial_size == 0) { + break; + } + if (data) { + memcpy(data, src, partial_size); + data += partial_size; + } + total_size += partial_size; + size -= partial_size; + } while (size != 0); + + err = ring_buf_get_finish(buf, total_size); + __ASSERT_NO_MSG(err == 0); + ARG_UNUSED(err); + + return total_size; +} + +uint32_t ring_buf_peek(struct ring_buf *buf, uint8_t *data, uint32_t size) +{ + uint8_t *src; + uint32_t partial_size; + uint32_t total_size = 0U; + int err; + + do { + partial_size = ring_buf_get_claim(buf, &src, size); + if (partial_size == 0) { + break; + } + __ASSERT_NO_MSG(data != NULL); + memcpy(data, src, partial_size); + data += partial_size; + total_size += partial_size; + size -= partial_size; + } while (size != 0); + + /* effectively unclaim total_size bytes */ + err = ring_buf_get_finish(buf, 0); + __ASSERT_NO_MSG(err == 0); + ARG_UNUSED(err); + + return total_size; +} + +/** + * Internal data structure for a buffer header. + * + * We want all of this to fit in a single uint32_t. Every item stored in the + * ring buffer will be one of these headers plus any extra data supplied + */ +struct ring_element { + uint32_t type :16; /**< Application-specific */ + uint32_t length :8; /**< length in 32-bit chunks */ + uint32_t value :8; /**< Room for small integral values */ +}; + +int ring_buf_item_put(struct ring_buf *buf, uint16_t type, uint8_t value, + uint32_t *data32, uint8_t size32) +{ + uint8_t *dst, *data = (uint8_t *)data32; + struct ring_element *header; + uint32_t space, size, partial_size, total_size; + int err; + + space = ring_buf_space_get(buf); + size = size32 * 4; + if (size + sizeof(struct ring_element) > space) { + return -EMSGSIZE; + } + + err = ring_buf_put_claim(buf, &dst, sizeof(struct ring_element)); + __ASSERT_NO_MSG(err == sizeof(struct ring_element)); + + header = (struct ring_element *)dst; + header->type = type; + header->length = size32; + header->value = value; + total_size = sizeof(struct ring_element); + + do { + partial_size = ring_buf_put_claim(buf, &dst, size); + if (partial_size == 0) { + break; + } + memcpy(dst, data, partial_size); + size -= partial_size; + total_size += partial_size; + data += partial_size; + } while (size != 0); + __ASSERT_NO_MSG(size == 0); + + err = ring_buf_put_finish(buf, total_size); + __ASSERT_NO_MSG(err == 0); + ARG_UNUSED(err); + + return 0; +} + +int ring_buf_item_get(struct ring_buf *buf, uint16_t *type, uint8_t *value, + uint32_t *data32, uint8_t *size32) +{ + uint8_t *src, *data = (uint8_t *)data32; + struct ring_element *header; + uint32_t size, partial_size, total_size; + int err; + + if (ring_buf_is_empty(buf)) { + return -EAGAIN; + } + + err = ring_buf_get_claim(buf, &src, sizeof(struct ring_element)); + __ASSERT_NO_MSG(err == sizeof(struct ring_element)); + + header = (struct ring_element *)src; + + if (data && (header->length > *size32)) { + *size32 = header->length; + ring_buf_get_finish(buf, 0); + return -EMSGSIZE; + } + + *size32 = header->length; + *type = header->type; + *value = header->value; + total_size = sizeof(struct ring_element); + + size = *size32 * 4; + + do { + partial_size = ring_buf_get_claim(buf, &src, size); + if (partial_size == 0) { + break; + } + if (data) { + memcpy(data, src, partial_size); + data += partial_size; + } + total_size += partial_size; + size -= partial_size; + } while (size != 0); + + err = ring_buf_get_finish(buf, total_size); + __ASSERT_NO_MSG(err == 0); + ARG_UNUSED(err); + + return 0; +} diff --git a/lib/utils/ring_buffer.c b/lib/utils/ring_buffer.c index 652b53b758f8b..27e3cb15865e1 100644 --- a/lib/utils/ring_buffer.c +++ b/lib/utils/ring_buffer.c @@ -1,234 +1,160 @@ -/* ring_buffer.c: Simple ring buffer API */ - /* - * Copyright (c) 2015 Intel Corporation + * Copyright (c) 2025 Måns Ansgariusson * * SPDX-License-Identifier: Apache-2.0 */ - #include -#include - -uint32_t ring_buf_area_claim(struct ring_buf *buf, struct ring_buf_index *ring, - uint8_t **data, uint32_t size) -{ - ring_buf_idx_t head_offset, wrap_size; - head_offset = ring->head - ring->base; - if (unlikely(head_offset >= buf->size)) { - /* ring->base is not yet adjusted */ - head_offset -= buf->size; - } - wrap_size = buf->size - head_offset; - size = MIN(size, wrap_size); - - *data = &buf->buffer[head_offset]; - ring->head += size; +#include +LOG_MODULE_REGISTER(ring_buffer, LOG_LEVEL_DBG); - return size; +static size_t index(size_t p, size_t size) +{ + return p % size; } -int ring_buf_area_finish(struct ring_buf *buf, struct ring_buf_index *ring, - uint32_t size) +static size_t read_index(const struct ring_buffer *rb) { - ring_buf_idx_t claimed_size, tail_offset; + return index(rb->read_ptr, rb->size); +} - claimed_size = ring->head - ring->tail; - if (unlikely(size > claimed_size)) { - return -EINVAL; - } +static size_t write_index(const struct ring_buffer *rb) +{ + return index(rb->write_ptr, rb->size); +} - ring->tail += size; - ring->head = ring->tail; +size_t ring_buffer_size(const struct ring_buffer *rb) +{ + return rb->write_ptr - rb->read_ptr; +} - tail_offset = ring->tail - ring->base; - if (unlikely(tail_offset >= buf->size)) { - /* we wrapped: adjust ring->base */ - ring->base += buf->size; - } +size_t ring_buffer_capacity(const struct ring_buffer *rb) +{ + return rb->size; +} - return 0; +bool ring_buffer_full(const struct ring_buffer *rb) +{ + return ring_buffer_size(rb) == ring_buffer_capacity(rb); } -uint32_t ring_buf_put(struct ring_buf *buf, const uint8_t *data, uint32_t size) +bool ring_buffer_empty(const struct ring_buffer *rb) { - uint8_t *dst; - uint32_t partial_size; - uint32_t total_size = 0U; - int err; + return ring_buffer_size(rb) == 0; +} - do { - partial_size = ring_buf_put_claim(buf, &dst, size); - if (partial_size == 0) { - break; - } - memcpy(dst, data, partial_size); - total_size += partial_size; - size -= partial_size; - data += partial_size; - } while (size != 0); +size_t ring_buffer_space(const struct ring_buffer *rb) +{ + return ring_buffer_capacity(rb) - ring_buffer_size(rb); +} - err = ring_buf_put_finish(buf, total_size); - __ASSERT_NO_MSG(err == 0); - ARG_UNUSED(err); +static size_t max_dma_read_size(const struct ring_buffer *rb) +{ + return MIN(ring_buffer_size(rb), ring_buffer_capacity(rb) - read_index(rb)); +} - return total_size; +static size_t max_dma_write_size(const struct ring_buffer *rb) +{ + return MIN(ring_buffer_space(rb), ring_buffer_capacity(rb) - write_index(rb)); } -uint32_t ring_buf_get(struct ring_buf *buf, uint8_t *data, uint32_t size) +size_t ring_buffer_write_ptr(const struct ring_buffer *rb, uint8_t **data) { - uint8_t *src; - uint32_t partial_size; - uint32_t total_size = 0U; - int err; + *data = &rb->buffer[write_index(rb)]; + return max_dma_write_size(rb); +} - do { - partial_size = ring_buf_get_claim(buf, &src, size); - if (partial_size == 0) { - break; - } - if (data) { - memcpy(data, src, partial_size); - data += partial_size; - } - total_size += partial_size; - size -= partial_size; - } while (size != 0); +void ring_buffer_commit(struct ring_buffer *rb, size_t appended) +{ + ring_buffer_index_t diff; + ring_buffer_index_t cache_write_ptr = rb->write_ptr; + + __ASSERT(appended <= ring_buffer_space(rb), + "Trying to commit more data than space available: appended %u, available %u", + appended, ring_buffer_space(rb)); + rb->write_ptr = rb->write_ptr + appended; + if (unlikely(rb->write_ptr < cache_write_ptr)) { + /* underlying index type overflowed */ + diff = cache_write_ptr - rb->read_ptr; + rb->read_ptr = read_index(rb); + rb->write_ptr = rb->read_ptr + diff + appended; + } +} - err = ring_buf_get_finish(buf, total_size); - __ASSERT_NO_MSG(err == 0); - ARG_UNUSED(err); +size_t ring_buffer_read_ptr(const struct ring_buffer *rb, uint8_t **data) +{ + *data = &rb->buffer[read_index(rb)]; + return max_dma_read_size(rb); +} - return total_size; +void ring_buffer_consume(struct ring_buffer *rb, size_t consumed) +{ + __ASSERT(consumed <= ring_buffer_size(rb), + "Trying to consume more data than available: consumed %u, available %u", + consumed, ring_buffer_size(rb)); + rb->read_ptr = rb->read_ptr + consumed; } -uint32_t ring_buf_peek(struct ring_buf *buf, uint8_t *data, uint32_t size) +size_t ring_buffer_read(struct ring_buffer *rb, uint8_t *data, size_t len) { - uint8_t *src; - uint32_t partial_size; - uint32_t total_size = 0U; - int err; + uint8_t *ref; + size_t read = 0; + size_t read_size; - do { - partial_size = ring_buf_get_claim(buf, &src, size); - if (partial_size == 0) { + while (read < len) { + read_size = MIN(ring_buffer_read_ptr(rb, &ref), len - read); + if (read_size == 0) { break; } - __ASSERT_NO_MSG(data != NULL); - memcpy(data, src, partial_size); - data += partial_size; - total_size += partial_size; - size -= partial_size; - } while (size != 0); - - /* effectively unclaim total_size bytes */ - err = ring_buf_get_finish(buf, 0); - __ASSERT_NO_MSG(err == 0); - ARG_UNUSED(err); - - return total_size; -} - -/** - * Internal data structure for a buffer header. - * - * We want all of this to fit in a single uint32_t. Every item stored in the - * ring buffer will be one of these headers plus any extra data supplied - */ -struct ring_element { - uint32_t type :16; /**< Application-specific */ - uint32_t length :8; /**< length in 32-bit chunks */ - uint32_t value :8; /**< Room for small integral values */ -}; - -int ring_buf_item_put(struct ring_buf *buf, uint16_t type, uint8_t value, - uint32_t *data32, uint8_t size32) -{ - uint8_t *dst, *data = (uint8_t *)data32; - struct ring_element *header; - uint32_t space, size, partial_size, total_size; - int err; - - space = ring_buf_space_get(buf); - size = size32 * 4; - if (size + sizeof(struct ring_element) > space) { - return -EMSGSIZE; + memcpy(&data[read], ref, read_size); + ring_buffer_consume(rb, read_size); + read += read_size; } + return read; +} - err = ring_buf_put_claim(buf, &dst, sizeof(struct ring_element)); - __ASSERT_NO_MSG(err == sizeof(struct ring_element)); - - header = (struct ring_element *)dst; - header->type = type; - header->length = size32; - header->value = value; - total_size = sizeof(struct ring_element); +size_t ring_buffer_write(struct ring_buffer *rb, const uint8_t *data, size_t len) +{ + uint8_t *ref; + size_t write_size; + size_t written = 0; - do { - partial_size = ring_buf_put_claim(buf, &dst, size); - if (partial_size == 0) { + while (written < len) { + write_size = MIN(ring_buffer_write_ptr(rb, &ref), len - written); + if (write_size == 0) { break; } - memcpy(dst, data, partial_size); - size -= partial_size; - total_size += partial_size; - data += partial_size; - } while (size != 0); - __ASSERT_NO_MSG(size == 0); - - err = ring_buf_put_finish(buf, total_size); - __ASSERT_NO_MSG(err == 0); - ARG_UNUSED(err); + memcpy(ref, &data[written], write_size); + ring_buffer_commit(rb, write_size); + written += write_size; + } - return 0; + return written; } -int ring_buf_item_get(struct ring_buf *buf, uint16_t *type, uint8_t *value, - uint32_t *data32, uint8_t *size32) +size_t ring_buffer_peek(struct ring_buffer *rb, uint8_t *data, size_t len) { - uint8_t *src, *data = (uint8_t *)data32; - struct ring_element *header; - uint32_t size, partial_size, total_size; - int err; - - if (ring_buf_is_empty(buf)) { - return -EAGAIN; - } - - err = ring_buf_get_claim(buf, &src, sizeof(struct ring_element)); - __ASSERT_NO_MSG(err == sizeof(struct ring_element)); - - header = (struct ring_element *)src; - - if (data && (header->length > *size32)) { - *size32 = header->length; - ring_buf_get_finish(buf, 0); - return -EMSGSIZE; - } + size_t cache_read_ptr; + size_t read; - *size32 = header->length; - *type = header->type; - *value = header->value; - total_size = sizeof(struct ring_element); + cache_read_ptr = rb->read_ptr; + read = ring_buffer_read(rb, data, len); - size = *size32 * 4; + rb->read_ptr = cache_read_ptr; - do { - partial_size = ring_buf_get_claim(buf, &src, size); - if (partial_size == 0) { - break; - } - if (data) { - memcpy(data, src, partial_size); - data += partial_size; - } - total_size += partial_size; - size -= partial_size; - } while (size != 0); + return read; +} - err = ring_buf_get_finish(buf, total_size); - __ASSERT_NO_MSG(err == 0); - ARG_UNUSED(err); +void ring_buffer_reset(struct ring_buffer *rb) +{ + rb->read_ptr = 0; + rb->write_ptr = 0; +} - return 0; +void ring_buffer_init(struct ring_buffer *rb, uint8_t *data, size_t size) +{ + rb->size = size; + rb->buffer = data; + __ASSERT(rb->size <= RING_BUFFER_MAX_SIZE, RING_BUFFER_SIZE_ERROR_MSG); + ring_buffer_reset(rb); } diff --git a/modules/openthread/Kconfig b/modules/openthread/Kconfig index 02a997028db40..3727a4fa39443 100644 --- a/modules/openthread/Kconfig +++ b/modules/openthread/Kconfig @@ -206,7 +206,6 @@ config OPENTHREAD_MBEDTLS_LIB_NAME config OPENTHREAD_COPROCESSOR bool "OpenThread Co-Processor" select OPENTHREAD_MANUAL_START - select RING_BUFFER select UART_INTERRUPT_DRIVEN help Enable Co-Processor in OpenThread stack. diff --git a/modules/openthread/platform/ble.c b/modules/openthread/platform/ble.c index ea5427abc4b3e..7aa31496d20b4 100644 --- a/modules/openthread/platform/ble.c +++ b/modules/openthread/platform/ble.c @@ -57,7 +57,7 @@ static uint8_t ot_plat_ble_msg_buf[PLAT_BLE_MSG_DATA_MAX]; static K_SEM_DEFINE(ot_plat_ble_init_semaphore, 0, 1); static K_SEM_DEFINE(ot_plat_ble_event_semaphore, 0, K_SEM_MAX_LIMIT); -RING_BUF_DECLARE(ot_plat_ble_ring_buf, CONFIG_OPENTHREAD_BLE_TCAT_RING_BUF_SIZE); +RING_BUFFER_DECLARE(ot_plat_ble_ring_buf, CONFIG_OPENTHREAD_BLE_TCAT_RING_BUF_SIZE); static K_THREAD_DEFINE(ot_plat_ble_tid, CONFIG_OPENTHREAD_BLE_TCAT_THREAD_STACK_SIZE, ot_plat_ble_thread, NULL, NULL, NULL, 5, 0, PLAT_BLE_THREAD_DEALY); @@ -126,11 +126,11 @@ static bool ot_plat_ble_queue_msg(const uint8_t *aData, uint16_t aLen, int8_t aR len = sizeof(aLen) + sizeof(aRssi) + ((aLen <= PLAT_BLE_MSG_DATA_MAX) ? aLen : 0); - if (ring_buf_space_get(&ot_plat_ble_ring_buf) >= len) { - ring_buf_put(&ot_plat_ble_ring_buf, (uint8_t *)&aLen, sizeof(aLen)); - ring_buf_put(&ot_plat_ble_ring_buf, &aRssi, sizeof(aRssi)); + if (ring_buffer_space(&ot_plat_ble_ring_buf) >= len) { + ring_buffer_write(&ot_plat_ble_ring_buf, (uint8_t *)&aLen, sizeof(aLen)); + ring_buffer_write(&ot_plat_ble_ring_buf, &aRssi, sizeof(aRssi)); if (aLen <= PLAT_BLE_MSG_DATA_MAX) { - ring_buf_put(&ot_plat_ble_ring_buf, aData, aLen); + ring_buffer_write(&ot_plat_ble_ring_buf, aData, aLen); } k_sem_give(&ot_plat_ble_event_semaphore); } else { @@ -156,10 +156,10 @@ static void ot_plat_ble_thread(void *unused1, void *unused2, void *unused3) while (1) { k_sem_take(&ot_plat_ble_event_semaphore, K_FOREVER); - ring_buf_get(&ot_plat_ble_ring_buf, (uint8_t *)&len, sizeof(len)); - ring_buf_get(&ot_plat_ble_ring_buf, &rssi, sizeof(rssi)); + ring_buffer_read(&ot_plat_ble_ring_buf, (uint8_t *)&len, sizeof(len)); + ring_buffer_read(&ot_plat_ble_ring_buf, &rssi, sizeof(rssi)); if (len <= PLAT_BLE_MSG_DATA_MAX) { - ring_buf_get(&ot_plat_ble_ring_buf, ot_plat_ble_msg_buf, len); + ring_buffer_read(&ot_plat_ble_ring_buf, ot_plat_ble_msg_buf, len); } openthread_mutex_lock(); diff --git a/modules/openthread/platform/uart.c b/modules/openthread/platform/uart.c index ea7319cbf3da0..f2d6a9f77e0a1 100644 --- a/modules/openthread/platform/uart.c +++ b/modules/openthread/platform/uart.c @@ -28,14 +28,14 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME); #include "platform-zephyr.h" struct openthread_uart { - struct ring_buf *rx_ringbuf; + struct ring_buffer *rx_ringbuf; const struct device *dev; atomic_t tx_busy; atomic_t tx_finished; }; #define OT_UART_DEFINE(_name, _ringbuf_size) \ - RING_BUF_DECLARE(_name##_rx_ringbuf, _ringbuf_size); \ + RING_BUFFER_DECLARE(_name##_rx_ringbuf, _ringbuf_size); \ static struct openthread_uart _name = { \ .rx_ringbuf = &_name##_rx_ringbuf, \ } @@ -56,19 +56,14 @@ static void uart_rx_handle(const struct device *dev) bool new_data = false; do { - len = ring_buf_put_claim( - ot_uart.rx_ringbuf, &data, - ot_uart.rx_ringbuf->size); + len = ring_buffer_write_ptr(ot_uart.rx_ringbuf, &data); if (len > 0) { rd_len = uart_fifo_read(dev, data, len); if (rd_len > 0) { new_data = true; } - int err = ring_buf_put_finish( - ot_uart.rx_ringbuf, rd_len); - (void)err; - __ASSERT_NO_MSG(err == 0); + ring_buffer_commit(ot_uart.rx_ringbuf, rd_len); } else { uint8_t dummy; @@ -133,18 +128,9 @@ void platformUartProcess(otInstance *aInstance) const uint8_t *data; /* Process UART RX */ - while ((len = ring_buf_get_claim( - ot_uart.rx_ringbuf, - (uint8_t **)&data, - ot_uart.rx_ringbuf->size)) > 0) { - int err; - + while ((len = ring_buffer_write_ptr(ot_uart.rx_ringbuf, (uint8_t **)&data)) > 0) { otPlatUartReceived(data, len); - err = ring_buf_get_finish( - ot_uart.rx_ringbuf, - len); - (void)err; - __ASSERT_NO_MSG(err == 0); + ring_buffer_consume(ot_uart.rx_ringbuf, len); } /* Process UART TX */ diff --git a/samples/bluetooth/bap_broadcast_sink/Kconfig b/samples/bluetooth/bap_broadcast_sink/Kconfig index bed4a4d30d86d..9b8d56f830795 100644 --- a/samples/bluetooth/bap_broadcast_sink/Kconfig +++ b/samples/bluetooth/bap_broadcast_sink/Kconfig @@ -58,7 +58,6 @@ config USE_USB_AUDIO_OUTPUT depends on ENABLE_LC3 select USB_DEVICE_STACK_NEXT select USBD_AUDIO2_CLASS - select RING_BUFFER help Enables USB audio as output as a USB peripheral. This does not support providing USB audio to e.g. speakers that are also USB peripherals, but can be connected to e.g. a diff --git a/samples/bluetooth/bap_broadcast_sink/src/usb.c b/samples/bluetooth/bap_broadcast_sink/src/usb.c index 5007df8460129..e6517ce8128e2 100644 --- a/samples/bluetooth/bap_broadcast_sink/src/usb.c +++ b/samples/bluetooth/bap_broadcast_sink/src/usb.c @@ -59,7 +59,7 @@ struct decoded_sdu { uint32_t ts; } decoded_sdu; -RING_BUF_DECLARE(usb_in_ring_buf, USB_IN_RING_BUF_SIZE); +RING_BUFFER_DECLARE(usb_in_ring_buf, USB_IN_RING_BUF_SIZE); K_MEM_SLAB_DEFINE_STATIC(usb_in_buf_pool, ROUND_UP(USB_STEREO_FRAME_SIZE, UDC_BUF_GRANULARITY), USB_ENQUEUE_COUNT, UDC_BUF_ALIGN); static volatile bool terminal_enabled; @@ -73,7 +73,7 @@ static void uac2_sof_cb(const struct device *dev, void *user_data) if (!terminal_enabled) { /* Simply discard the data then */ - (void)ring_buf_get(&usb_in_ring_buf, NULL, USB_STEREO_FRAME_SIZE); + (void)ring_buffer_read(&usb_in_ring_buf, NULL, USB_STEREO_FRAME_SIZE); return; } @@ -83,7 +83,7 @@ static void uac2_sof_cb(const struct device *dev, void *user_data) return; } - size = ring_buf_get(&usb_in_ring_buf, pcm_buf, USB_STEREO_FRAME_SIZE); + size = ring_buffer_read(&usb_in_ring_buf, pcm_buf, USB_STEREO_FRAME_SIZE); if (size != USB_STEREO_FRAME_SIZE) { /* If we could not fill the buffer, zero-fill the rest (possibly all) */ memset(((uint8_t *)pcm_buf) + size, 0, USB_STEREO_FRAME_SIZE - size); @@ -155,7 +155,7 @@ static void usb_send_frames_to_usb(void) uint32_t rb_size; /* Not enough space to store data */ - if (ring_buf_space_get(&usb_in_ring_buf) < sizeof(stereo_frame)) { + if (ring_buffer_space(&usb_in_ring_buf) < sizeof(stereo_frame)) { if (CONFIG_INFO_REPORTING_INTERVAL > 0 && (fail_cnt % CONFIG_INFO_REPORTING_INTERVAL) == 0U) { LOG_WRN("[%zu] Could not send more than %zu frames to USB", @@ -194,7 +194,7 @@ static void usb_send_frames_to_usb(void) } } - rb_size = ring_buf_put(&usb_in_ring_buf, (uint8_t *)stereo_frame, + rb_size = ring_buffer_write(&usb_in_ring_buf, (uint8_t *)stereo_frame, sizeof(stereo_frame)); if (rb_size != sizeof(stereo_frame)) { LOG_WRN("Failed to put frame on USB ring buf"); diff --git a/samples/bluetooth/bap_broadcast_source/Kconfig b/samples/bluetooth/bap_broadcast_source/Kconfig index 5be74441695f3..11727a97f7bdf 100644 --- a/samples/bluetooth/bap_broadcast_source/Kconfig +++ b/samples/bluetooth/bap_broadcast_source/Kconfig @@ -41,7 +41,6 @@ config USE_USB_AUDIO_INPUT depends on ENABLE_LC3 select USB_DEVICE_STACK_NEXT select USBD_AUDIO2_CLASS - select RING_BUFFER config BROADCAST_CODE string "The broadcast code (if any) to use for encrypted broadcast" diff --git a/samples/bluetooth/bap_broadcast_source/src/main.c b/samples/bluetooth/bap_broadcast_source/src/main.c index b03ca962c14b2..f763f4356ac60 100644 --- a/samples/bluetooth/bap_broadcast_source/src/main.c +++ b/samples/bluetooth/bap_broadcast_source/src/main.c @@ -142,7 +142,7 @@ static struct broadcast_source_stream { lc3_encoder_mem_48k_t lc3_encoder_mem; #endif #if defined(CONFIG_USE_USB_AUDIO_INPUT) - struct ring_buf audio_ring_buf; + struct ring_buffer audio_ring_buf; uint8_t _ring_buffer_memory[AUDIO_RING_BUF_BYTES]; #endif /* defined(CONFIG_USE_USB_AUDIO_INPUT) */ #endif /* defined(CONFIG_LIBLC3) */ @@ -197,7 +197,7 @@ static void send_data(struct broadcast_source_stream *source_stream) } #if defined(CONFIG_USE_USB_AUDIO_INPUT) - uint32_t size = ring_buf_get(&source_stream->audio_ring_buf, (uint8_t *)send_pcm_data, + uint32_t size = ring_buffer_read(&source_stream->audio_ring_buf, (uint8_t *)send_pcm_data, sizeof(send_pcm_data)); if (size < sizeof(send_pcm_data)) { @@ -373,13 +373,14 @@ static void data_recv_cb(const struct device *dev, uint8_t terminal, void *buf, for (size_t i = 0U; i < MIN(ARRAY_SIZE(streams), 2); i++) { const uint32_t size_put = - ring_buf_put(&(streams[i].audio_ring_buf), (uint8_t *)(usb_pcm_data[i]), - nsamples * USB_BYTES_PER_SAMPLE); + ring_buffer_write(&(streams[i].audio_ring_buf), + (uint8_t *)(usb_pcm_data[i]), + nsamples * USB_BYTES_PER_SAMPLE); if (size_put < nsamples * USB_BYTES_PER_SAMPLE) { printk("Not enough room for samples in %s buffer: %u < %u, total capacity: " "%u\n", i == 0 ? "left" : "right", size_put, nsamples * USB_BYTES_PER_SAMPLE, - ring_buf_capacity_get(&(streams[i].audio_ring_buf))); + ring_buffer_capacity(&(streams[i].audio_ring_buf))); } } @@ -540,10 +541,11 @@ int main(void) (void)memset(streams, 0, sizeof(streams)); for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) { - ring_buf_init(&(streams[i].audio_ring_buf), sizeof(streams[i]._ring_buffer_memory), - streams[i]._ring_buffer_memory); + ring_buffer_init(&(streams[i].audio_ring_buf), + streams[i]._ring_buffer_memory, + sizeof(streams[i]._ring_buffer_memory)); printk("Initialized ring buf %zu: capacity: %u\n", i, - ring_buf_capacity_get(&(streams[i].audio_ring_buf))); + ring_buffer_capacity(&(streams[i].audio_ring_buf))); } usbd_uac2_set_ops(broadcaster_dev, &usb_audio_ops, NULL); diff --git a/samples/boards/st/uart/circular_dma/prj.conf b/samples/boards/st/uart/circular_dma/prj.conf index ad49326a82a7f..3779b9f36688f 100644 --- a/samples/boards/st/uart/circular_dma/prj.conf +++ b/samples/boards/st/uart/circular_dma/prj.conf @@ -1,3 +1,2 @@ CONFIG_SERIAL=y CONFIG_UART_ASYNC_API=y -CONFIG_RING_BUFFER=y diff --git a/samples/boards/st/uart/circular_dma/src/main.c b/samples/boards/st/uart/circular_dma/src/main.c index f230a500dc210..100194d9492a1 100644 --- a/samples/boards/st/uart/circular_dma/src/main.c +++ b/samples/boards/st/uart/circular_dma/src/main.c @@ -31,7 +31,7 @@ const struct uart_config uart_cfg = {.baudrate = 115200, .flow_ctrl = UART_CFG_FLOW_CTRL_NONE}; /* define a ring buffer to get raw bytes*/ -RING_BUF_DECLARE(ring_buf, RING_BUF_SIZE); +RING_BUFFER_DECLARE(ring_buf, RING_BUF_SIZE); /* define uart rx buffer */ static uint8_t rx_buffer[RX_BUF_SIZE]; @@ -63,7 +63,7 @@ static void uart_rx_thread(void *p1, void *p2, void *p3) while (1) { /* Check if there's data in the ring buffer */ - len = ring_buf_get(&ring_buf, rx_data, sizeof(rx_data)); + len = ring_buffer_read(&ring_buf, rx_data, sizeof(rx_data)); if (len > 0) { @@ -78,7 +78,8 @@ void uart_cb(const struct device *dev, struct uart_event *evt, void *user_data) switch (evt->type) { case UART_RX_RDY: /* Data received; place into ring buffer */ - ring_buf_put(&ring_buf, evt->data.rx.buf + evt->data.rx.offset, evt->data.rx.len); + ring_buffer_write(&ring_buf, evt->data.rx.buf + evt->data.rx.offset, + evt->data.rx.len); break; diff --git a/samples/drivers/uart/passthrough/prj.conf b/samples/drivers/uart/passthrough/prj.conf index 70eec2fbac3df..8698ce3bd31c6 100644 --- a/samples/drivers/uart/passthrough/prj.conf +++ b/samples/drivers/uart/passthrough/prj.conf @@ -1,3 +1,2 @@ CONFIG_SERIAL=y -CONFIG_RING_BUFFER=y CONFIG_UART_INTERRUPT_DRIVEN=y diff --git a/samples/drivers/uart/passthrough/src/main.c b/samples/drivers/uart/passthrough/src/main.c index 2168207a7516d..ecbf0d1c3d312 100644 --- a/samples/drivers/uart/passthrough/src/main.c +++ b/samples/drivers/uart/passthrough/src/main.c @@ -16,7 +16,7 @@ struct patch_info { const uint8_t * const name; const struct device *rx_dev; - struct ring_buf *rx_ring_buf; + struct ring_buffer *rx_ring_buf; bool rx_error; bool rx_overflow; @@ -28,7 +28,7 @@ struct patch_info { #define RING_BUF_SIZE 64 -RING_BUF_DECLARE(rb_console, RING_BUF_SIZE); +RING_BUFFER_DECLARE(rb_console, RING_BUF_SIZE); struct patch_info patch_c2o = { .name = "c2o", @@ -40,7 +40,7 @@ struct patch_info patch_c2o = { .tx_dev = DEV_OTHER, }; -RING_BUF_DECLARE(rb_other, RING_BUF_SIZE); +RING_BUFFER_DECLARE(rb_other, RING_BUF_SIZE); struct patch_info patch_o2c = { .name = "o2c", @@ -68,7 +68,7 @@ static void uart_cb(const struct device *dev, void *ctx) break; } - len = ring_buf_put_claim(patch->rx_ring_buf, &buf, RING_BUF_SIZE); + len = ring_buffer_write_ptr(patch->rx_ring_buf, &buf); if (len == 0) { /* no space for Rx, disable the IRQ */ uart_irq_rx_disable(patch->rx_dev); @@ -84,12 +84,7 @@ static void uart_cb(const struct device *dev, void *ctx) break; } len = ret; - - ret = ring_buf_put_finish(patch->rx_ring_buf, len); - if (ret != 0) { - patch->rx_error = true; - break; - } + ring_buffer_commit(patch->rx_ring_buf, len); } } @@ -109,7 +104,7 @@ static void passthrough(struct patch_info *patch) patch->rx_overflow = false; } - len = ring_buf_get_claim(patch->rx_ring_buf, &buf, RING_BUF_SIZE); + len = ring_buffer_read_ptr(patch->rx_ring_buf, &buf); if (len == 0) { goto done; } @@ -120,10 +115,7 @@ static void passthrough(struct patch_info *patch) } len = ret; - ret = ring_buf_get_finish(patch->rx_ring_buf, len); - if (ret < 0) { - goto error; - } + ring_buffer_consume(patch->rx_ring_buf, len); done: uart_irq_rx_enable(patch->rx_dev); diff --git a/samples/subsys/usb/cdc_acm/src/main.c b/samples/subsys/usb/cdc_acm/src/main.c index c80d253966b7f..db5da65baffd5 100644 --- a/samples/subsys/usb/cdc_acm/src/main.c +++ b/samples/subsys/usb/cdc_acm/src/main.c @@ -22,7 +22,7 @@ const struct device *const uart_dev = DEVICE_DT_GET_ONE(zephyr_cdc_acm_uart); #define RING_BUF_SIZE 1024 uint8_t ring_buffer[RING_BUF_SIZE]; -struct ring_buf ringbuf; +struct ring_buffer ringbuf; static bool rx_throttled; @@ -105,7 +105,7 @@ static void interrupt_handler(const struct device *dev, void *user_data) if (!rx_throttled && uart_irq_rx_ready(dev)) { int recv_len, rb_len; uint8_t buffer[64]; - size_t len = MIN(ring_buf_space_get(&ringbuf), + size_t len = MIN(ring_buffer_space(&ringbuf), sizeof(buffer)); if (len == 0) { @@ -121,7 +121,7 @@ static void interrupt_handler(const struct device *dev, void *user_data) recv_len = 0; }; - rb_len = ring_buf_put(&ringbuf, buffer, recv_len); + rb_len = ring_buffer_write(&ringbuf, buffer, recv_len); if (rb_len < recv_len) { LOG_ERR("Drop %u bytes", recv_len - rb_len); } @@ -136,7 +136,7 @@ static void interrupt_handler(const struct device *dev, void *user_data) uint8_t buffer[64]; int rb_len, send_len; - rb_len = ring_buf_get(&ringbuf, buffer, sizeof(buffer)); + rb_len = ring_buffer_read(&ringbuf, buffer, sizeof(buffer)); if (!rb_len) { LOG_DBG("Ring buffer empty, disable TX IRQ"); uart_irq_tx_disable(dev); @@ -173,7 +173,7 @@ int main(void) return 0; } - ring_buf_init(&ringbuf, sizeof(ring_buffer), ring_buffer); + ring_buffer_init(&ringbuf, ring_buffer, sizeof(ring_buffer)); LOG_INF("Wait for DTR"); k_sem_take(&dtr_sem, K_FOREVER); diff --git a/samples/subsys/usb/legacy/cdc_acm/src/main.c b/samples/subsys/usb/legacy/cdc_acm/src/main.c index d06161f0f5ca4..c77af5e63a5eb 100644 --- a/samples/subsys/usb/legacy/cdc_acm/src/main.c +++ b/samples/subsys/usb/legacy/cdc_acm/src/main.c @@ -20,7 +20,7 @@ const struct device *const uart_dev = DEVICE_DT_GET_ONE(zephyr_cdc_acm_uart); #define RING_BUF_SIZE 1024 uint8_t ring_buffer[RING_BUF_SIZE]; -struct ring_buf ringbuf; +struct ring_buffer ringbuf; static bool rx_throttled; @@ -45,7 +45,7 @@ static void interrupt_handler(const struct device *dev, void *user_data) if (!rx_throttled && uart_irq_rx_ready(dev)) { int recv_len, rb_len; uint8_t buffer[64]; - size_t len = MIN(ring_buf_space_get(&ringbuf), + size_t len = MIN(ring_buffer_space(&ringbuf), sizeof(buffer)); if (len == 0) { @@ -61,7 +61,7 @@ static void interrupt_handler(const struct device *dev, void *user_data) recv_len = 0; }; - rb_len = ring_buf_put(&ringbuf, buffer, recv_len); + rb_len = ring_buffer_write(&ringbuf, buffer, recv_len); if (rb_len < recv_len) { LOG_ERR("Drop %u bytes", recv_len - rb_len); } @@ -76,7 +76,7 @@ static void interrupt_handler(const struct device *dev, void *user_data) uint8_t buffer[64]; int rb_len, send_len; - rb_len = ring_buf_get(&ringbuf, buffer, sizeof(buffer)); + rb_len = ring_buffer_read(&ringbuf, buffer, sizeof(buffer)); if (!rb_len) { LOG_DBG("Ring buffer empty, disable TX IRQ"); uart_irq_tx_disable(dev); @@ -113,7 +113,7 @@ int main(void) return 0; } - ring_buf_init(&ringbuf, sizeof(ring_buffer), ring_buffer); + ring_buffer_init(&ringbuf, ring_buffer, sizeof(ring_buffer)); LOG_INF("Wait for DTR"); diff --git a/subsys/bluetooth/audio/shell/bap_usb.c b/subsys/bluetooth/audio/shell/bap_usb.c index 330cff1eb19b7..360caaa79d540 100644 --- a/subsys/bluetooth/audio/shell/bap_usb.c +++ b/subsys/bluetooth/audio/shell/bap_usb.c @@ -95,7 +95,7 @@ struct decoded_sdu { uint32_t ts; } decoded_sdu; -RING_BUF_DECLARE(usb_in_ring_buf, USB_IN_RING_BUF_SIZE); +RING_BUFFER_DECLARE(usb_in_ring_buf, USB_IN_RING_BUF_SIZE); K_MEM_SLAB_DEFINE_STATIC(usb_in_buf_pool, ROUND_UP(USB_STEREO_FRAME_SIZE, UDC_BUF_GRANULARITY), USB_ENQUEUE_COUNT, UDC_BUF_ALIGN); @@ -118,7 +118,7 @@ static void usb_data_request(const struct device *dev) } /* This may fail without causing issues since usb_audio_data is 0-initialized */ - size = ring_buf_get(&usb_in_ring_buf, pcm_buf, USB_STEREO_FRAME_SIZE); + size = ring_buffer_read(&usb_in_ring_buf, pcm_buf, USB_STEREO_FRAME_SIZE); if (size != USB_STEREO_FRAME_SIZE) { /* If we could not fill the buffer, zero-fill the rest (possibly all) */ memset(((uint8_t *)pcm_buf) + size, 0, USB_STEREO_FRAME_SIZE - size); @@ -181,7 +181,7 @@ static void bap_usb_send_frames_to_usb(void) uint32_t rb_size; /* Not enough space to store data */ - if (ring_buf_space_get(&usb_in_ring_buf) < sizeof(stereo_frame)) { + if (ring_buffer_space(&usb_in_ring_buf) < sizeof(stereo_frame)) { if ((fail_cnt % bap_get_stats_interval()) == 0U) { LOG_WRN("[%zu] Could not send more than %zu frames to USB", fail_cnt, i); @@ -219,7 +219,7 @@ static void bap_usb_send_frames_to_usb(void) } } - rb_size = ring_buf_put(&usb_in_ring_buf, (uint8_t *)stereo_frame, + rb_size = ring_buffer_write(&usb_in_ring_buf, (uint8_t *)stereo_frame, sizeof(stereo_frame)); if (rb_size != sizeof(stereo_frame)) { LOG_WRN("Failed to put frame on USB ring buf"); diff --git a/subsys/gnss/rtk/serial/serial.c b/subsys/gnss/rtk/serial/serial.c index c6ba5d51a9080..ce27a76350dba 100644 --- a/subsys/gnss/rtk/serial/serial.c +++ b/subsys/gnss/rtk/serial/serial.c @@ -18,13 +18,13 @@ LOG_MODULE_REGISTER(rtk_serial, CONFIG_GNSS_RTK_LOG_LEVEL); static const struct device *rtk_serial_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_rtk_serial)); -static struct ring_buf process_ringbuf; +static struct ring_buffer process_ringbuf; static uint8_t process_buf[2048]; static void gnss_rtk_process_work_handler(struct k_work *work) { static uint8_t work_buf[2048]; - uint32_t len = ring_buf_get(&process_ringbuf, work_buf, sizeof(work_buf)); + uint32_t len = ring_buffer_read(&process_ringbuf, work_buf, sizeof(work_buf)); uint32_t offset = 0; ARG_UNUSED(work); @@ -70,7 +70,7 @@ static void rtk_uart_isr_callback(const struct device *dev, void *user_data) ret = uart_fifo_read(dev, &c, 1); if (ret > 0) { - ret = ring_buf_put(&process_ringbuf, &c, 1); + ret = ring_buffer_write(&process_ringbuf, &c, 1); } } while (ret > 0); @@ -86,7 +86,7 @@ static int rtk_serial_client_init(void) { int err; - ring_buf_init(&process_ringbuf, ARRAY_SIZE(process_buf), process_buf); + ring_buffer_init(&process_ringbuf, process_buf, ARRAY_SIZE(process_buf)); err = uart_irq_callback_user_data_set(rtk_serial_dev, rtk_uart_isr_callback, NULL); if (err < 0) { diff --git a/subsys/mgmt/osdp/Kconfig b/subsys/mgmt/osdp/Kconfig index ba10a839a2f83..b24d0f3f7b52c 100644 --- a/subsys/mgmt/osdp/Kconfig +++ b/subsys/mgmt/osdp/Kconfig @@ -6,7 +6,6 @@ menuconfig OSDP bool "Open Supervised Device Protocol (OSDP) driver" - select RING_BUFFER imply SERIAL_SUPPORT_INTERRUPT imply UART_INTERRUPT_DRIVEN imply UART_USE_RUNTIME_CONFIGURE diff --git a/subsys/mgmt/osdp/src/osdp.c b/subsys/mgmt/osdp/src/osdp.c index e1a0f7f1167f5..372ae2fb003b2 100644 --- a/subsys/mgmt/osdp/src/osdp.c +++ b/subsys/mgmt/osdp/src/osdp.c @@ -26,8 +26,8 @@ LOG_MODULE_REGISTER(osdp, CONFIG_OSDP_LOG_LEVEL); #endif /* CONFIG_OSDP_SC_ENABLED */ struct osdp_device { - struct ring_buf rx_buf; - struct ring_buf tx_buf; + struct ring_buffer rx_buf; + struct ring_buffer tx_buf; #ifdef CONFIG_OSDP_MODE_PD int rx_event_data; struct k_fifo rx_event_fifo; @@ -52,15 +52,15 @@ static void osdp_handle_in_byte(struct osdp_device *p, uint8_t *buf, int len) /* Check for new packet beginning with [FF,53,...] sequence */ if (p->last_byte == 0xFF && buf[0] == 0x53) { buf[0] = 0xFF; - ring_buf_put(&p->rx_buf, buf, 1); /* put last byte */ + ring_buffer_write(&p->rx_buf, buf, 1); /* put last byte */ buf[0] = 0x53; - ring_buf_put(&p->rx_buf, buf, len); /* put rest */ + ring_buffer_write(&p->rx_buf, buf, len); /* put rest */ p->wait_for_mark = 0; /* Mark found. Clear flag */ } p->last_byte = buf[0]; return; } - ring_buf_put(&p->rx_buf, buf, len); + ring_buffer_write(&p->rx_buf, buf, len); } static void osdp_uart_isr(const struct device *dev, void *user_data) @@ -79,7 +79,7 @@ static void osdp_uart_isr(const struct device *dev, void *user_data) } if (uart_irq_tx_ready(dev)) { - len = ring_buf_get(&p->tx_buf, buf, 1); + len = ring_buffer_read(&p->tx_buf, buf, 1); if (!len) { uart_irq_tx_disable(dev); } else { @@ -99,7 +99,7 @@ static int osdp_uart_receive(void *data, uint8_t *buf, int len) { struct osdp_device *p = data; - return (int)ring_buf_get(&p->rx_buf, buf, len); + return (int)ring_buffer_read(&p->rx_buf, buf, len); } static int osdp_uart_send(void *data, uint8_t *buf, int len) @@ -107,7 +107,7 @@ static int osdp_uart_send(void *data, uint8_t *buf, int len) int sent = 0; struct osdp_device *p = data; - sent = (int)ring_buf_put(&p->tx_buf, buf, len); + sent = (int)ring_buffer_write(&p->tx_buf, buf, len); uart_irq_tx_enable(p->dev); return sent; } @@ -117,8 +117,8 @@ static void osdp_uart_flush(void *data) struct osdp_device *p = data; p->wait_for_mark = 1; - ring_buf_reset(&p->tx_buf); - ring_buf_reset(&p->rx_buf); + ring_buffer_reset(&p->tx_buf); + ring_buffer_reset(&p->rx_buf); } struct osdp *osdp_get_ctx() @@ -193,8 +193,8 @@ static int osdp_init(void) k_fifo_init(&p->rx_event_fifo); #endif - ring_buf_init(&p->rx_buf, sizeof(p->rx_fbuf), p->rx_fbuf); - ring_buf_init(&p->tx_buf, sizeof(p->tx_fbuf), p->tx_fbuf); + ring_buffer_init(&p->rx_buf, p->rx_fbuf, sizeof(p->rx_fbuf)); + ring_buffer_init(&p->tx_buf, p->tx_fbuf, sizeof(p->tx_fbuf)); /* init OSDP uart device */ p->dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_osdp_uart)); diff --git a/subsys/modem/Kconfig b/subsys/modem/Kconfig index dfcb7ddae9a38..5a31cb8985335 100644 --- a/subsys/modem/Kconfig +++ b/subsys/modem/Kconfig @@ -23,7 +23,6 @@ endif # MODEM_DEDICATED_WORKQUEUE config MODEM_CHAT bool "Modem chat module" - select RING_BUFFER select MODEM_PIPE if MODEM_CHAT @@ -37,7 +36,6 @@ endif config MODEM_CMUX bool "Modem CMUX module" select MODEM_PIPE - select RING_BUFFER select EVENTS select CRC @@ -88,7 +86,6 @@ config MODEM_PPP bool "Modem PPP module" depends on NET_L2_PPP select MODEM_PIPE - select RING_BUFFER select CRC select PM_DEVICE_RUNTIME_ASYNC if PM_DEVICE_RUNTIME @@ -116,7 +113,6 @@ config MODEM_STATS_BUFFER_NAME_SIZE config MODEM_UBX bool "Modem U-BLOX module" - select RING_BUFFER select MODEM_PIPE help Enable Modem U-BLOX module. diff --git a/subsys/modem/backends/Kconfig b/subsys/modem/backends/Kconfig index 2ce81c6dce11c..44f4a0efc3682 100644 --- a/subsys/modem/backends/Kconfig +++ b/subsys/modem/backends/Kconfig @@ -9,7 +9,6 @@ config MODEM_BACKEND_TTY config MODEM_BACKEND_UART bool "Modem UART backend module" select MODEM_PIPE - select RING_BUFFER depends on UART_INTERRUPT_DRIVEN || UART_ASYNC_API if MODEM_BACKEND_UART diff --git a/subsys/modem/backends/modem_backend_uart_async.c b/subsys/modem/backends/modem_backend_uart_async.c index 9b5edc8965c52..cdc1a080886e0 100644 --- a/subsys/modem/backends/modem_backend_uart_async.c +++ b/subsys/modem/backends/modem_backend_uart_async.c @@ -45,7 +45,7 @@ static bool modem_backend_uart_async_is_open(struct modem_backend_uart *backend) static uint32_t get_receive_buf_length(struct modem_backend_uart *backend) { - return ring_buf_size_get(&backend->async.receive_rb); + return ring_buffer_size(&backend->async.receive_rb); } static void modem_backend_uart_async_event_handler(const struct device *dev, @@ -112,14 +112,14 @@ static void modem_backend_uart_async_event_handler(const struct device *dev, case UART_RX_RDY: key = k_spin_lock(&backend->async.receive_rb_lock); - received = ring_buf_put(&backend->async.receive_rb, + received = ring_buffer_write(&backend->async.receive_rb, &evt->data.rx.buf[evt->data.rx.offset], evt->data.rx.len); if (received < evt->data.rx.len) { const unsigned int buf_size = get_receive_buf_length(backend); - ring_buf_reset(&backend->async.receive_rb); + ring_buffer_reset(&backend->async.receive_rb); k_spin_unlock(&backend->async.receive_rb_lock, key); LOG_WRN("Receive buffer overrun (dropped %u + %u)", @@ -155,7 +155,7 @@ static int modem_backend_uart_async_open(void *data) int ret; atomic_clear(&backend->async.common.state); - ring_buf_reset(&backend->async.receive_rb); + ring_buffer_reset(&backend->async.receive_rb); atomic_set_bit(&backend->async.common.state, MODEM_BACKEND_UART_ASYNC_STATE_RX_BUF0_USED_BIT); @@ -180,7 +180,7 @@ static int modem_backend_uart_async_open(void *data) #if CONFIG_MODEM_STATS static uint32_t get_receive_buf_size(struct modem_backend_uart *backend) { - return ring_buf_capacity_get(&backend->async.receive_rb); + return ring_buffer_capacity(&backend->async.receive_rb); } static void advertise_transmit_buf_stats(struct modem_backend_uart *backend, uint32_t length) @@ -250,8 +250,8 @@ static int modem_backend_uart_async_receive(void *data, uint8_t *buf, size_t siz advertise_receive_buf_stats(backend); #endif - received = ring_buf_get(&backend->async.receive_rb, buf, size); - empty = ring_buf_is_empty(&backend->async.receive_rb); + received = ring_buffer_read(&backend->async.receive_rb, buf, size); + empty = ring_buffer_empty(&backend->async.receive_rb); k_spin_unlock(&backend->async.receive_rb_lock, key); if (!empty) { @@ -326,8 +326,9 @@ int modem_backend_uart_async_init(struct modem_backend_uart *backend, backend->async.receive_bufs[1] = &config->receive_buf[receive_buf_size_quarter]; /* Use half the receive buffer for the received data ring buffer */ - ring_buf_init(&backend->async.receive_rb, (receive_buf_size_quarter * 2), - &config->receive_buf[receive_buf_size_quarter * 2]); + ring_buffer_init(&backend->async.receive_rb, + &config->receive_buf[receive_buf_size_quarter * 2], + (receive_buf_size_quarter * 2)); backend->async.common.transmit_buf = config->transmit_buf; backend->async.common.transmit_buf_size = config->transmit_buf_size; diff --git a/subsys/modem/backends/modem_backend_uart_isr.c b/subsys/modem/backends/modem_backend_uart_isr.c index 6c6c27c2db2e7..71ce6fd0a75de 100644 --- a/subsys/modem/backends/modem_backend_uart_isr.c +++ b/subsys/modem/backends/modem_backend_uart_isr.c @@ -24,12 +24,12 @@ static void modem_backend_uart_isr_flush(struct modem_backend_uart *backend) static void modem_backend_uart_isr_irq_handler_receive_ready(struct modem_backend_uart *backend) { uint32_t size; - struct ring_buf *receive_rb; + struct ring_buffer *receive_rb; uint8_t *buffer; int ret; receive_rb = &backend->isr.receive_rdb[backend->isr.receive_rdb_used]; - size = ring_buf_put_claim(receive_rb, &buffer, UINT32_MAX); + size = ring_buffer_write_ptr(receive_rb, &buffer); if (size == 0) { /* This can be caused by * - a too long CONFIG_MODEM_BACKEND_UART_ISR_RECEIVE_IDLE_TIMEOUT_MS @@ -37,19 +37,17 @@ static void modem_backend_uart_isr_irq_handler_receive_ready(struct modem_backen * relatively to the (too high) baud rate and amount of incoming data. */ LOG_WRN("Receive buffer overrun"); - ring_buf_put_finish(receive_rb, 0); - ring_buf_reset(receive_rb); - size = ring_buf_put_claim(receive_rb, &buffer, UINT32_MAX); + ring_buffer_reset(receive_rb); + size = ring_buffer_write_ptr(receive_rb, &buffer); } ret = uart_fifo_read(backend->uart, buffer, size); if (ret <= 0) { - ring_buf_put_finish(receive_rb, 0); return; } - ring_buf_put_finish(receive_rb, (uint32_t)ret); + ring_buffer_commit(receive_rb, (uint32_t)ret); - if (ring_buf_space_get(receive_rb) > ring_buf_capacity_get(receive_rb) / 20) { + if (ring_buffer_space(receive_rb) > ring_buffer_capacity(receive_rb) / 20) { /* * Avoid having the receiver call modem_pipe_receive() too often (e.g. every byte). * It temporarily disables the UART RX IRQ when swapping buffers @@ -69,18 +67,16 @@ static void modem_backend_uart_isr_irq_handler_transmit_ready(struct modem_backe uint8_t *buffer; int ret; - if (ring_buf_is_empty(&backend->isr.transmit_rb) == true) { + if (ring_buffer_empty(&backend->isr.transmit_rb) == true) { uart_irq_tx_disable(backend->uart); modem_work_submit(&backend->transmit_idle_work); return; } - size = ring_buf_get_claim(&backend->isr.transmit_rb, &buffer, UINT32_MAX); + size = ring_buffer_read_ptr(&backend->isr.transmit_rb, &buffer); ret = uart_fifo_fill(backend->uart, buffer, size); - if (ret < 0) { - ring_buf_get_finish(&backend->isr.transmit_rb, 0); - } else { - ring_buf_get_finish(&backend->isr.transmit_rb, (uint32_t)ret); + if (ret > 0) { + ring_buffer_consume(&backend->isr.transmit_rb, (uint32_t)ret); /* Update transmit buf capacity tracker */ atomic_sub(&backend->isr.transmit_buf_len, (uint32_t)ret); @@ -108,9 +104,9 @@ static int modem_backend_uart_isr_open(void *data) { struct modem_backend_uart *backend = (struct modem_backend_uart *)data; - ring_buf_reset(&backend->isr.receive_rdb[0]); - ring_buf_reset(&backend->isr.receive_rdb[1]); - ring_buf_reset(&backend->isr.transmit_rb); + ring_buffer_reset(&backend->isr.receive_rdb[0]); + ring_buffer_reset(&backend->isr.receive_rdb[1]); + ring_buffer_reset(&backend->isr.transmit_rb); atomic_set(&backend->isr.transmit_buf_len, 0); modem_backend_uart_isr_flush(backend); uart_irq_rx_enable(backend->uart); @@ -127,19 +123,19 @@ static uint32_t get_transmit_buf_length(struct modem_backend_uart *backend) #if CONFIG_MODEM_STATS static uint32_t get_receive_buf_length(struct modem_backend_uart *backend) { - return ring_buf_size_get(&backend->isr.receive_rdb[0]) + - ring_buf_size_get(&backend->isr.receive_rdb[1]); + return ring_buffer_size(&backend->isr.receive_rdb[0]) + + ring_buffer_size(&backend->isr.receive_rdb[1]); } static uint32_t get_receive_buf_size(struct modem_backend_uart *backend) { - return ring_buf_capacity_get(&backend->isr.receive_rdb[0]) + - ring_buf_capacity_get(&backend->isr.receive_rdb[1]); + return ring_buffer_capacity(&backend->isr.receive_rdb[0]) + + ring_buffer_capacity(&backend->isr.receive_rdb[1]); } static uint32_t get_transmit_buf_size(struct modem_backend_uart *backend) { - return ring_buf_capacity_get(&backend->isr.transmit_rb); + return ring_buffer_capacity(&backend->isr.transmit_rb); } static void advertise_transmit_buf_stats(struct modem_backend_uart *backend) @@ -176,7 +172,7 @@ static int modem_backend_uart_isr_transmit(void *data, const uint8_t *buf, size_ } uart_irq_tx_disable(backend->uart); - written = ring_buf_put(&backend->isr.transmit_rb, buf, size); + written = ring_buffer_write(&backend->isr.transmit_rb, buf, size); uart_irq_tx_enable(backend->uart); /* Update transmit buf capacity tracker */ @@ -204,9 +200,9 @@ static int modem_backend_uart_isr_receive(void *data, uint8_t *buf, size_t size) receive_rdb_unused = (backend->isr.receive_rdb_used == 1) ? 0 : 1; /* Read data from unused ring double buffer first */ - read_bytes += ring_buf_get(&backend->isr.receive_rdb[receive_rdb_unused], buf, size); + read_bytes += ring_buffer_read(&backend->isr.receive_rdb[receive_rdb_unused], buf, size); - if (ring_buf_is_empty(&backend->isr.receive_rdb[receive_rdb_unused]) == false) { + if (ring_buffer_empty(&backend->isr.receive_rdb[receive_rdb_unused]) == false) { return (int)read_bytes; } @@ -218,7 +214,7 @@ static int modem_backend_uart_isr_receive(void *data, uint8_t *buf, size_t size) /* Read data from previously used buffer */ receive_rdb_unused = (backend->isr.receive_rdb_used == 1) ? 0 : 1; - read_bytes += ring_buf_get(&backend->isr.receive_rdb[receive_rdb_unused], + read_bytes += ring_buffer_read(&backend->isr.receive_rdb[receive_rdb_unused], &buf[read_bytes], (size - read_bytes)); return (int)read_bytes; @@ -268,14 +264,14 @@ void modem_backend_uart_isr_init(struct modem_backend_uart *backend, receive_double_buf_size = config->receive_buf_size / 2; - ring_buf_init(&backend->isr.receive_rdb[0], receive_double_buf_size, - &config->receive_buf[0]); + ring_buffer_init(&backend->isr.receive_rdb[0], + &config->receive_buf[0], receive_double_buf_size); - ring_buf_init(&backend->isr.receive_rdb[1], receive_double_buf_size, - &config->receive_buf[receive_double_buf_size]); + ring_buffer_init(&backend->isr.receive_rdb[1], + &config->receive_buf[receive_double_buf_size], receive_double_buf_size); - ring_buf_init(&backend->isr.transmit_rb, config->transmit_buf_size, - config->transmit_buf); + ring_buffer_init(&backend->isr.transmit_rb, + config->transmit_buf, config->transmit_buf_size); atomic_set(&backend->isr.transmit_buf_len, 0); uart_irq_rx_disable(backend->uart); diff --git a/subsys/modem/modem_cmux.c b/subsys/modem/modem_cmux.c index 17e5bb72fba27..1bdb6d475c19e 100644 --- a/subsys/modem/modem_cmux.c +++ b/subsys/modem/modem_cmux.c @@ -167,12 +167,12 @@ static uint32_t modem_cmux_get_receive_buf_size(struct modem_cmux *cmux) static uint32_t modem_cmux_get_transmit_buf_length(struct modem_cmux *cmux) { - return ring_buf_size_get(&cmux->transmit_rb); + return ring_buffer_size(&cmux->transmit_rb); } static uint32_t modem_cmux_get_transmit_buf_size(struct modem_cmux *cmux) { - return ring_buf_capacity_get(&cmux->transmit_rb); + return ring_buffer_capacity(&cmux->transmit_rb); } static void modem_cmux_init_buf_stats(struct modem_cmux *cmux) @@ -280,7 +280,7 @@ static uint16_t modem_cmux_transmit_frame(struct modem_cmux *cmux, uint16_t data_len; uint16_t buf_idx; - space = ring_buf_space_get(&cmux->transmit_rb) - MODEM_CMUX_FRAME_SIZE_MAX; + space = ring_buffer_space(&cmux->transmit_rb) - MODEM_CMUX_FRAME_SIZE_MAX; data_len = MIN(space, frame->data_len); data_len = MIN(data_len, CONFIG_MODEM_CMUX_MTU); @@ -314,15 +314,15 @@ static uint16_t modem_cmux_transmit_frame(struct modem_cmux *cmux, } /* Frame header */ - ring_buf_put(&cmux->transmit_rb, buf, buf_idx); + ring_buffer_write(&cmux->transmit_rb, buf, buf_idx); /* Data */ - ring_buf_put(&cmux->transmit_rb, frame->data, data_len); + ring_buffer_write(&cmux->transmit_rb, frame->data, data_len); /* FCS and EOF will be put on the same call */ buf[0] = fcs; buf[1] = MODEM_CMUX_SOF; - ring_buf_put(&cmux->transmit_rb, buf, 2); + ring_buffer_write(&cmux->transmit_rb, buf, 2); modem_work_schedule(&cmux->transmit_work, K_NO_WAIT); return data_len; } @@ -334,7 +334,7 @@ static bool modem_cmux_transmit_cmd_frame(struct modem_cmux *cmux, struct modem_cmux_command *command; k_mutex_lock(&cmux->transmit_rb_lock, K_FOREVER); - space = ring_buf_space_get(&cmux->transmit_rb); + space = ring_buffer_space(&cmux->transmit_rb); if (space < MODEM_CMUX_CMD_FRAME_SIZE_MAX) { k_mutex_unlock(&cmux->transmit_rb_lock); @@ -364,7 +364,7 @@ static int16_t modem_cmux_transmit_data_frame(struct modem_cmux *cmux, return 0; } - space = ring_buf_space_get(&cmux->transmit_rb); + space = ring_buffer_space(&cmux->transmit_rb); /* * One command frame is reserved for command channel, and we shall prefer @@ -602,7 +602,7 @@ static void modem_cmux_on_dlci_frame_ua(struct modem_cmux_dlci *dlci) modem_pipe_notify_opened(&dlci->pipe); k_work_cancel_delayable(&dlci->open_work); k_mutex_lock(&dlci->receive_rb_lock, K_FOREVER); - ring_buf_reset(&dlci->receive_rb); + ring_buffer_reset(&dlci->receive_rb); k_mutex_unlock(&dlci->receive_rb_lock); break; @@ -630,7 +630,7 @@ static void modem_cmux_on_dlci_frame_uih(struct modem_cmux_dlci *dlci) } k_mutex_lock(&dlci->receive_rb_lock, K_FOREVER); - written = ring_buf_put(&dlci->receive_rb, cmux->frame.data, cmux->frame.data_len); + written = ring_buffer_write(&dlci->receive_rb, cmux->frame.data, cmux->frame.data_len); k_mutex_unlock(&dlci->receive_rb_lock); if (written != cmux->frame.data_len) { LOG_WRN("DLCI %u receive buffer overrun (dropped %u out of %u bytes)", @@ -654,7 +654,7 @@ static void modem_cmux_on_dlci_frame_sabm(struct modem_cmux_dlci *dlci) dlci->state = MODEM_CMUX_DLCI_STATE_OPEN; modem_pipe_notify_opened(&dlci->pipe); k_mutex_lock(&dlci->receive_rb_lock, K_FOREVER); - ring_buf_reset(&dlci->receive_rb); + ring_buffer_reset(&dlci->receive_rb); k_mutex_unlock(&dlci->receive_rb_lock); } @@ -972,17 +972,16 @@ static void modem_cmux_transmit_handler(struct k_work *item) #endif while (true) { - transmit_rb_empty = ring_buf_is_empty(&cmux->transmit_rb); + transmit_rb_empty = ring_buffer_empty(&cmux->transmit_rb); if (transmit_rb_empty) { break; } - reserved_size = ring_buf_get_claim(&cmux->transmit_rb, &reserved, UINT32_MAX); + reserved_size = ring_buffer_read_ptr(&cmux->transmit_rb, &reserved); ret = modem_pipe_transmit(cmux->pipe, reserved, reserved_size); if (ret < 0) { - ring_buf_get_finish(&cmux->transmit_rb, 0); if (ret != -EPERM) { LOG_ERR("Failed to %s %u bytes. (%d)", "transmit", reserved_size, ret); @@ -990,7 +989,7 @@ static void modem_cmux_transmit_handler(struct k_work *item) break; } - ring_buf_get_finish(&cmux->transmit_rb, (uint32_t)ret); + ring_buffer_consume(&cmux->transmit_rb, (uint32_t)ret); if (ret < reserved_size) { LOG_DBG("Transmitted only %u out of %u bytes at once.", ret, reserved_size); @@ -1065,12 +1064,12 @@ static void modem_cmux_disconnect_handler(struct k_work *item) #if CONFIG_MODEM_STATS static uint32_t modem_cmux_dlci_get_receive_buf_length(struct modem_cmux_dlci *dlci) { - return ring_buf_size_get(&dlci->receive_rb); + return ring_buffer_size(&dlci->receive_rb); } static uint32_t modem_cmux_dlci_get_receive_buf_size(struct modem_cmux_dlci *dlci) { - return ring_buf_capacity_get(&dlci->receive_rb); + return ring_buffer_capacity(&dlci->receive_rb); } static void modem_cmux_dlci_init_buf_stats(struct modem_cmux_dlci *dlci) @@ -1153,7 +1152,7 @@ static int modem_cmux_dlci_pipe_api_receive(void *data, uint8_t *buf, size_t siz modem_cmux_dlci_advertise_receive_buf_stat(dlci); #endif - ret = ring_buf_get(&dlci->receive_rb, buf, size); + ret = ring_buffer_read(&dlci->receive_rb, buf, size); k_mutex_unlock(&dlci->receive_rb_lock); return ret; } @@ -1276,7 +1275,7 @@ void modem_cmux_init(struct modem_cmux *cmux, const struct modem_cmux_config *co cmux->receive_buf_size = config->receive_buf_size; sys_slist_init(&cmux->dlcis); cmux->state = MODEM_CMUX_STATE_DISCONNECTED; - ring_buf_init(&cmux->transmit_rb, config->transmit_buf_size, config->transmit_buf); + ring_buffer_init(&cmux->transmit_rb, config->transmit_buf, config->transmit_buf_size); k_mutex_init(&cmux->transmit_rb_lock); k_work_init_delayable(&cmux->receive_work, modem_cmux_receive_handler); k_work_init_delayable(&cmux->transmit_work, modem_cmux_transmit_handler); @@ -1304,7 +1303,7 @@ struct modem_pipe *modem_cmux_dlci_init(struct modem_cmux *cmux, struct modem_cm memset(dlci, 0x00, sizeof(*dlci)); dlci->cmux = cmux; dlci->dlci_address = config->dlci_address; - ring_buf_init(&dlci->receive_rb, config->receive_buf_size, config->receive_buf); + ring_buffer_init(&dlci->receive_rb, config->receive_buf, config->receive_buf_size); k_mutex_init(&dlci->receive_rb_lock); modem_pipe_init(&dlci->pipe, dlci, &modem_cmux_dlci_pipe_api); k_work_init_delayable(&dlci->open_work, modem_cmux_dlci_open_handler); @@ -1326,7 +1325,7 @@ int modem_cmux_attach(struct modem_cmux *cmux, struct modem_pipe *pipe) } cmux->pipe = pipe; - ring_buf_reset(&cmux->transmit_rb); + ring_buffer_reset(&cmux->transmit_rb); cmux->receive_state = MODEM_CMUX_RECEIVE_STATE_SOF; modem_pipe_attach(cmux->pipe, modem_cmux_bus_callback, cmux); diff --git a/subsys/modem/modem_ppp.c b/subsys/modem/modem_ppp.c index 176413119b6d9..a590cd0255c9e 100644 --- a/subsys/modem/modem_ppp.c +++ b/subsys/modem/modem_ppp.c @@ -319,7 +319,7 @@ static void modem_ppp_process_received_byte(struct modem_ppp *ppp, uint8_t byte) #if CONFIG_MODEM_STATS static uint32_t get_transmit_buf_length(struct modem_ppp *ppp) { - return ring_buf_size_get(&ppp->transmit_rb); + return ring_buffer_size(&ppp->transmit_rb); } static void advertise_transmit_buf_stats(struct modem_ppp *ppp) @@ -361,7 +361,7 @@ static void modem_ppp_send_handler(struct k_work *item) struct modem_ppp *ppp = CONTAINER_OF(item, struct modem_ppp, send_work); uint8_t byte; uint8_t *reserved; - uint32_t reserved_size; + uint32_t max_read_size; int ret; if (ppp->tx_pkt == NULL) { @@ -375,10 +375,10 @@ static void modem_ppp_send_handler(struct k_work *item) } /* Fill transmit ring buffer */ - while (ring_buf_space_get(&ppp->transmit_rb) > 0) { + while (ring_buffer_space(&ppp->transmit_rb) > 0) { byte = modem_ppp_wrap_net_pkt_byte(ppp); - ring_buf_put(&ppp->transmit_rb, &byte, 1); + ring_buffer_write(&ppp->transmit_rb, &byte, 1); if (ppp->transmit_state == MODEM_PPP_TRANSMIT_STATE_IDLE) { net_pkt_unref(ppp->tx_pkt); @@ -392,18 +392,17 @@ static void modem_ppp_send_handler(struct k_work *item) advertise_transmit_buf_stats(ppp); #endif - while (!ring_buf_is_empty(&ppp->transmit_rb)) { - reserved_size = ring_buf_get_claim(&ppp->transmit_rb, &reserved, UINT32_MAX); + while (!ring_buffer_empty(&ppp->transmit_rb)) { + max_read_size = ring_buffer_read_ptr(&ppp->transmit_rb, &reserved); - ret = modem_pipe_transmit(ppp->pipe, reserved, reserved_size); + ret = modem_pipe_transmit(ppp->pipe, reserved, max_read_size); if (ret < 0) { - ring_buf_get_finish(&ppp->transmit_rb, 0); break; } - ring_buf_get_finish(&ppp->transmit_rb, (uint32_t)ret); + ring_buffer_consume(&ppp->transmit_rb, (uint32_t)ret); - if (ret < reserved_size) { + if (ret < max_read_size) { break; } } @@ -599,7 +598,7 @@ int modem_ppp_init_internal(const struct device *dev) struct modem_ppp *ppp = (struct modem_ppp *)dev->data; atomic_set(&ppp->state, 0); - ring_buf_init(&ppp->transmit_rb, ppp->buf_size, ppp->transmit_buf); + ring_buffer_init(&ppp->transmit_rb, ppp->transmit_buf, ppp->buf_size); k_work_init(&ppp->send_work, modem_ppp_send_handler); k_work_init(&ppp->process_work, modem_ppp_process_handler); k_fifo_init(&ppp->tx_pkt_fifo); diff --git a/subsys/net/lib/lwm2m/Kconfig b/subsys/net/lib/lwm2m/Kconfig index 3d89075a85711..953459cf917c6 100644 --- a/subsys/net/lib/lwm2m/Kconfig +++ b/subsys/net/lib/lwm2m/Kconfig @@ -227,7 +227,6 @@ config LWM2M_RD_CLIENT_MAX_RETRIES config LWM2M_RESOURCE_DATA_CACHE_SUPPORT bool "Resource Time series data cache support" depends on (LWM2M_RW_SENML_JSON_SUPPORT || LWM2M_RW_SENML_CBOR_SUPPORT) - depends on RING_BUFFER help Enable time series data storage. Requires time() to provide current Unix timestamp. diff --git a/subsys/net/lib/lwm2m/lwm2m_message_handling.c b/subsys/net/lib/lwm2m/lwm2m_message_handling.c index 58f467adcf19a..8127cd4aae410 100644 --- a/subsys/net/lib/lwm2m/lwm2m_message_handling.c +++ b/subsys/net/lib/lwm2m/lwm2m_message_handling.c @@ -1404,7 +1404,7 @@ static int lwm2m_read_cached_data(struct lwm2m_message *msg, read_info = &msg->cache_info->read_info[msg->cache_info->entry_size]; /* Store original timeseries ring buffer get states for failure handling */ read_info->cache_data = cached_data; - read_info->original_rb_get = cached_data->rb.get; + read_info->original_rb_get = cached_data->rb.read_ptr; msg->cache_info->entry_size++; if (msg->cache_info->entry_limit) { length = MIN(length, msg->cache_info->entry_limit); @@ -3523,7 +3523,7 @@ static bool init_next_pending_timeseries_data(struct lwm2m_cache_read_info *cach /* Check do we have still pending data to send */ for (int i = 0; i < cache_temp->entry_size; i++) { - if (ring_buf_is_empty(&cache_temp->read_info[i].cache_data->rb)) { + if (ring_buffer_empty(&cache_temp->read_info[i].cache_data->rb)) { /* Skip Empty cached buffers */ continue; } @@ -3534,7 +3534,7 @@ static bool init_next_pending_timeseries_data(struct lwm2m_cache_read_info *cach return false; } - bytes_available += ring_buf_size_get(&cache_temp->read_info[i].cache_data->rb); + bytes_available += ring_buffer_size(&cache_temp->read_info[i].cache_data->rb); } if (bytes_available == 0) { diff --git a/subsys/net/lib/lwm2m/lwm2m_registry.c b/subsys/net/lib/lwm2m/lwm2m_registry.c index 5893093167f79..4b993a0dee5b7 100644 --- a/subsys/net/lib/lwm2m/lwm2m_registry.c +++ b/subsys/net/lib/lwm2m/lwm2m_registry.c @@ -1618,7 +1618,7 @@ int lwm2m_enable_cache(const struct lwm2m_obj_path *path, struct lwm2m_time_seri return -ENODATA; } - ring_buf_init(&cache_entry->rb, cache_entry_size * cache_len, (uint8_t *)data_cache); + ring_buffer_init(&cache_entry->rb, (uint8_t *)data_cache, cache_entry_size * cache_len); return 0; #else @@ -1651,27 +1651,29 @@ bool lwm2m_cache_write(struct lwm2m_time_series_resource *cache_entry, uint8_t *buf_ptr; uint32_t element_size = sizeof(struct lwm2m_time_series_elem); - if (ring_buf_space_get(&cache_entry->rb) < element_size) { + if (ring_buffer_space(&cache_entry->rb) < element_size) { /* No space */ if (IS_ENABLED(CONFIG_LWM2M_CACHE_DROP_LATEST)) { return false; } /* Free entry */ - length = ring_buf_get_claim(&cache_entry->rb, &buf_ptr, element_size); - ring_buf_get_finish(&cache_entry->rb, length); + if (ring_buffer_size(&cache_entry->rb) < element_size) { + LOG_ERR("Cache entry too small %u", ring_buffer_size(&cache_entry->rb)); + return false; + } + ring_buffer_consume(&cache_entry->rb, element_size); } - length = ring_buf_put_claim(&cache_entry->rb, &buf_ptr, element_size); + length = ring_buffer_write_ptr(&cache_entry->rb, &buf_ptr, element_size); - if (length != element_size) { - ring_buf_put_finish(&cache_entry->rb, 0); + if (length < element_size) { LOG_ERR("Allocation failed %u", length); return false; } - ring_buf_put_finish(&cache_entry->rb, length); /* Store data */ memcpy(buf_ptr, buf, element_size); + ring_buffer_commit(&cache_entry->rb, length); return true; #else return NULL; @@ -1686,21 +1688,19 @@ bool lwm2m_cache_read(struct lwm2m_time_series_resource *cache_entry, uint8_t *buf_ptr; uint32_t element_size = sizeof(struct lwm2m_time_series_elem); - if (ring_buf_is_empty(&cache_entry->rb)) { + if (ring_buffer_empty(&cache_entry->rb)) { return false; } - length = ring_buf_get_claim(&cache_entry->rb, &buf_ptr, element_size); - - if (length != element_size) { + length = ring_buffer_read_ptr(&cache_entry->rb, &buf_ptr); + if (length < element_size) { LOG_ERR("Cache read fail %u", length); - ring_buf_get_finish(&cache_entry->rb, 0); return false; } /* Read Data */ memcpy(buf, buf_ptr, element_size); - ring_buf_get_finish(&cache_entry->rb, length); + ring_buffer_consume(&cache_entry->rb, element_size); return true; #else @@ -1713,11 +1713,11 @@ size_t lwm2m_cache_size(const struct lwm2m_time_series_resource *cache_entry) #if defined(CONFIG_LWM2M_RESOURCE_DATA_CACHE_SUPPORT) uint32_t bytes_available; - if (ring_buf_is_empty(&cache_entry->rb)) { + if (ring_buffer_empty(&cache_entry->rb)) { return 0; } - bytes_available = ring_buf_size_get(&cache_entry->rb); + bytes_available = ring_buffer_size(&cache_entry->rb); return (bytes_available / sizeof(struct lwm2m_time_series_elem)); #else diff --git a/subsys/net/lib/lwm2m/lwm2m_registry.h b/subsys/net/lib/lwm2m/lwm2m_registry.h index 6c876551bf552..caf9d96415646 100644 --- a/subsys/net/lib/lwm2m/lwm2m_registry.h +++ b/subsys/net/lib/lwm2m/lwm2m_registry.h @@ -210,7 +210,7 @@ struct lwm2m_time_series_resource { /* Resource Path url */ struct lwm2m_obj_path path; /* Ring buffer */ - struct ring_buf rb; + struct ring_buffer rb; }; #if defined(CONFIG_LWM2M_RESOURCE_DATA_CACHE_SUPPORT) @@ -219,7 +219,7 @@ struct lwm2m_time_series_resource { struct lwm2m_cache_read_entry { struct lwm2m_time_series_resource *cache_data; - struct ring_buf_index original_rb_get; + ring_buffer_index_t original_rb_get; }; struct lwm2m_cache_read_info { diff --git a/subsys/net/lib/sockets/socketpair.c b/subsys/net/lib/sockets/socketpair.c index d7fbe6e162306..4e29fba3acbf0 100644 --- a/subsys/net/lib/sockets/socketpair.c +++ b/subsys/net/lib/sockets/socketpair.c @@ -47,7 +47,7 @@ __net_socket struct spair { int remote; /**< the remote endpoint file descriptor */ uint32_t flags; /**< status and option bits */ struct k_sem sem; /**< semaphore for exclusive structure access */ - struct ring_buf recv_q; + struct ring_buffer recv_q; /** indicates local @a recv_q isn't empty */ struct k_poll_signal readable; /** indicates local @a recv_q isn't full */ @@ -106,7 +106,7 @@ static inline size_t spair_write_avail(struct spair *spair) return 0; } - return ring_buf_space_get(&remote->recv_q); + return ring_buffer_space(&remote->recv_q); } /** @@ -117,7 +117,7 @@ static inline size_t spair_write_avail(struct spair *spair) */ static inline size_t spair_read_avail(struct spair *spair) { - return ring_buf_size_get(&spair->recv_q); + return ring_buffer_size(&spair->recv_q); } /** Swap two 32-bit integers */ @@ -250,7 +250,7 @@ static struct spair *spair_new(void) spair->flags = SPAIR_FLAGS_DEFAULT; k_sem_init(&spair->sem, 1, 1); - ring_buf_init(&spair->recv_q, sizeof(spair->buf), spair->buf); + ring_buffer_init(&spair->recv_q, spair->buf, sizeof(spair->buf)); k_poll_signal_init(&spair->readable); k_poll_signal_init(&spair->writeable); @@ -550,7 +550,7 @@ static ssize_t spair_write(void *obj, const void *buffer, size_t count) } } - bytes_written = ring_buf_put(&remote->recv_q, (void *)buffer, count); + bytes_written = ring_buffer_write(&remote->recv_q, (void *)buffer, count); if (spair_write_avail(spair) == 0) { k_poll_signal_reset(&remote->writeable); } @@ -721,7 +721,7 @@ static ssize_t spair_read(void *obj, void *buffer, size_t count) } } - bytes_read = ring_buf_get(&spair->recv_q, (void *)buffer, count); + bytes_read = ring_buffer_read(&spair->recv_q, (void *)buffer, count); if (spair_read_avail(spair) == 0 && !sock_is_eof(spair)) { k_poll_signal_reset(&spair->readable); } diff --git a/subsys/shell/backends/Kconfig.backends b/subsys/shell/backends/Kconfig.backends index 13b6997947268..428959a6f2e60 100644 --- a/subsys/shell/backends/Kconfig.backends +++ b/subsys/shell/backends/Kconfig.backends @@ -18,7 +18,6 @@ config SHELL_BACKEND_SERIAL bool "Serial backend" default "$(dt_chosen_enabled,$(DT_CHOSEN_Z_SHELL_UART))" select SERIAL - select RING_BUFFER help Enable serial backend. diff --git a/subsys/shell/backends/shell_mqtt.c b/subsys/shell/backends/shell_mqtt.c index 5b9b452f720b1..fae0a7b55beb6 100644 --- a/subsys/shell/backends/shell_mqtt.c +++ b/subsys/shell/backends/shell_mqtt.c @@ -571,20 +571,20 @@ static void mqtt_evt_handler(struct mqtt_client *const client, const struct mqtt while (payload_left > 0) { /* Attempt to claim `payload_left` bytes of buffer in rb */ - size = (size_t)ring_buf_put_claim(&sh->rx_rb, &sh->rx_rb_ptr, - payload_left); + size = (size_t)MIN(ring_buffer_write_ptr(&sh->rx_rb, &sh->rx_rb_ptr), + payload_left); /* Read `size` bytes of payload from mqtt */ rc = mqtt_read_publish_payload_blocking(client, sh->rx_rb_ptr, size); /* errno value, return */ if (rc < 0) { - ring_buf_reset(&sh->rx_rb); + ring_buffer_reset(&sh->rx_rb); return; } size = (size_t)rc; /* Indicate that `size` bytes of payload has been written into rb */ - (void)ring_buf_put_finish(&sh->rx_rb, size); + ring_buffer_commit(&sh->rx_rb, size); /* Update `payload_left` */ payload_left -= size; /* Tells the shell that we have new data for it */ @@ -596,9 +596,9 @@ static void mqtt_evt_handler(struct mqtt_client *const client, const struct mqtt /* Shell won't execute the cmds without \r\n */ while (true) { /* Check if rb's free space is enough to fit in \r\n */ - size = ring_buf_space_get(&sh->rx_rb); + size = ring_buffer_space(&sh->rx_rb); if (size >= sizeof("\r\n")) { - (void)ring_buf_put(&sh->rx_rb, "\r\n", sizeof("\r\n")); + (void)ring_buffer_write(&sh->rx_rb, "\r\n", sizeof("\r\n")); break; } /* Arbitrary sleep for the shell to do its thing */ @@ -650,7 +650,7 @@ static int init(const struct shell_transport *transport, const void *config, (void)snprintf(sh->sub_topic, SH_MQTT_TOPIC_RX_MAX_SIZE, "%s" CONFIG_SHELL_MQTT_TOPIC_RX_ID, sh->device_id); - ring_buf_init(&sh->rx_rb, RX_RB_SIZE, sh->rx_rb_buf); + ring_buffer_init(&sh->rx_rb, sh->rx_rb_buf, RX_RB_SIZE); LOG_DBG("Initializing shell MQTT backend"); @@ -793,10 +793,10 @@ static int read_data(const struct shell_transport *transport, void *data, size_t return 0; } - *cnt = ring_buf_get(&sh->rx_rb, data, length); + *cnt = ring_buffer_read(&sh->rx_rb, data, length); /* Inform the shell if there are still data in the rb */ - if (ring_buf_size_get(&sh->rx_rb) > 0) { + if (ring_buffer_size(&sh->rx_rb) > 0) { sh->shell_handler(SHELL_TRANSPORT_EVT_RX_RDY, sh->shell_context); } diff --git a/subsys/shell/backends/shell_uart.c b/subsys/shell/backends/shell_uart.c index 1d19ce19a3181..b88439ee3f47b 100644 --- a/subsys/shell/backends/shell_uart.c +++ b/subsys/shell/backends/shell_uart.c @@ -78,9 +78,7 @@ static void uart_rx_handle(const struct device *dev, struct shell_uart_int_drive #endif do { - len = ring_buf_put_claim(&sh_uart->rx_ringbuf, &data, - sh_uart->rx_ringbuf.size); - + len = ring_buffer_write_ptr(&sh_uart->rx_ringbuf, &data); if (len > 0) { rd_len = uart_fifo_read(dev, data, len); @@ -105,9 +103,7 @@ static void uart_rx_handle(const struct device *dev, struct shell_uart_int_drive } } #endif /* CONFIG_MCUMGR_TRANSPORT_SHELL */ - int err = ring_buf_put_finish(&sh_uart->rx_ringbuf, rd_len); - (void)err; - __ASSERT_NO_MSG(err == 0); + ring_buffer_commit(&sh_uart->rx_ringbuf, rd_len); } else { uint8_t dummy; @@ -176,15 +172,10 @@ static void uart_tx_handle(const struct device *dev, struct shell_uart_int_drive return; } - len = ring_buf_get_claim(&sh_uart->tx_ringbuf, (uint8_t **)&data, - sh_uart->tx_ringbuf.size); + len = ring_buffer_read_ptr(&sh_uart->tx_ringbuf, (uint8_t **)&data); if (len) { - int err; - len = uart_fifo_fill(dev, data, len); - err = ring_buf_get_finish(&sh_uart->tx_ringbuf, len); - __ASSERT_NO_MSG(err == 0); - ARG_UNUSED(err); + ring_buffer_consume(&sh_uart->tx_ringbuf, len); } else { uart_irq_tx_disable(dev); sh_uart->tx_busy = 0; @@ -212,10 +203,10 @@ static void irq_init(struct shell_uart_int_driven *sh_uart) { const struct device *dev = sh_uart->common.dev; - ring_buf_init(&sh_uart->rx_ringbuf, CONFIG_SHELL_BACKEND_SERIAL_RX_RING_BUFFER_SIZE, - sh_uart->rx_buf); - ring_buf_init(&sh_uart->tx_ringbuf, CONFIG_SHELL_BACKEND_SERIAL_TX_RING_BUFFER_SIZE, - sh_uart->tx_buf); + ring_buffer_init(&sh_uart->rx_ringbuf, sh_uart->rx_buf, + CONFIG_SHELL_BACKEND_SERIAL_RX_RING_BUFFER_SIZE); + ring_buffer_init(&sh_uart->tx_ringbuf, sh_uart->tx_buf, + CONFIG_SHELL_BACKEND_SERIAL_TX_RING_BUFFER_SIZE); sh_uart->tx_busy = 0; uart_irq_callback_user_data_set(dev, uart_callback, (void *)sh_uart); uart_irq_rx_enable(dev); @@ -266,7 +257,7 @@ static void polling_rx_timeout_handler(struct k_timer *timer) struct shell_uart_polling *sh_uart = k_timer_user_data_get(timer); while (uart_poll_in(sh_uart->common.dev, &c) == 0) { - if (ring_buf_put(&sh_uart->rx_ringbuf, &c, 1) == 0U) { + if (ring_buffer_write(&sh_uart->rx_ringbuf, &c, 1) == 0U) { /* ring buffer full. */ LOG_WRN("RX ring buffer full."); } @@ -280,8 +271,8 @@ static void polling_init(struct shell_uart_polling *sh_uart) k_timer_user_data_set(&sh_uart->rx_timer, (void *)sh_uart); k_timer_start(&sh_uart->rx_timer, RX_POLL_PERIOD, RX_POLL_PERIOD); - ring_buf_init(&sh_uart->rx_ringbuf, CONFIG_SHELL_BACKEND_SERIAL_RX_RING_BUFFER_SIZE, - sh_uart->rx_buf); + ring_buffer_init(&sh_uart->rx_ringbuf, sh_uart->rx_buf, + CONFIG_SHELL_BACKEND_SERIAL_RX_RING_BUFFER_SIZE); } static int init(const struct shell_transport *transport, @@ -375,7 +366,7 @@ static int polling_write(struct shell_uart_common *sh_uart, static int irq_write(struct shell_uart_int_driven *sh_uart, const void *data, size_t length, size_t *cnt) { - *cnt = ring_buf_put(&sh_uart->tx_ringbuf, data, length); + *cnt = ring_buffer_write(&sh_uart->tx_ringbuf, data, length); if (atomic_set(&sh_uart->tx_busy, 1) == 0) { uart_irq_tx_enable(sh_uart->common.dev); @@ -420,7 +411,7 @@ static int write_uart(const struct shell_transport *transport, static int irq_read(struct shell_uart_int_driven *sh_uart, void *data, size_t length, size_t *cnt) { - *cnt = ring_buf_get(&sh_uart->rx_ringbuf, data, length); + *cnt = ring_buffer_read(&sh_uart->rx_ringbuf, data, length); return 0; } @@ -428,7 +419,7 @@ static int irq_read(struct shell_uart_int_driven *sh_uart, static int polling_read(struct shell_uart_polling *sh_uart, void *data, size_t length, size_t *cnt) { - *cnt = ring_buf_get(&sh_uart->rx_ringbuf, data, length); + *cnt = ring_buffer_read(&sh_uart->rx_ringbuf, data, length); return 0; } diff --git a/subsys/testsuite/Kconfig b/subsys/testsuite/Kconfig index 6fb7d695fc283..de499ea331e5e 100644 --- a/subsys/testsuite/Kconfig +++ b/subsys/testsuite/Kconfig @@ -210,7 +210,6 @@ config TEST_BUSY_SIM bool "Busy simulator" depends on TEST select ENTROPY_GENERATOR - select RING_BUFFER if !XOSHIRO_RANDOM_GENERATOR select COUNTER help It simulates cpu load by using counter device to generate interrupts diff --git a/subsys/testsuite/busy_sim/busy_sim.c b/subsys/testsuite/busy_sim/busy_sim.c index 6b8d311951577..2c8171d1527fe 100644 --- a/subsys/testsuite/busy_sim/busy_sim.c +++ b/subsys/testsuite/busy_sim/busy_sim.c @@ -84,7 +84,7 @@ static uint32_t get_timeout(bool idle, bool use_rand) if (use_rand) { sys_rand_get(&rand_val, sizeof(rand_val)); } else { - len = ring_buf_get(&rnd_rbuf, + len = ring_buffer_read(&rnd_rbuf, (uint8_t *)&rand_val, sizeof(rand_val)); if (len < sizeof(rand_val)) { @@ -198,7 +198,7 @@ static int busy_sim_init(const struct device *dev) if (config->entropy) { k_work_init(&sim_work, rng_pool_work_handler); - ring_buf_init(&rnd_rbuf, BUFFER_SIZE, rnd_buf); + ring_buffer_init(&rnd_rbuf, rnd_buf, BUFFER_SIZE); } data->us_tick = freq / 1000000; diff --git a/subsys/tracing/Kconfig b/subsys/tracing/Kconfig index 5e3d349345b2e..8edffbaf15c8d 100644 --- a/subsys/tracing/Kconfig +++ b/subsys/tracing/Kconfig @@ -80,14 +80,12 @@ choice TRACING_METHOD_CHOICE config TRACING_SYNC bool "Synchronous Tracing" - select RING_BUFFER help Enable synchronous tracing. This requires the backend to be very low-latency. config TRACING_ASYNC bool "Asynchronous Tracing" - select RING_BUFFER help Enable asynchronous tracing. This will buffer all the tracing packets to the ring buffer first, tracing thread will try to diff --git a/subsys/tracing/tracing_buffer.c b/subsys/tracing/tracing_buffer.c index e3eeea8aebf18..91a24f1193c91 100644 --- a/subsys/tracing/tracing_buffer.c +++ b/subsys/tracing/tracing_buffer.c @@ -3,10 +3,10 @@ * * SPDX-License-Identifier: Apache-2.0 */ - +#include #include -static struct ring_buf tracing_ring_buf; +static struct ring_buffer tracing_ring_buf; static uint8_t tracing_buffer[CONFIG_TRACING_BUFFER_SIZE + 1]; static uint8_t tracing_cmd_buffer[CONFIG_TRACING_CMD_BUFFER_SIZE]; @@ -19,51 +19,52 @@ uint32_t tracing_cmd_buffer_alloc(uint8_t **data) uint32_t tracing_buffer_put_claim(uint8_t **data, uint32_t size) { - return ring_buf_put_claim(&tracing_ring_buf, data, size); + return MIN(ring_buffer_write_ptr(&tracing_ring_buf, data), size); } int tracing_buffer_put_finish(uint32_t size) { - return ring_buf_put_finish(&tracing_ring_buf, size); + ring_buffer_commit(&tracing_ring_buf, size); + return 0; } uint32_t tracing_buffer_put(uint8_t *data, uint32_t size) { - return ring_buf_put(&tracing_ring_buf, data, size); + return ring_buffer_write(&tracing_ring_buf, data, size); } uint32_t tracing_buffer_get_claim(uint8_t **data, uint32_t size) { - return ring_buf_get_claim(&tracing_ring_buf, data, size); + return MIN(ring_buffer_read_ptr(&tracing_ring_buf, data), size); } int tracing_buffer_get_finish(uint32_t size) { - return ring_buf_get_finish(&tracing_ring_buf, size); + ring_buffer_consume(&tracing_ring_buf, size); + return 0; } uint32_t tracing_buffer_get(uint8_t *data, uint32_t size) { - return ring_buf_get(&tracing_ring_buf, data, size); + return ring_buffer_read(&tracing_ring_buf, data, size); } void tracing_buffer_init(void) { - ring_buf_init(&tracing_ring_buf, - sizeof(tracing_buffer), tracing_buffer); + ring_buffer_init(&tracing_ring_buf, tracing_buffer, sizeof(tracing_buffer)); } bool tracing_buffer_is_empty(void) { - return ring_buf_is_empty(&tracing_ring_buf); + return ring_buffer_empty(&tracing_ring_buf); } uint32_t tracing_buffer_capacity_get(void) { - return ring_buf_capacity_get(&tracing_ring_buf); + return ring_buffer_capacity(&tracing_ring_buf); } uint32_t tracing_buffer_space_get(void) { - return ring_buf_space_get(&tracing_ring_buf); + return ring_buffer_space(&tracing_ring_buf); } diff --git a/subsys/usb/device/class/Kconfig.cdc b/subsys/usb/device/class/Kconfig.cdc index 59a72e25b6e3b..310914d769167 100644 --- a/subsys/usb/device/class/Kconfig.cdc +++ b/subsys/usb/device/class/Kconfig.cdc @@ -11,7 +11,6 @@ config USB_CDC_ACM depends on DT_HAS_ZEPHYR_CDC_ACM_UART_ENABLED select SERIAL_HAS_DRIVER select SERIAL_SUPPORT_INTERRUPT - select RING_BUFFER select UART_INTERRUPT_DRIVEN help USB CDC ACM class support. diff --git a/subsys/usb/device/class/cdc_acm.c b/subsys/usb/device/class/cdc_acm.c index 377e0642cb4b6..70113b2715b01 100644 --- a/subsys/usb/device/class/cdc_acm.c +++ b/subsys/usb/device/class/cdc_acm.c @@ -112,8 +112,8 @@ struct cdc_acm_dev_data_t { bool tx_irq_ena; /* Tx interrupt enable status */ bool rx_irq_ena; /* Rx interrupt enable status */ uint8_t rx_buf[CDC_ACM_BUFFER_SIZE]; /* Internal RX buffer */ - struct ring_buf *rx_ringbuf; - struct ring_buf *tx_ringbuf; + struct ring_buffer *rx_ringbuf; + struct ring_buffer *tx_ringbuf; /* Interface data buffer */ /* CDC ACM line coding properties. LE order */ struct cdc_acm_line_coding line_coding; @@ -230,7 +230,7 @@ static void cdc_acm_write_cb(uint8_t ep, int size, void *priv) * ensure that actual payload will not be sent before initialization * timeout passes. */ - if (ring_buf_is_empty(dev_data->tx_ringbuf) && size) { + if (ring_buffer_empty(dev_data->tx_ringbuf) && size) { LOG_DBG("tx_ringbuf is empty"); return; } @@ -269,9 +269,7 @@ static void tx_work_handler(struct k_work *work) return; } - len = ring_buf_get_claim(dev_data->tx_ringbuf, &data, - CONFIG_USB_CDC_ACM_RINGBUF_SIZE); - + len = ring_buffer_read_ptr(dev_data->tx_ringbuf, &data); if (!len) { LOG_DBG("Nothing to send"); return; @@ -294,7 +292,7 @@ static void tx_work_handler(struct k_work *work) usb_transfer(ep, data, len, USB_TRANS_WRITE, cdc_acm_write_cb, dev_data); - ring_buf_get_finish(dev_data->tx_ringbuf, len); + ring_buffer_consume(dev_data->tx_ringbuf, len); } static void cdc_acm_read_cb(uint8_t ep, int size, void *priv) @@ -303,13 +301,13 @@ static void cdc_acm_read_cb(uint8_t ep, int size, void *priv) size_t wrote; LOG_DBG("ep %x size %d dev_data %p rx_ringbuf space %u", - ep, size, dev_data, ring_buf_space_get(dev_data->rx_ringbuf)); + ep, size, dev_data, ring_buffer_space(dev_data->rx_ringbuf)); if (size <= 0) { goto done; } - wrote = ring_buf_put(dev_data->rx_ringbuf, dev_data->rx_buf, size); + wrote = ring_buffer_write(dev_data->rx_ringbuf, dev_data->rx_buf, size); if (wrote < size) { LOG_ERR("Ring buffer full, drop %zd bytes", size - wrote); } @@ -321,7 +319,7 @@ static void cdc_acm_read_cb(uint8_t ep, int size, void *priv) k_work_submit_to_queue(&USB_WORK_Q, &dev_data->cb_work); } - if (ring_buf_space_get(dev_data->rx_ringbuf) < sizeof(dev_data->rx_buf)) { + if (ring_buffer_space(dev_data->rx_ringbuf) < sizeof(dev_data->rx_buf)) { dev_data->rx_paused = true; return; } @@ -526,10 +524,10 @@ static int cdc_acm_fifo_fill(const struct device *dev, size_t wrote; LOG_DBG("dev_data %p len %d tx_ringbuf space %u", - dev_data, len, ring_buf_space_get(dev_data->tx_ringbuf)); + dev_data, len, ring_buffer_space(dev_data->tx_ringbuf)); lock = irq_lock(); - wrote = ring_buf_put(dev_data->tx_ringbuf, tx_data, len); + wrote = ring_buffer_write(dev_data->tx_ringbuf, tx_data, len); irq_unlock(lock); LOG_DBG("Wrote %zu of %d bytes to TX ringbuffer", wrote, len); @@ -557,12 +555,12 @@ static int cdc_acm_fifo_read(const struct device *dev, uint8_t *rx_data, uint32_t len; LOG_DBG("dev %p size %d rx_ringbuf space %u", - dev, size, ring_buf_space_get(dev_data->rx_ringbuf)); + dev, size, ring_buffer_space(dev_data->rx_ringbuf)); - len = ring_buf_get(dev_data->rx_ringbuf, rx_data, size); + len = ring_buffer_read(dev_data->rx_ringbuf, rx_data, size); if (dev_data->rx_paused == true) { - if (ring_buf_space_get(dev_data->rx_ringbuf) >= CDC_ACM_BUFFER_SIZE) { + if (ring_buffer_space(dev_data->rx_ringbuf) >= CDC_ACM_BUFFER_SIZE) { struct usb_cfg_data *cfg = (void *)dev->config; if (dev_data->configured) { @@ -615,7 +613,7 @@ static int cdc_acm_irq_tx_ready(const struct device *dev) struct cdc_acm_dev_data_t * const dev_data = dev->data; if (dev_data->tx_irq_ena && dev_data->tx_ready) { - return ring_buf_space_get(dev_data->tx_ringbuf); + return ring_buffer_space(dev_data->tx_ringbuf); } return 0; @@ -694,11 +692,11 @@ static int cdc_acm_irq_update(const struct device *dev) { struct cdc_acm_dev_data_t * const dev_data = dev->data; - if (!ring_buf_space_get(dev_data->tx_ringbuf)) { + if (!ring_buffer_space(dev_data->tx_ringbuf)) { dev_data->tx_ready = false; } - if (ring_buf_is_empty(dev_data->rx_ringbuf)) { + if (ring_buffer_empty(dev_data->rx_ringbuf)) { dev_data->rx_ready = false; } @@ -1033,7 +1031,7 @@ static void cdc_acm_poll_out(const struct device *dev, unsigned char c) while (true) { lock = irq_lock(); - wrote = ring_buf_put(dev_data->tx_ringbuf, &c, 1); + wrote = ring_buffer_write(dev_data->tx_ringbuf, &c, 1); irq_unlock(lock); if (wrote == 1) { break; @@ -1202,9 +1200,9 @@ static DEVICE_API(uart, cdc_acm_driver_api) = { .endpoint = cdc_acm_ep_data_##x, \ }; \ \ - RING_BUF_DECLARE(cdc_acm_rx_rb_##x, \ + RING_BUFFER_DECLARE(cdc_acm_rx_rb_##x, \ CONFIG_USB_CDC_ACM_RINGBUF_SIZE); \ - RING_BUF_DECLARE(cdc_acm_tx_rb_##x, \ + RING_BUFFER_DECLARE(cdc_acm_tx_rb_##x, \ CONFIG_USB_CDC_ACM_RINGBUF_SIZE); \ static struct cdc_acm_dev_data_t cdc_acm_dev_data_##x = { \ .line_coding = CDC_ACM_DEFAULT_BAUDRATE, \ diff --git a/subsys/usb/device_next/class/Kconfig.cdc_acm b/subsys/usb/device_next/class/Kconfig.cdc_acm index 4162ca751babb..4d115848eed9e 100644 --- a/subsys/usb/device_next/class/Kconfig.cdc_acm +++ b/subsys/usb/device_next/class/Kconfig.cdc_acm @@ -8,7 +8,6 @@ config USBD_CDC_ACM_CLASS depends on DT_HAS_ZEPHYR_CDC_ACM_UART_ENABLED select SERIAL_HAS_DRIVER select SERIAL_SUPPORT_INTERRUPT - select RING_BUFFER select UART_INTERRUPT_DRIVEN default y help diff --git a/subsys/usb/device_next/class/Kconfig.midi2 b/subsys/usb/device_next/class/Kconfig.midi2 index cd991c93e69be..459e9748f4933 100644 --- a/subsys/usb/device_next/class/Kconfig.midi2 +++ b/subsys/usb/device_next/class/Kconfig.midi2 @@ -4,7 +4,6 @@ config USBD_MIDI2_CLASS bool "USB MIDI 2.0 class support [EXPERIMENTAL]" - select RING_BUFFER help Enable the USB MIDI 2.0 device class support. diff --git a/subsys/usb/device_next/class/usbd_cdc_acm.c b/subsys/usb/device_next/class/usbd_cdc_acm.c index d3921c0f146c2..626de72897c84 100644 --- a/subsys/usb/device_next/class/usbd_cdc_acm.c +++ b/subsys/usb/device_next/class/usbd_cdc_acm.c @@ -49,7 +49,7 @@ LOG_MODULE_REGISTER(usbd_cdc_acm, CONFIG_USBD_CDC_ACM_LOG_LEVEL); #define CDC_ACM_TX_FIFO_BUSY 5 struct cdc_acm_uart_fifo { - struct ring_buf *rb; + struct ring_buffer *rb; bool irq; bool altered; }; @@ -309,7 +309,7 @@ static int usbd_cdc_acm_request(struct usbd_class_data *const c_data, size_t done; LOG_HEXDUMP_INF(buf->data, buf->len, ""); - done = ring_buf_put(data->rx_fifo.rb, buf->data, buf->len); + done = ring_buffer_write(data->rx_fifo.rb, buf->data, buf->len); if (done && data->cb) { cdc_acm_work_submit(&data->irq_cb_work); } @@ -326,7 +326,7 @@ static int usbd_cdc_acm_request(struct usbd_class_data *const c_data, atomic_clear_bit(&data->state, CDC_ACM_TX_FIFO_BUSY); - if (!ring_buf_is_empty(data->tx_fifo.rb)) { + if (!ring_buffer_empty(data->tx_fifo.rb)) { /* Queue pending TX data on IN endpoint */ cdc_acm_work_schedule(&data->tx_fifo_work, K_NO_WAIT); } @@ -361,7 +361,7 @@ static void usbd_cdc_acm_enable(struct usbd_class_data *const c_data) } if (atomic_test_bit(&data->state, CDC_ACM_IRQ_TX_ENABLED)) { - if (ring_buf_space_get(data->tx_fifo.rb)) { + if (ring_buffer_space(data->tx_fifo.rb)) { /* Raise TX ready interrupt */ cdc_acm_work_submit(&data->irq_cb_work); } else { @@ -658,7 +658,7 @@ static void cdc_acm_tx_fifo_handler(struct k_work *work) return; } - len = ring_buf_get(data->tx_fifo.rb, buf->data, buf->size); + len = ring_buffer_read(data->tx_fifo.rb, buf->data, buf->size); net_buf_add(buf, len); data->zlp_needed = len != 0 && len % cdc_acm_get_bulk_mps(c_data) == 0; @@ -697,7 +697,7 @@ static void cdc_acm_rx_fifo_handler(struct k_work *work) return; } - if (ring_buf_space_get(data->rx_fifo.rb) < cdc_acm_get_bulk_mps(c_data)) { + if (ring_buffer_space(data->rx_fifo.rb) < cdc_acm_get_bulk_mps(c_data)) { LOG_INF("RX buffer to small, throttle"); return; } @@ -729,7 +729,7 @@ static void cdc_acm_irq_tx_enable(const struct device *dev) atomic_set_bit(&data->state, CDC_ACM_IRQ_TX_ENABLED); - if (ring_buf_space_get(data->tx_fifo.rb)) { + if (ring_buffer_space(data->tx_fifo.rb)) { LOG_INF("tx_en: trigger irq_cb_work"); cdc_acm_work_submit(&data->irq_cb_work); } @@ -749,7 +749,7 @@ static void cdc_acm_irq_rx_enable(const struct device *dev) atomic_set_bit(&data->state, CDC_ACM_IRQ_RX_ENABLED); /* Permit buffer to be drained regardless of USB state */ - if (!ring_buf_is_empty(data->rx_fifo.rb)) { + if (!ring_buffer_empty(data->rx_fifo.rb)) { LOG_INF("rx_en: trigger irq_cb_work"); cdc_acm_work_submit(&data->irq_cb_work); } @@ -782,14 +782,14 @@ static int cdc_acm_fifo_fill(const struct device *dev, } key = k_spin_lock(&data->lock); - done = ring_buf_put(data->tx_fifo.rb, tx_data, len); + done = ring_buffer_write(data->tx_fifo.rb, tx_data, len); k_spin_unlock(&data->lock, key); if (done) { data->tx_fifo.altered = true; } LOG_INF("UART dev %p, len %d, remaining space %u", - dev, len, ring_buf_space_get(data->tx_fifo.rb)); + dev, len, ring_buffer_space(data->tx_fifo.rb)); return done; } @@ -802,7 +802,7 @@ static int cdc_acm_fifo_read(const struct device *dev, uint32_t len; LOG_INF("UART dev %p size %d length %u", - dev, size, ring_buf_size_get(data->rx_fifo.rb)); + dev, size, ring_buffer_size(data->rx_fifo.rb)); if (!check_wq_ctx(dev)) { LOG_WRN("Invoked by inappropriate context"); @@ -810,7 +810,7 @@ static int cdc_acm_fifo_read(const struct device *dev, return 0; } - len = ring_buf_get(data->rx_fifo.rb, rx_data, size); + len = ring_buffer_read(data->rx_fifo.rb, rx_data, size); if (len) { data->rx_fifo.altered = true; } @@ -824,7 +824,7 @@ static int cdc_acm_irq_tx_ready(const struct device *dev) if (check_wq_ctx(dev)) { if (data->tx_fifo.irq) { - return ring_buf_space_get(data->tx_fifo.rb); + return ring_buffer_space(data->tx_fifo.rb); } } else { LOG_WRN("Invoked by inappropriate context"); @@ -878,14 +878,14 @@ static int cdc_acm_irq_update(const struct device *dev) } if (atomic_test_bit(&data->state, CDC_ACM_IRQ_RX_ENABLED) && - !ring_buf_is_empty(data->rx_fifo.rb)) { + !ring_buffer_empty(data->rx_fifo.rb)) { data->rx_fifo.irq = true; } else { data->rx_fifo.irq = false; } if (atomic_test_bit(&data->state, CDC_ACM_IRQ_TX_ENABLED) && - ring_buf_space_get(data->tx_fifo.rb)) { + ring_buffer_space(data->tx_fifo.rb)) { data->tx_fifo.irq = true; } else { data->tx_fifo.irq = false; @@ -947,13 +947,13 @@ static void cdc_acm_irq_cb_handler(struct k_work *work) } if (atomic_test_bit(&data->state, CDC_ACM_IRQ_RX_ENABLED) && - !ring_buf_is_empty(data->rx_fifo.rb)) { + !ring_buffer_empty(data->rx_fifo.rb)) { LOG_DBG("rx irq pending, submit irq_cb_work"); cdc_acm_work_submit(&data->irq_cb_work); } if (atomic_test_bit(&data->state, CDC_ACM_IRQ_TX_ENABLED) && - ring_buf_space_get(data->tx_fifo.rb)) { + ring_buffer_space(data->tx_fifo.rb)) { LOG_DBG("tx irq pending, submit irq_cb_work"); cdc_acm_work_submit(&data->irq_cb_work); } @@ -975,11 +975,11 @@ static int cdc_acm_poll_in(const struct device *dev, unsigned char *const c) uint32_t len; int ret = -1; - if (ring_buf_is_empty(data->rx_fifo.rb)) { + if (ring_buffer_empty(data->rx_fifo.rb)) { return ret; } - len = ring_buf_get(data->rx_fifo.rb, c, 1); + len = ring_buffer_read(data->rx_fifo.rb, c, 1); if (len) { cdc_acm_work_submit(&data->rx_fifo_work); ret = 0; @@ -996,7 +996,7 @@ static void cdc_acm_poll_out(const struct device *dev, const unsigned char c) while (true) { key = k_spin_lock(&data->lock); - wrote = ring_buf_put(data->tx_fifo.rb, &c, 1); + wrote = ring_buffer_write(data->tx_fifo.rb, &c, 1); k_spin_unlock(&data->lock, key); if (wrote == 1) { @@ -1119,8 +1119,8 @@ static int usbd_cdc_acm_preinit(const struct device *dev) { struct cdc_acm_uart_data *const data = dev->data; - ring_buf_reset(data->tx_fifo.rb); - ring_buf_reset(data->rx_fifo.rb); + ring_buffer_reset(data->tx_fifo.rb); + ring_buffer_reset(data->rx_fifo.rb); k_work_init_delayable(&data->tx_fifo_work, cdc_acm_tx_fifo_handler); k_work_init(&data->rx_fifo_work, cdc_acm_rx_fifo_handler); @@ -1350,8 +1350,8 @@ const static struct usb_desc_header *cdc_acm_hs_desc_##n[] = { \ USBD_DUT_STRING_INTERFACE); \ )) \ \ - RING_BUF_DECLARE(cdc_acm_rb_rx_##n, DT_INST_PROP(n, rx_fifo_size)); \ - RING_BUF_DECLARE(cdc_acm_rb_tx_##n, DT_INST_PROP(n, tx_fifo_size)); \ + RING_BUFFER_DECLARE(cdc_acm_rb_rx_##n, DT_INST_PROP(n, rx_fifo_size)); \ + RING_BUFFER_DECLARE(cdc_acm_rb_tx_##n, DT_INST_PROP(n, tx_fifo_size)); \ \ static const struct cdc_acm_uart_config uart_config_##n = { \ .c_data = &cdc_acm_##n, \ diff --git a/subsys/usb/device_next/class/usbd_midi2.c b/subsys/usb/device_next/class/usbd_midi2.c index 78b8fa4ccc41c..dc523d02938b1 100644 --- a/subsys/usb/device_next/class/usbd_midi2.c +++ b/subsys/usb/device_next/class/usbd_midi2.c @@ -151,7 +151,7 @@ struct usbd_midi_data { struct k_work rx_work; struct k_work tx_work; uint8_t tx_queue_buf[MIDI_QUEUE_SIZE]; - struct ring_buf tx_queue; + struct ring_buffer tx_queue; uint8_t altsetting; struct usbd_midi_ops ops; }; @@ -201,7 +201,7 @@ static int usbd_midi_class_request(struct usbd_class_data *const class_data, k_work_submit(&data->rx_work); } else { LOG_HEXDUMP_DBG(buf->data, buf->len, "Tx DATA complete"); - if (ring_buf_size_get(&data->tx_queue)) { + if (ring_buffer_size(&data->tx_queue)) { k_work_submit(&data->tx_work); } } @@ -440,7 +440,7 @@ static void usbd_midi_tx_work(struct k_work *work) return; } - net_buf_add(buf, ring_buf_get(&data->tx_queue, buf->data, buf->size)); + net_buf_add(buf, ring_buffer_read(&data->tx_queue, buf->data, buf->size)); LOG_HEXDUMP_DBG(buf->data, buf->len, "MIDI2 - Tx DATA"); ret = usbd_ep_enqueue(data->class_data, buf); @@ -455,7 +455,7 @@ static int usbd_midi_preinit(const struct device *dev) struct usbd_midi_data *data = dev->data; LOG_DBG("Init device %s", dev->name); - ring_buf_init(&data->tx_queue, MIDI_QUEUE_SIZE, data->tx_queue_buf); + ring_buffer_init(&data->tx_queue, data->tx_queue_buf, MIDI_QUEUE_SIZE); k_work_init(&data->rx_work, usbd_midi_rx_work); k_work_init(&data->tx_work, usbd_midi_tx_work); @@ -475,14 +475,14 @@ int usbd_midi_send(const struct device *dev, const struct midi_ump ump) return -EIO; } - if (buflen > ring_buf_space_get(&data->tx_queue)) { + if (buflen > ring_buffer_space(&data->tx_queue)) { LOG_WRN("Not enough space in tx queue"); return -ENOBUFS; } for (size_t i = 0; i < words; i++) { word = sys_cpu_to_le32(ump.data[i]); - ring_buf_put(&data->tx_queue, (const uint8_t *)&word, 4); + ring_buffer_write(&data->tx_queue, (const uint8_t *)&word, 4); } k_work_submit(&data->tx_work); diff --git a/tests/bluetooth/hci_uart_async/prj.conf b/tests/bluetooth/hci_uart_async/prj.conf index 5f558afdebf51..c2ec7f3241c00 100644 --- a/tests/bluetooth/hci_uart_async/prj.conf +++ b/tests/bluetooth/hci_uart_async/prj.conf @@ -1,5 +1,3 @@ -CONFIG_RING_BUFFER=y - CONFIG_ASSERT=y CONFIG_LOG=y CONFIG_TEST=y diff --git a/tests/bluetooth/shell/boards/nrf5340_audio_dk_nrf5340_cpuapp.conf b/tests/bluetooth/shell/boards/nrf5340_audio_dk_nrf5340_cpuapp.conf index ba20761468a57..3fa15b9178c01 100644 --- a/tests/bluetooth/shell/boards/nrf5340_audio_dk_nrf5340_cpuapp.conf +++ b/tests/bluetooth/shell/boards/nrf5340_audio_dk_nrf5340_cpuapp.conf @@ -3,6 +3,5 @@ CONFIG_FPU=y CONFIG_LIBLC3=y # For USB audio the following configs are needed -CONFIG_RING_BUFFER=y CONFIG_USB_DEVICE_STACK_NEXT=y CONFIG_USBD_AUDIO2_CLASS=y diff --git a/tests/bluetooth/shell/boards/nrf5340dk_nrf5340_cpuapp.conf b/tests/bluetooth/shell/boards/nrf5340dk_nrf5340_cpuapp.conf index ba20761468a57..3fa15b9178c01 100644 --- a/tests/bluetooth/shell/boards/nrf5340dk_nrf5340_cpuapp.conf +++ b/tests/bluetooth/shell/boards/nrf5340dk_nrf5340_cpuapp.conf @@ -3,6 +3,5 @@ CONFIG_FPU=y CONFIG_LIBLC3=y # For USB audio the following configs are needed -CONFIG_RING_BUFFER=y CONFIG_USB_DEVICE_STACK_NEXT=y CONFIG_USBD_AUDIO2_CLASS=y diff --git a/tests/bluetooth/tester/overlay-le-audio.conf b/tests/bluetooth/tester/overlay-le-audio.conf index 30f609b9ace5b..5b224c2a165fd 100644 --- a/tests/bluetooth/tester/overlay-le-audio.conf +++ b/tests/bluetooth/tester/overlay-le-audio.conf @@ -25,9 +25,6 @@ CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT=2 CONFIG_BT_BAP_UNICAST_CLIENT_GROUP_STREAM_COUNT=4 CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE=22 -# Ring buffer for streaming ISO data -CONFIG_RING_BUFFER=y - # These have to be the same as in the controller (hci_ipc) CONFIG_BT_MAX_CONN=3 CONFIG_BT_MAX_PAIRED=3 diff --git a/tests/bluetooth/tester/src/audio/btp_bap_broadcast.c b/tests/bluetooth/tester/src/audio/btp_bap_broadcast.c index e26046d2b083b..a6d539384d30c 100644 --- a/tests/bluetooth/tester/src/audio/btp_bap_broadcast.c +++ b/tests/bluetooth/tester/src/audio/btp_bap_broadcast.c @@ -28,7 +28,6 @@ #include #include #include -#include #include #include #include diff --git a/tests/bluetooth/tester/src/audio/btp_bap_unicast.c b/tests/bluetooth/tester/src/audio/btp_bap_unicast.c index e839a0ddb7673..2985b14fecb78 100644 --- a/tests/bluetooth/tester/src/audio/btp_bap_unicast.c +++ b/tests/bluetooth/tester/src/audio/btp_bap_unicast.c @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include diff --git a/tests/drivers/uart/uart_async_dual/prj.conf b/tests/drivers/uart/uart_async_dual/prj.conf index aaf360d69136e..ece2ec1b7bf8a 100644 --- a/tests/drivers/uart/uart_async_dual/prj.conf +++ b/tests/drivers/uart/uart_async_dual/prj.conf @@ -1,5 +1,4 @@ CONFIG_ZTEST=y -CONFIG_RING_BUFFER=y CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_SERIAL=y CONFIG_UART_ASYNC_API=y diff --git a/tests/drivers/uart/uart_async_dual/src/main.c b/tests/drivers/uart/uart_async_dual/src/main.c index 37ce73120acb8..54efd1ca8614f 100644 --- a/tests/drivers/uart/uart_async_dual/src/main.c +++ b/tests/drivers/uart/uart_async_dual/src/main.c @@ -104,7 +104,7 @@ enum test_tx_mode { struct test_tx_data { uint8_t buf[512]; - struct ring_buf rbuf; + struct ring_buffer rbuf; atomic_t busy; uint8_t packet_len; uint8_t cnt; @@ -163,7 +163,7 @@ static void fill_tx(struct test_tx_data *data) return; } - while ((len = ring_buf_put_claim(&data->rbuf, &buf, 255)) > 1) { + while ((len = ring_buffer_write_ptr(&data->rbuf, &buf)) > 1) { uint8_t r = (sys_rand8_get() % MAX_PACKET_LEN) % len; uint8_t packet_len = MAX(r, 2); uint8_t rem = len - packet_len; @@ -174,7 +174,7 @@ static void fill_tx(struct test_tx_data *data) buf[i] = packet_len - i; } - ring_buf_put_finish(&data->rbuf, packet_len); + ring_buffer_commit(&data->rbuf, packet_len); } } @@ -205,7 +205,7 @@ static void try_tx(const struct device *dev, bool irq) return; } - len = ring_buf_get_claim(&tx_data.rbuf, &buf, 255); + len = MIN(ring_buffer_read_ptr(&tx_data.rbuf, &buf), 255); if (len > 0) { err = uart_tx(dev, buf, len, TX_TIMEOUT); zassert_equal(err, 0, @@ -222,7 +222,7 @@ static void on_tx_done(const struct device *dev, struct uart_event *evt) } /* Finish previous data chunk and start new if any pending. */ - ring_buf_get_finish(&tx_data.rbuf, evt->data.tx.len); + ring_buffer_consume(&tx_data.rbuf, evt->data.tx.len); atomic_set(&tx_data.busy, 0); try_tx(dev, true); } @@ -401,7 +401,7 @@ static void var_packet_hwfc(uint32_t baudrate, bool tx_packets, bool cont) rx_data.state = RX_HDR; rx_data.mode = cont ? RX_CONT : RX_DIS; - ring_buf_init(&tx_data.rbuf, sizeof(tx_data.buf), tx_data.buf); + ring_buffer_init(&tx_data.rbuf, tx_data.buf, sizeof(tx_data.buf)); k_timer_start(&test_timer, K_MSEC(CONFIG_UART_ASYNC_DUAL_TEST_TIMEOUT), K_NO_WAIT); diff --git a/tests/lib/ringbuffer/CMakeLists.txt b/tests/lib/depricated/ringbuffer/CMakeLists.txt similarity index 100% rename from tests/lib/ringbuffer/CMakeLists.txt rename to tests/lib/depricated/ringbuffer/CMakeLists.txt diff --git a/tests/lib/ringbuffer/Kconfig b/tests/lib/depricated/ringbuffer/Kconfig similarity index 100% rename from tests/lib/ringbuffer/Kconfig rename to tests/lib/depricated/ringbuffer/Kconfig diff --git a/tests/lib/ringbuffer/prj.conf b/tests/lib/depricated/ringbuffer/prj.conf similarity index 100% rename from tests/lib/ringbuffer/prj.conf rename to tests/lib/depricated/ringbuffer/prj.conf diff --git a/tests/lib/ringbuffer/src/concurrent.c b/tests/lib/depricated/ringbuffer/src/concurrent.c similarity index 100% rename from tests/lib/ringbuffer/src/concurrent.c rename to tests/lib/depricated/ringbuffer/src/concurrent.c diff --git a/tests/lib/ringbuffer/src/main.c b/tests/lib/depricated/ringbuffer/src/main.c similarity index 100% rename from tests/lib/ringbuffer/src/main.c rename to tests/lib/depricated/ringbuffer/src/main.c diff --git a/tests/lib/ringbuffer/testcase.yaml b/tests/lib/depricated/ringbuffer/testcase.yaml similarity index 79% rename from tests/lib/ringbuffer/testcase.yaml rename to tests/lib/depricated/ringbuffer/testcase.yaml index 2733ffe3f76bc..34dd21cc904cb 100644 --- a/tests/lib/ringbuffer/testcase.yaml +++ b/tests/lib/depricated/ringbuffer/testcase.yaml @@ -2,15 +2,16 @@ common: tags: - ring_buffer - circular_buffer + ignore_faults: true timeout: 150 tests: - libraries.ring_buffer: + libraries.ring_buf: integration_platforms: - native_sim - native_sim/native/64 - libraries.ring_buffer.concurrent: + libraries.ring_buf.concurrent: platform_allow: qemu_x86 extra_configs: - CONFIG_SYS_CLOCK_TICKS_PER_SEC=100000 diff --git a/tests/lib/ring_buffer/CMakeLists.txt b/tests/lib/ring_buffer/CMakeLists.txt new file mode 100644 index 0000000000000..7295e108f2f37 --- /dev/null +++ b/tests/lib/ring_buffer/CMakeLists.txt @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(ringbuffer) + +target_sources(app PRIVATE + src/main.c +) diff --git a/tests/lib/ring_buffer/prj.conf b/tests/lib/ring_buffer/prj.conf new file mode 100644 index 0000000000000..e75ccb6bd9130 --- /dev/null +++ b/tests/lib/ring_buffer/prj.conf @@ -0,0 +1,6 @@ +CONFIG_ZTEST=y + +CONFIG_ENTROPY_GENERATOR=y +CONFIG_TEST_RANDOM_GENERATOR=y +CONFIG_RING_BUFFER=n +CONFIG_ASSERT=n diff --git a/tests/lib/ring_buffer/src/main.c b/tests/lib/ring_buffer/src/main.c new file mode 100644 index 0000000000000..cd93f501f6884 --- /dev/null +++ b/tests/lib/ring_buffer/src/main.c @@ -0,0 +1,206 @@ +/* + * Copyright (c) Måns Ansgariusson 2025 + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#include +LOG_MODULE_REGISTER(ring_buffer_test, LOG_LEVEL_DBG); + +ZTEST_SUITE(ringbuffer_api, NULL, NULL, NULL, NULL, NULL); + +ZTEST(ringbuffer_api, test_init) +{ + uint8_t buffer[12]; + struct ring_buffer rb; + + ring_buffer_init(&rb, buffer, sizeof(buffer)); + + zassert_true(rb.buffer == buffer, "Incorrect buffer pointer"); + zassert_true(ring_buffer_size(&rb) == 0, "No bytes should be available"); + zassert_true(ring_buffer_capacity(&rb) == sizeof(buffer), "capacity != input size"); +} + +ZTEST(ringbuffer_api, test_io) +{ + struct ring_buffer rb; + uint8_t buffer[12]; + uint8_t input[12]; + uint8_t res[12]; + + sys_rand_get(input, sizeof(input)); + ring_buffer_init(&rb, buffer, sizeof(buffer)); + + zassert_true(sizeof(input) == ring_buffer_write(&rb, input, sizeof(input)), + "Failed to write input to ring_buffer"); + zassert_true(ring_buffer_size(&rb) == sizeof(input), + "ring_buffer should have written bytes available for reading"); + zassert_true(sizeof(res) == ring_buffer_read(&rb, res, sizeof(res)), + "failed to read written bytes"); + zassert_true(!memcmp(input, res, sizeof(input)), "read != written"); + zassert_true(ring_buffer_size(&rb) == 0, "ring_buffer should be empty"); +} + +ZTEST(ringbuffer_api, test_dma_io) +{ + size_t read = 0; + size_t written = 0; + struct ring_buffer rb; + uint8_t buffer[12]; + uint8_t input[12]; + uint8_t res[12]; + uint8_t *ref; + + sys_rand_get(input, sizeof(input)); + ring_buffer_init(&rb, buffer, sizeof(buffer)); + + while (written < sizeof(input)) { + size_t write_sz = MIN(ring_buffer_write_ptr(&rb, &ref), sizeof(input) - written); + + zassert_true(write_sz > 0, "there should be space in the buffer"); + memcpy(ref, &input[written], write_sz); + ring_buffer_commit(&rb, write_sz); + written += write_sz; + } + + zassert_true(sizeof(input) == ring_buffer_size(&rb), + "there should be written bytes available to read"); + + while (read < sizeof(res)) { + size_t read_sz = MIN(ring_buffer_read_ptr(&rb, &ref), sizeof(input) - read); + + zassert_true(read_sz > 0, "there should be data in the buffer"); + memcpy(&res[read], ref, read_sz); + ring_buffer_consume(&rb, read_sz); + read += read_sz; + } + + zassert_true(!memcmp(input, res, sizeof(input)), "read != written"); + zassert_true(ring_buffer_size(&rb) == 0, "no bytes should be available to read"); +} + +#define MONOTONIC_MAX_VALUE ((((size_t)1) << (8 * sizeof(ring_buffer_index_t))) - 1) +ZTEST(ringbuffer_api, test_index_overflow) +{ + struct ring_buffer rb; + uint8_t buffer[12]; + uint8_t input[12]; + uint8_t res[12]; + + sys_rand_get(input, sizeof(input)); + ring_buffer_init(&rb, buffer, sizeof(buffer)); + + /* Moving the monotonic counters to the end of their range. */ + rb.write_ptr = MONOTONIC_MAX_VALUE - 3; + rb.read_ptr = MONOTONIC_MAX_VALUE - 3; + + zassert_true(sizeof(input) == ring_buffer_write(&rb, input, sizeof(input)), + "Failed to write input to ring_buffer"); + zassert_true(ring_buffer_size(&rb) == sizeof(input), + "ring_buffer should have written bytes available for reading"); + zassert_true(sizeof(res) == ring_buffer_read(&rb, res, sizeof(res)), + "failed to read written bytes"); + zassert_true(!memcmp(input, res, sizeof(input)), "read != written"); + zassert_true(ring_buffer_size(&rb) == 0, "ring_buffer should be empty"); + zassert_true(ring_buffer_space(&rb) == ring_buffer_capacity(&rb), "buffer should be empty, full space"); +} + +ZTEST(ringbuffer_api, test_stress) +{ + struct ring_buffer rb; + uint8_t buffer[12]; + uint8_t input[128]; + uint8_t res[128]; + size_t read = 0; + size_t written = 0; + size_t to_write; + size_t to_read; + + sys_rand_get(input, sizeof(input)); + ring_buffer_init(&rb, buffer, sizeof(buffer)); + + while (read < sizeof(input)) { + to_write = MIN(sys_rand32_get() % (ring_buffer_space(&rb) + 1), + sizeof(input) - written); + zassert_true(ring_buffer_write(&rb, &input[written], to_write) == to_write, + "Failed to write"); + written += to_write; + + zassert_true(ring_buffer_size(&rb) == to_write, "buffer should contain written data"); + zassert_true(ring_buffer_space(&rb) == ring_buffer_capacity(&rb) - to_write, "buffer should contain written data"); + while (read < written) { + to_read = MIN(sys_rand32_get() % (ring_buffer_size(&rb) + 1), + written - read); + zassert_true(ring_buffer_read(&rb, &res[read], to_read) == to_read, + "Failed to read"); + read += to_read; + } + zassert_true(ring_buffer_size(&rb) == 0, "buffer should be empty"); + zassert_true(ring_buffer_space(&rb) == ring_buffer_capacity(&rb), "buffer should be empty, full space"); + } + zassert_true(!memcmp(input, res, read), "read != written"); +} + +ZTEST(ringbuffer_api, test_ringbuffer_performance) +{ + struct ring_buffer rb; + uint8_t buffer[12]; + uint8_t indata[16]; + uint8_t outdata[16]; + uint8_t *ptr; + uint32_t timestamp; + int loop = 1000; + + ring_buffer_init(&rb, buffer, sizeof(buffer)); + + timestamp = k_cycle_get_32(); + for (int i = 0; i < loop; i++) { + ring_buffer_write(&rb, indata, 1); + ring_buffer_read(&rb, outdata, 1); + } + timestamp = k_cycle_get_32() - timestamp; + LOG_INF("1 byte write+read, avg cycles: %d", timestamp/loop); + + ring_buffer_reset(&rb); + timestamp = k_cycle_get_32(); + for (int i = 0; i < loop; i++) { + ring_buffer_write(&rb, indata, 4); + ring_buffer_read(&rb, outdata, 4); + } + timestamp = k_cycle_get_32() - timestamp; + LOG_INF("4 byte write+read, avg cycles: %d", timestamp/loop); + + ring_buffer_reset(&rb); + timestamp = k_cycle_get_32(); + for (int i = 0; i < loop; i++) { + ring_buffer_write_ptr(&rb, &ptr); + ring_buffer_commit(&rb, 1); + ring_buffer_read(&rb, outdata, 1); + } + timestamp = k_cycle_get_32() - timestamp; + LOG_INF("1 byte write_ptr+commit+read, avg cycles: %d", timestamp/loop); + + ring_buffer_reset(&rb); + timestamp = k_cycle_get_32(); + for (int i = 0; i < loop; i++) { + ring_buffer_write_ptr(&rb, &ptr); + ring_buffer_commit(&rb, 5); + ring_buffer_read(&rb, outdata, 5); + } + timestamp = k_cycle_get_32() - timestamp; + LOG_INF("5 byte write_ptr+commit+read, avg cycles: %d", timestamp/loop); + + ring_buffer_reset(&rb); + timestamp = k_cycle_get_32(); + for (int i = 0; i < loop; i++) { + ring_buffer_write(&rb, indata, 5); + ring_buffer_read_ptr(&rb, &ptr); + ring_buffer_consume(&rb, 5); + } + timestamp = k_cycle_get_32() - timestamp; + LOG_INF("5 byte write+read_ptr+consume, avg cycles: %d", timestamp/loop); +} diff --git a/tests/lib/ring_buffer/testcase.yaml b/tests/lib/ring_buffer/testcase.yaml new file mode 100644 index 0000000000000..62d630dcbe87e --- /dev/null +++ b/tests/lib/ring_buffer/testcase.yaml @@ -0,0 +1,11 @@ +common: + tags: + - ring_buffer + - circular_buffer + timeout: 150 + +tests: + libraries.ring_buffer: + integration_platforms: + - native_sim + - native_sim/native/64 diff --git a/tests/subsys/modem/backends/uart/src/main.c b/tests/subsys/modem/backends/uart/src/main.c index 68bc167a0c0c8..ca5ca28f4665b 100644 --- a/tests/subsys/modem/backends/uart/src/main.c +++ b/tests/subsys/modem/backends/uart/src/main.c @@ -44,7 +44,7 @@ K_SEM_DEFINE(receive_ready_sem, 0, 1); /*************************************************************************************************/ static uint8_t backend_receive_buffer[TEST_BACKEND_RECEIVE_BUFFER_SIZE]; static uint8_t backend_transmit_buffer[4096]; -RING_BUF_DECLARE(transmit_ring_buf, 4096); +RING_BUFFER_DECLARE(transmit_ring_buf, 4096); static uint8_t receive_buffer[4096]; /*************************************************************************************************/ @@ -90,12 +90,12 @@ static void prng_reset(void) static void fill_transmit_ring_buf(void) { - uint32_t space = ring_buf_space_get(&transmit_ring_buf); + uint32_t space = ring_buffer_space(&transmit_ring_buf); uint8_t data; for (uint32_t i = 0; i < space; i++) { data = transmit_prng_random(); - ring_buf_put(&transmit_ring_buf, &data, 1); + ring_buffer_write(&transmit_ring_buf, &data, 1); } } @@ -122,7 +122,7 @@ static int transmit_prng(uint32_t remaining) int ret; fill_transmit_ring_buf(); - reserved_size = ring_buf_get_claim(&transmit_ring_buf, &reserved, UINT32_MAX); + reserved_size = ring_buffer_read_ptr(&transmit_ring_buf, &reserved); transmit_size = MIN(transmit_size_prng_random(), reserved_size); transmit_size = MIN(remaining, transmit_size); ret = modem_pipe_transmit(pipe, reserved, transmit_size); @@ -131,7 +131,7 @@ static int transmit_prng(uint32_t remaining) } printk("TX: %u,%u\n", transmit_size, (uint32_t)ret); __ASSERT(ret <= remaining, "Impossible number of bytes sent %u", (uint32_t)ret); - ring_buf_get_finish(&transmit_ring_buf, ret); + ring_buffer_consume(&transmit_ring_buf, ret); return ret; } @@ -187,7 +187,7 @@ static void *test_modem_backend_uart_setup(void) static void test_modem_backend_uart_before(void *f) { prng_reset(); - ring_buf_reset(&transmit_ring_buf); + ring_buffer_reset(&transmit_ring_buf); k_sem_reset(&receive_ready_sem); __ASSERT_NO_MSG(modem_pipe_open(pipe, K_SECONDS(1)) == 0); } diff --git a/tests/subsys/modem/mock/modem_backend_mock.c b/tests/subsys/modem/mock/modem_backend_mock.c index 5b7b47c0eb497..f921e0ea31c70 100644 --- a/tests/subsys/modem/mock/modem_backend_mock.c +++ b/tests/subsys/modem/mock/modem_backend_mock.c @@ -46,13 +46,13 @@ static int modem_backend_mock_transmit(void *data, const uint8_t *buf, size_t si if (mock->bridge) { struct modem_backend_mock *t_mock = mock->bridge; - ret = ring_buf_put(&t_mock->rx_rb, buf, size); + ret = ring_buffer_write(&t_mock->rx_rb, buf, size); k_work_submit(&t_mock->receive_ready_work); k_work_submit(&mock->transmit_idle_work); return ret; } - ret = ring_buf_put(&mock->tx_rb, buf, size); + ret = ring_buffer_write(&mock->tx_rb, buf, size); if (modem_backend_mock_update(mock, buf, size)) { modem_backend_mock_put(mock, mock->transaction->put, mock->transaction->put_size); @@ -69,7 +69,7 @@ static int modem_backend_mock_receive(void *data, uint8_t *buf, size_t size) struct modem_backend_mock *mock = (struct modem_backend_mock *)data; size = (mock->limit < size) ? mock->limit : size; - return ring_buf_get(&mock->rx_rb, buf, size); + return ring_buffer_read(&mock->rx_rb, buf, size); } static int modem_backend_mock_close(void *data) @@ -108,8 +108,8 @@ struct modem_pipe *modem_backend_mock_init(struct modem_backend_mock *mock, { memset(mock, 0, sizeof(*mock)); - ring_buf_init(&mock->rx_rb, config->rx_buf_size, config->rx_buf); - ring_buf_init(&mock->tx_rb, config->tx_buf_size, config->tx_buf); + ring_buffer_init(&mock->rx_rb, config->rx_buf, config->rx_buf_size); + ring_buffer_init(&mock->tx_rb, config->tx_buf, config->tx_buf_size); k_work_init(&mock->receive_ready_work, modem_backend_mock_receive_ready_handler); k_work_init(&mock->transmit_idle_work, modem_backend_mock_transmit_idle_handler); mock->limit = config->limit; @@ -124,20 +124,20 @@ struct modem_pipe *modem_backend_mock_get_pipe(struct modem_backend_mock *mock) void modem_backend_mock_reset(struct modem_backend_mock *mock) { - ring_buf_reset(&mock->rx_rb); - ring_buf_reset(&mock->tx_rb); + ring_buffer_reset(&mock->rx_rb); + ring_buffer_reset(&mock->tx_rb); mock->transaction = NULL; mock->transaction_match_cnt = 0; } int modem_backend_mock_get(struct modem_backend_mock *mock, uint8_t *buf, size_t size) { - return ring_buf_get(&mock->tx_rb, buf, size); + return ring_buffer_read(&mock->tx_rb, buf, size); } void modem_backend_mock_put(struct modem_backend_mock *mock, const uint8_t *buf, size_t size) { - __ASSERT(ring_buf_put(&mock->rx_rb, buf, size) == size, + __ASSERT(ring_buffer_write(&mock->rx_rb, buf, size) == size, "Mock buffer capacity exceeded"); k_work_submit(&mock->receive_ready_work); diff --git a/tests/subsys/modem/mock/modem_backend_mock.h b/tests/subsys/modem/mock/modem_backend_mock.h index 56a5b585cb12c..fb54ad28f6f29 100644 --- a/tests/subsys/modem/mock/modem_backend_mock.h +++ b/tests/subsys/modem/mock/modem_backend_mock.h @@ -24,8 +24,8 @@ struct modem_backend_mock_transaction { struct modem_backend_mock { struct modem_pipe pipe; - struct ring_buf rx_rb; - struct ring_buf tx_rb; + struct ring_buffer rx_rb; + struct ring_buffer tx_rb; struct k_work receive_ready_work; struct k_work transmit_idle_work;