2012-09-30 15:33:13 +08:00
|
|
|
/*
|
|
|
|
* File : ringbuffer.c
|
|
|
|
* This file is part of RT-Thread RTOS
|
|
|
|
* COPYRIGHT (C) 2012, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* The license and distribution terms for this file may be
|
|
|
|
* found in the file LICENSE in this distribution or at
|
|
|
|
* http://www.rt-thread.org/license/LICENSE
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2012-09-30 Bernard first version.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
}
|
2012-09-30 15:33:13 +08:00
|
|
|
RTM_EXPORT(rt_ringbuffer_init);
|
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-09-30 15:33:13 +08:00
|
|
|
rt_uint16_t write_position;
|
|
|
|
|
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-09-30 15:33:13 +08:00
|
|
|
size = rb->buffer_size - (rb->write_index - rb->read_index);
|
2012-04-23 14:02:28 +08:00
|
|
|
|
|
|
|
/* no space */
|
|
|
|
if (size == 0) return 0;
|
|
|
|
/* drop some data */
|
|
|
|
if (size < length) length = size;
|
|
|
|
|
2012-09-30 15:33:13 +08:00
|
|
|
write_position = (rb->write_index & mask);
|
|
|
|
if (rb->buffer_size - write_position> length)
|
2012-04-23 14:02:28 +08:00
|
|
|
{
|
|
|
|
/* read_index - write_index = empty space */
|
2012-09-30 15:33:13 +08:00
|
|
|
memcpy(&rb->buffer_ptr[write_position], ptr, length);
|
2012-04-23 14:02:28 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-30 15:33:13 +08:00
|
|
|
memcpy(&rb->buffer_ptr[write_position], ptr, rb->buffer_size - write_position);
|
|
|
|
memcpy(&rb->buffer_ptr[0], &ptr[rb->buffer_size - write_position],
|
|
|
|
length - (rb->buffer_size - write_position));
|
2012-04-23 14:02:28 +08:00
|
|
|
}
|
2012-09-30 15:33:13 +08:00
|
|
|
rb->write_index += length;
|
2012-04-23 14:02:28 +08:00
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
2012-09-30 15:33:13 +08:00
|
|
|
RTM_EXPORT(rt_ringbuffer_put);
|
2012-04-23 14:02:28 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* put a character into ring buffer
|
|
|
|
*/
|
|
|
|
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer* rb, const rt_uint8_t ch)
|
|
|
|
{
|
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;
|
2012-09-30 15:33:13 +08:00
|
|
|
|
|
|
|
/* whether has enough space */
|
|
|
|
if (rb->write_index - rb->read_index == rb->buffer_size) return 0;
|
2012-04-23 14:02:28 +08:00
|
|
|
|
|
|
|
/* put character */
|
2012-09-30 15:33:13 +08:00
|
|
|
rb->buffer_ptr[rb->write_index & mask] = ch;
|
|
|
|
rb->write_index += 1;
|
2012-04-23 14:02:28 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2012-09-30 15:33:13 +08:00
|
|
|
RTM_EXPORT(rt_ringbuffer_putchar);
|
2012-04-23 14:02:28 +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;
|
2012-07-02 23:51:58 +08:00
|
|
|
rt_uint16_t mask;
|
2012-09-30 15:33:13 +08:00
|
|
|
rt_uint16_t read_position;
|
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;
|
2012-09-30 15:33:13 +08:00
|
|
|
size = rb->write_index - rb->read_index;
|
2012-04-23 14:02:28 +08:00
|
|
|
|
|
|
|
/* no data */
|
|
|
|
if (size == 0) return 0;
|
|
|
|
/* less data */
|
|
|
|
if (size < length) length = size;
|
|
|
|
|
2012-09-30 15:33:13 +08:00
|
|
|
read_position = rb->read_index & mask;
|
|
|
|
if (rb->buffer_size - read_position >= length)
|
2012-04-23 14:02:28 +08:00
|
|
|
{
|
2012-09-30 15:33:13 +08:00
|
|
|
/* copy all of data */
|
|
|
|
memcpy(ptr, &rb->buffer_ptr[read_position], length);
|
2012-04-23 14:02:28 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-30 15:33:13 +08:00
|
|
|
/* copy first and second */
|
|
|
|
memcpy(ptr, &rb->buffer_ptr[read_position], rb->buffer_size - read_position);
|
|
|
|
memcpy(&ptr[rb->buffer_size - read_position], &rb->buffer_ptr[0],
|
|
|
|
length - rb->buffer_size + read_position);
|
2012-04-23 14:02:28 +08:00
|
|
|
}
|
2012-09-30 15:33:13 +08:00
|
|
|
rb->read_index += length;
|
2012-04-23 14:02:28 +08:00
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
2012-09-30 15:33:13 +08:00
|
|
|
RTM_EXPORT(rt_ringbuffer_get);
|
2012-04-23 14:02:28 +08:00
|
|
|
|
2012-09-30 15:33:13 +08:00
|
|
|
/**
|
|
|
|
* get a character from a ringbuffer
|
|
|
|
*/
|
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 mask;
|
|
|
|
|
|
|
|
RT_ASSERT(rb != RT_NULL);
|
|
|
|
|
|
|
|
/* ringbuffer is empty */
|
|
|
|
if (rb->read_index == rb->write_index) return 0;
|
|
|
|
|
|
|
|
mask = rb->buffer_size - 1;
|
|
|
|
|
|
|
|
/* put character */
|
2012-09-30 15:33:13 +08:00
|
|
|
*ch = rb->buffer_ptr[rb->read_index & mask];
|
|
|
|
rb->read_index += 1;
|
2012-07-02 23:51:58 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2012-09-30 15:33:13 +08:00
|
|
|
RTM_EXPORT(rt_ringbuffer_getchar);
|
2012-07-02 23:51:58 +08:00
|
|
|
|