2012-04-23 14:02:28 +08:00
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rtdevice.h>
|
2012-07-02 23:51:58 +08:00
|
|
|
#include <string.h>
|
2012-04-23 14:02:28 +08:00
|
|
|
|
|
|
|
void rt_ringbuffer_init(struct rt_ringbuffer* rb, rt_uint8_t *pool, rt_uint16_t size)
|
|
|
|
{
|
|
|
|
RT_ASSERT(rb != RT_NULL);
|
|
|
|
|
|
|
|
/* initialize read and write index */
|
|
|
|
rb->read_index = rb->write_index = 0;
|
|
|
|
|
|
|
|
/* set buffer pool and size */
|
|
|
|
rb->buffer_ptr = pool;
|
2012-07-02 23:51:58 +08:00
|
|
|
rb->buffer_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
|
2012-04-23 14:02:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_size_t rt_ringbuffer_put(struct rt_ringbuffer* rb, const rt_uint8_t *ptr, rt_uint16_t length)
|
|
|
|
{
|
2012-07-02 23:51:58 +08:00
|
|
|
rt_uint16_t size;
|
|
|
|
rt_uint16_t mask;
|
2012-04-23 14:02:28 +08:00
|
|
|
|
|
|
|
RT_ASSERT(rb != RT_NULL);
|
|
|
|
|
2012-07-02 23:51:58 +08:00
|
|
|
mask = rb->buffer_size - 1;
|
2012-04-23 14:02:28 +08:00
|
|
|
/* whether has enough space */
|
2012-07-02 23:51:58 +08:00
|
|
|
size = rb->buffer_size - ((rb->write_index - rb->read_index) & mask);
|
2012-04-23 14:02:28 +08:00
|
|
|
|
|
|
|
/* no space */
|
|
|
|
if (size == 0) return 0;
|
|
|
|
/* drop some data */
|
|
|
|
if (size < length) length = size;
|
|
|
|
|
|
|
|
if (rb->read_index > rb->write_index)
|
|
|
|
{
|
|
|
|
/* read_index - write_index = empty space */
|
2012-07-02 23:51:58 +08:00
|
|
|
memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
|
2012-04-23 14:02:28 +08:00
|
|
|
rb->write_index += length;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-02 23:51:58 +08:00
|
|
|
if (rb->buffer_size - rb->write_index >= length)
|
2012-04-23 14:02:28 +08:00
|
|
|
{
|
|
|
|
/* there is enough space after write_index */
|
2012-07-02 23:51:58 +08:00
|
|
|
memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
|
|
|
|
rb->write_index = (rb->write_index + length) & mask;
|
2012-04-23 14:02:28 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-02 23:51:58 +08:00
|
|
|
memcpy(&rb->buffer_ptr[rb->write_index], ptr,
|
|
|
|
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));
|
2012-04-23 14:02:28 +08:00
|
|
|
rb->write_index = length - (rb->buffer_size - rb->write_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* put a character into ring buffer
|
|
|
|
*/
|
|
|
|
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer* rb, const rt_uint8_t ch)
|
|
|
|
{
|
|
|
|
rt_uint16_t next;
|
2012-07-02 23:51:58 +08:00
|
|
|
rt_uint16_t mask;
|
2012-04-23 14:02:28 +08:00
|
|
|
|
|
|
|
RT_ASSERT(rb != RT_NULL);
|
|
|
|
/* whether has enough space */
|
2012-07-02 23:51:58 +08:00
|
|
|
mask = rb->buffer_size - 1;
|
|
|
|
next = (rb->write_index + 1) & mask;
|
2012-04-23 14:02:28 +08:00
|
|
|
|
|
|
|
if (next == rb->read_index) return 0;
|
|
|
|
|
|
|
|
/* put character */
|
|
|
|
rb->buffer_ptr[rb->write_index] = ch;
|
|
|
|
rb->write_index = next;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2012-07-02 23:51:58 +08:00
|
|
|
rt_uint16_t mask;
|
2012-04-23 14:02:28 +08:00
|
|
|
|
|
|
|
RT_ASSERT(rb != RT_NULL);
|
|
|
|
/* whether has enough data */
|
2012-07-02 23:51:58 +08:00
|
|
|
mask = rb->buffer_size - 1;
|
|
|
|
size = (rb->write_index - rb->read_index) & mask;
|
2012-04-23 14:02:28 +08:00
|
|
|
|
|
|
|
/* no data */
|
|
|
|
if (size == 0) return 0;
|
|
|
|
/* less data */
|
|
|
|
if (size < length) length = size;
|
|
|
|
|
|
|
|
if (rb->read_index > rb->write_index)
|
|
|
|
{
|
2012-07-02 23:51:58 +08:00
|
|
|
if (rb->buffer_size - rb->read_index >= length)
|
2012-04-23 14:02:28 +08:00
|
|
|
{
|
|
|
|
/* copy directly */
|
2012-07-02 23:51:58 +08:00
|
|
|
memcpy(ptr, &rb->buffer_ptr[rb->read_index], length);
|
|
|
|
rb->read_index = (rb->read_index + length) & mask;
|
2012-04-23 14:02:28 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* copy first and second */
|
2012-07-02 23:51:58 +08:00
|
|
|
memcpy(ptr, &rb->buffer_ptr[rb->read_index],
|
2012-04-23 14:02:28 +08:00
|
|
|
rb->buffer_size - rb->read_index);
|
2012-07-02 23:51:58 +08:00
|
|
|
memcpy(&ptr[rb->buffer_size - rb->read_index], &rb->buffer_ptr[0],
|
2012-04-23 14:02:28 +08:00
|
|
|
length - rb->buffer_size + rb->read_index);
|
|
|
|
rb->read_index = length - rb->buffer_size + rb->read_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-02 23:51:58 +08:00
|
|
|
memcpy(ptr, &rb->buffer_ptr[rb->read_index], length);
|
2012-04-23 14:02:28 +08:00
|
|
|
rb->read_index += length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2012-07-02 23:51:58 +08:00
|
|
|
rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer* rb, rt_uint8_t *ch)
|
|
|
|
{
|
|
|
|
rt_uint16_t next;
|
|
|
|
rt_uint16_t mask;
|
|
|
|
|
|
|
|
RT_ASSERT(rb != RT_NULL);
|
|
|
|
|
|
|
|
/* ringbuffer is empty */
|
|
|
|
if (rb->read_index == rb->write_index) return 0;
|
|
|
|
|
|
|
|
/* whether has data */
|
|
|
|
mask = rb->buffer_size - 1;
|
|
|
|
next = (rb->read_index + 1) & mask;
|
|
|
|
|
|
|
|
/* put character */
|
|
|
|
*ch = rb->buffer_ptr[rb->read_index];
|
|
|
|
rb->read_index = next;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-04-23 14:02:28 +08:00
|
|
|
/**
|
|
|
|
* get available data size
|
|
|
|
*/
|
|
|
|
rt_size_t rt_ringbuffer_available_size(struct rt_ringbuffer* rb)
|
|
|
|
{
|
|
|
|
rt_size_t size;
|
2012-07-02 23:51:58 +08:00
|
|
|
rt_uint16_t mask;
|
2012-04-23 14:02:28 +08:00
|
|
|
|
|
|
|
RT_ASSERT(rb != RT_NULL);
|
2012-07-02 23:51:58 +08:00
|
|
|
|
|
|
|
mask = rb->buffer_size - 1;
|
|
|
|
size = (rb->write_index - rb->read_index) & mask;
|
2012-04-23 14:02:28 +08:00
|
|
|
|
|
|
|
/* return the available size */
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get empty space size
|
|
|
|
*/
|
|
|
|
rt_size_t rt_ringbuffer_emptry_size(struct rt_ringbuffer* rb)
|
|
|
|
{
|
|
|
|
RT_ASSERT(rb != RT_NULL);
|
|
|
|
|
|
|
|
return rb->buffer_size - rt_ringbuffer_available_size(rb);
|
|
|
|
}
|