summaryrefslogtreecommitdiff
path: root/components/drivers/src/pipe.c
diff options
context:
space:
mode:
author[email protected] <[email protected]>2012-11-02 04:05:38 +0000
committer[email protected] <[email protected]>2012-11-02 04:05:38 +0000
commit1aab195a7dd1557eb02e403f4667933cc76d4347 (patch)
treecafeff0d2e20a4c62598658d47fbb75759df4387 /components/drivers/src/pipe.c
parent5d710e8862190783868e73676882606fe79925f5 (diff)
fixed coding style in component/drivers/src
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2384 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'components/drivers/src/pipe.c')
-rw-r--r--components/drivers/src/pipe.c283
1 files changed, 146 insertions, 137 deletions
diff --git a/components/drivers/src/pipe.c b/components/drivers/src/pipe.c
index 142f1d9399..00aa863c8c 100644
--- a/components/drivers/src/pipe.c
+++ b/components/drivers/src/pipe.c
@@ -11,186 +11,195 @@
* 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)
+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;
+ 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);
+ pipe = PIPE_DEVICE(dev);
+ RT_ASSERT(pipe != RT_NULL);
- thread = rt_thread_self();
+ thread = rt_thread_self();
- /* current context checking */
- RT_DEBUG_NOT_IN_INTERRUPT;
+ /* 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);
+ 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);
+ 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);
+ /* 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);
+ rt_schedule();
+ }
+ else
+ {
+ rt_hw_interrupt_enable(level);
+ }
+ break;
+ }
+ } while (read_nbytes == 0);
- return read_nbytes;
+ 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)
+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;
+ 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;
- pipe = PIPE_DEVICE(dev);
- RT_ASSERT(pipe != RT_NULL);
- if (_pipe == RT_NULL)
- _pipe = pipe;
-
- thread = rt_thread_self();
+ thread = rt_thread_self();
- /* current context checking */
- RT_DEBUG_NOT_IN_INTERRUPT;
+ /* 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);
+ 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);
+ 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);
+ /* 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);
+ rt_schedule();
+ }
+ else
+ {
+ rt_hw_interrupt_enable(level);
+ }
+ break;
+ }
+ }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)
{
- 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_uint8_t* rb_memptr = RT_NULL;
- struct rt_pipe_device *pipe = RT_NULL;
+ 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);
+ /* 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;
+ /* 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;
- }
+ 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);
+ if (pipe != RT_NULL)
+ rt_free(pipe);
+ if (rb_memptr != RT_NULL)
+ rt_free(rb_memptr);
- return result;
+ return result;
}
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 */
- rt_device_unregister(&(pipe->parent));
+ /* un-register pipe device */
+ rt_device_unregister(&(pipe->parent));
- /* release memory */
- rt_free(pipe->ringbuffer.buffer_ptr);
- rt_free(pipe);
+ /* release memory */
+ rt_free(pipe->ringbuffer.buffer_ptr);
+ rt_free(pipe);
- return;
+ return;
}
RTM_EXPORT(rt_pipe_destroy);
-