diff options
| author | [email protected] <[email protected]> | 2012-11-22 08:43:40 +0000 |
|---|---|---|
| committer | [email protected] <[email protected]> | 2012-11-22 08:43:40 +0000 |
| commit | 68b7e7c4812bc3203120c925f8fc92ec4d3ecf46 (patch) | |
| tree | 1001d7dc1bc8822e953bb1e6759c5b2c2b4a6c9d /components/drivers/src | |
| parent | 871951930bedf2a4e1d6bbf860acceb89b6f7c20 (diff) | |
fixed the coding style in the components/drivers
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2431 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'components/drivers/src')
| -rw-r--r-- | components/drivers/src/completion.c | 1 | ||||
| -rw-r--r-- | components/drivers/src/pipe.c | 410 | ||||
| -rw-r--r-- | components/drivers/src/ringbuffer.c | 316 |
3 files changed, 367 insertions, 360 deletions
diff --git a/components/drivers/src/completion.c b/components/drivers/src/completion.c index 17ca1fa406..de3101375d 100644 --- a/components/drivers/src/completion.c +++ b/components/drivers/src/completion.c @@ -89,6 +89,7 @@ rt_err_t rt_completion_wait(struct rt_completion *completion, __exit: rt_hw_interrupt_enable(level); + return result; } diff --git a/components/drivers/src/pipe.c b/components/drivers/src/pipe.c index 00aa863c8c..a0ea434ba3 100644 --- a/components/drivers/src/pipe.c +++ b/components/drivers/src/pipe.c @@ -1,205 +1,205 @@ -/*
- * File : pipe.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.
- */
-
-#include <rthw.h>
-#include <rtthread.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)
-{
- rt_uint32_t level;
- rt_thread_t thread;
- struct rt_pipe_device *pipe;
- rt_size_t read_nbytes;
-
- pipe = PIPE_DEVICE(dev);
- RT_ASSERT(pipe != RT_NULL);
-
- thread = rt_thread_self();
-
- /* current context checking */
- RT_DEBUG_NOT_IN_INTERRUPT;
-
- do
- {
- level = rt_hw_interrupt_disable();
- read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), buffer, size);
- if (read_nbytes == 0)
- {
- rt_thread_suspend(thread);
- /* waiting on suspended read list */
- rt_list_insert_before(&(pipe->suspended_read_list), &(thread->tlist));
- rt_hw_interrupt_enable(level);
-
- rt_schedule();
- }
- else
- {
- if (!rt_list_isempty(&pipe->suspended_write_list))
- {
- /* get suspended thread */
- thread = rt_list_entry(pipe->suspended_write_list.next,
- struct rt_thread, tlist);
-
- /* resume the write thread */
- rt_thread_resume(thread);
- rt_hw_interrupt_enable(level);
-
- rt_schedule();
- }
- else
- {
- rt_hw_interrupt_enable(level);
- }
- break;
- }
- } while (read_nbytes == 0);
-
- return read_nbytes;
-}
-
-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)
-{
- rt_uint32_t level;
- rt_thread_t thread;
- struct rt_pipe_device *pipe;
- rt_size_t write_nbytes;
-
- pipe = PIPE_DEVICE(dev);
- RT_ASSERT(pipe != RT_NULL);
- if (_pipe == RT_NULL)
- _pipe = pipe;
-
- thread = rt_thread_self();
-
- /* current context checking */
- RT_DEBUG_NOT_IN_INTERRUPT;
-
- do
- {
- level = rt_hw_interrupt_disable();
- write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer), buffer, size);
- if (write_nbytes == 0)
- {
- /* pipe full, waiting on suspended write list */
- rt_thread_suspend(thread);
- /* waiting on suspended read list */
- rt_list_insert_before(&(pipe->suspended_write_list), &(thread->tlist));
- rt_hw_interrupt_enable(level);
-
- rt_schedule();
- }
- else
- {
- if (!rt_list_isempty(&pipe->suspended_read_list))
- {
- /* get suspended thread */
- thread = rt_list_entry(pipe->suspended_read_list.next,
- struct rt_thread, tlist);
-
- /* resume the read thread */
- rt_thread_resume(thread);
- rt_hw_interrupt_enable(level);
-
- rt_schedule();
- }
- else
- {
- rt_hw_interrupt_enable(level);
- }
- break;
- }
- }while (write_nbytes == 0);
-
- return write_nbytes;
-}
-
-static rt_err_t rt_pipe_control(rt_device_t dev, rt_uint8_t cmd, void *args)
-{
- return RT_EOK;
-}
-
-rt_err_t rt_pipe_create(const char *name, rt_size_t size)
-{
- rt_err_t result = RT_EOK;
- rt_uint8_t *rb_memptr = RT_NULL;
- struct rt_pipe_device *pipe = RT_NULL;
-
- /* get aligned size */
- size = RT_ALIGN(size, RT_ALIGN_SIZE);
- pipe = (struct rt_pipe_device *)rt_calloc(1, sizeof(struct rt_pipe_device));
- if (pipe != RT_NULL)
- {
- /* create ring buffer of pipe */
- rb_memptr = rt_malloc(size);
- if (rb_memptr == RT_NULL)
- {
- result = -RT_ENOMEM;
- goto __exit;
- }
- /* initialize suspended list */
- rt_list_init(&pipe->suspended_read_list);
- rt_list_init(&pipe->suspended_write_list);
-
- /* initialize ring buffer */
- rt_ringbuffer_init(&pipe->ringbuffer, rb_memptr, size);
-
- /* create device */
- pipe->parent.type = RT_Device_Class_Char;
- pipe->parent.init = RT_NULL;
- pipe->parent.open = RT_NULL;
- pipe->parent.close = RT_NULL;
- pipe->parent.read = rt_pipe_read;
- pipe->parent.write = rt_pipe_write;
- pipe->parent.control = rt_pipe_control;
-
- return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR);
- }
- else
- {
- result = -RT_ENOMEM;
- }
-
-__exit:
- if (pipe != RT_NULL)
- rt_free(pipe);
- if (rb_memptr != RT_NULL)
- rt_free(rb_memptr);
-
- return result;
-}
-RTM_EXPORT(rt_pipe_create);
-
-void rt_pipe_destroy(struct rt_pipe_device *pipe)
-{
- if (pipe == RT_NULL)
- return;
-
- /* un-register pipe device */
- rt_device_unregister(&(pipe->parent));
-
- /* release memory */
- rt_free(pipe->ringbuffer.buffer_ptr);
- rt_free(pipe);
-
- return;
-}
-RTM_EXPORT(rt_pipe_destroy);
+/* + * File : pipe.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. + */ + +#include <rthw.h> +#include <rtthread.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) +{ + rt_uint32_t level; + rt_thread_t thread; + struct rt_pipe_device *pipe; + rt_size_t read_nbytes; + + pipe = PIPE_DEVICE(dev); + RT_ASSERT(pipe != RT_NULL); + + thread = rt_thread_self(); + + /* current context checking */ + RT_DEBUG_NOT_IN_INTERRUPT; + + do + { + level = rt_hw_interrupt_disable(); + read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), buffer, size); + if (read_nbytes == 0) + { + rt_thread_suspend(thread); + /* waiting on suspended read list */ + rt_list_insert_before(&(pipe->suspended_read_list), &(thread->tlist)); + rt_hw_interrupt_enable(level); + + rt_schedule(); + } + else + { + if (!rt_list_isempty(&pipe->suspended_write_list)) + { + /* get suspended thread */ + thread = rt_list_entry(pipe->suspended_write_list.next, + struct rt_thread, tlist); + + /* resume the write thread */ + rt_thread_resume(thread); + rt_hw_interrupt_enable(level); + + rt_schedule(); + } + else + { + rt_hw_interrupt_enable(level); + } + break; + } + } while (read_nbytes == 0); + + return read_nbytes; +} + +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) +{ + rt_uint32_t level; + rt_thread_t thread; + struct rt_pipe_device *pipe; + rt_size_t write_nbytes; + + pipe = PIPE_DEVICE(dev); + RT_ASSERT(pipe != RT_NULL); + if (_pipe == RT_NULL) + _pipe = pipe; + + thread = rt_thread_self(); + + /* current context checking */ + RT_DEBUG_NOT_IN_INTERRUPT; + + do + { + level = rt_hw_interrupt_disable(); + write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer), buffer, size); + if (write_nbytes == 0) + { + /* pipe full, waiting on suspended write list */ + rt_thread_suspend(thread); + /* waiting on suspended read list */ + rt_list_insert_before(&(pipe->suspended_write_list), &(thread->tlist)); + rt_hw_interrupt_enable(level); + + rt_schedule(); + } + else + { + if (!rt_list_isempty(&pipe->suspended_read_list)) + { + /* get suspended thread */ + thread = rt_list_entry(pipe->suspended_read_list.next, + struct rt_thread, tlist); + + /* resume the read thread */ + rt_thread_resume(thread); + rt_hw_interrupt_enable(level); + + rt_schedule(); + } + else + { + rt_hw_interrupt_enable(level); + } + break; + } + }while (write_nbytes == 0); + + return write_nbytes; +} + +static rt_err_t rt_pipe_control(rt_device_t dev, rt_uint8_t cmd, void *args) +{ + return RT_EOK; +} + +rt_err_t rt_pipe_create(const char *name, rt_size_t size) +{ + rt_err_t result = RT_EOK; + rt_uint8_t *rb_memptr = RT_NULL; + struct rt_pipe_device *pipe = RT_NULL; + + /* get aligned size */ + size = RT_ALIGN(size, RT_ALIGN_SIZE); + pipe = (struct rt_pipe_device *)rt_calloc(1, sizeof(struct rt_pipe_device)); + if (pipe != RT_NULL) + { + /* create ring buffer of pipe */ + rb_memptr = rt_malloc(size); + if (rb_memptr == RT_NULL) + { + result = -RT_ENOMEM; + goto __exit; + } + /* initialize suspended list */ + rt_list_init(&pipe->suspended_read_list); + rt_list_init(&pipe->suspended_write_list); + + /* initialize ring buffer */ + rt_ringbuffer_init(&pipe->ringbuffer, rb_memptr, size); + + /* create device */ + pipe->parent.type = RT_Device_Class_Char; + pipe->parent.init = RT_NULL; + pipe->parent.open = RT_NULL; + pipe->parent.close = RT_NULL; + pipe->parent.read = rt_pipe_read; + pipe->parent.write = rt_pipe_write; + pipe->parent.control = rt_pipe_control; + + return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR); + } + else + { + result = -RT_ENOMEM; + } + +__exit: + if (pipe != RT_NULL) + rt_free(pipe); + if (rb_memptr != RT_NULL) + rt_free(rb_memptr); + + return result; +} +RTM_EXPORT(rt_pipe_create); + +void rt_pipe_destroy(struct rt_pipe_device *pipe) +{ + if (pipe == RT_NULL) + return; + + /* un-register pipe device */ + rt_device_unregister(&(pipe->parent)); + + /* release memory */ + rt_free(pipe->ringbuffer.buffer_ptr); + rt_free(pipe); + + return; +} +RTM_EXPORT(rt_pipe_destroy); diff --git a/components/drivers/src/ringbuffer.c b/components/drivers/src/ringbuffer.c index 6223ba6935..6454fd9932 100644 --- a/components/drivers/src/ringbuffer.c +++ b/components/drivers/src/ringbuffer.c @@ -1,155 +1,161 @@ -/*
- * 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.
- */
-
-#include <rtthread.h>
-#include <rtdevice.h>
-#include <string.h>
-
-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;
- rb->buffer_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
-}
-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_uint16_t size;
- rt_uint16_t mask;
- rt_uint16_t write_position;
-
- RT_ASSERT(rb != RT_NULL);
-
- mask = rb->buffer_size - 1;
- /* whether has enough space */
- size = rb->buffer_size - (rb->write_index - rb->read_index);
-
- /* no space */
- if (size == 0) return 0;
- /* drop some data */
- if (size < length) length = size;
-
- write_position = (rb->write_index & mask);
- if (rb->buffer_size - write_position> length)
- {
- /* read_index - write_index = empty space */
- memcpy(&rb->buffer_ptr[write_position], ptr, length);
- }
- else
- {
- 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));
- }
- rb->write_index += length;
-
- return length;
-}
-RTM_EXPORT(rt_ringbuffer_put);
-
-/**
- * put a character into ring buffer
- */
-rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer* rb, const rt_uint8_t ch)
-{
- rt_uint16_t mask;
-
- RT_ASSERT(rb != RT_NULL);
- /* whether has enough space */
- mask = rb->buffer_size - 1;
-
- /* whether has enough space */
- if (rb->write_index - rb->read_index == rb->buffer_size) return 0;
-
- /* put character */
- rb->buffer_ptr[rb->write_index & mask] = ch;
- rb->write_index += 1;
-
- return 1;
-}
-RTM_EXPORT(rt_ringbuffer_putchar);
-
-/**
- * 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_uint16_t mask;
- rt_uint16_t read_position;
-
- RT_ASSERT(rb != RT_NULL);
- /* whether has enough data */
- mask = rb->buffer_size - 1;
- size = rb->write_index - rb->read_index;
-
- /* no data */
- if (size == 0) return 0;
- /* less data */
- if (size < length) length = size;
-
- read_position = rb->read_index & mask;
- if (rb->buffer_size - read_position >= length)
- {
- /* copy all of data */
- memcpy(ptr, &rb->buffer_ptr[read_position], length);
- }
- else
- {
- /* 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);
- }
- rb->read_index += length;
-
- return length;
-}
-RTM_EXPORT(rt_ringbuffer_get);
-
-/**
- * get a character from a ringbuffer
- */
-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 */
- *ch = rb->buffer_ptr[rb->read_index & mask];
- rb->read_index += 1;
-
- return 1;
-}
-RTM_EXPORT(rt_ringbuffer_getchar);
+/* + * 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. + */ + +#include <rtthread.h> +#include <rtdevice.h> +#include <string.h> + +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; + rb->buffer_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE); +} +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_uint16_t size; + rt_uint16_t mask; + rt_uint16_t write_position; + + RT_ASSERT(rb != RT_NULL); + + mask = rb->buffer_size - 1; + /* whether has enough space */ + size = rb->buffer_size - (rb->write_index - rb->read_index); + + /* no space */ + if (size == 0) + return 0; + /* drop some data */ + if (size < length) + length = size; + + write_position = (rb->write_index & mask); + if (rb->buffer_size - write_position> length) + { + /* read_index - write_index = empty space */ + memcpy(&rb->buffer_ptr[write_position], ptr, length); + } + else + { + 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)); + } + rb->write_index += length; + + return length; +} +RTM_EXPORT(rt_ringbuffer_put); + +/** + * put a character into ring buffer + */ +rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch) +{ + rt_uint16_t mask; + + RT_ASSERT(rb != RT_NULL); + /* whether has enough space */ + mask = rb->buffer_size - 1; + + /* whether has enough space */ + if (rb->write_index - rb->read_index == rb->buffer_size) + return 0; + + /* put character */ + rb->buffer_ptr[rb->write_index & mask] = ch; + rb->write_index += 1; + + return 1; +} +RTM_EXPORT(rt_ringbuffer_putchar); + +/** + * 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_uint16_t mask; + rt_uint16_t read_position; + + RT_ASSERT(rb != RT_NULL); + /* whether has enough data */ + mask = rb->buffer_size - 1; + size = rb->write_index - rb->read_index; + + /* no data */ + if (size == 0) + return 0; + /* less data */ + if (size < length) + length = size; + + read_position = rb->read_index & mask; + if (rb->buffer_size - read_position >= length) + { + /* copy all of data */ + memcpy(ptr, &rb->buffer_ptr[read_position], length); + } + else + { + /* 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); + } + rb->read_index += length; + + return length; +} +RTM_EXPORT(rt_ringbuffer_get); + +/** + * get a character from a ringbuffer + */ +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 */ + *ch = rb->buffer_ptr[rb->read_index & mask]; + rb->read_index += 1; + + return 1; +} +RTM_EXPORT(rt_ringbuffer_getchar); |
