summaryrefslogtreecommitdiff
path: root/components/drivers/src
diff options
context:
space:
mode:
authorbernard <[email protected]>2017-10-15 22:56:46 +0800
committerbernard <[email protected]>2017-10-15 22:56:46 +0800
commitd7087fdd3b998192afb03b34fd8034071d06bde7 (patch)
tree6a99d97408c4b3bc708b3a0601bff80907d8beac /components/drivers/src
parent0f5a68a55e2a3c44f1e85029d22de6180ad30390 (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')
-rw-r--r--components/drivers/src/pipe.c512
-rw-r--r--components/drivers/src/portal.c256
-rw-r--r--components/drivers/src/ringbuffer.c88
-rw-r--r--components/drivers/src/waitqueue.c101
4 files changed, 511 insertions, 446 deletions
diff --git a/components/drivers/src/pipe.c b/components/drivers/src/pipe.c
index f438a92898..b455870c8a 100644
--- a/components/drivers/src/pipe.c
+++ b/components/drivers/src/pipe.c
@@ -1,7 +1,7 @@
/*
* File : pipe.c
* This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2012, RT-Thread Development Team
+ * COPYRIGHT (C) 2012-2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -21,273 +21,407 @@
* Date Author Notes
* 2012-09-30 Bernard first version.
*/
-
#include <rthw.h>
-#include <rtthread.h>
#include <rtdevice.h>
+#include <dfs_file.h>
+#include <dfs_posix.h>
-static void _rt_pipe_resume_writer(struct rt_pipe_device *pipe)
+#if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS)
+static int pipe_open(struct dfs_fd *fd)
{
- if (!rt_list_isempty(&pipe->suspended_write_list))
- {
- rt_thread_t thread;
+ rt_device_t device;
+ rt_pipe_t *pipe;
- RT_ASSERT(pipe->flag & RT_PIPE_FLAG_BLOCK_WR);
+ pipe = (rt_pipe_t *)fd->data;
+ if (!pipe) return -1;
- /* get suspended thread */
- thread = rt_list_entry(pipe->suspended_write_list.next,
- struct rt_thread,
- tlist);
+ device = &(pipe->parent);
+ rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
- /* resume the write thread */
- rt_thread_resume(thread);
+ if (device->ref_count == 0)
+ {
+ pipe->fifo = rt_ringbuffer_create(PIPE_BUFSZ);
+ }
- rt_schedule();
+ switch (fd->flags & O_ACCMODE)
+ {
+ case O_RDONLY:
+ pipe->readers ++;
+ break;
+ case O_WRONLY:
+ pipe->writers ++;
+ break;
+ case O_RDWR:
+ pipe->readers ++;
+ pipe->writers ++;
+ break;
}
+ device->ref_count ++;
+
+ rt_mutex_release(&(pipe->lock));
+
+ return 0;
}
-static rt_size_t rt_pipe_read(rt_device_t dev,
- rt_off_t pos,
- void *buffer,
- rt_size_t size)
+static int pipe_close(struct dfs_fd *fd)
{
- rt_uint32_t level;
- rt_thread_t thread;
- struct rt_pipe_device *pipe;
- rt_size_t read_nbytes;
+ rt_device_t device;
+ rt_pipe_t *pipe;
- pipe = PIPE_DEVICE(dev);
- RT_ASSERT(pipe != RT_NULL);
+ pipe = (rt_pipe_t *)fd->data;
+ if (!pipe) return -1;
- if (!(pipe->flag & RT_PIPE_FLAG_BLOCK_RD))
+ device = &(pipe->parent);
+ rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
+
+ switch (fd->flags & O_ACCMODE)
{
- level = rt_hw_interrupt_disable();
- read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), buffer, size);
+ case O_RDONLY:
+ pipe->readers --;
+ break;
+ case O_WRONLY:
+ pipe->writers --;
+ break;
+ case O_RDWR:
+ pipe->readers --;
+ pipe->writers --;
+ break;
+ }
- /* if the ringbuffer is empty, there won't be any writer waiting */
- if (read_nbytes)
- _rt_pipe_resume_writer(pipe);
+ if (pipe->writers == 0)
+ {
+ rt_wqueue_wakeup(&(pipe->reader_queue), (void*)(POLLIN | POLLERR | POLLHUP));
+ }
- rt_hw_interrupt_enable(level);
+ if (pipe->readers == 0)
+ {
+ rt_wqueue_wakeup(&(pipe->writer_queue), (void*)(POLLOUT | POLLERR | POLLHUP));
+ }
- return read_nbytes;
+ if (device->ref_count == 1)
+ {
+ rt_free(pipe->fifo);
+ pipe->fifo = RT_NULL;
}
+ device->ref_count --;
- thread = rt_thread_self();
+ rt_mutex_release(&(pipe->lock));
- /* current context checking */
- RT_DEBUG_NOT_IN_INTERRUPT;
+ return 0;
+}
- 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);
+static int pipe_ioctl(struct dfs_fd *fd, int cmd, void *args)
+{
+ rt_pipe_t *pipe;
+ int ret = 0;
- rt_schedule();
- }
- else
- {
- _rt_pipe_resume_writer(pipe);
- rt_hw_interrupt_enable(level);
- break;
- }
- } while (read_nbytes == 0);
+ pipe = (rt_pipe_t *)fd->data;
+
+ switch (cmd)
+ {
+ case FIONREAD:
+ *((int*)args) = rt_ringbuffer_data_len(pipe->fifo);
+ break;
+ case FIONWRITE:
+ *((int*)args) = rt_ringbuffer_space_len(pipe->fifo);
+ break;
+ default:
+ ret = -EINVAL;
+ break;
+ }
- return read_nbytes;
+ return ret;
}
-static void _rt_pipe_resume_reader(struct rt_pipe_device *pipe)
+static int pipe_read(struct dfs_fd *fd, void *buf, size_t count)
{
- if (pipe->parent.rx_indicate)
- pipe->parent.rx_indicate(&pipe->parent,
- rt_ringbuffer_data_len(&pipe->ringbuffer));
+ int len = 0;
+ rt_pipe_t *pipe;
- if (!rt_list_isempty(&pipe->suspended_read_list))
- {
- rt_thread_t thread;
+ pipe = (rt_pipe_t *)fd->data;
- RT_ASSERT(pipe->flag & RT_PIPE_FLAG_BLOCK_RD);
+ /* no process has the pipe open for writing, return end-of-file */
+ if (pipe->writers == 0)
+ return 0;
- /* get suspended thread */
- thread = rt_list_entry(pipe->suspended_read_list.next,
- struct rt_thread,
- tlist);
+ rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
- /* resume the read thread */
- rt_thread_resume(thread);
+ while (1)
+ {
+ if (pipe->writers == 0)
+ {
+ goto out;
+ }
+
+ len = rt_ringbuffer_get(pipe->fifo, buf, count);
+
+ if (len > 0)
+ {
+ break;
+ }
+ else
+ {
+ if (fd->flags & O_NONBLOCK)
+ {
+ len = -EAGAIN;
+ goto out;
+ }
- rt_schedule();
+ rt_mutex_release(&pipe->lock);
+ rt_wqueue_wakeup(&(pipe->writer_queue), (void*)POLLOUT);
+ rt_wqueue_wait(&(pipe->reader_queue), 0, -1);
+ rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
+ }
}
+
+ /* wakeup writer */
+ rt_wqueue_wakeup(&(pipe->writer_queue), (void*)POLLOUT);
+
+out:
+ rt_mutex_release(&pipe->lock);
+
+ return len;
}
-static rt_size_t rt_pipe_write(rt_device_t dev,
- rt_off_t pos,
- const void *buffer,
- rt_size_t size)
+static int pipe_write(struct dfs_fd *fd, const void *buf, size_t count)
{
- rt_uint32_t level;
- rt_thread_t thread;
- struct rt_pipe_device *pipe;
- rt_size_t write_nbytes;
+ int len;
+ rt_pipe_t *pipe;
+ int wakeup = 0;
+ int ret = 0;
+ uint8_t *pbuf;
- pipe = PIPE_DEVICE(dev);
- RT_ASSERT(pipe != RT_NULL);
+ pipe = (rt_pipe_t *)fd->data;
- if ((pipe->flag & RT_PIPE_FLAG_FORCE_WR) ||
- !(pipe->flag & RT_PIPE_FLAG_BLOCK_WR))
+ if (pipe->readers == 0)
{
- level = rt_hw_interrupt_disable();
+ ret = -EPIPE;
+ goto out;
+ }
+
+ if (count == 0)
+ return 0;
- if (pipe->flag & RT_PIPE_FLAG_FORCE_WR)
- write_nbytes = rt_ringbuffer_put_force(&(pipe->ringbuffer),
- buffer, size);
+ pbuf = (uint8_t*)buf;
+ rt_mutex_take(&pipe->lock, -1);
+
+ while (1)
+ {
+ if (pipe->readers == 0)
+ {
+ if (ret == 0)
+ ret = -EPIPE;
+ break;
+ }
+
+ len = rt_ringbuffer_put(pipe->fifo, pbuf, count - ret);
+ ret += len;
+ pbuf += len;
+ wakeup = 1;
+
+ if (ret == count)
+ {
+ break;
+ }
else
- write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer),
- buffer, size);
+ {
+ if (fd->flags & O_NONBLOCK)
+ {
+ if (ret == 0)
+ {
+ ret = -EAGAIN;
+ }
- _rt_pipe_resume_reader(pipe);
+ break;
+ }
+ }
- rt_hw_interrupt_enable(level);
+ rt_mutex_release(&pipe->lock);
+ rt_wqueue_wakeup(&(pipe->reader_queue), (void*)POLLIN);
+ /* pipe full, waiting on suspended write list */
+ rt_wqueue_wait(&(pipe->writer_queue), 0, -1);
+ rt_mutex_take(&pipe->lock, -1);
+ }
- return write_nbytes;
+ rt_mutex_release(&pipe->lock);
+ if (wakeup)
+ {
+ rt_wqueue_wakeup(&(pipe->reader_queue), (void*)POLLIN);
}
- thread = rt_thread_self();
+out:
+
+ return ret;
+}
- /* current context checking */
- RT_DEBUG_NOT_IN_INTERRUPT;
+static int pipe_poll(struct dfs_fd *fd, rt_pollreq_t *req)
+{
+ int mask = 0;
+ rt_pipe_t *pipe;
+ int mode = 0;
+ pipe = (rt_pipe_t *)fd->data;
+
+ rt_poll_add(&(pipe->reader_queue), req);
+ rt_poll_add(&(pipe->writer_queue), req);
+
+ switch (fd->flags & O_ACCMODE)
+ {
+ case O_RDONLY:
+ mode = 1;
+ break;
+ case O_WRONLY:
+ mode = 2;
+ break;
+ case O_RDWR:
+ mode = 3;
+ break;
+ }
- do {
- level = rt_hw_interrupt_disable();
- write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer), buffer, size);
- if (write_nbytes == 0)
+ if (mode & 1)
+ {
+ if (rt_ringbuffer_data_len(pipe->fifo) != 0)
+ {
+ mask |= POLLIN;
+ }
+ if (pipe->writers == 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);
+ mask |= POLLHUP;
+ }
+ }
- rt_schedule();
+ if (mode & 2)
+ {
+ if (rt_ringbuffer_space_len(pipe->fifo) != 0)
+ {
+ mask |= POLLOUT;
}
- else
+ if (pipe->readers == 0)
{
- _rt_pipe_resume_reader(pipe);
- rt_hw_interrupt_enable(level);
- break;
+ mask |= POLLERR;
}
- } while (write_nbytes == 0);
+ }
- return write_nbytes;
+ return mask;
}
-static rt_err_t rt_pipe_control(rt_device_t dev, rt_uint8_t cmd, void *args)
+static const struct dfs_file_ops pipe_fops =
{
- if (cmd == PIPE_CTRL_GET_SPACE && args)
- *(rt_size_t*)args = rt_ringbuffer_space_len(&PIPE_DEVICE(dev)->ringbuffer);
- return RT_EOK;
-}
+ pipe_open,
+ pipe_close,
+ pipe_ioctl,
+ pipe_read,
+ pipe_write,
+ RT_NULL,
+ RT_NULL,
+ RT_NULL,
+ pipe_poll,
+};
-/**
- * This function will initialize a pipe device and put it under control of
- * resource management.
- *
- * @param pipe the pipe device
- * @param name the name of pipe device
- * @param flag the attribute of the pipe device
- * @param buf the buffer of pipe device
- * @param size the size of pipe device buffer
- *
- * @return the operation status, RT_EOK on successful
- */
-rt_err_t rt_pipe_init(struct rt_pipe_device *pipe,
- const char *name,
- enum rt_pipe_flag flag,
- rt_uint8_t *buf,
- rt_size_t size)
+rt_pipe_t *rt_pipe_create(const char *name)
{
- RT_ASSERT(pipe);
- RT_ASSERT(buf);
+ rt_pipe_t *pipe;
+ rt_device_t dev;
- /* initialize suspended list */
- rt_list_init(&pipe->suspended_read_list);
- rt_list_init(&pipe->suspended_write_list);
+ pipe = rt_malloc(sizeof(rt_pipe_t));
+ if (pipe == RT_NULL) return RT_NULL;
- /* initialize ring buffer */
- rt_ringbuffer_init(&pipe->ringbuffer, buf, size);
+ rt_memset(pipe, 0, sizeof(rt_pipe_t));
+ rt_mutex_init(&(pipe->lock), name, RT_IPC_FLAG_FIFO);
+ rt_list_init(&(pipe->reader_queue));
+ rt_list_init(&(pipe->writer_queue));
- pipe->flag = flag;
+ dev = &(pipe->parent);
+ dev->type = RT_Device_Class_Pipe;
- /* create pipe */
- pipe->parent.type = RT_Device_Class_Pipe;
- 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;
+ if (rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE) != 0)
+ {
+ rt_free(pipe);
+ return RT_NULL;
+ }
+ dev->fops = (void*)&pipe_fops;
- return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR);
+ return pipe;
}
-RTM_EXPORT(rt_pipe_init);
-/**
- * This function will detach a pipe device from resource management
- *
- * @param pipe the pipe device
- *
- * @return the operation status, RT_EOK on successful
- */
-rt_err_t rt_pipe_detach(struct rt_pipe_device *pipe)
+int rt_pipe_delete(const char *name)
{
- return rt_device_unregister(&pipe->parent);
+ int result = 0;
+ rt_device_t device;
+
+ device = rt_device_find(name);
+ if (device)
+ {
+ if (device->type == RT_Device_Class_Pipe)
+ {
+ rt_pipe_t *pipe;
+
+ if (device->ref_count != 0)
+ {
+ return -RT_EBUSY;
+ }
+
+ pipe = (rt_pipe_t *)device;
+
+ rt_mutex_detach(&(pipe->lock));
+ rt_device_unregister(device);
+
+ rt_free(pipe);
+ }
+ else
+ {
+ result = -1;
+ }
+ }
+ else
+ {
+ result = -1;
+ }
+
+ return result;
}
-RTM_EXPORT(rt_pipe_detach);
-#ifdef RT_USING_HEAP
-rt_err_t rt_pipe_create(const char *name, enum rt_pipe_flag flag, rt_size_t size)
+int pipe(int fildes[2])
{
- rt_uint8_t *rb_memptr = RT_NULL;
- struct rt_pipe_device *pipe = RT_NULL;
+ rt_pipe_t *pipe;
+ char dname[8];
+ char dev_name[32];
+ static int pipeno = 0;
- /* get aligned size */
- size = RT_ALIGN(size, RT_ALIGN_SIZE);
- pipe = (struct rt_pipe_device *)rt_calloc(1, sizeof(struct rt_pipe_device));
+ rt_snprintf(dname, sizeof(dname), "pipe%d", pipeno++);
+
+ pipe = rt_pipe_create(dname);
if (pipe == RT_NULL)
- return -RT_ENOMEM;
+ {
+ return -1;
+ }
- /* create ring buffer of pipe */
- rb_memptr = rt_malloc(size);
- if (rb_memptr == RT_NULL)
+ rt_snprintf(dev_name, sizeof(dev_name), "/dev/%s", dname);
+ fildes[0] = open(dev_name, O_RDONLY, 0);
+ if (fildes[0] < 0)
{
- rt_free(pipe);
- return -RT_ENOMEM;
+ return -1;
}
- return rt_pipe_init(pipe, name, flag, rb_memptr, size);
+ fildes[1] = open(dev_name, O_WRONLY, 0);
+ if (fildes[1] < 0)
+ {
+ close(fildes[1]);
+ return -1;
+ }
+
+ return 0;
}
-RTM_EXPORT(rt_pipe_create);
-void rt_pipe_destroy(struct rt_pipe_device *pipe)
+int mkfifo(const char *path, mode_t mode)
{
+ rt_pipe_t *pipe;
+
+ pipe = rt_pipe_create(path);
if (pipe == RT_NULL)
- return;
-
- /* un-register pipe device */
- rt_pipe_detach(pipe);
-
- /* release memory */
- rt_free(pipe->ringbuffer.buffer_ptr);
- rt_free(pipe);
+ {
+ return -1;
+ }
- return;
+ return 0;
}
-RTM_EXPORT(rt_pipe_destroy);
-#endif /* RT_USING_HEAP */
+#endif
diff --git a/components/drivers/src/portal.c b/components/drivers/src/portal.c
deleted file mode 100644
index 779812e9ca..0000000000
--- a/components/drivers/src/portal.c
+++ /dev/null
@@ -1,256 +0,0 @@
-/*
- * File : portal.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2013, RT-Thread Development Team
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Change Logs:
- * Date Author Notes
- * 2013-08-19 Grissiom initial version
- */
-
-#include <rtthread.h>
-#include <rtdevice.h>
-
-#define PT_WRITE_DEV(pt) (((struct rt_portal_device*)pt)->write_dev)
-#define PT_READ_DEV(pt) (((struct rt_portal_device*)pt)->read_dev)
-
-static rt_err_t _portal_init(rt_device_t dev)
-{
- rt_err_t err;
- struct rt_portal_device *portal;
-
- RT_ASSERT(dev);
-
- portal = (struct rt_portal_device*)dev;
-
- err = rt_device_init(portal->write_dev);
- if (err != RT_EOK)
- return err;
-
- err = rt_device_init(portal->read_dev);
-
- return err;
-}
-
-static rt_err_t _portal_open(rt_device_t dev, rt_uint16_t oflag)
-{
- rt_err_t err;
- struct rt_portal_device *portal;
-
- RT_ASSERT(dev);
-
- if (!oflag)
- return -RT_ERROR;
-
- portal = (struct rt_portal_device*)dev;
-
- if (oflag & RT_DEVICE_OFLAG_RDONLY)
- {
- err = rt_device_open(portal->read_dev, RT_DEVICE_OFLAG_RDONLY);
- if (err != RT_EOK)
- return err;
- }
-
- if (oflag & RT_DEVICE_OFLAG_WRONLY)
- {
- err = rt_device_open(portal->write_dev, RT_DEVICE_OFLAG_WRONLY);
- if (err != RT_EOK)
- return err;
- }
-
- return RT_EOK;
-}
-
-static rt_err_t _portal_close(rt_device_t dev)
-{
- struct rt_portal_device *portal;
-
- RT_ASSERT(dev);
-
- portal = (struct rt_portal_device*)dev;
-
- rt_device_close(portal->write_dev);
- rt_device_close(portal->read_dev);
-
- return RT_EOK;
-}
-
-static rt_size_t _portal_read(rt_device_t dev,
- rt_off_t pos,
- void *buffer,
- rt_size_t size)
-{
- return rt_device_read(PT_READ_DEV(dev),
- pos, buffer, size);
-}
-
-static rt_size_t _portal_write(rt_device_t dev,
- rt_off_t pos,
- const void *buffer,
- rt_size_t size)
-{
- return rt_device_write(PT_WRITE_DEV(dev),
- pos, buffer, size);
-}
-
-static rt_err_t _portal_rx_indicate(rt_device_t dev, rt_size_t size)
-{
- struct rt_pipe_device *pipe;
-
- RT_ASSERT(dev && dev->type == RT_Device_Class_Pipe);
-
- pipe = (struct rt_pipe_device*)dev;
-
- if (pipe->read_portal->parent.rx_indicate)
- return pipe->read_portal->parent.rx_indicate(
- (rt_device_t)pipe->read_portal, size);
-
- return -RT_ENOSYS;
-}
-
-static rt_err_t _portal_tx_complete(rt_device_t dev, void *buf)
-{
- struct rt_pipe_device *pipe;
-
- RT_ASSERT(dev && dev->type == RT_Device_Class_Pipe);
-
- pipe = (struct rt_pipe_device*)dev;
-
- if (pipe->write_portal->parent.tx_complete)
- return pipe->write_portal->parent.tx_complete(
- (rt_device_t)pipe->write_portal, buf);
-
- return -RT_ENOSYS;
-}
-
-/**
- * This function will initialize a portal device and put it under control of
- * resource management.
- *
- * Portal is a device that connect devices
- *
- * Currently, you can only connect pipes in portal. Pipes are unidirectional.
- * But with portal, you can construct a bidirectional device with two pipes.
- * The inner connection is just like this:
- *
- * portal0 portal1
- * read || || write
- * <--<---||<---<---||<---<-- (pipe0)
- * || ||
- * -->--->||--->--->||--->--> (pipe1)
- * write || || read
- *
- * You will always construct two portals on two pipes, say, "portal0" and
- * "portal1". Data written into "portal0" can be retrieved in "portal1" and
- * vice versa. `rx_indicate` and `tx_complete` events are propagated
- * accordingly.
- *
- * @param portal the portal device
- * @param portal_name the name of the portal device
- * @param write_dev the name of the pipe device that this portal write into
- * @param read_dev the name of the pipe device that this portal read from
- *
- * @return the operation status, RT_EOK on successful. -RT_ENOSYS on one pipe
- * device could not be found.
- */
-rt_err_t rt_portal_init(struct rt_portal_device *portal,
- const char *portal_name,
- const char *write_dev,
- const char *read_dev)
-{
- rt_device_t dev;
-
- RT_ASSERT(portal);
-
- portal->parent.type = RT_Device_Class_Portal;
- portal->parent.init = _portal_init;
- portal->parent.open = _portal_open;
- portal->parent.close = _portal_close;
- portal->parent.write = _portal_write;
- portal->parent.read = _portal_read;
- /* single control of the two devices makes no sense */
- portal->parent.control = RT_NULL;
-
- dev = rt_device_find(write_dev);
- if (dev == RT_NULL)
- return -RT_ENOSYS;
- RT_ASSERT(dev->type == RT_Device_Class_Pipe);
- portal->write_dev = dev;
- rt_device_set_tx_complete(&portal->parent, dev->tx_complete);
- rt_device_set_tx_complete(dev, _portal_tx_complete);
- ((struct rt_pipe_device*)dev)->write_portal = portal;
-
- dev = rt_device_find(read_dev);
- if (dev == RT_NULL)
- {
- rt_device_set_tx_complete(dev, portal->parent.tx_complete);
- return -RT_ENOSYS;
- }
- RT_ASSERT(dev->type == RT_Device_Class_Pipe);
- portal->read_dev = dev;
- rt_device_set_rx_indicate(&portal->parent, dev->rx_indicate);
- rt_device_set_rx_indicate(dev, _portal_rx_indicate);
- ((struct rt_pipe_device*)dev)->read_portal = portal;
-
- return rt_device_register(&(portal->parent),
- portal_name,
- RT_DEVICE_FLAG_RDWR);
-}
-RTM_EXPORT(rt_portal_init);
-
-/**
- * This function will detach a portal device from resource management
- *
- * @param portal the portal device
- *
- * @return the operation status, RT_EOK on successful
- */
-rt_err_t rt_portal_detach(struct rt_portal_device *portal)
-{
- return rt_device_unregister(&portal->parent);
-}
-RTM_EXPORT(rt_portal_detach);
-
-#ifdef RT_USING_HEAP
-rt_err_t rt_portal_create(const char *name,
- const char *write_dev,
- const char *read_dev)
-{
- struct rt_portal_device *portal;
-
- portal = (struct rt_portal_device*)rt_calloc(1, sizeof(*portal));
- if (portal == RT_NULL)
- return -RT_ENOMEM;
-
- return rt_portal_init(portal, name, write_dev, read_dev);
-}
-RTM_EXPORT(rt_portal_create);
-
-void rt_portal_destroy(struct rt_portal_device *portal)
-{
- if (portal == RT_NULL)
- return;
-
- rt_portal_detach(portal);
-
- rt_free(portal);
-
- return;
-}
-RTM_EXPORT(rt_portal_destroy);
-#endif /* RT_USING_HEAP */
-
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
diff --git a/components/drivers/src/waitqueue.c b/components/drivers/src/waitqueue.c
new file mode 100644
index 0000000000..a5be7eb36e
--- /dev/null
+++ b/components/drivers/src/waitqueue.c
@@ -0,0 +1,101 @@
+#include <stdint.h>
+
+#include <rthw.h>
+#include <rtdevice.h>
+#include <rtservice.h>
+
+extern struct rt_thread *rt_current_thread;
+
+void rt_wqueue_add(rt_wqueue_t *queue, struct rt_wqueue_node *node)
+{
+ rt_base_t level;
+
+ level = rt_hw_interrupt_disable();
+ rt_list_insert_before(queue, &(node->list));
+ rt_hw_interrupt_enable(level);
+}
+
+void rt_wqueue_remove(struct rt_wqueue_node *node)
+{
+ rt_base_t level;
+
+ level = rt_hw_interrupt_disable();
+ rt_list_remove(&(node->list));
+ rt_hw_interrupt_enable(level);
+}
+
+int __wqueue_default_wake(struct rt_wqueue_node *wait, void *key)
+{
+ return 0;
+}
+
+void rt_wqueue_wakeup(rt_wqueue_t *queue, void *key)
+{
+ rt_base_t level;
+ register int need_schedule = 0;
+
+ struct rt_list_node *node;
+ struct rt_wqueue_node *entry;
+
+ if (rt_list_isempty(queue))
+ return;
+
+ level = rt_hw_interrupt_disable();
+ for (node = queue->next; node != queue; node = node->next)
+ {
+ entry = rt_list_entry(node, struct rt_wqueue_node, list);
+ if (entry->wakeup(entry, key) == 0)
+ {
+ rt_thread_resume(entry->polling_thread);
+ need_schedule = 1;
+
+ rt_wqueue_remove(entry);
+ break;
+ }
+ }
+ rt_hw_interrupt_enable(level);
+
+ if (need_schedule)
+ rt_schedule();
+}
+
+int rt_wqueue_wait(rt_wqueue_t *queue, int condition, int msec)
+{
+ int tick;
+ rt_thread_t tid = rt_current_thread;
+ rt_timer_t tmr = &(tid->thread_timer);
+ struct rt_wqueue_node __wait;
+
+ tick = rt_tick_from_millisecond(msec);
+
+ if ((condition) || (tick == 0))
+ return 0;
+
+ __wait.polling_thread = rt_thread_self();
+ __wait.key = 0;
+ __wait.wakeup = __wqueue_default_wake;
+ rt_list_init(&__wait.list);
+
+ rt_wqueue_add(queue, &__wait);
+
+ /* current context checking */
+ RT_DEBUG_NOT_IN_INTERRUPT;
+ rt_thread_suspend(tid);
+
+ /* start timer */
+ if (tick != RT_WAITING_FOREVER)
+ {
+ rt_timer_control(tmr,
+ RT_TIMER_CTRL_SET_TIME,
+ &tick);
+
+ rt_timer_start(tmr);
+ }
+
+ rt_schedule();
+
+ rt_wqueue_remove(&__wait);
+
+ return 0;
+}
+