rtdevice.h 5.1 KB
Newer Older
1
/*
qiuyiuestc's avatar
qiuyiuestc 已提交
2
 * File      : rtdevice.h
3 4 5 6 7 8 9 10 11 12 13 14
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006 - 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-01-08     bernard      first version.
 */

wuyangyong's avatar
wuyangyong 已提交
15 16 17 18 19
#ifndef __RT_DEVICE_H__
#define __RT_DEVICE_H__

#include <rtthread.h>

20
#define RT_DEVICE(device)            ((rt_device_t)device)
wuyangyong's avatar
wuyangyong 已提交
21 22 23 24 25 26 27 28 29 30

/* completion flag */
struct rt_completion
{
    rt_uint32_t flag;

    /* suspended list */
    rt_list_t suspended_list;
};

31 32
#define RT_RINGBUFFER_SIZE(rb)       ((rb)->write_index - (rb)->read_index)
#define RT_RINGBUFFER_EMPTY(rb)      ((rb)->buffer_size - RT_RINGBUFFER_SIZE(rb))
wuyangyong's avatar
wuyangyong 已提交
33 34 35 36 37 38 39 40 41
/* ring buffer */
struct rt_ringbuffer
{
    rt_uint16_t read_index, write_index;
    rt_uint8_t *buffer_ptr;
    rt_uint16_t buffer_size;
};

/* pipe device */
42
#define PIPE_DEVICE(device)          ((struct rt_pipe_device*)(device))
wuyangyong's avatar
wuyangyong 已提交
43 44
struct rt_pipe_device
{
45
    struct rt_device parent;
wuyangyong's avatar
wuyangyong 已提交
46

47 48
    /* ring buffer in pipe device */
    struct rt_ringbuffer ringbuffer;
wuyangyong's avatar
wuyangyong 已提交
49

50 51 52
    /* suspended list */
    rt_list_t suspended_read_list;
    rt_list_t suspended_write_list;
wuyangyong's avatar
wuyangyong 已提交
53 54 55 56 57 58 59 60
};

#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;
61 62
#define RT_DATAQUEUE_SIZE(dq)        ((dq)->put_index - (dq)->get_index)
#define RT_DATAQUEUE_EMPTY(dq)       ((dq)->size - RT_DATAQUEUE_SIZE(dq))
wuyangyong's avatar
wuyangyong 已提交
63 64 65 66 67
/* data queue implementation */
struct rt_data_queue
{
    rt_uint16_t size;
    rt_uint16_t lwm;
68
    rt_bool_t   waiting_lwm;
wuyangyong's avatar
wuyangyong 已提交
69 70 71 72 73 74 75 76 77 78

    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 */
79
    void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event);
wuyangyong's avatar
wuyangyong 已提交
80 81 82 83 84
};

/**
 * Completion
 */
85 86 87 88
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);
wuyangyong's avatar
wuyangyong 已提交
89 90 91 92 93 94 95

/**
 * RingBuffer for DeviceDriver
 *
 * Please note that the ring buffer implementation of RT-Thread
 * has no thread wait or resume feature.
 */
96 97 98 99 100 101 102 103 104 105 106 107 108
void rt_ringbuffer_init(struct rt_ringbuffer *rb,
                        rt_uint8_t           *pool,
                        rt_uint16_t           size);
rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
                            const rt_uint8_t     *ptr,
                            rt_uint16_t           length);
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb,
                                const rt_uint8_t      ch);
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);
rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
wuyangyong's avatar
wuyangyong 已提交
109
{
110 111
    RT_ASSERT(rb != RT_NULL);
    return rb->buffer_size;
wuyangyong's avatar
wuyangyong 已提交
112 113 114 115 116
}

/**
 * Pipe Device
 */
117 118
rt_err_t rt_pipe_create(const char *name, rt_size_t size);
void rt_pipe_destroy(struct rt_pipe_device *pipe);
wuyangyong's avatar
wuyangyong 已提交
119 120 121 122

/**
 * DataQueue for DeviceDriver
 */
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
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,
                            void                 *data_ptr,
                            rt_size_t             data_size,
                            rt_int32_t            timeout);
rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
                           void                **data_ptr,
                           rt_size_t            *size,
                           rt_int32_t            timeout);
rt_err_t rt_data_queue_peak(struct rt_data_queue *queue,
                            void                **data_ptr,
                            rt_size_t            *size);
void rt_data_queue_reset(struct rt_data_queue *queue);
wuyangyong's avatar
wuyangyong 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

#ifdef RT_USING_RTC
#include "drivers/rtc.h"
#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

#endif /* __RT_DEVICE_H__ */