2012-11-22 16:43:40 +08:00
|
|
|
/*
|
|
|
|
* File : ringbuffer.c
|
|
|
|
* This file is part of RT-Thread RTOS
|
|
|
|
* COPYRIGHT (C) 2012, RT-Thread Development Team
|
|
|
|
*
|
2013-06-28 00:36:54 +08:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
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
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* put a block of data into ring buffer
|
|
|
|
*/
|
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 */
|
2013-05-08 15:45:31 +08:00
|
|
|
memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
|
|
|
|
/* 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
|
|
|
|
|
|
|
memcpy(&rb->buffer_ptr[rb->write_index],
|
|
|
|
&ptr[0],
|
|
|
|
rb->buffer_size - rb->write_index);
|
|
|
|
memcpy(&rb->buffer_ptr[0],
|
|
|
|
&ptr[rb->buffer_size - rb->write_index],
|
|
|
|
length - (rb->buffer_size - rb->write_index));
|
|
|
|
|
|
|
|
/* 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
|
|
|
/**
|
|
|
|
* put a block of data into ring buffer
|
|
|
|
*
|
|
|
|
* When the buffer is full, it will discard the old data.
|
|
|
|
*/
|
|
|
|
rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb,
|
|
|
|
const rt_uint8_t *ptr,
|
|
|
|
rt_uint16_t length)
|
|
|
|
{
|
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
|
|
|
|
2015-01-23 13:27:52 +08:00
|
|
|
if (length > space_length)
|
2013-08-19 11:52:04 +08:00
|
|
|
length = rb->buffer_size;
|
|
|
|
|
|
|
|
if (rb->buffer_size - rb->write_index > length)
|
|
|
|
{
|
|
|
|
/* read_index - write_index = empty space */
|
|
|
|
memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&rb->buffer_ptr[rb->write_index],
|
|
|
|
&ptr[0],
|
|
|
|
rb->buffer_size - rb->write_index);
|
|
|
|
memcpy(&rb->buffer_ptr[0],
|
|
|
|
&ptr[rb->buffer_size - rb->write_index],
|
|
|
|
length - (rb->buffer_size - rb->write_index));
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
|
|
|
rb->read_mirror = ~rb->read_mirror;
|
|
|
|
rb->read_index = rb->write_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_ringbuffer_put_force);
|
|
|
|
|
2012-11-22 16:43:40 +08:00
|
|
|
/**
|
|
|
|
* get data from ring buffer
|
|
|
|
*/
|
|
|
|
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)
|
|
|
|
length = size;
|
|
|
|
|
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 */
|
2013-05-08 15:45:31 +08:00
|
|
|
memcpy(ptr, &rb->buffer_ptr[rb->read_index], length);
|
|
|
|
/* 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
|
|
|
|
|
|
|
memcpy(&ptr[0],
|
|
|
|
&rb->buffer_ptr[rb->read_index],
|
|
|
|
rb->buffer_size - rb->read_index);
|
|
|
|
memcpy(&ptr[rb->buffer_size - rb->read_index],
|
|
|
|
&rb->buffer_ptr[0],
|
|
|
|
length - (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 = length - (rb->buffer_size - rb->read_index);
|
2012-11-22 16:43:40 +08:00
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_ringbuffer_get);
|
|
|
|
|
2013-05-08 15:54:30 +08:00
|
|
|
/**
|
|
|
|
* put a character into ring buffer
|
|
|
|
*/
|
|
|
|
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 */
|
|
|
|
if (rb->write_index == rb->buffer_size-1)
|
|
|
|
{
|
|
|
|
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
|
|
|
/**
|
|
|
|
* put a character into ring buffer
|
|
|
|
*
|
|
|
|
* When the buffer is full, it will discard one old data.
|
|
|
|
*/
|
|
|
|
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 */
|
|
|
|
if (rb->write_index == rb->buffer_size-1)
|
|
|
|
{
|
|
|
|
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
|
|
|
/**
|
|
|
|
* get a character from a ringbuffer
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* put character */
|
2013-05-08 15:45:31 +08:00
|
|
|
*ch = rb->buffer_ptr[rb->read_index];
|
|
|
|
|
|
|
|
if (rb->read_index == rb->buffer_size-1)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
/**
|
|
|
|
* get the size of data in rb
|
|
|
|
*/
|
|
|
|
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:
|
|
|
|
if (rb->write_index > rb->read_index)
|
|
|
|
return rb->write_index - rb->read_index;
|
|
|
|
else
|
|
|
|
return rb->buffer_size - (rb->read_index - rb->write_index);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_ringbuffer_data_len);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* empty the rb
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
|
struct rt_ringbuffer* rt_ringbuffer_create(rt_uint16_t size)
|
|
|
|
{
|
|
|
|
struct rt_ringbuffer *rb;
|
|
|
|
rt_uint8_t *pool;
|
|
|
|
|
|
|
|
RT_ASSERT(size > 0);
|
|
|
|
|
|
|
|
size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
|
|
|
|
|
|
|
|
rb = rt_malloc(sizeof(struct rt_ringbuffer));
|
|
|
|
if (rb == RT_NULL)
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
pool = rt_malloc(size);
|
|
|
|
if (pool == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_free(rb);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
rt_ringbuffer_init(rb, pool, size);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return rb;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(rt_ringbuffer_create);
|
|
|
|
|
|
|
|
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
|