2012-10-10 21:21:06 +08:00
|
|
|
/*
|
2012-10-12 01:03:31 +08:00
|
|
|
* File : rtdevice.h
|
2012-10-10 21:21:06 +08:00
|
|
|
* This file is part of RT-Thread RTOS
|
|
|
|
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
|
|
|
|
*
|
2013-06-28 00:36:54 +08:00
|
|
|
* 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.
|
2012-10-10 21:21:06 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2012-01-08 bernard first version.
|
2014-07-13 07:27:57 +08:00
|
|
|
* 2014-07-12 bernard Add workqueue implementation.
|
2012-10-10 21:21:06 +08:00
|
|
|
*/
|
|
|
|
|
2012-10-10 14:14:53 +08:00
|
|
|
#ifndef __RT_DEVICE_H__
|
|
|
|
#define __RT_DEVICE_H__
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
2015-04-24 11:42:40 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2012-10-10 21:21:06 +08:00
|
|
|
#define RT_DEVICE(device) ((rt_device_t)device)
|
2012-10-10 14:14:53 +08:00
|
|
|
|
|
|
|
/* completion flag */
|
|
|
|
struct rt_completion
|
|
|
|
{
|
|
|
|
rt_uint32_t flag;
|
|
|
|
|
|
|
|
/* suspended list */
|
|
|
|
rt_list_t suspended_list;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ring buffer */
|
|
|
|
struct rt_ringbuffer
|
|
|
|
{
|
|
|
|
rt_uint8_t *buffer_ptr;
|
2013-05-08 15:45:31 +08:00
|
|
|
/* use the msb of the {read,write}_index as mirror bit. You can see this as
|
|
|
|
* if the buffer adds a virtual mirror and the pointers point either to the
|
|
|
|
* normal or to the mirrored buffer. If the write_index has the same value
|
2013-08-19 10:50:20 +08:00
|
|
|
* with the read_index, but in a different mirror, the buffer is full.
|
|
|
|
* While if the write_index and the read_index are the same and within the
|
|
|
|
* same mirror, the buffer is empty. The ASCII art of the ringbuffer is:
|
2013-05-08 15:45:31 +08:00
|
|
|
*
|
|
|
|
* mirror = 0 mirror = 1
|
|
|
|
* +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
|
|
|
|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Full
|
|
|
|
* +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
|
|
|
|
* read_idx-^ write_idx-^
|
|
|
|
*
|
|
|
|
* +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
|
|
|
|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Empty
|
|
|
|
* +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
|
|
|
|
* read_idx-^ ^-write_idx
|
|
|
|
*
|
|
|
|
* The tradeoff is we could only use 32KiB of buffer for 16 bit of index.
|
|
|
|
* But it should be enough for most of the cases.
|
|
|
|
*
|
|
|
|
* Ref: http://en.wikipedia.org/wiki/Circular_buffer#Mirroring */
|
|
|
|
rt_uint16_t read_mirror : 1;
|
|
|
|
rt_uint16_t read_index : 15;
|
|
|
|
rt_uint16_t write_mirror : 1;
|
|
|
|
rt_uint16_t write_index : 15;
|
|
|
|
/* as we use msb of index as mirror bit, the size should be signed and
|
|
|
|
* could only be positive. */
|
|
|
|
rt_int16_t buffer_size;
|
2012-10-10 14:14:53 +08:00
|
|
|
};
|
|
|
|
|
2013-08-20 10:48:15 +08:00
|
|
|
/* portal device */
|
|
|
|
struct rt_portal_device
|
|
|
|
{
|
|
|
|
struct rt_device parent;
|
|
|
|
struct rt_device *write_dev;
|
|
|
|
struct rt_device *read_dev;
|
|
|
|
};
|
|
|
|
|
2012-10-10 14:14:53 +08:00
|
|
|
/* pipe device */
|
2012-10-10 21:21:06 +08:00
|
|
|
#define PIPE_DEVICE(device) ((struct rt_pipe_device*)(device))
|
2013-08-19 15:35:56 +08:00
|
|
|
enum rt_pipe_flag
|
|
|
|
{
|
|
|
|
/* both read and write won't block */
|
|
|
|
RT_PIPE_FLAG_NONBLOCK_RDWR = 0x00,
|
|
|
|
/* read would block */
|
|
|
|
RT_PIPE_FLAG_BLOCK_RD = 0x01,
|
|
|
|
/* write would block */
|
|
|
|
RT_PIPE_FLAG_BLOCK_WR = 0x02,
|
|
|
|
/* write to this pipe will discard some data when the pipe is full.
|
|
|
|
* When this flag is set, RT_PIPE_FLAG_BLOCK_WR will be ignored since write
|
|
|
|
* operation will always be success. */
|
|
|
|
RT_PIPE_FLAG_FORCE_WR = 0x04,
|
|
|
|
};
|
|
|
|
|
2012-10-10 14:14:53 +08:00
|
|
|
struct rt_pipe_device
|
|
|
|
{
|
2012-10-10 21:21:06 +08:00
|
|
|
struct rt_device parent;
|
2012-10-10 14:14:53 +08:00
|
|
|
|
2012-10-10 21:21:06 +08:00
|
|
|
/* ring buffer in pipe device */
|
|
|
|
struct rt_ringbuffer ringbuffer;
|
2012-10-10 14:14:53 +08:00
|
|
|
|
2013-08-19 15:35:56 +08:00
|
|
|
enum rt_pipe_flag flag;
|
|
|
|
|
2012-10-10 21:21:06 +08:00
|
|
|
/* suspended list */
|
|
|
|
rt_list_t suspended_read_list;
|
|
|
|
rt_list_t suspended_write_list;
|
2013-08-20 10:48:15 +08:00
|
|
|
|
|
|
|
struct rt_portal_device *write_portal;
|
|
|
|
struct rt_portal_device *read_portal;
|
2012-10-10 14:14:53 +08:00
|
|
|
};
|
|
|
|
|
2013-10-23 01:43:18 +08:00
|
|
|
#define PIPE_CTRL_GET_SPACE 0x14 /**< get the remaining size of a pipe device */
|
|
|
|
|
2012-10-10 14:14:53 +08:00
|
|
|
#define RT_DATAQUEUE_EVENT_UNKNOWN 0x00
|
|
|
|
#define RT_DATAQUEUE_EVENT_POP 0x01
|
|
|
|
#define RT_DATAQUEUE_EVENT_PUSH 0x02
|
|
|
|
#define RT_DATAQUEUE_EVENT_LWM 0x03
|
|
|
|
|
|
|
|
struct rt_data_item;
|
2012-10-10 21:21:06 +08:00
|
|
|
#define RT_DATAQUEUE_SIZE(dq) ((dq)->put_index - (dq)->get_index)
|
|
|
|
#define RT_DATAQUEUE_EMPTY(dq) ((dq)->size - RT_DATAQUEUE_SIZE(dq))
|
2012-10-10 14:14:53 +08:00
|
|
|
/* data queue implementation */
|
|
|
|
struct rt_data_queue
|
|
|
|
{
|
|
|
|
rt_uint16_t size;
|
|
|
|
rt_uint16_t lwm;
|
2012-10-10 21:21:06 +08:00
|
|
|
rt_bool_t waiting_lwm;
|
2012-10-10 14:14:53 +08:00
|
|
|
|
|
|
|
rt_uint16_t get_index;
|
|
|
|
rt_uint16_t put_index;
|
|
|
|
|
|
|
|
struct rt_data_item *queue;
|
|
|
|
|
|
|
|
rt_list_t suspended_push_list;
|
|
|
|
rt_list_t suspended_pop_list;
|
|
|
|
|
|
|
|
/* event notify */
|
2012-10-10 21:21:06 +08:00
|
|
|
void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event);
|
2012-10-10 14:14:53 +08:00
|
|
|
};
|
|
|
|
|
2014-07-13 07:27:57 +08:00
|
|
|
/* workqueue implementation */
|
|
|
|
struct rt_workqueue
|
|
|
|
{
|
|
|
|
rt_list_t work_list;
|
|
|
|
rt_thread_t work_thread;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct rt_work
|
|
|
|
{
|
|
|
|
rt_list_t list;
|
|
|
|
|
|
|
|
void (*work_func)(struct rt_work* work, void* work_data);
|
|
|
|
void *work_data;
|
|
|
|
};
|
|
|
|
|
2012-10-10 14:14:53 +08:00
|
|
|
/**
|
|
|
|
* Completion
|
|
|
|
*/
|
2012-10-10 21:21:06 +08:00
|
|
|
void rt_completion_init(struct rt_completion *completion);
|
|
|
|
rt_err_t rt_completion_wait(struct rt_completion *completion,
|
|
|
|
rt_int32_t timeout);
|
|
|
|
void rt_completion_done(struct rt_completion *completion);
|
2012-10-10 14:14:53 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* RingBuffer for DeviceDriver
|
|
|
|
*
|
|
|
|
* Please note that the ring buffer implementation of RT-Thread
|
|
|
|
* has no thread wait or resume feature.
|
|
|
|
*/
|
2012-10-10 21:21:06 +08:00
|
|
|
void rt_ringbuffer_init(struct rt_ringbuffer *rb,
|
|
|
|
rt_uint8_t *pool,
|
2013-05-08 15:45:31 +08:00
|
|
|
rt_int16_t size);
|
2012-10-10 21:21:06 +08:00
|
|
|
rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
|
|
|
|
const rt_uint8_t *ptr,
|
|
|
|
rt_uint16_t length);
|
2013-08-19 11:52:04 +08:00
|
|
|
rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb,
|
|
|
|
const rt_uint8_t *ptr,
|
|
|
|
rt_uint16_t length);
|
2012-10-10 21:21:06 +08:00
|
|
|
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb,
|
|
|
|
const rt_uint8_t ch);
|
2013-08-19 11:52:04 +08:00
|
|
|
rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb,
|
|
|
|
const rt_uint8_t ch);
|
2012-10-10 21:21:06 +08:00
|
|
|
rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
|
|
|
|
rt_uint8_t *ptr,
|
|
|
|
rt_uint16_t length);
|
|
|
|
rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch);
|
2013-08-19 10:50:20 +08:00
|
|
|
|
2013-08-19 11:52:04 +08:00
|
|
|
enum rt_ringbuffer_state
|
|
|
|
{
|
|
|
|
RT_RINGBUFFER_EMPTY,
|
|
|
|
RT_RINGBUFFER_FULL,
|
|
|
|
/* half full is neither full nor empty */
|
|
|
|
RT_RINGBUFFER_HALFFULL,
|
|
|
|
};
|
|
|
|
|
2012-10-10 21:21:06 +08:00
|
|
|
rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
|
2012-10-10 14:14:53 +08:00
|
|
|
{
|
2012-10-10 21:21:06 +08:00
|
|
|
RT_ASSERT(rb != RT_NULL);
|
|
|
|
return rb->buffer_size;
|
2012-10-10 14:14:53 +08:00
|
|
|
}
|
|
|
|
|
2013-08-19 11:52:04 +08:00
|
|
|
rt_inline enum rt_ringbuffer_state
|
|
|
|
rt_ringbuffer_status(struct rt_ringbuffer *rb)
|
2013-08-19 10:50:20 +08:00
|
|
|
{
|
|
|
|
if (rb->read_index == rb->write_index)
|
|
|
|
{
|
|
|
|
if (rb->read_mirror == rb->write_mirror)
|
2013-08-19 11:52:04 +08:00
|
|
|
return RT_RINGBUFFER_EMPTY;
|
2013-08-19 10:50:20 +08:00
|
|
|
else
|
2013-08-19 11:52:04 +08:00
|
|
|
return RT_RINGBUFFER_FULL;
|
2013-08-19 10:50:20 +08:00
|
|
|
}
|
2013-08-19 11:52:04 +08:00
|
|
|
return RT_RINGBUFFER_HALFFULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** return the size of data in rb */
|
|
|
|
rt_inline rt_uint16_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb)
|
|
|
|
{
|
|
|
|
switch (rt_ringbuffer_status(rb))
|
2013-08-19 10:50:20 +08:00
|
|
|
{
|
2013-08-19 11:52:04 +08:00
|
|
|
case RT_RINGBUFFER_EMPTY:
|
|
|
|
return 0;
|
|
|
|
case RT_RINGBUFFER_FULL:
|
|
|
|
return rb->buffer_size;
|
|
|
|
case RT_RINGBUFFER_HALFFULL:
|
|
|
|
default:
|
2013-08-19 10:50:20 +08:00
|
|
|
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);
|
2013-08-19 11:52:04 +08:00
|
|
|
};
|
2013-08-19 10:50:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** return the size of empty space in rb */
|
|
|
|
#define rt_ringbuffer_space_len(rb) ((rb)->buffer_size - rt_ringbuffer_data_len(rb))
|
|
|
|
|
2012-10-10 14:14:53 +08:00
|
|
|
/**
|
|
|
|
* Pipe Device
|
|
|
|
*/
|
2013-08-19 12:24:15 +08:00
|
|
|
rt_err_t rt_pipe_init(struct rt_pipe_device *pipe,
|
|
|
|
const char *name,
|
2013-08-19 15:35:56 +08:00
|
|
|
enum rt_pipe_flag flag,
|
2013-08-19 12:24:15 +08:00
|
|
|
rt_uint8_t *buf,
|
|
|
|
rt_size_t size);
|
|
|
|
rt_err_t rt_pipe_detach(struct rt_pipe_device *pipe);
|
|
|
|
#ifdef RT_USING_HEAP
|
2013-08-19 15:35:56 +08:00
|
|
|
rt_err_t rt_pipe_create(const char *name, enum rt_pipe_flag flag, rt_size_t size);
|
2012-10-10 21:21:06 +08:00
|
|
|
void rt_pipe_destroy(struct rt_pipe_device *pipe);
|
2013-08-19 12:24:15 +08:00
|
|
|
#endif
|
2012-10-10 14:14:53 +08:00
|
|
|
|
2013-08-20 10:48:15 +08:00
|
|
|
/**
|
|
|
|
* Portal for DeviceDriver
|
|
|
|
*/
|
|
|
|
|
|
|
|
rt_err_t rt_portal_init(struct rt_portal_device *portal,
|
|
|
|
const char *portal_name,
|
|
|
|
const char *write_dev,
|
|
|
|
const char *read_dev);
|
|
|
|
rt_err_t rt_portal_detach(struct rt_portal_device *portal);
|
|
|
|
|
|
|
|
#ifdef RT_USING_HEAP
|
|
|
|
rt_err_t rt_portal_create(const char *name,
|
|
|
|
const char *write_dev,
|
|
|
|
const char *read_dev);
|
|
|
|
void rt_portal_destroy(struct rt_portal_device *portal);
|
|
|
|
#endif
|
|
|
|
|
2012-10-10 14:14:53 +08:00
|
|
|
/**
|
|
|
|
* DataQueue for DeviceDriver
|
|
|
|
*/
|
2012-10-10 21:21:06 +08:00
|
|
|
rt_err_t rt_data_queue_init(struct rt_data_queue *queue,
|
|
|
|
rt_uint16_t size,
|
|
|
|
rt_uint16_t lwm,
|
|
|
|
void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event));
|
|
|
|
rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
|
2012-11-01 15:17:54 +08:00
|
|
|
const void *data_ptr,
|
2012-10-10 21:21:06 +08:00
|
|
|
rt_size_t data_size,
|
|
|
|
rt_int32_t timeout);
|
|
|
|
rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
|
2012-11-01 15:17:54 +08:00
|
|
|
const void **data_ptr,
|
2012-10-10 21:21:06 +08:00
|
|
|
rt_size_t *size,
|
|
|
|
rt_int32_t timeout);
|
|
|
|
rt_err_t rt_data_queue_peak(struct rt_data_queue *queue,
|
2012-11-01 15:17:54 +08:00
|
|
|
const void **data_ptr,
|
2012-10-10 21:21:06 +08:00
|
|
|
rt_size_t *size);
|
|
|
|
void rt_data_queue_reset(struct rt_data_queue *queue);
|
2012-10-10 14:14:53 +08:00
|
|
|
|
2014-07-13 07:27:57 +08:00
|
|
|
#ifdef RT_USING_HEAP
|
|
|
|
/**
|
|
|
|
* WorkQueue for DeviceDriver
|
|
|
|
*/
|
|
|
|
struct rt_workqueue *rt_workqueue_create(const char* name, rt_uint16_t stack_size, rt_uint8_t priority);
|
|
|
|
rt_err_t rt_workqueue_destroy(struct rt_workqueue* queue);
|
|
|
|
rt_err_t rt_workqueue_dowork(struct rt_workqueue* queue, struct rt_work* work);
|
|
|
|
rt_err_t rt_workqueue_cancel_work(struct rt_workqueue* queue, struct rt_work* work);
|
|
|
|
|
|
|
|
rt_inline void rt_work_init(struct rt_work* work, void (*work_func)(struct rt_work* work, void* work_data),
|
|
|
|
void* work_data)
|
|
|
|
{
|
|
|
|
rt_list_init(&(work->list));
|
|
|
|
work->work_func = work_func;
|
|
|
|
work->work_data = work_data;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-10 14:14:53 +08:00
|
|
|
#ifdef RT_USING_RTC
|
2013-01-08 22:40:58 +08:00
|
|
|
#include "drivers/rtc.h"
|
|
|
|
#ifdef RT_USING_ALARM
|
|
|
|
#include "drivers/alarm.h"
|
2012-10-28 23:31:22 +08:00
|
|
|
#endif
|
2012-10-10 14:14:53 +08:00
|
|
|
#endif /* RT_USING_RTC */
|
|
|
|
|
|
|
|
#ifdef RT_USING_SPI
|
|
|
|
#include "drivers/spi.h"
|
|
|
|
#endif /* RT_USING_SPI */
|
|
|
|
|
|
|
|
#ifdef RT_USING_MTD_NOR
|
|
|
|
#include "drivers/mtd_nor.h"
|
|
|
|
#endif /* RT_USING_MTD_NOR */
|
|
|
|
|
|
|
|
#ifdef RT_USING_MTD_NAND
|
|
|
|
#include "drivers/mtd_nand.h"
|
|
|
|
#endif /* RT_USING_MTD_NAND */
|
|
|
|
|
|
|
|
#ifdef RT_USING_USB_DEVICE
|
|
|
|
#include "drivers/usb_device.h"
|
|
|
|
#endif /* RT_USING_USB_DEVICE */
|
|
|
|
|
|
|
|
#ifdef RT_USING_USB_HOST
|
|
|
|
#include "drivers/usb_host.h"
|
|
|
|
#endif /* RT_USING_USB_HOST */
|
|
|
|
|
|
|
|
#ifdef RT_USING_SERIAL
|
|
|
|
#include "drivers/serial.h"
|
|
|
|
#endif /* RT_USING_SERIAL */
|
|
|
|
|
|
|
|
#ifdef RT_USING_I2C
|
|
|
|
#include "drivers/i2c.h"
|
|
|
|
#include "drivers/i2c_dev.h"
|
|
|
|
|
|
|
|
#ifdef RT_USING_I2C_BITOPS
|
|
|
|
#include "drivers/i2c-bit-ops.h"
|
|
|
|
#endif /* RT_USING_I2C_BITOPS */
|
|
|
|
#endif /* RT_USING_I2C */
|
|
|
|
|
|
|
|
#ifdef RT_USING_SDIO
|
|
|
|
#include "drivers/mmcsd_core.h"
|
|
|
|
#include "drivers/sd.h"
|
|
|
|
#include "drivers/sdio.h"
|
|
|
|
#endif
|
|
|
|
|
2014-03-04 14:32:56 +08:00
|
|
|
#ifdef RT_USING_WDT
|
|
|
|
#include "drivers/watchdog.h"
|
|
|
|
#endif
|
|
|
|
|
2015-01-20 15:24:49 +08:00
|
|
|
#ifdef RT_USING_PIN
|
|
|
|
#include "drivers/pin.h"
|
|
|
|
#endif
|
|
|
|
|
2015-05-14 15:44:06 +08:00
|
|
|
#ifdef RT_USING_CAN
|
|
|
|
#include "drivers/can.h"
|
|
|
|
#endif
|
|
|
|
|
2015-09-02 22:00:24 +08:00
|
|
|
#ifdef RT_USING_HWTIMER
|
|
|
|
#include "drivers/hwtimer.h"
|
|
|
|
#endif
|
|
|
|
|
2015-04-24 11:42:40 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-10 14:14:53 +08:00
|
|
|
#endif /* __RT_DEVICE_H__ */
|
|
|
|
|