fixed coding style in component/drivers/src
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2384 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
parent
5d710e8862
commit
1aab195a7d
|
@ -16,108 +16,110 @@
|
||||||
#include <rtthread.h>
|
#include <rtthread.h>
|
||||||
#include <rtdevice.h>
|
#include <rtdevice.h>
|
||||||
|
|
||||||
#define RT_COMPLETED 1
|
#define RT_COMPLETED 1
|
||||||
#define RT_UNCOMPLETED 0
|
#define RT_UNCOMPLETED 0
|
||||||
|
|
||||||
void rt_completion_init(struct rt_completion* completion)
|
void rt_completion_init(struct rt_completion *completion)
|
||||||
{
|
{
|
||||||
rt_base_t level;
|
rt_base_t level;
|
||||||
RT_ASSERT(completion != RT_NULL);
|
RT_ASSERT(completion != RT_NULL);
|
||||||
|
|
||||||
level = rt_hw_interrupt_disable();
|
level = rt_hw_interrupt_disable();
|
||||||
completion->flag = RT_UNCOMPLETED;
|
completion->flag = RT_UNCOMPLETED;
|
||||||
rt_list_init(&completion->suspended_list);
|
rt_list_init(&completion->suspended_list);
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
}
|
}
|
||||||
|
|
||||||
rt_err_t rt_completion_wait(struct rt_completion* completion, rt_int32_t timeout)
|
rt_err_t rt_completion_wait(struct rt_completion *completion,
|
||||||
|
rt_int32_t timeout)
|
||||||
{
|
{
|
||||||
rt_err_t result;
|
rt_err_t result;
|
||||||
rt_base_t level;
|
rt_base_t level;
|
||||||
rt_thread_t thread;
|
rt_thread_t thread;
|
||||||
RT_ASSERT(completion != RT_NULL);
|
RT_ASSERT(completion != RT_NULL);
|
||||||
|
|
||||||
result = RT_EOK;
|
result = RT_EOK;
|
||||||
thread = rt_thread_self();
|
thread = rt_thread_self();
|
||||||
|
|
||||||
level = rt_hw_interrupt_disable();
|
level = rt_hw_interrupt_disable();
|
||||||
if (completion->flag != RT_COMPLETED)
|
if (completion->flag != RT_COMPLETED)
|
||||||
{
|
{
|
||||||
/* only one thread can suspend on complete */
|
/* only one thread can suspend on complete */
|
||||||
RT_ASSERT(rt_list_isempty(&(completion->suspended_list)));
|
RT_ASSERT(rt_list_isempty(&(completion->suspended_list)));
|
||||||
|
|
||||||
if (timeout == 0)
|
if (timeout == 0)
|
||||||
{
|
{
|
||||||
result = -RT_ETIMEOUT;
|
result = -RT_ETIMEOUT;
|
||||||
goto __exit;
|
goto __exit;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* reset thread error number */
|
/* reset thread error number */
|
||||||
thread->error = RT_EOK;
|
thread->error = RT_EOK;
|
||||||
|
|
||||||
/* suspend thread */
|
/* suspend thread */
|
||||||
rt_thread_suspend(thread);
|
rt_thread_suspend(thread);
|
||||||
/* add to suspended list */
|
/* add to suspended list */
|
||||||
rt_list_insert_before(&(completion->suspended_list), &(thread->tlist));
|
rt_list_insert_before(&(completion->suspended_list), &(thread->tlist));
|
||||||
|
|
||||||
/* current context checking */
|
/* current context checking */
|
||||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||||
|
|
||||||
/* start timer */
|
/* start timer */
|
||||||
if (timeout > 0)
|
if (timeout > 0)
|
||||||
{
|
{
|
||||||
/* reset the timeout of thread timer and start it */
|
/* reset the timeout of thread timer and start it */
|
||||||
rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
|
rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
|
||||||
rt_timer_start(&(thread->thread_timer));
|
rt_timer_start(&(thread->thread_timer));
|
||||||
}
|
}
|
||||||
/* enable interrupt */
|
/* enable interrupt */
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
|
|
||||||
/* do schedule */
|
/* do schedule */
|
||||||
rt_schedule();
|
rt_schedule();
|
||||||
|
|
||||||
/* thread is waked up */
|
/* thread is waked up */
|
||||||
result = thread->error;
|
result = thread->error;
|
||||||
|
|
||||||
level = rt_hw_interrupt_disable();
|
level = rt_hw_interrupt_disable();
|
||||||
/* clean completed flag */
|
/* clean completed flag */
|
||||||
completion->flag = RT_UNCOMPLETED;
|
completion->flag = RT_UNCOMPLETED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
__exit:
|
__exit:
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void rt_completion_done(struct rt_completion* completion)
|
void rt_completion_done(struct rt_completion *completion)
|
||||||
{
|
{
|
||||||
rt_base_t level;
|
rt_base_t level;
|
||||||
RT_ASSERT(completion != RT_NULL);
|
RT_ASSERT(completion != RT_NULL);
|
||||||
|
|
||||||
if(completion->flag == RT_COMPLETED) return;
|
if (completion->flag == RT_COMPLETED)
|
||||||
|
return;
|
||||||
|
|
||||||
level = rt_hw_interrupt_disable();
|
level = rt_hw_interrupt_disable();
|
||||||
completion->flag = RT_COMPLETED;
|
completion->flag = RT_COMPLETED;
|
||||||
|
|
||||||
if (!rt_list_isempty(&(completion->suspended_list)))
|
if (!rt_list_isempty(&(completion->suspended_list)))
|
||||||
{
|
{
|
||||||
/* there is one thread in suspended list */
|
/* there is one thread in suspended list */
|
||||||
struct rt_thread *thread;
|
struct rt_thread *thread;
|
||||||
|
|
||||||
/* get thread entry */
|
/* get thread entry */
|
||||||
thread = rt_list_entry(completion->suspended_list.next, struct rt_thread, tlist);
|
thread = rt_list_entry(completion->suspended_list.next, struct rt_thread, tlist);
|
||||||
|
|
||||||
/* resume it */
|
/* resume it */
|
||||||
rt_thread_resume(thread);
|
rt_thread_resume(thread);
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
|
|
||||||
/* perform a schedule */
|
/* perform a schedule */
|
||||||
rt_schedule();
|
rt_schedule();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,186 +11,195 @@
|
||||||
* Date Author Notes
|
* Date Author Notes
|
||||||
* 2012-09-30 Bernard first version.
|
* 2012-09-30 Bernard first version.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <rthw.h>
|
#include <rthw.h>
|
||||||
#include <rtthread.h>
|
#include <rtthread.h>
|
||||||
#include <rtdevice.h>
|
#include <rtdevice.h>
|
||||||
|
|
||||||
static rt_size_t rt_pipe_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
|
static rt_size_t rt_pipe_read(rt_device_t dev,
|
||||||
|
rt_off_t pos,
|
||||||
|
void *buffer,
|
||||||
|
rt_size_t size)
|
||||||
{
|
{
|
||||||
rt_uint32_t level;
|
rt_uint32_t level;
|
||||||
rt_thread_t thread;
|
rt_thread_t thread;
|
||||||
struct rt_pipe_device *pipe;
|
struct rt_pipe_device *pipe;
|
||||||
rt_size_t read_nbytes;
|
rt_size_t read_nbytes;
|
||||||
|
|
||||||
pipe = PIPE_DEVICE(dev);
|
pipe = PIPE_DEVICE(dev);
|
||||||
RT_ASSERT(pipe != RT_NULL);
|
RT_ASSERT(pipe != RT_NULL);
|
||||||
|
|
||||||
thread = rt_thread_self();
|
thread = rt_thread_self();
|
||||||
|
|
||||||
/* current context checking */
|
/* current context checking */
|
||||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
level = rt_hw_interrupt_disable();
|
level = rt_hw_interrupt_disable();
|
||||||
read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), buffer, size);
|
read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), buffer, size);
|
||||||
if (read_nbytes == 0)
|
if (read_nbytes == 0)
|
||||||
{
|
{
|
||||||
rt_thread_suspend(thread);
|
rt_thread_suspend(thread);
|
||||||
/* waiting on suspended read list */
|
/* waiting on suspended read list */
|
||||||
rt_list_insert_before(&(pipe->suspended_read_list), &(thread->tlist));
|
rt_list_insert_before(&(pipe->suspended_read_list), &(thread->tlist));
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
|
|
||||||
rt_schedule();
|
rt_schedule();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!rt_list_isempty(&pipe->suspended_write_list))
|
if (!rt_list_isempty(&pipe->suspended_write_list))
|
||||||
{
|
{
|
||||||
/* get suspended thread */
|
/* get suspended thread */
|
||||||
thread = rt_list_entry(pipe->suspended_write_list.next,
|
thread = rt_list_entry(pipe->suspended_write_list.next,
|
||||||
struct rt_thread, tlist);
|
struct rt_thread, tlist);
|
||||||
|
|
||||||
/* resume the write thread */
|
/* resume the write thread */
|
||||||
rt_thread_resume(thread);
|
rt_thread_resume(thread);
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
|
|
||||||
rt_schedule();
|
rt_schedule();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} while (read_nbytes == 0);
|
} while (read_nbytes == 0);
|
||||||
|
|
||||||
return read_nbytes;
|
return read_nbytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct rt_pipe_device *_pipe = RT_NULL;
|
struct rt_pipe_device *_pipe = RT_NULL;
|
||||||
static rt_size_t rt_pipe_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
|
static rt_size_t rt_pipe_write(rt_device_t dev,
|
||||||
|
rt_off_t pos,
|
||||||
|
const void *buffer,
|
||||||
|
rt_size_t size)
|
||||||
{
|
{
|
||||||
rt_uint32_t level;
|
rt_uint32_t level;
|
||||||
rt_thread_t thread;
|
rt_thread_t thread;
|
||||||
struct rt_pipe_device *pipe;
|
struct rt_pipe_device *pipe;
|
||||||
rt_size_t write_nbytes;
|
rt_size_t write_nbytes;
|
||||||
|
|
||||||
pipe = PIPE_DEVICE(dev);
|
pipe = PIPE_DEVICE(dev);
|
||||||
RT_ASSERT(pipe != RT_NULL);
|
RT_ASSERT(pipe != RT_NULL);
|
||||||
if (_pipe == RT_NULL)
|
if (_pipe == RT_NULL)
|
||||||
_pipe = pipe;
|
_pipe = pipe;
|
||||||
|
|
||||||
thread = rt_thread_self();
|
thread = rt_thread_self();
|
||||||
|
|
||||||
/* current context checking */
|
/* current context checking */
|
||||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
RT_DEBUG_NOT_IN_INTERRUPT;
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
level = rt_hw_interrupt_disable();
|
level = rt_hw_interrupt_disable();
|
||||||
write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer), buffer, size);
|
write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer), buffer, size);
|
||||||
if (write_nbytes == 0)
|
if (write_nbytes == 0)
|
||||||
{
|
{
|
||||||
/* pipe full, waiting on suspended write list */
|
/* pipe full, waiting on suspended write list */
|
||||||
rt_thread_suspend(thread);
|
rt_thread_suspend(thread);
|
||||||
/* waiting on suspended read list */
|
/* waiting on suspended read list */
|
||||||
rt_list_insert_before(&(pipe->suspended_write_list), &(thread->tlist));
|
rt_list_insert_before(&(pipe->suspended_write_list), &(thread->tlist));
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
|
|
||||||
rt_schedule();
|
rt_schedule();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!rt_list_isempty(&pipe->suspended_read_list))
|
if (!rt_list_isempty(&pipe->suspended_read_list))
|
||||||
{
|
{
|
||||||
/* get suspended thread */
|
/* get suspended thread */
|
||||||
thread = rt_list_entry(pipe->suspended_read_list.next,
|
thread = rt_list_entry(pipe->suspended_read_list.next,
|
||||||
struct rt_thread, tlist);
|
struct rt_thread, tlist);
|
||||||
|
|
||||||
/* resume the read thread */
|
/* resume the read thread */
|
||||||
rt_thread_resume(thread);
|
rt_thread_resume(thread);
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
|
|
||||||
rt_schedule();
|
rt_schedule();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
rt_hw_interrupt_enable(level);
|
rt_hw_interrupt_enable(level);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}while (write_nbytes == 0);
|
}while (write_nbytes == 0);
|
||||||
|
|
||||||
return write_nbytes;
|
return write_nbytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t rt_pipe_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
static rt_err_t rt_pipe_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
||||||
{
|
{
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
rt_err_t rt_pipe_create(const char* name, rt_size_t size)
|
rt_err_t rt_pipe_create(const char *name, rt_size_t size)
|
||||||
{
|
{
|
||||||
rt_err_t result = RT_EOK;
|
rt_err_t result = RT_EOK;
|
||||||
rt_uint8_t* rb_memptr = RT_NULL;
|
rt_uint8_t *rb_memptr = RT_NULL;
|
||||||
struct rt_pipe_device *pipe = RT_NULL;
|
struct rt_pipe_device *pipe = RT_NULL;
|
||||||
|
|
||||||
/* get aligned size */
|
/* get aligned size */
|
||||||
size = RT_ALIGN(size, RT_ALIGN_SIZE);
|
size = RT_ALIGN(size, RT_ALIGN_SIZE);
|
||||||
pipe = (struct rt_pipe_device*) rt_calloc (1, sizeof(struct rt_pipe_device));
|
pipe = (struct rt_pipe_device *)rt_calloc(1, sizeof(struct rt_pipe_device));
|
||||||
if (pipe != RT_NULL)
|
if (pipe != RT_NULL)
|
||||||
{
|
{
|
||||||
/* create ring buffer of pipe */
|
/* create ring buffer of pipe */
|
||||||
rb_memptr = rt_malloc(size);
|
rb_memptr = rt_malloc(size);
|
||||||
if (rb_memptr == RT_NULL)
|
if (rb_memptr == RT_NULL)
|
||||||
{
|
{
|
||||||
result = -RT_ENOMEM;
|
result = -RT_ENOMEM;
|
||||||
goto __exit;
|
goto __exit;
|
||||||
}
|
}
|
||||||
/* initialize suspended list */
|
/* initialize suspended list */
|
||||||
rt_list_init(&pipe->suspended_read_list);
|
rt_list_init(&pipe->suspended_read_list);
|
||||||
rt_list_init(&pipe->suspended_write_list);
|
rt_list_init(&pipe->suspended_write_list);
|
||||||
|
|
||||||
/* initialize ring buffer */
|
/* initialize ring buffer */
|
||||||
rt_ringbuffer_init(&pipe->ringbuffer, rb_memptr, size);
|
rt_ringbuffer_init(&pipe->ringbuffer, rb_memptr, size);
|
||||||
|
|
||||||
/* create device */
|
/* create device */
|
||||||
pipe->parent.type = RT_Device_Class_Char;
|
pipe->parent.type = RT_Device_Class_Char;
|
||||||
pipe->parent.init = RT_NULL;
|
pipe->parent.init = RT_NULL;
|
||||||
pipe->parent.open = RT_NULL;
|
pipe->parent.open = RT_NULL;
|
||||||
pipe->parent.close = RT_NULL;
|
pipe->parent.close = RT_NULL;
|
||||||
pipe->parent.read = rt_pipe_read;
|
pipe->parent.read = rt_pipe_read;
|
||||||
pipe->parent.write = rt_pipe_write;
|
pipe->parent.write = rt_pipe_write;
|
||||||
pipe->parent.control = rt_pipe_control;
|
pipe->parent.control = rt_pipe_control;
|
||||||
|
|
||||||
return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR);
|
return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
result = -RT_ENOMEM;
|
result = -RT_ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
__exit:
|
__exit:
|
||||||
if (pipe != RT_NULL) rt_free(pipe);
|
if (pipe != RT_NULL)
|
||||||
if (rb_memptr != RT_NULL) rt_free(rb_memptr);
|
rt_free(pipe);
|
||||||
|
if (rb_memptr != RT_NULL)
|
||||||
|
rt_free(rb_memptr);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
RTM_EXPORT(rt_pipe_create);
|
RTM_EXPORT(rt_pipe_create);
|
||||||
|
|
||||||
void rt_pipe_destroy(struct rt_pipe_device* pipe)
|
void rt_pipe_destroy(struct rt_pipe_device *pipe)
|
||||||
{
|
{
|
||||||
if (pipe == RT_NULL) return;
|
if (pipe == RT_NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
/* un-register pipe device */
|
/* un-register pipe device */
|
||||||
rt_device_unregister(&(pipe->parent));
|
rt_device_unregister(&(pipe->parent));
|
||||||
|
|
||||||
/* release memory */
|
/* release memory */
|
||||||
rt_free(pipe->ringbuffer.buffer_ptr);
|
rt_free(pipe->ringbuffer.buffer_ptr);
|
||||||
rt_free(pipe);
|
rt_free(pipe);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
RTM_EXPORT(rt_pipe_destroy);
|
RTM_EXPORT(rt_pipe_destroy);
|
||||||
|
|
||||||
|
|
|
@ -16,51 +16,56 @@
|
||||||
#include <rtdevice.h>
|
#include <rtdevice.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
void rt_ringbuffer_init(struct rt_ringbuffer* rb, rt_uint8_t *pool, rt_uint16_t size)
|
void rt_ringbuffer_init(struct rt_ringbuffer *rb,
|
||||||
|
rt_uint8_t *pool,
|
||||||
|
rt_uint16_t size)
|
||||||
{
|
{
|
||||||
RT_ASSERT(rb != RT_NULL);
|
RT_ASSERT(rb != RT_NULL);
|
||||||
|
|
||||||
/* initialize read and write index */
|
/* initialize read and write index */
|
||||||
rb->read_index = rb->write_index = 0;
|
rb->read_index = rb->write_index = 0;
|
||||||
|
|
||||||
/* set buffer pool and size */
|
/* set buffer pool and size */
|
||||||
rb->buffer_ptr = pool;
|
rb->buffer_ptr = pool;
|
||||||
rb->buffer_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
|
rb->buffer_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
|
||||||
}
|
}
|
||||||
RTM_EXPORT(rt_ringbuffer_init);
|
RTM_EXPORT(rt_ringbuffer_init);
|
||||||
|
|
||||||
rt_size_t rt_ringbuffer_put(struct rt_ringbuffer* rb, const rt_uint8_t *ptr, rt_uint16_t length)
|
rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
|
||||||
|
const rt_uint8_t *ptr,
|
||||||
|
rt_uint16_t length)
|
||||||
{
|
{
|
||||||
rt_uint16_t size;
|
rt_uint16_t size;
|
||||||
rt_uint16_t mask;
|
rt_uint16_t mask;
|
||||||
rt_uint16_t write_position;
|
rt_uint16_t write_position;
|
||||||
|
|
||||||
RT_ASSERT(rb != RT_NULL);
|
RT_ASSERT(rb != RT_NULL);
|
||||||
|
|
||||||
mask = rb->buffer_size - 1;
|
mask = rb->buffer_size - 1;
|
||||||
/* whether has enough space */
|
/* whether has enough space */
|
||||||
size = rb->buffer_size - (rb->write_index - rb->read_index);
|
size = rb->buffer_size - (rb->write_index - rb->read_index);
|
||||||
|
|
||||||
/* no space */
|
/* no space */
|
||||||
if (size == 0) return 0;
|
if (size == 0) return 0;
|
||||||
/* drop some data */
|
/* drop some data */
|
||||||
if (size < length) length = size;
|
if (size < length) length = size;
|
||||||
|
|
||||||
write_position = (rb->write_index & mask);
|
write_position = (rb->write_index & mask);
|
||||||
if (rb->buffer_size - write_position> length)
|
if (rb->buffer_size - write_position> length)
|
||||||
{
|
{
|
||||||
/* read_index - write_index = empty space */
|
/* read_index - write_index = empty space */
|
||||||
memcpy(&rb->buffer_ptr[write_position], ptr, length);
|
memcpy(&rb->buffer_ptr[write_position], ptr, length);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
memcpy(&rb->buffer_ptr[write_position], ptr, rb->buffer_size - write_position);
|
memcpy(&rb->buffer_ptr[write_position], ptr,
|
||||||
memcpy(&rb->buffer_ptr[0], &ptr[rb->buffer_size - write_position],
|
rb->buffer_size - write_position);
|
||||||
length - (rb->buffer_size - write_position));
|
memcpy(&rb->buffer_ptr[0], &ptr[rb->buffer_size - write_position],
|
||||||
}
|
length - (rb->buffer_size - write_position));
|
||||||
rb->write_index += length;
|
}
|
||||||
|
rb->write_index += length;
|
||||||
|
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
RTM_EXPORT(rt_ringbuffer_put);
|
RTM_EXPORT(rt_ringbuffer_put);
|
||||||
|
|
||||||
|
@ -69,80 +74,82 @@ RTM_EXPORT(rt_ringbuffer_put);
|
||||||
*/
|
*/
|
||||||
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer* rb, const rt_uint8_t ch)
|
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer* rb, const rt_uint8_t ch)
|
||||||
{
|
{
|
||||||
rt_uint16_t mask;
|
rt_uint16_t mask;
|
||||||
|
|
||||||
RT_ASSERT(rb != RT_NULL);
|
RT_ASSERT(rb != RT_NULL);
|
||||||
/* whether has enough space */
|
/* whether has enough space */
|
||||||
mask = rb->buffer_size - 1;
|
mask = rb->buffer_size - 1;
|
||||||
|
|
||||||
/* whether has enough space */
|
/* whether has enough space */
|
||||||
if (rb->write_index - rb->read_index == rb->buffer_size) return 0;
|
if (rb->write_index - rb->read_index == rb->buffer_size) return 0;
|
||||||
|
|
||||||
/* put character */
|
/* put character */
|
||||||
rb->buffer_ptr[rb->write_index & mask] = ch;
|
rb->buffer_ptr[rb->write_index & mask] = ch;
|
||||||
rb->write_index += 1;
|
rb->write_index += 1;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
RTM_EXPORT(rt_ringbuffer_putchar);
|
RTM_EXPORT(rt_ringbuffer_putchar);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get data from ring buffer
|
* 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 rt_ringbuffer_get(struct rt_ringbuffer *rb,
|
||||||
|
rt_uint8_t *ptr,
|
||||||
|
rt_uint16_t length)
|
||||||
{
|
{
|
||||||
rt_size_t size;
|
rt_size_t size;
|
||||||
rt_uint16_t mask;
|
rt_uint16_t mask;
|
||||||
rt_uint16_t read_position;
|
rt_uint16_t read_position;
|
||||||
|
|
||||||
RT_ASSERT(rb != RT_NULL);
|
RT_ASSERT(rb != RT_NULL);
|
||||||
/* whether has enough data */
|
/* whether has enough data */
|
||||||
mask = rb->buffer_size - 1;
|
mask = rb->buffer_size - 1;
|
||||||
size = rb->write_index - rb->read_index;
|
size = rb->write_index - rb->read_index;
|
||||||
|
|
||||||
/* no data */
|
/* no data */
|
||||||
if (size == 0) return 0;
|
if (size == 0) return 0;
|
||||||
/* less data */
|
/* less data */
|
||||||
if (size < length) length = size;
|
if (size < length) length = size;
|
||||||
|
|
||||||
read_position = rb->read_index & mask;
|
read_position = rb->read_index & mask;
|
||||||
if (rb->buffer_size - read_position >= length)
|
if (rb->buffer_size - read_position >= length)
|
||||||
{
|
{
|
||||||
/* copy all of data */
|
/* copy all of data */
|
||||||
memcpy(ptr, &rb->buffer_ptr[read_position], length);
|
memcpy(ptr, &rb->buffer_ptr[read_position], length);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* copy first and second */
|
/* copy first and second */
|
||||||
memcpy(ptr, &rb->buffer_ptr[read_position], rb->buffer_size - read_position);
|
memcpy(ptr, &rb->buffer_ptr[read_position],
|
||||||
memcpy(&ptr[rb->buffer_size - read_position], &rb->buffer_ptr[0],
|
rb->buffer_size - read_position);
|
||||||
length - rb->buffer_size + read_position);
|
memcpy(&ptr[rb->buffer_size - read_position], &rb->buffer_ptr[0],
|
||||||
}
|
length - rb->buffer_size + read_position);
|
||||||
rb->read_index += length;
|
}
|
||||||
|
rb->read_index += length;
|
||||||
|
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
RTM_EXPORT(rt_ringbuffer_get);
|
RTM_EXPORT(rt_ringbuffer_get);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get a character from a ringbuffer
|
* get a character from a ringbuffer
|
||||||
*/
|
*/
|
||||||
rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer* rb, rt_uint8_t *ch)
|
rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch)
|
||||||
{
|
{
|
||||||
rt_uint16_t mask;
|
rt_uint16_t mask;
|
||||||
|
|
||||||
RT_ASSERT(rb != RT_NULL);
|
RT_ASSERT(rb != RT_NULL);
|
||||||
|
|
||||||
/* ringbuffer is empty */
|
/* ringbuffer is empty */
|
||||||
if (rb->read_index == rb->write_index) return 0;
|
if (rb->read_index == rb->write_index) return 0;
|
||||||
|
|
||||||
mask = rb->buffer_size - 1;
|
mask = rb->buffer_size - 1;
|
||||||
|
|
||||||
/* put character */
|
/* put character */
|
||||||
*ch = rb->buffer_ptr[rb->read_index & mask];
|
*ch = rb->buffer_ptr[rb->read_index & mask];
|
||||||
rb->read_index += 1;
|
rb->read_index += 1;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
RTM_EXPORT(rt_ringbuffer_getchar);
|
RTM_EXPORT(rt_ringbuffer_getchar);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue