diff options
| author | bernard <[email protected]> | 2017-10-15 22:56:46 +0800 |
|---|---|---|
| committer | bernard <[email protected]> | 2017-10-15 22:56:46 +0800 |
| commit | d7087fdd3b998192afb03b34fd8034071d06bde7 (patch) | |
| tree | 6a99d97408c4b3bc708b3a0601bff80907d8beac /components/drivers/src/ringbuffer.c | |
| parent | 0f5a68a55e2a3c44f1e85029d22de6180ad30390 (diff) | |
[DeviceDrivers] change cmd type.
1. Change 'rt_uint8_t' type of cmd to 'int';
2. Add waitqueue;
3. Split device ipc header files;
Diffstat (limited to 'components/drivers/src/ringbuffer.c')
| -rw-r--r-- | components/drivers/src/ringbuffer.c | 88 |
1 files changed, 87 insertions, 1 deletions
diff --git a/components/drivers/src/ringbuffer.c b/components/drivers/src/ringbuffer.c index 8c2705ceb1..baeeea0e50 100644 --- a/components/drivers/src/ringbuffer.c +++ b/components/drivers/src/ringbuffer.c @@ -21,18 +21,31 @@ * Date Author Notes * 2012-09-30 Bernard first version. * 2013-05-08 Grissiom reimplement + * 2016-08-18 heyuanjie add interface */ #include <rtthread.h> #include <rtdevice.h> #include <string.h> +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; +} + void rt_ringbuffer_init(struct rt_ringbuffer *rb, rt_uint8_t *pool, rt_int16_t size) { RT_ASSERT(rb != RT_NULL); - RT_ASSERT(size > 0) + RT_ASSERT(size > 0); /* initialize read and write index */ rb->read_mirror = rb->read_index = 0; @@ -284,3 +297,76 @@ rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch) } RTM_EXPORT(rt_ringbuffer_getchar); +/** + * 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 |
