2012-11-22 16:43:40 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2012-11-22 16:43:40 +08:00
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2012-11-22 16:43:40 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2012-09-30 Bernard first version.
|
2013-05-08 15:54:30 +08:00
|
|
|
* 2013-05-08 Grissiom reimplement
|
2017-10-15 22:56:46 +08:00
|
|
|
* 2016-08-18 heyuanjie add interface
|
2021-07-20 15:47:03 +08:00
|
|
|
* 2021-07-20 arminker fix write_index bug in function rt_ringbuffer_put_force
|
2021-08-17 22:44:29 +08:00
|
|
|
* 2021-08-14 Jackistang add comments for function interface.
|
2012-11-22 16:43:40 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
rt_inline enum rt_ringbuffer_state rt_ringbuffer_status(struct rt_ringbuffer *rb)
|
|
|
|
{
|
|
|
|
if (rb->read_index == rb->write_index)
|
|
|
|
{
|
|
|
|
if (rb->read_mirror == rb->write_mirror)
|
|
|
|
return RT_RINGBUFFER_EMPTY;
|
|
|
|
else
|
|
|
|
return RT_RINGBUFFER_FULL;
|
|
|
|
}
|
|
|
|
return RT_RINGBUFFER_HALFFULL;
|
|
|
|
}
|
|
|
|
|
2021-08-14 13:47:05 +08:00
|
|
|
/**
|
2021-08-18 22:51:00 +08:00
|
|
|
* @brief Initialize the ring buffer object.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-17 22:44:29 +08:00
|
|
|
* @param rb A pointer to the ring buffer object.
|
2021-08-14 13:47:05 +08:00
|
|
|
* @param pool A pointer to the buffer.
|
2021-08-18 09:44:52 +08:00
|
|
|
* @param size The size of the buffer in bytes.
|
2021-08-14 13:47:05 +08:00
|
|
|
*/
|
2012-11-22 16:43:40 +08:00
|
|
|
void rt_ringbuffer_init(struct rt_ringbuffer *rb,
|
|
|
|
rt_uint8_t *pool,
|
2013-05-08 15:45:31 +08:00
|
|
|
rt_int16_t size)
|
2012-11-22 16:43:40 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(rb != RT_NULL);
|
2017-10-15 22:56:46 +08:00
|
|
|
RT_ASSERT(size > 0);
|
2012-11-22 16:43:40 +08:00
|
|
|
|
|
|
|
/* initialize read and write index */
|
2013-05-08 15:45:31 +08:00
|
|
|
rb->read_mirror = rb->read_index = 0;
|
|
|
|
rb->write_mirror = rb->write_index = 0;
|
2012-11-22 16:43:40 +08:00
|
|
|
|
|
|
|
/* set buffer pool and size */
|
|
|
|
rb->buffer_ptr = pool;
|
|
|
|
rb->buffer_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_ringbuffer_init);
|
|
|
|
|
2013-08-19 11:52:04 +08:00
|
|
|
/**
|
2021-08-18 09:44:52 +08:00
|
|
|
* @brief Put a block of data into the ring buffer. If the capacity of ring buffer is insufficient, it will discard out-of-range data.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-17 22:44:29 +08:00
|
|
|
* @param rb A pointer to the ring buffer object.
|
2021-08-14 13:47:05 +08:00
|
|
|
* @param ptr A pointer to the data buffer.
|
|
|
|
* @param length The size of data in bytes.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-18 09:44:52 +08:00
|
|
|
* @return Return the data size we put into the ring buffer.
|
2013-08-19 11:52:04 +08:00
|
|
|
*/
|
2012-11-22 16:43:40 +08:00
|
|
|
rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
|
|
|
|
const rt_uint8_t *ptr,
|
|
|
|
rt_uint16_t length)
|
|
|
|
{
|
|
|
|
rt_uint16_t size;
|
2012-12-26 08:29:42 +08:00
|
|
|
|
2012-11-22 16:43:40 +08:00
|
|
|
RT_ASSERT(rb != RT_NULL);
|
|
|
|
|
|
|
|
/* whether has enough space */
|
2013-08-19 10:50:20 +08:00
|
|
|
size = rt_ringbuffer_space_len(rb);
|
2012-11-22 16:43:40 +08:00
|
|
|
|
|
|
|
/* no space */
|
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
2013-05-08 15:45:31 +08:00
|
|
|
|
2012-11-22 16:43:40 +08:00
|
|
|
/* drop some data */
|
|
|
|
if (size < length)
|
|
|
|
length = size;
|
|
|
|
|
2013-05-08 15:45:31 +08:00
|
|
|
if (rb->buffer_size - rb->write_index > length)
|
2012-11-22 16:43:40 +08:00
|
|
|
{
|
|
|
|
/* read_index - write_index = empty space */
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
|
2013-05-08 15:45:31 +08:00
|
|
|
/* this should not cause overflow because there is enough space for
|
|
|
|
* length of data in current mirror */
|
|
|
|
rb->write_index += length;
|
|
|
|
return length;
|
2012-11-22 16:43:40 +08:00
|
|
|
}
|
2013-05-08 15:45:31 +08:00
|
|
|
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memcpy(&rb->buffer_ptr[rb->write_index],
|
2022-07-25 10:21:18 +08:00
|
|
|
&ptr[0],
|
|
|
|
rb->buffer_size - rb->write_index);
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memcpy(&rb->buffer_ptr[0],
|
2022-07-25 10:21:18 +08:00
|
|
|
&ptr[rb->buffer_size - rb->write_index],
|
|
|
|
length - (rb->buffer_size - rb->write_index));
|
2013-05-08 15:45:31 +08:00
|
|
|
|
|
|
|
/* we are going into the other side of the mirror */
|
|
|
|
rb->write_mirror = ~rb->write_mirror;
|
|
|
|
rb->write_index = length - (rb->buffer_size - rb->write_index);
|
2012-11-22 16:43:40 +08:00
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_ringbuffer_put);
|
|
|
|
|
2013-08-19 11:52:04 +08:00
|
|
|
/**
|
2021-08-18 09:44:52 +08:00
|
|
|
* @brief Put a block of data into the ring buffer. If the capacity of ring buffer is insufficient, it will overwrite the existing data in the ring buffer.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-17 22:44:29 +08:00
|
|
|
* @param rb A pointer to the ring buffer object.
|
2021-08-14 13:47:05 +08:00
|
|
|
* @param ptr A pointer to the data buffer.
|
|
|
|
* @param length The size of data in bytes.
|
2013-08-19 11:52:04 +08:00
|
|
|
*
|
2021-08-18 09:44:52 +08:00
|
|
|
* @return Return the data size we put into the ring buffer.
|
2013-08-19 11:52:04 +08:00
|
|
|
*/
|
|
|
|
rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb,
|
2022-07-25 10:21:18 +08:00
|
|
|
const rt_uint8_t *ptr,
|
|
|
|
rt_uint16_t length)
|
2013-08-19 11:52:04 +08:00
|
|
|
{
|
2015-01-23 13:27:52 +08:00
|
|
|
rt_uint16_t space_length;
|
2013-08-19 11:52:04 +08:00
|
|
|
|
|
|
|
RT_ASSERT(rb != RT_NULL);
|
|
|
|
|
2015-01-23 13:27:52 +08:00
|
|
|
space_length = rt_ringbuffer_space_len(rb);
|
2013-08-19 11:52:04 +08:00
|
|
|
|
2018-03-25 07:54:25 +08:00
|
|
|
if (length > rb->buffer_size)
|
|
|
|
{
|
|
|
|
ptr = &ptr[length - rb->buffer_size];
|
2013-08-19 11:52:04 +08:00
|
|
|
length = rb->buffer_size;
|
2018-03-25 07:54:25 +08:00
|
|
|
}
|
2013-08-19 11:52:04 +08:00
|
|
|
|
|
|
|
if (rb->buffer_size - rb->write_index > length)
|
|
|
|
{
|
|
|
|
/* read_index - write_index = empty space */
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
|
2013-08-19 11:52:04 +08:00
|
|
|
/* this should not cause overflow because there is enough space for
|
|
|
|
* length of data in current mirror */
|
|
|
|
rb->write_index += length;
|
|
|
|
|
2015-01-23 13:27:52 +08:00
|
|
|
if (length > space_length)
|
2013-08-19 11:52:04 +08:00
|
|
|
rb->read_index = rb->write_index;
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memcpy(&rb->buffer_ptr[rb->write_index],
|
2022-07-25 10:21:18 +08:00
|
|
|
&ptr[0],
|
|
|
|
rb->buffer_size - rb->write_index);
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memcpy(&rb->buffer_ptr[0],
|
2022-07-25 10:21:18 +08:00
|
|
|
&ptr[rb->buffer_size - rb->write_index],
|
|
|
|
length - (rb->buffer_size - rb->write_index));
|
2013-08-19 11:52:04 +08:00
|
|
|
|
|
|
|
/* we are going into the other side of the mirror */
|
|
|
|
rb->write_mirror = ~rb->write_mirror;
|
|
|
|
rb->write_index = length - (rb->buffer_size - rb->write_index);
|
|
|
|
|
2015-01-23 13:27:52 +08:00
|
|
|
if (length > space_length)
|
2013-08-19 11:52:04 +08:00
|
|
|
{
|
2021-07-20 15:47:03 +08:00
|
|
|
if (rb->write_index <= rb->read_index)
|
|
|
|
rb->read_mirror = ~rb->read_mirror;
|
2013-08-19 11:52:04 +08:00
|
|
|
rb->read_index = rb->write_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_ringbuffer_put_force);
|
|
|
|
|
2012-11-22 16:43:40 +08:00
|
|
|
/**
|
2021-08-17 22:44:29 +08:00
|
|
|
* @brief Get data from the ring buffer.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-17 22:44:29 +08:00
|
|
|
* @param rb A pointer to the ring buffer.
|
2021-08-14 13:47:05 +08:00
|
|
|
* @param ptr A pointer to the data buffer.
|
2021-08-18 09:44:52 +08:00
|
|
|
* @param length The size of the data we want to read from the ring buffer.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-18 09:44:52 +08:00
|
|
|
* @return Return the data size we read from the ring buffer.
|
2012-11-22 16:43:40 +08:00
|
|
|
*/
|
|
|
|
rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
|
|
|
|
rt_uint8_t *ptr,
|
|
|
|
rt_uint16_t length)
|
|
|
|
{
|
|
|
|
rt_size_t size;
|
|
|
|
|
|
|
|
RT_ASSERT(rb != RT_NULL);
|
2013-05-08 15:45:31 +08:00
|
|
|
|
2012-11-22 16:43:40 +08:00
|
|
|
/* whether has enough data */
|
2013-08-19 10:50:20 +08:00
|
|
|
size = rt_ringbuffer_data_len(rb);
|
2012-11-22 16:43:40 +08:00
|
|
|
|
|
|
|
/* no data */
|
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
2013-05-08 15:45:31 +08:00
|
|
|
|
2012-11-22 16:43:40 +08:00
|
|
|
/* less data */
|
|
|
|
if (size < length)
|
2022-09-20 22:31:11 +08:00
|
|
|
length = (rt_uint16_t)size;
|
2012-11-22 16:43:40 +08:00
|
|
|
|
2013-05-08 15:45:31 +08:00
|
|
|
if (rb->buffer_size - rb->read_index > length)
|
2012-11-22 16:43:40 +08:00
|
|
|
{
|
|
|
|
/* copy all of data */
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memcpy(ptr, &rb->buffer_ptr[rb->read_index], length);
|
2013-05-08 15:45:31 +08:00
|
|
|
/* this should not cause overflow because there is enough space for
|
|
|
|
* length of data in current mirror */
|
|
|
|
rb->read_index += length;
|
|
|
|
return length;
|
2012-11-22 16:43:40 +08:00
|
|
|
}
|
2013-05-08 15:45:31 +08:00
|
|
|
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memcpy(&ptr[0],
|
2022-07-25 10:21:18 +08:00
|
|
|
&rb->buffer_ptr[rb->read_index],
|
|
|
|
rb->buffer_size - rb->read_index);
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memcpy(&ptr[rb->buffer_size - rb->read_index],
|
2022-07-25 10:21:18 +08:00
|
|
|
&rb->buffer_ptr[0],
|
|
|
|
length - (rb->buffer_size - rb->read_index));
|
2013-05-08 15:45:31 +08:00
|
|
|
|
|
|
|
/* we are going into the other side of the mirror */
|
|
|
|
rb->read_mirror = ~rb->read_mirror;
|
|
|
|
rb->read_index = length - (rb->buffer_size - rb->read_index);
|
2012-11-22 16:43:40 +08:00
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_ringbuffer_get);
|
|
|
|
|
2020-11-22 14:20:02 +08:00
|
|
|
/**
|
2021-08-18 09:44:52 +08:00
|
|
|
* @brief Get the first readable byte of the ring buffer.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-14 13:47:05 +08:00
|
|
|
* @param rb A pointer to the ringbuffer.
|
2021-08-17 22:44:29 +08:00
|
|
|
* @param ptr When this function return, *ptr is a pointer to the first readable byte of the ring buffer.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-18 09:44:52 +08:00
|
|
|
* @note It is recommended to read only one byte, otherwise it may cause buffer overflow.
|
|
|
|
*
|
|
|
|
* @return Return the size of the ring buffer.
|
2020-11-22 14:20:02 +08:00
|
|
|
*/
|
2022-06-17 18:46:46 +08:00
|
|
|
rt_size_t rt_ringbuffer_peek(struct rt_ringbuffer *rb, rt_uint8_t **ptr)
|
2020-11-22 14:20:02 +08:00
|
|
|
{
|
2022-03-23 14:28:24 +08:00
|
|
|
rt_size_t size;
|
|
|
|
|
2020-11-22 14:20:02 +08:00
|
|
|
RT_ASSERT(rb != RT_NULL);
|
|
|
|
|
|
|
|
*ptr = RT_NULL;
|
|
|
|
|
|
|
|
/* whether has enough data */
|
2022-03-23 14:28:24 +08:00
|
|
|
size = rt_ringbuffer_data_len(rb);
|
2020-11-22 14:20:02 +08:00
|
|
|
|
|
|
|
/* no data */
|
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*ptr = &rb->buffer_ptr[rb->read_index];
|
|
|
|
|
2022-07-25 10:21:18 +08:00
|
|
|
if ((rt_size_t)(rb->buffer_size - rb->read_index) > size)
|
2020-11-22 14:20:02 +08:00
|
|
|
{
|
2022-09-20 22:31:11 +08:00
|
|
|
rb->read_index += (rt_uint16_t)size;
|
2020-11-22 14:20:02 +08:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = rb->buffer_size - rb->read_index;
|
|
|
|
|
|
|
|
/* we are going into the other side of the mirror */
|
|
|
|
rb->read_mirror = ~rb->read_mirror;
|
|
|
|
rb->read_index = 0;
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
2022-06-17 18:46:46 +08:00
|
|
|
RTM_EXPORT(rt_ringbuffer_peek);
|
2020-11-22 14:20:02 +08:00
|
|
|
|
2013-05-08 15:54:30 +08:00
|
|
|
/**
|
2021-08-17 22:44:29 +08:00
|
|
|
* @brief Put a byte into the ring buffer. If ring buffer is full, this operation will fail.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-17 22:44:29 +08:00
|
|
|
* @param rb A pointer to the ring buffer object.
|
2021-08-18 09:44:52 +08:00
|
|
|
* @param ch A byte put into the ring buffer.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-18 09:44:52 +08:00
|
|
|
* @return Return the data size we put into the ring buffer. The ring buffer is full if returns 0. Otherwise, it will return 1.
|
2013-05-08 15:54:30 +08:00
|
|
|
*/
|
|
|
|
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch)
|
|
|
|
{
|
|
|
|
RT_ASSERT(rb != RT_NULL);
|
|
|
|
|
|
|
|
/* whether has enough space */
|
2013-08-19 10:50:20 +08:00
|
|
|
if (!rt_ringbuffer_space_len(rb))
|
2013-05-08 15:54:30 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
rb->buffer_ptr[rb->write_index] = ch;
|
|
|
|
|
|
|
|
/* flip mirror */
|
2022-07-25 10:21:18 +08:00
|
|
|
if (rb->write_index == rb->buffer_size - 1)
|
2013-05-08 15:54:30 +08:00
|
|
|
{
|
|
|
|
rb->write_mirror = ~rb->write_mirror;
|
|
|
|
rb->write_index = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rb->write_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_ringbuffer_putchar);
|
|
|
|
|
2013-08-19 11:52:04 +08:00
|
|
|
/**
|
2021-08-18 09:44:52 +08:00
|
|
|
* @brief Put a byte into the ring buffer. If ring buffer is full, it will discard an old data and put into a new data.
|
2013-08-19 11:52:04 +08:00
|
|
|
*
|
2021-08-17 22:44:29 +08:00
|
|
|
* @param rb A pointer to the ring buffer object.
|
2021-08-18 09:44:52 +08:00
|
|
|
* @param ch A byte put into the ring buffer.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-18 09:44:52 +08:00
|
|
|
* @return Return the data size we put into the ring buffer. Always return 1.
|
2013-08-19 11:52:04 +08:00
|
|
|
*/
|
|
|
|
rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch)
|
|
|
|
{
|
|
|
|
enum rt_ringbuffer_state old_state;
|
|
|
|
|
|
|
|
RT_ASSERT(rb != RT_NULL);
|
|
|
|
|
|
|
|
old_state = rt_ringbuffer_status(rb);
|
|
|
|
|
|
|
|
rb->buffer_ptr[rb->write_index] = ch;
|
|
|
|
|
|
|
|
/* flip mirror */
|
2022-07-25 10:21:18 +08:00
|
|
|
if (rb->write_index == rb->buffer_size - 1)
|
2013-08-19 11:52:04 +08:00
|
|
|
{
|
|
|
|
rb->write_mirror = ~rb->write_mirror;
|
|
|
|
rb->write_index = 0;
|
|
|
|
if (old_state == RT_RINGBUFFER_FULL)
|
|
|
|
{
|
|
|
|
rb->read_mirror = ~rb->read_mirror;
|
|
|
|
rb->read_index = rb->write_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rb->write_index++;
|
|
|
|
if (old_state == RT_RINGBUFFER_FULL)
|
|
|
|
rb->read_index = rb->write_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_ringbuffer_putchar_force);
|
|
|
|
|
2012-11-22 16:43:40 +08:00
|
|
|
/**
|
2021-08-17 22:44:29 +08:00
|
|
|
* @brief Get a byte from the ring buffer.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-18 09:44:52 +08:00
|
|
|
* @param rb The pointer to the ring buffer object.
|
|
|
|
* @param ch A pointer to the buffer, used to store one byte.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-18 09:44:52 +08:00
|
|
|
* @return 0 The ring buffer is empty.
|
2021-08-14 13:47:05 +08:00
|
|
|
* @return 1 Success
|
2012-11-22 16:43:40 +08:00
|
|
|
*/
|
|
|
|
rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch)
|
|
|
|
{
|
|
|
|
RT_ASSERT(rb != RT_NULL);
|
2013-05-08 15:45:31 +08:00
|
|
|
|
2012-11-22 16:43:40 +08:00
|
|
|
/* ringbuffer is empty */
|
2013-08-19 10:50:20 +08:00
|
|
|
if (!rt_ringbuffer_data_len(rb))
|
2012-11-22 16:43:40 +08:00
|
|
|
return 0;
|
|
|
|
|
2021-08-17 22:44:29 +08:00
|
|
|
/* put byte */
|
2013-05-08 15:45:31 +08:00
|
|
|
*ch = rb->buffer_ptr[rb->read_index];
|
|
|
|
|
2022-07-25 10:21:18 +08:00
|
|
|
if (rb->read_index == rb->buffer_size - 1)
|
2013-05-08 15:45:31 +08:00
|
|
|
{
|
|
|
|
rb->read_mirror = ~rb->read_mirror;
|
|
|
|
rb->read_index = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rb->read_index++;
|
|
|
|
}
|
2012-11-22 16:43:40 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_ringbuffer_getchar);
|
2013-05-08 15:54:30 +08:00
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
/**
|
2021-08-17 22:44:29 +08:00
|
|
|
* @brief Get the size of data in the ring buffer in bytes.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-18 09:44:52 +08:00
|
|
|
* @param rb The pointer to the ring buffer object.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-17 22:44:29 +08:00
|
|
|
* @return Return the size of data in the ring buffer in bytes.
|
2017-10-15 22:56:46 +08:00
|
|
|
*/
|
|
|
|
rt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb)
|
|
|
|
{
|
|
|
|
switch (rt_ringbuffer_status(rb))
|
|
|
|
{
|
|
|
|
case RT_RINGBUFFER_EMPTY:
|
|
|
|
return 0;
|
|
|
|
case RT_RINGBUFFER_FULL:
|
|
|
|
return rb->buffer_size;
|
|
|
|
case RT_RINGBUFFER_HALFFULL:
|
|
|
|
default:
|
2021-07-31 01:37:53 +08:00
|
|
|
{
|
|
|
|
rt_size_t wi = rb->write_index, ri = rb->read_index;
|
|
|
|
|
|
|
|
if (wi > ri)
|
|
|
|
return wi - ri;
|
2017-10-15 22:56:46 +08:00
|
|
|
else
|
2021-07-31 01:37:53 +08:00
|
|
|
return rb->buffer_size - (ri - wi);
|
|
|
|
}
|
|
|
|
}
|
2017-10-15 22:56:46 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_ringbuffer_data_len);
|
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
/**
|
2021-08-17 22:44:29 +08:00
|
|
|
* @brief Reset the ring buffer object, and clear all contents in the buffer.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-17 22:44:29 +08:00
|
|
|
* @param rb A pointer to the ring buffer object.
|
2017-10-15 22:56:46 +08:00
|
|
|
*/
|
|
|
|
void rt_ringbuffer_reset(struct rt_ringbuffer *rb)
|
|
|
|
{
|
|
|
|
RT_ASSERT(rb != RT_NULL);
|
|
|
|
|
|
|
|
rb->read_mirror = 0;
|
|
|
|
rb->read_index = 0;
|
|
|
|
rb->write_mirror = 0;
|
|
|
|
rb->write_index = 0;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_ringbuffer_reset);
|
|
|
|
|
|
|
|
#ifdef RT_USING_HEAP
|
|
|
|
|
2021-08-14 13:47:05 +08:00
|
|
|
/**
|
2021-08-17 22:44:29 +08:00
|
|
|
* @brief Create a ring buffer object with a given size.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-18 09:44:52 +08:00
|
|
|
* @param size The size of the buffer in bytes.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-18 22:51:00 +08:00
|
|
|
* @return Return a pointer to ring buffer object. When the return value is RT_NULL, it means this creation failed.
|
2021-08-14 13:47:05 +08:00
|
|
|
*/
|
2021-08-14 14:32:58 +08:00
|
|
|
struct rt_ringbuffer *rt_ringbuffer_create(rt_uint16_t size)
|
2017-10-15 22:56:46 +08:00
|
|
|
{
|
|
|
|
struct rt_ringbuffer *rb;
|
|
|
|
rt_uint8_t *pool;
|
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
RT_ASSERT(size > 0);
|
2017-10-15 22:56:46 +08:00
|
|
|
|
|
|
|
size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
|
|
|
|
|
2019-06-18 20:09:19 +08:00
|
|
|
rb = (struct rt_ringbuffer *)rt_malloc(sizeof(struct rt_ringbuffer));
|
2017-10-15 22:56:46 +08:00
|
|
|
if (rb == RT_NULL)
|
|
|
|
goto exit;
|
|
|
|
|
2019-06-18 20:09:19 +08:00
|
|
|
pool = (rt_uint8_t *)rt_malloc(size);
|
2017-10-15 22:56:46 +08:00
|
|
|
if (pool == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_free(rb);
|
2019-01-21 21:59:39 +08:00
|
|
|
rb = RT_NULL;
|
2017-10-15 22:56:46 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
rt_ringbuffer_init(rb, pool, size);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return rb;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_ringbuffer_create);
|
|
|
|
|
2021-08-14 13:47:05 +08:00
|
|
|
/**
|
2021-08-18 09:44:52 +08:00
|
|
|
* @brief Destroy the ring buffer object, which is created by rt_ringbuffer_create() .
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-17 22:44:29 +08:00
|
|
|
* @param rb A pointer to the ring buffer object.
|
2021-08-14 13:47:05 +08:00
|
|
|
*/
|
2017-10-15 22:56:46 +08:00
|
|
|
void rt_ringbuffer_destroy(struct rt_ringbuffer *rb)
|
|
|
|
{
|
|
|
|
RT_ASSERT(rb != RT_NULL);
|
|
|
|
|
|
|
|
rt_free(rb->buffer_ptr);
|
|
|
|
rt_free(rb);
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_ringbuffer_destroy);
|
|
|
|
|
|
|
|
#endif
|