[DeviceDrivers] change cmd type.
1. Change 'rt_uint8_t' type of cmd to 'int'; 2. Add waitqueue; 3. Split device ipc header files;
This commit is contained in:
parent
0f5a68a55e
commit
d7087fdd3b
|
@ -293,7 +293,7 @@ static rt_size_t _audio_dev_write(struct rt_device *dev, rt_off_t pos, const voi
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t _audio_dev_control(struct rt_device *dev, rt_uint8_t cmd, void *args)
|
static rt_err_t _audio_dev_control(struct rt_device *dev, int cmd, void *args)
|
||||||
{
|
{
|
||||||
rt_err_t result = RT_EOK;
|
rt_err_t result = RT_EOK;
|
||||||
struct rt_audio_device *audio;
|
struct rt_audio_device *audio;
|
||||||
|
|
|
@ -206,7 +206,7 @@ static rt_size_t rt_hwtimer_write(struct rt_device *dev, rt_off_t pos, const voi
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t rt_hwtimer_control(struct rt_device *dev, rt_uint8_t cmd, void *args)
|
static rt_err_t rt_hwtimer_control(struct rt_device *dev, int cmd, void *args)
|
||||||
{
|
{
|
||||||
rt_err_t result = RT_EOK;
|
rt_err_t result = RT_EOK;
|
||||||
rt_hwtimer_t *timer;
|
rt_hwtimer_t *timer;
|
||||||
|
|
|
@ -47,7 +47,7 @@ static rt_err_t fm24clxx_close(rt_device_t dev)
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t fm24clxx_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
static rt_err_t fm24clxx_control(rt_device_t dev, int cmd, void *args)
|
||||||
{
|
{
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,7 +66,7 @@ static rt_size_t i2c_bus_device_write(rt_device_t dev,
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t i2c_bus_device_control(rt_device_t dev,
|
static rt_err_t i2c_bus_device_control(rt_device_t dev,
|
||||||
rt_uint8_t cmd,
|
int cmd,
|
||||||
void *args)
|
void *args)
|
||||||
{
|
{
|
||||||
rt_err_t ret;
|
rt_err_t ret;
|
||||||
|
|
|
@ -76,7 +76,7 @@ struct rt_alarm_container
|
||||||
|
|
||||||
rt_alarm_t rt_alarm_create(rt_alarm_callback_t callback,
|
rt_alarm_t rt_alarm_create(rt_alarm_callback_t callback,
|
||||||
struct rt_alarm_setup *setup);
|
struct rt_alarm_setup *setup);
|
||||||
rt_err_t rt_alarm_control(rt_alarm_t alarm, rt_uint8_t cmd, void *arg);
|
rt_err_t rt_alarm_control(rt_alarm_t alarm, int cmd, void *arg);
|
||||||
void rt_alarm_update(rt_device_t dev, rt_uint32_t event);
|
void rt_alarm_update(rt_device_t dev, rt_uint32_t event);
|
||||||
rt_err_t rt_alarm_delete(rt_alarm_t alarm);
|
rt_err_t rt_alarm_delete(rt_alarm_t alarm);
|
||||||
rt_err_t rt_alarm_start(rt_alarm_t alarm);
|
rt_err_t rt_alarm_start(rt_alarm_t alarm);
|
||||||
|
|
|
@ -159,7 +159,7 @@ struct rt_audio_ops
|
||||||
rt_err_t (*suspend) (struct rt_audio_device *audio,int stream);
|
rt_err_t (*suspend) (struct rt_audio_device *audio,int stream);
|
||||||
rt_err_t (*resume) (struct rt_audio_device *audio,int stream);
|
rt_err_t (*resume) (struct rt_audio_device *audio,int stream);
|
||||||
|
|
||||||
rt_err_t (*control) (struct rt_audio_device *audio, rt_uint8_t cmd, void *arg);
|
rt_err_t (*control) (struct rt_audio_device *audio, int cmd, void *arg);
|
||||||
rt_size_t (*transmit) (struct rt_audio_device *audio, const void *writeBuf,void *readBuf, rt_size_t size);
|
rt_size_t (*transmit) (struct rt_audio_device *audio, const void *writeBuf,void *readBuf, rt_size_t size);
|
||||||
|
|
||||||
//get page size of codec or private buffer's info
|
//get page size of codec or private buffer's info
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
#ifndef COMPLETION_H_
|
||||||
|
#define COMPLETION_H_
|
||||||
|
|
||||||
|
#include <rtthread.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Completion
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct rt_completion
|
||||||
|
{
|
||||||
|
rt_uint32_t flag;
|
||||||
|
|
||||||
|
/* suspended list */
|
||||||
|
rt_list_t suspended_list;
|
||||||
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,53 @@
|
||||||
|
#ifndef DATAQUEUE_H__
|
||||||
|
#define DATAQUEUE_H__
|
||||||
|
|
||||||
|
#include <rtthread.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
#define RT_DATAQUEUE_SIZE(dq) ((dq)->put_index - (dq)->get_index)
|
||||||
|
#define RT_DATAQUEUE_EMPTY(dq) ((dq)->size - RT_DATAQUEUE_SIZE(dq))
|
||||||
|
/* data queue implementation */
|
||||||
|
struct rt_data_queue
|
||||||
|
{
|
||||||
|
rt_uint16_t size;
|
||||||
|
rt_uint16_t lwm;
|
||||||
|
rt_bool_t waiting_lwm;
|
||||||
|
|
||||||
|
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 */
|
||||||
|
void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DataQueue for DeviceDriver
|
||||||
|
*/
|
||||||
|
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,
|
||||||
|
const void *data_ptr,
|
||||||
|
rt_size_t data_size,
|
||||||
|
rt_int32_t timeout);
|
||||||
|
rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
|
||||||
|
const void **data_ptr,
|
||||||
|
rt_size_t *size,
|
||||||
|
rt_int32_t timeout);
|
||||||
|
rt_err_t rt_data_queue_peak(struct rt_data_queue *queue,
|
||||||
|
const void **data_ptr,
|
||||||
|
rt_size_t *size);
|
||||||
|
void rt_data_queue_reset(struct rt_data_queue *queue);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,36 @@
|
||||||
|
#ifndef PIPE_H__
|
||||||
|
#define PIPE_H__
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pipe Device
|
||||||
|
*/
|
||||||
|
#include <rtthread.h>
|
||||||
|
#include <rtdevice.h>
|
||||||
|
|
||||||
|
#ifndef RT_PIPE_BUFSZ
|
||||||
|
#define PIPE_BUFSZ 512
|
||||||
|
#else
|
||||||
|
#define PIPE_BUFSZ RT_PIPE_BUFSZ
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct rt_pipe_device
|
||||||
|
{
|
||||||
|
struct rt_device parent;
|
||||||
|
|
||||||
|
/* ring buffer in pipe device */
|
||||||
|
struct rt_ringbuffer *fifo;
|
||||||
|
|
||||||
|
rt_uint8_t readers;
|
||||||
|
rt_uint8_t writers;
|
||||||
|
|
||||||
|
rt_wqueue_t reader_queue;
|
||||||
|
rt_wqueue_t writer_queue;
|
||||||
|
|
||||||
|
struct rt_mutex lock;
|
||||||
|
};
|
||||||
|
typedef struct rt_pipe_device rt_pipe_t;
|
||||||
|
|
||||||
|
rt_pipe_t *rt_pipe_create(const char *name);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
/*
|
||||||
|
* File : poll.h
|
||||||
|
* This file is part of Device File System in RT-Thread RTOS
|
||||||
|
* COPYRIGHT (C) 2006-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
|
||||||
|
* 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
|
||||||
|
* 2016-09-19 Heyuanjie The first version.
|
||||||
|
* 2016-12-26 Bernard Update poll interface
|
||||||
|
*/
|
||||||
|
#ifndef IPC_POLL_H__
|
||||||
|
#define IPC_POLL_H__
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct rt_pollreq;
|
||||||
|
typedef void (*poll_queue_proc)(rt_wqueue_t *, struct rt_pollreq *);
|
||||||
|
|
||||||
|
typedef struct rt_pollreq
|
||||||
|
{
|
||||||
|
poll_queue_proc _proc;
|
||||||
|
short _key;
|
||||||
|
} rt_pollreq_t;
|
||||||
|
|
||||||
|
rt_inline void rt_poll_add(rt_wqueue_t *wq, rt_pollreq_t *req)
|
||||||
|
{
|
||||||
|
if (req && req->_proc && wq)
|
||||||
|
{
|
||||||
|
req->_proc(wq, req);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,88 @@
|
||||||
|
#ifndef RINGBUFFER_H__
|
||||||
|
#define RINGBUFFER_H__
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <rtthread.h>
|
||||||
|
|
||||||
|
/* ring buffer */
|
||||||
|
struct rt_ringbuffer
|
||||||
|
{
|
||||||
|
rt_uint8_t *buffer_ptr;
|
||||||
|
/* 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
|
||||||
|
* 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:
|
||||||
|
*
|
||||||
|
* 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;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum rt_ringbuffer_state
|
||||||
|
{
|
||||||
|
RT_RINGBUFFER_EMPTY,
|
||||||
|
RT_RINGBUFFER_FULL,
|
||||||
|
/* half full is neither full nor empty */
|
||||||
|
RT_RINGBUFFER_HALFFULL,
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RingBuffer for DeviceDriver
|
||||||
|
*
|
||||||
|
* Please note that the ring buffer implementation of RT-Thread
|
||||||
|
* has no thread wait or resume feature.
|
||||||
|
*/
|
||||||
|
void rt_ringbuffer_init(struct rt_ringbuffer *rb, rt_uint8_t *pool, rt_int16_t size);
|
||||||
|
void rt_ringbuffer_reset(struct rt_ringbuffer *rb);
|
||||||
|
rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint16_t length);
|
||||||
|
rt_size_t rt_ringbuffer_put_force(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_putchar_force(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_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb);
|
||||||
|
|
||||||
|
#ifdef RT_USING_HEAP
|
||||||
|
struct rt_ringbuffer* rt_ringbuffer_create(rt_uint16_t length);
|
||||||
|
void rt_ringbuffer_destroy(struct rt_ringbuffer *rb);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
|
||||||
|
{
|
||||||
|
RT_ASSERT(rb != RT_NULL);
|
||||||
|
return rb->buffer_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** return the size of empty space in rb */
|
||||||
|
#define rt_ringbuffer_space_len(rb) ((rb)->buffer_size - rt_ringbuffer_data_len(rb))
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,39 @@
|
||||||
|
#ifndef WAITQUEUE_H__
|
||||||
|
#define WAITQUEUE_H__
|
||||||
|
|
||||||
|
#include <rtthread.h>
|
||||||
|
|
||||||
|
struct rt_wqueue_node;
|
||||||
|
|
||||||
|
typedef rt_list_t rt_wqueue_t;
|
||||||
|
typedef int (*rt_wqueue_func_t)(struct rt_wqueue_node *wait, void *key);
|
||||||
|
|
||||||
|
struct rt_wqueue_node
|
||||||
|
{
|
||||||
|
rt_thread_t polling_thread;
|
||||||
|
rt_list_t list;
|
||||||
|
|
||||||
|
rt_wqueue_func_t wakeup;
|
||||||
|
rt_uint32_t key;
|
||||||
|
};
|
||||||
|
typedef struct rt_wqueue_node rt_wqueue_node_t;
|
||||||
|
|
||||||
|
int __wqueue_default_wake(struct rt_wqueue_node *wait, void *key);
|
||||||
|
|
||||||
|
void rt_wqueue_add(rt_wqueue_t *queue, struct rt_wqueue_node *node);
|
||||||
|
void rt_wqueue_remove(struct rt_wqueue_node *node);
|
||||||
|
int rt_wqueue_wait(rt_wqueue_t *queue, int condition, int timeout);
|
||||||
|
void rt_wqueue_wakeup(rt_wqueue_t *queue, void *key);
|
||||||
|
|
||||||
|
#define DEFINE_WAIT_FUNC(name, function) \
|
||||||
|
struct rt_wqueue_node name = { \
|
||||||
|
rt_current_thread, \
|
||||||
|
RT_LIST_OBJECT_INIT(((name).list)), \
|
||||||
|
\
|
||||||
|
function, \
|
||||||
|
0 \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, __wqueue_default_wake)
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,41 @@
|
||||||
|
#ifndef WORKQUEUE_H__
|
||||||
|
#define WORKQUEUE_H__
|
||||||
|
|
||||||
|
#include <rtthread.h>
|
||||||
|
|
||||||
|
/* workqueue implementation */
|
||||||
|
struct rt_workqueue
|
||||||
|
{
|
||||||
|
rt_list_t work_list;
|
||||||
|
struct rt_work *work_current; /* current work */
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
@ -28,290 +28,20 @@
|
||||||
|
|
||||||
#include <rtthread.h>
|
#include <rtthread.h>
|
||||||
|
|
||||||
|
#include "ipc/ringbuffer.h"
|
||||||
|
#include "ipc/completion.h"
|
||||||
|
#include "ipc/dataqueue.h"
|
||||||
|
#include "ipc/workqueue.h"
|
||||||
|
#include "ipc/waitqueue.h"
|
||||||
|
#include "ipc/pipe.h"
|
||||||
|
#include "ipc/poll.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define RT_DEVICE(device) ((rt_device_t)device)
|
#define RT_DEVICE(device) ((rt_device_t)device)
|
||||||
|
|
||||||
/* 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;
|
|
||||||
/* 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
|
|
||||||
* 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:
|
|
||||||
*
|
|
||||||
* 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;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* portal device */
|
|
||||||
struct rt_portal_device
|
|
||||||
{
|
|
||||||
struct rt_device parent;
|
|
||||||
struct rt_device *write_dev;
|
|
||||||
struct rt_device *read_dev;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* pipe device */
|
|
||||||
#define PIPE_DEVICE(device) ((struct rt_pipe_device*)(device))
|
|
||||||
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,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct rt_pipe_device
|
|
||||||
{
|
|
||||||
struct rt_device parent;
|
|
||||||
|
|
||||||
/* ring buffer in pipe device */
|
|
||||||
struct rt_ringbuffer ringbuffer;
|
|
||||||
|
|
||||||
enum rt_pipe_flag flag;
|
|
||||||
|
|
||||||
/* suspended list */
|
|
||||||
rt_list_t suspended_read_list;
|
|
||||||
rt_list_t suspended_write_list;
|
|
||||||
|
|
||||||
struct rt_portal_device *write_portal;
|
|
||||||
struct rt_portal_device *read_portal;
|
|
||||||
};
|
|
||||||
|
|
||||||
#define PIPE_CTRL_GET_SPACE 0x14 /**< get the remaining size of a pipe device */
|
|
||||||
|
|
||||||
#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;
|
|
||||||
#define RT_DATAQUEUE_SIZE(dq) ((dq)->put_index - (dq)->get_index)
|
|
||||||
#define RT_DATAQUEUE_EMPTY(dq) ((dq)->size - RT_DATAQUEUE_SIZE(dq))
|
|
||||||
/* data queue implementation */
|
|
||||||
struct rt_data_queue
|
|
||||||
{
|
|
||||||
rt_uint16_t size;
|
|
||||||
rt_uint16_t lwm;
|
|
||||||
|
|
||||||
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 */
|
|
||||||
void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event);
|
|
||||||
};
|
|
||||||
|
|
||||||
/* workqueue implementation */
|
|
||||||
struct rt_workqueue
|
|
||||||
{
|
|
||||||
rt_list_t work_list;
|
|
||||||
struct rt_work *work_current; /* current work */
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Completion
|
|
||||||
*/
|
|
||||||
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);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* RingBuffer for DeviceDriver
|
|
||||||
*
|
|
||||||
* Please note that the ring buffer implementation of RT-Thread
|
|
||||||
* has no thread wait or resume feature.
|
|
||||||
*/
|
|
||||||
void rt_ringbuffer_init(struct rt_ringbuffer *rb,
|
|
||||||
rt_uint8_t *pool,
|
|
||||||
rt_int16_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_put_force(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_putchar_force(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);
|
|
||||||
|
|
||||||
enum rt_ringbuffer_state
|
|
||||||
{
|
|
||||||
RT_RINGBUFFER_EMPTY,
|
|
||||||
RT_RINGBUFFER_FULL,
|
|
||||||
/* half full is neither full nor empty */
|
|
||||||
RT_RINGBUFFER_HALFFULL,
|
|
||||||
};
|
|
||||||
|
|
||||||
rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
|
|
||||||
{
|
|
||||||
RT_ASSERT(rb != RT_NULL);
|
|
||||||
return rb->buffer_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 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))
|
|
||||||
{
|
|
||||||
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);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/** return the size of empty space in rb */
|
|
||||||
#define rt_ringbuffer_space_len(rb) ((rb)->buffer_size - rt_ringbuffer_data_len(rb))
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Pipe Device
|
|
||||||
*/
|
|
||||||
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_err_t rt_pipe_detach(struct rt_pipe_device *pipe);
|
|
||||||
#ifdef RT_USING_HEAP
|
|
||||||
rt_err_t rt_pipe_create(const char *name, enum rt_pipe_flag flag, rt_size_t size);
|
|
||||||
void rt_pipe_destroy(struct rt_pipe_device *pipe);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
|
|
||||||
/**
|
|
||||||
* DataQueue for DeviceDriver
|
|
||||||
*/
|
|
||||||
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,
|
|
||||||
const void *data_ptr,
|
|
||||||
rt_size_t data_size,
|
|
||||||
rt_int32_t timeout);
|
|
||||||
rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
|
|
||||||
const void **data_ptr,
|
|
||||||
rt_size_t *size,
|
|
||||||
rt_int32_t timeout);
|
|
||||||
rt_err_t rt_data_queue_peak(struct rt_data_queue *queue,
|
|
||||||
const void **data_ptr,
|
|
||||||
rt_size_t *size);
|
|
||||||
void rt_data_queue_reset(struct rt_data_queue *queue);
|
|
||||||
|
|
||||||
#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
|
|
||||||
|
|
||||||
#ifdef RT_USING_RTC
|
#ifdef RT_USING_RTC
|
||||||
#include "drivers/rtc.h"
|
#include "drivers/rtc.h"
|
||||||
#ifdef RT_USING_ALARM
|
#ifdef RT_USING_ALARM
|
||||||
|
@ -383,4 +113,3 @@ rt_inline void rt_work_init(struct rt_work* work, void (*work_func)(struct rt_wo
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* __RT_DEVICE_H__ */
|
#endif /* __RT_DEVICE_H__ */
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,7 @@ static rt_size_t _pin_write(rt_device_t dev, rt_off_t pos, const void *buffer, r
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t _pin_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
static rt_err_t _pin_control(rt_device_t dev, int cmd, void *args)
|
||||||
{
|
{
|
||||||
struct rt_device_pin_mode *mode;
|
struct rt_device_pin_mode *mode;
|
||||||
struct rt_device_pin *pin = (struct rt_device_pin *)dev;
|
struct rt_device_pin *pin = (struct rt_device_pin *)dev;
|
||||||
|
|
|
@ -64,7 +64,7 @@ static rt_size_t _mtd_write(rt_device_t dev,
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t _mtd_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
static rt_err_t _mtd_control(rt_device_t dev, int cmd, void *args)
|
||||||
{
|
{
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,7 @@ static rt_size_t _mtd_write(rt_device_t dev,
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t _mtd_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
static rt_err_t _mtd_control(rt_device_t dev, int cmd, void *args)
|
||||||
{
|
{
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
|
@ -397,7 +397,7 @@ void rt_alarm_update(rt_device_t dev, rt_uint32_t event)
|
||||||
* \param cmd control command
|
* \param cmd control command
|
||||||
* \param arg argument
|
* \param arg argument
|
||||||
*/
|
*/
|
||||||
rt_err_t rt_alarm_control(rt_alarm_t alarm, rt_uint8_t cmd, void *arg)
|
rt_err_t rt_alarm_control(rt_alarm_t alarm, int cmd, void *arg)
|
||||||
{
|
{
|
||||||
rt_err_t ret = RT_ERROR;
|
rt_err_t ret = RT_ERROR;
|
||||||
|
|
||||||
|
|
|
@ -222,7 +222,7 @@ static rt_err_t rt_mmcsd_close(rt_device_t dev)
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t rt_mmcsd_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
static rt_err_t rt_mmcsd_control(rt_device_t dev, int cmd, void *args)
|
||||||
{
|
{
|
||||||
struct mmcsd_blk_device *blk_dev = (struct mmcsd_blk_device *)dev->user_data;
|
struct mmcsd_blk_device *blk_dev = (struct mmcsd_blk_device *)dev->user_data;
|
||||||
switch (cmd)
|
switch (cmd)
|
||||||
|
@ -247,7 +247,7 @@ static rt_size_t rt_mmcsd_read(rt_device_t dev,
|
||||||
|
|
||||||
if (dev == RT_NULL)
|
if (dev == RT_NULL)
|
||||||
{
|
{
|
||||||
rt_set_errno(-DFS_STATUS_EINVAL);
|
rt_set_errno(-EINVAL);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -259,7 +259,7 @@ static rt_size_t rt_mmcsd_read(rt_device_t dev,
|
||||||
/* the length of reading must align to SECTOR SIZE */
|
/* the length of reading must align to SECTOR SIZE */
|
||||||
if (err)
|
if (err)
|
||||||
{
|
{
|
||||||
rt_set_errno(-DFS_STATUS_EIO);
|
rt_set_errno(-EIO);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
|
@ -276,7 +276,7 @@ static rt_size_t rt_mmcsd_write(rt_device_t dev,
|
||||||
|
|
||||||
if (dev == RT_NULL)
|
if (dev == RT_NULL)
|
||||||
{
|
{
|
||||||
rt_set_errno(-DFS_STATUS_EINVAL);
|
rt_set_errno(-EINVAL);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -288,7 +288,7 @@ static rt_size_t rt_mmcsd_write(rt_device_t dev,
|
||||||
/* the length of reading must align to SECTOR SIZE */
|
/* the length of reading must align to SECTOR SIZE */
|
||||||
if (err)
|
if (err)
|
||||||
{
|
{
|
||||||
rt_set_errno(-DFS_STATUS_EIO);
|
rt_set_errno(-EIO);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,178 @@
|
||||||
#include <rtthread.h>
|
#include <rtthread.h>
|
||||||
#include <rtdevice.h>
|
#include <rtdevice.h>
|
||||||
|
|
||||||
|
// #define DEBUG_ENABLE
|
||||||
|
#define DEBUG_LEVEL DBG_LOG
|
||||||
|
#define DBG_SECTION_NAME "[UART]"
|
||||||
|
#define DEBUG_COLOR
|
||||||
|
#include <rtdbg.h>
|
||||||
|
|
||||||
|
#ifdef RT_USING_POSIX_TERMIOS
|
||||||
|
#include <posix_termios.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef RT_USING_DFS_DEVFS
|
||||||
|
#include <dfs_posix.h>
|
||||||
|
|
||||||
|
/* it's possible the 'getc/putc' is defined by stdio.h in gcc/newlib. */
|
||||||
|
#ifdef getc
|
||||||
|
#undef getc
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef putc
|
||||||
|
#undef putc
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static rt_err_t serial_fops_rx_ind(rt_device_t dev, rt_size_t size)
|
||||||
|
{
|
||||||
|
rt_wqueue_wakeup(&(dev->wait_queue), (void*)POLLIN);
|
||||||
|
|
||||||
|
return RT_EOK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* fops for serial */
|
||||||
|
static int serial_fops_open(struct dfs_fd *fd)
|
||||||
|
{
|
||||||
|
rt_err_t ret = 0;
|
||||||
|
rt_uint16_t flags = 0;
|
||||||
|
rt_device_t device;
|
||||||
|
|
||||||
|
device = (rt_device_t)fd->data;
|
||||||
|
RT_ASSERT(device != RT_NULL);
|
||||||
|
|
||||||
|
switch (fd->flags & O_ACCMODE)
|
||||||
|
{
|
||||||
|
case O_RDONLY:
|
||||||
|
dbg_log(DBG_LOG, "fops open: O_RDONLY!\n");
|
||||||
|
flags = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_RDONLY;
|
||||||
|
break;
|
||||||
|
case O_WRONLY:
|
||||||
|
dbg_log(DBG_LOG, "fops open: O_WRONLY!\n");
|
||||||
|
flags = RT_DEVICE_FLAG_WRONLY;
|
||||||
|
break;
|
||||||
|
case O_RDWR:
|
||||||
|
dbg_log(DBG_LOG, "fops open: O_RDWR!\n");
|
||||||
|
flags = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_RDWR;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
dbg_log(DBG_ERROR, "fops open: unknown mode - %d!\n", fd->flags & O_ACCMODE);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
rt_device_set_rx_indicate(device, serial_fops_rx_ind);
|
||||||
|
ret = rt_device_open(device, flags);
|
||||||
|
if (ret == RT_EOK) return 0;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int serial_fops_close(struct dfs_fd *fd)
|
||||||
|
{
|
||||||
|
rt_device_t device;
|
||||||
|
|
||||||
|
device = (rt_device_t)fd->data;
|
||||||
|
|
||||||
|
rt_device_set_rx_indicate(device, RT_NULL);
|
||||||
|
rt_device_close(device);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int serial_fops_ioctl(struct dfs_fd *fd, int cmd, void *args)
|
||||||
|
{
|
||||||
|
rt_device_t device;
|
||||||
|
|
||||||
|
device = (rt_device_t)fd->data;
|
||||||
|
switch (cmd)
|
||||||
|
{
|
||||||
|
case FIONREAD:
|
||||||
|
break;
|
||||||
|
case FIONWRITE:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rt_device_control(device, cmd, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int serial_fops_read(struct dfs_fd *fd, void *buf, size_t count)
|
||||||
|
{
|
||||||
|
int size = 0;
|
||||||
|
rt_device_t device;
|
||||||
|
|
||||||
|
device = (rt_device_t)fd->data;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
size = rt_device_read(device, -1, buf, count);
|
||||||
|
if (size <= 0)
|
||||||
|
{
|
||||||
|
if (fd->flags & O_NONBLOCK)
|
||||||
|
{
|
||||||
|
size = -EAGAIN;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
rt_wqueue_wait(&(device->wait_queue), 0, RT_WAITING_FOREVER);
|
||||||
|
}
|
||||||
|
}while (size <= 0);
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int serial_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
|
||||||
|
{
|
||||||
|
rt_device_t device;
|
||||||
|
|
||||||
|
device = (rt_device_t)fd->data;
|
||||||
|
return rt_device_write(device, -1, buf, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int serial_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
|
||||||
|
{
|
||||||
|
int mask = 0;
|
||||||
|
int flags = 0;
|
||||||
|
rt_device_t device;
|
||||||
|
struct rt_serial_device *serial;
|
||||||
|
|
||||||
|
device = (rt_device_t)fd->data;
|
||||||
|
RT_ASSERT(device != RT_NULL);
|
||||||
|
|
||||||
|
serial = (struct rt_serial_device *)device;
|
||||||
|
|
||||||
|
/* only support POLLIN */
|
||||||
|
flags = fd->flags & O_ACCMODE;
|
||||||
|
if (flags == O_RDONLY || flags == O_RDWR)
|
||||||
|
{
|
||||||
|
rt_base_t level;
|
||||||
|
struct rt_serial_rx_fifo* rx_fifo;
|
||||||
|
|
||||||
|
rt_poll_add(&(device->wait_queue), req);
|
||||||
|
|
||||||
|
rx_fifo = (struct rt_serial_rx_fifo*) serial->serial_rx;
|
||||||
|
|
||||||
|
level = rt_hw_interrupt_disable();
|
||||||
|
if (rx_fifo->get_index != rx_fifo->put_index)
|
||||||
|
mask |= POLLIN;
|
||||||
|
rt_hw_interrupt_enable(level);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
const static struct dfs_file_ops _serial_fops =
|
||||||
|
{
|
||||||
|
serial_fops_open,
|
||||||
|
serial_fops_close,
|
||||||
|
serial_fops_ioctl,
|
||||||
|
serial_fops_read,
|
||||||
|
serial_fops_write,
|
||||||
|
RT_NULL, /* flush */
|
||||||
|
RT_NULL, /* lseek */
|
||||||
|
RT_NULL, /* getdents */
|
||||||
|
serial_fops_poll,
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Serial poll routines
|
* Serial poll routines
|
||||||
*/
|
*/
|
||||||
|
@ -371,11 +543,14 @@ static rt_err_t rt_serial_init(struct rt_device *dev)
|
||||||
|
|
||||||
static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
|
static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
|
||||||
{
|
{
|
||||||
|
rt_uint16_t stream_flag = 0;
|
||||||
struct rt_serial_device *serial;
|
struct rt_serial_device *serial;
|
||||||
|
|
||||||
RT_ASSERT(dev != RT_NULL);
|
RT_ASSERT(dev != RT_NULL);
|
||||||
serial = (struct rt_serial_device *)dev;
|
serial = (struct rt_serial_device *)dev;
|
||||||
|
|
||||||
|
dbg_log(DBG_LOG, "open serial device: 0x%08x with open flag: 0x%04x\n",
|
||||||
|
dev, oflag);
|
||||||
/* check device flag with the open flag */
|
/* check device flag with the open flag */
|
||||||
if ((oflag & RT_DEVICE_FLAG_DMA_RX) && !(dev->flag & RT_DEVICE_FLAG_DMA_RX))
|
if ((oflag & RT_DEVICE_FLAG_DMA_RX) && !(dev->flag & RT_DEVICE_FLAG_DMA_RX))
|
||||||
return -RT_EIO;
|
return -RT_EIO;
|
||||||
|
@ -386,6 +561,10 @@ static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
|
||||||
if ((oflag & RT_DEVICE_FLAG_INT_TX) && !(dev->flag & RT_DEVICE_FLAG_INT_TX))
|
if ((oflag & RT_DEVICE_FLAG_INT_TX) && !(dev->flag & RT_DEVICE_FLAG_INT_TX))
|
||||||
return -RT_EIO;
|
return -RT_EIO;
|
||||||
|
|
||||||
|
/* keep steam flag */
|
||||||
|
if ((oflag & RT_DEVICE_FLAG_STREAM) || (dev->open_flag & RT_DEVICE_FLAG_STREAM))
|
||||||
|
stream_flag = RT_DEVICE_FLAG_STREAM;
|
||||||
|
|
||||||
/* get open flags */
|
/* get open flags */
|
||||||
dev->open_flag = oflag & 0xff;
|
dev->open_flag = oflag & 0xff;
|
||||||
|
|
||||||
|
@ -477,6 +656,9 @@ static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* set stream flag */
|
||||||
|
dev->open_flag |= stream_flag;
|
||||||
|
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -604,10 +786,61 @@ static rt_size_t rt_serial_write(struct rt_device *dev,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef RT_USING_POSIX_TERMIOS
|
||||||
|
struct speed_baudrate_item
|
||||||
|
{
|
||||||
|
speed_t speed;
|
||||||
|
int baudrate;
|
||||||
|
};
|
||||||
|
|
||||||
|
const static struct speed_baudrate_item _tbl[] =
|
||||||
|
{
|
||||||
|
{B2400, BAUD_RATE_2400},
|
||||||
|
{B4800, BAUD_RATE_4800},
|
||||||
|
{B9600, BAUD_RATE_9600},
|
||||||
|
{B19200, BAUD_RATE_19200},
|
||||||
|
{B38400, BAUD_RATE_38400},
|
||||||
|
{B57600, BAUD_RATE_57600},
|
||||||
|
{B115200, BAUD_RATE_115200},
|
||||||
|
{B230400, BAUD_RATE_230400},
|
||||||
|
{B460800, BAUD_RATE_460800},
|
||||||
|
{B921600, BAUD_RATE_921600},
|
||||||
|
{B2000000, BAUD_RATE_2000000},
|
||||||
|
{B3000000, BAUD_RATE_3000000},
|
||||||
|
};
|
||||||
|
|
||||||
|
static speed_t _get_speed(int baudrate)
|
||||||
|
{
|
||||||
|
int index;
|
||||||
|
|
||||||
|
for (index = 0; index < sizeof(_tbl)/sizeof(_tbl[0]); index ++)
|
||||||
|
{
|
||||||
|
if (_tbl[index].baudrate == baudrate)
|
||||||
|
return _tbl[index].speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
return B0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int _get_baudrate(speed_t speed)
|
||||||
|
{
|
||||||
|
int index;
|
||||||
|
|
||||||
|
for (index = 0; index < sizeof(_tbl)/sizeof(_tbl[0]); index ++)
|
||||||
|
{
|
||||||
|
if (_tbl[index].speed == speed)
|
||||||
|
return _tbl[index].baudrate;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static rt_err_t rt_serial_control(struct rt_device *dev,
|
static rt_err_t rt_serial_control(struct rt_device *dev,
|
||||||
rt_uint8_t cmd,
|
int cmd,
|
||||||
void *args)
|
void *args)
|
||||||
{
|
{
|
||||||
|
rt_err_t ret = RT_EOK;
|
||||||
struct rt_serial_device *serial;
|
struct rt_serial_device *serial;
|
||||||
|
|
||||||
RT_ASSERT(dev != RT_NULL);
|
RT_ASSERT(dev != RT_NULL);
|
||||||
|
@ -645,13 +878,89 @@ static rt_err_t rt_serial_control(struct rt_device *dev,
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
#ifdef RT_USING_POSIX_TERMIOS
|
||||||
|
case TCGETA:
|
||||||
|
{
|
||||||
|
struct termios *tio = (struct termios*)args;
|
||||||
|
if (tio == RT_NULL) return -RT_EINVAL;
|
||||||
|
|
||||||
|
tio->c_iflag = 0;
|
||||||
|
tio->c_oflag = 0;
|
||||||
|
tio->c_lflag = 0;
|
||||||
|
|
||||||
|
/* update oflag for console device */
|
||||||
|
if (rt_console_get_device() == dev)
|
||||||
|
tio->c_oflag = OPOST | ONLCR;
|
||||||
|
|
||||||
|
/* set cflag */
|
||||||
|
tio->c_cflag = 0;
|
||||||
|
if (serial->config.data_bits == DATA_BITS_5)
|
||||||
|
tio->c_cflag = CS5;
|
||||||
|
else if (serial->config.data_bits == DATA_BITS_6)
|
||||||
|
tio->c_cflag = CS6;
|
||||||
|
else if (serial->config.data_bits == DATA_BITS_7)
|
||||||
|
tio->c_cflag = CS7;
|
||||||
|
else if (serial->config.data_bits == DATA_BITS_8)
|
||||||
|
tio->c_cflag = CS8;
|
||||||
|
|
||||||
|
if (serial->config.stop_bits == STOP_BITS_2)
|
||||||
|
tio->c_cflag |= CSTOPB;
|
||||||
|
|
||||||
|
if (serial->config.parity == PARITY_EVEN)
|
||||||
|
tio->c_cflag |= PARENB;
|
||||||
|
else if (serial->config.parity == PARITY_ODD)
|
||||||
|
tio->c_cflag |= (PARODD | PARENB);
|
||||||
|
|
||||||
|
cfsetospeed(tio, _get_speed(serial->config.baud_rate));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case TCSETAW:
|
||||||
|
case TCSETAF:
|
||||||
|
case TCSETA:
|
||||||
|
{
|
||||||
|
int baudrate;
|
||||||
|
struct serial_configure config;
|
||||||
|
|
||||||
|
struct termios *tio = (struct termios*)args;
|
||||||
|
if (tio == RT_NULL) return -RT_EINVAL;
|
||||||
|
|
||||||
|
config = serial->config;
|
||||||
|
|
||||||
|
baudrate = _get_baudrate(cfgetospeed(tio));
|
||||||
|
config.baud_rate = baudrate;
|
||||||
|
|
||||||
|
if (tio->c_cflag & CS6) config.data_bits = DATA_BITS_6;
|
||||||
|
else if (tio->c_cflag & CS7) config.data_bits = DATA_BITS_7;
|
||||||
|
else if (tio->c_cflag & CS8) config.data_bits = DATA_BITS_8;
|
||||||
|
else config.data_bits = DATA_BITS_5;
|
||||||
|
|
||||||
|
if (tio->c_cflag & CSTOPB) config.data_bits = STOP_BITS_2;
|
||||||
|
else config.data_bits = STOP_BITS_1;
|
||||||
|
|
||||||
|
if (tio->c_cflag & PARENB)
|
||||||
|
{
|
||||||
|
if (tio->c_cflag & PARODD) config.parity = PARITY_ODD;
|
||||||
|
else config.parity = PARITY_EVEN;
|
||||||
|
}
|
||||||
|
else config.parity = PARITY_NONE;
|
||||||
|
|
||||||
|
serial->ops->configure(serial, &config);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case TCFLSH:
|
||||||
|
break;
|
||||||
|
case TCXONC:
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
default :
|
default :
|
||||||
/* control device */
|
/* control device */
|
||||||
serial->ops->control(serial, cmd, args);
|
ret = serial->ops->control(serial, cmd, args);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return RT_EOK;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -662,6 +971,7 @@ rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
|
||||||
rt_uint32_t flag,
|
rt_uint32_t flag,
|
||||||
void *data)
|
void *data)
|
||||||
{
|
{
|
||||||
|
rt_err_t ret;
|
||||||
struct rt_device *device;
|
struct rt_device *device;
|
||||||
RT_ASSERT(serial != RT_NULL);
|
RT_ASSERT(serial != RT_NULL);
|
||||||
|
|
||||||
|
@ -680,7 +990,14 @@ rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
|
||||||
device->user_data = data;
|
device->user_data = data;
|
||||||
|
|
||||||
/* register a character device */
|
/* register a character device */
|
||||||
return rt_device_register(device, name, flag);
|
ret = rt_device_register(device, name, flag);
|
||||||
|
|
||||||
|
#if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS)
|
||||||
|
/* set fops */
|
||||||
|
device->fops = &_serial_fops;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ISR for serial interrupt */
|
/* ISR for serial interrupt */
|
||||||
|
@ -812,3 +1129,4 @@ void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -343,7 +343,7 @@ static rt_err_t enc28j60_init(rt_device_t dev)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* control the interface */
|
/* control the interface */
|
||||||
static rt_err_t enc28j60_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
static rt_err_t enc28j60_control(rt_device_t dev, int cmd, void *args)
|
||||||
{
|
{
|
||||||
struct net_device * enc28j60 = (struct net_device *)dev;
|
struct net_device * enc28j60 = (struct net_device *)dev;
|
||||||
switch(cmd)
|
switch(cmd)
|
||||||
|
|
|
@ -54,7 +54,7 @@ static rt_size_t _spi_bus_device_write(rt_device_t dev,
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t _spi_bus_device_control(rt_device_t dev,
|
static rt_err_t _spi_bus_device_control(rt_device_t dev,
|
||||||
rt_uint8_t cmd,
|
int cmd,
|
||||||
void *args)
|
void *args)
|
||||||
{
|
{
|
||||||
/* TODO: add control command handle */
|
/* TODO: add control command handle */
|
||||||
|
@ -120,7 +120,7 @@ static rt_size_t _spidev_device_write(rt_device_t dev,
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t _spidev_device_control(rt_device_t dev,
|
static rt_err_t _spidev_device_control(rt_device_t dev,
|
||||||
rt_uint8_t cmd,
|
int cmd,
|
||||||
void *args)
|
void *args)
|
||||||
{
|
{
|
||||||
switch (cmd)
|
switch (cmd)
|
||||||
|
|
|
@ -117,7 +117,7 @@ static rt_err_t AT45DB_flash_close(rt_device_t dev)
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t AT45DB_flash_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
static rt_err_t AT45DB_flash_control(rt_device_t dev, int cmd, void *args)
|
||||||
{
|
{
|
||||||
RT_ASSERT(dev != RT_NULL);
|
RT_ASSERT(dev != RT_NULL);
|
||||||
|
|
||||||
|
|
|
@ -195,7 +195,7 @@ static rt_err_t w25qxx_flash_close(rt_device_t dev)
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t w25qxx_flash_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
static rt_err_t w25qxx_flash_control(rt_device_t dev, int cmd, void *args)
|
||||||
{
|
{
|
||||||
RT_ASSERT(dev != RT_NULL);
|
RT_ASSERT(dev != RT_NULL);
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ static char log_buf[RT_CONSOLEBUF_SIZE];
|
||||||
|
|
||||||
void sfud_log_debug(const char *file, const long line, const char *format, ...);
|
void sfud_log_debug(const char *file, const long line, const char *format, ...);
|
||||||
|
|
||||||
static rt_err_t rt_sfud_control(rt_device_t dev, rt_uint8_t cmd, void *args) {
|
static rt_err_t rt_sfud_control(rt_device_t dev, int cmd, void *args) {
|
||||||
RT_ASSERT(dev != RT_NULL);
|
RT_ASSERT(dev != RT_NULL);
|
||||||
|
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
|
|
|
@ -173,7 +173,7 @@ static rt_err_t sst25vfxx_flash_close(rt_device_t dev)
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t sst25vfxx_flash_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
static rt_err_t sst25vfxx_flash_control(rt_device_t dev, int cmd, void *args)
|
||||||
{
|
{
|
||||||
struct spi_flash_sst25vfxx * spi_flash;
|
struct spi_flash_sst25vfxx * spi_flash;
|
||||||
|
|
||||||
|
|
|
@ -205,7 +205,7 @@ static rt_err_t w25qxx_flash_close(rt_device_t dev)
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t w25qxx_flash_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
static rt_err_t w25qxx_flash_control(rt_device_t dev, int cmd, void *args)
|
||||||
{
|
{
|
||||||
RT_ASSERT(dev != RT_NULL);
|
RT_ASSERT(dev != RT_NULL);
|
||||||
|
|
||||||
|
|
|
@ -530,7 +530,7 @@ static rt_size_t rw009_wifi_write(rt_device_t dev, rt_off_t pos, const void *buf
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t rw009_wifi_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
static rt_err_t rw009_wifi_control(rt_device_t dev, int cmd, void *args)
|
||||||
{
|
{
|
||||||
struct rw009_wifi *wifi_device = (struct rw009_wifi *)dev;
|
struct rw009_wifi *wifi_device = (struct rw009_wifi *)dev;
|
||||||
rt_err_t result = RT_EOK;
|
rt_err_t result = RT_EOK;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* File : pipe.c
|
* File : pipe.c
|
||||||
* This file is part of RT-Thread RTOS
|
* 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
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -21,273 +21,407 @@
|
||||||
* Date Author Notes
|
* Date Author Notes
|
||||||
* 2012-09-30 Bernard first version.
|
* 2012-09-30 Bernard first version.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <rthw.h>
|
#include <rthw.h>
|
||||||
#include <rtthread.h>
|
|
||||||
#include <rtdevice.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_device_t device;
|
||||||
|
rt_pipe_t *pipe;
|
||||||
|
|
||||||
|
pipe = (rt_pipe_t *)fd->data;
|
||||||
|
if (!pipe) return -1;
|
||||||
|
|
||||||
|
device = &(pipe->parent);
|
||||||
|
rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
|
||||||
|
|
||||||
|
if (device->ref_count == 0)
|
||||||
{
|
{
|
||||||
rt_thread_t thread;
|
pipe->fifo = rt_ringbuffer_create(PIPE_BUFSZ);
|
||||||
|
|
||||||
RT_ASSERT(pipe->flag & RT_PIPE_FLAG_BLOCK_WR);
|
|
||||||
|
|
||||||
/* 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_schedule();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
if (!(pipe->flag & RT_PIPE_FLAG_BLOCK_RD))
|
|
||||||
{
|
|
||||||
level = rt_hw_interrupt_disable();
|
|
||||||
read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), buffer, size);
|
|
||||||
|
|
||||||
/* if the ringbuffer is empty, there won't be any writer waiting */
|
|
||||||
if (read_nbytes)
|
|
||||||
_rt_pipe_resume_writer(pipe);
|
|
||||||
|
|
||||||
rt_hw_interrupt_enable(level);
|
|
||||||
|
|
||||||
return read_nbytes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
thread = rt_thread_self();
|
switch (fd->flags & O_ACCMODE)
|
||||||
|
|
||||||
/* 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);
|
case O_RDONLY:
|
||||||
/* waiting on suspended read list */
|
pipe->readers ++;
|
||||||
rt_list_insert_before(&(pipe->suspended_read_list),
|
break;
|
||||||
&(thread->tlist));
|
case O_WRONLY:
|
||||||
rt_hw_interrupt_enable(level);
|
pipe->writers ++;
|
||||||
|
break;
|
||||||
rt_schedule();
|
case O_RDWR:
|
||||||
}
|
pipe->readers ++;
|
||||||
else
|
pipe->writers ++;
|
||||||
{
|
|
||||||
_rt_pipe_resume_writer(pipe);
|
|
||||||
rt_hw_interrupt_enable(level);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} while (read_nbytes == 0);
|
device->ref_count ++;
|
||||||
|
|
||||||
return read_nbytes;
|
rt_mutex_release(&(pipe->lock));
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _rt_pipe_resume_reader(struct rt_pipe_device *pipe)
|
static int pipe_close(struct dfs_fd *fd)
|
||||||
{
|
{
|
||||||
if (pipe->parent.rx_indicate)
|
rt_device_t device;
|
||||||
pipe->parent.rx_indicate(&pipe->parent,
|
rt_pipe_t *pipe;
|
||||||
rt_ringbuffer_data_len(&pipe->ringbuffer));
|
|
||||||
|
|
||||||
if (!rt_list_isempty(&pipe->suspended_read_list))
|
pipe = (rt_pipe_t *)fd->data;
|
||||||
|
if (!pipe) return -1;
|
||||||
|
|
||||||
|
device = &(pipe->parent);
|
||||||
|
rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
|
||||||
|
|
||||||
|
switch (fd->flags & O_ACCMODE)
|
||||||
{
|
{
|
||||||
rt_thread_t thread;
|
case O_RDONLY:
|
||||||
|
pipe->readers --;
|
||||||
RT_ASSERT(pipe->flag & RT_PIPE_FLAG_BLOCK_RD);
|
break;
|
||||||
|
case O_WRONLY:
|
||||||
/* get suspended thread */
|
pipe->writers --;
|
||||||
thread = rt_list_entry(pipe->suspended_read_list.next,
|
break;
|
||||||
struct rt_thread,
|
case O_RDWR:
|
||||||
tlist);
|
pipe->readers --;
|
||||||
|
pipe->writers --;
|
||||||
/* resume the read thread */
|
break;
|
||||||
rt_thread_resume(thread);
|
|
||||||
|
|
||||||
rt_schedule();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (pipe->writers == 0)
|
||||||
|
{
|
||||||
|
rt_wqueue_wakeup(&(pipe->reader_queue), (void*)(POLLIN | POLLERR | POLLHUP));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pipe->readers == 0)
|
||||||
|
{
|
||||||
|
rt_wqueue_wakeup(&(pipe->writer_queue), (void*)(POLLOUT | POLLERR | POLLHUP));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (device->ref_count == 1)
|
||||||
|
{
|
||||||
|
rt_free(pipe->fifo);
|
||||||
|
pipe->fifo = RT_NULL;
|
||||||
|
}
|
||||||
|
device->ref_count --;
|
||||||
|
|
||||||
|
rt_mutex_release(&(pipe->lock));
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_size_t rt_pipe_write(rt_device_t dev,
|
static int pipe_ioctl(struct dfs_fd *fd, int cmd, void *args)
|
||||||
rt_off_t pos,
|
|
||||||
const void *buffer,
|
|
||||||
rt_size_t size)
|
|
||||||
{
|
{
|
||||||
rt_uint32_t level;
|
rt_pipe_t *pipe;
|
||||||
rt_thread_t thread;
|
int ret = 0;
|
||||||
struct rt_pipe_device *pipe;
|
|
||||||
rt_size_t write_nbytes;
|
|
||||||
|
|
||||||
pipe = PIPE_DEVICE(dev);
|
pipe = (rt_pipe_t *)fd->data;
|
||||||
RT_ASSERT(pipe != RT_NULL);
|
|
||||||
|
|
||||||
if ((pipe->flag & RT_PIPE_FLAG_FORCE_WR) ||
|
switch (cmd)
|
||||||
!(pipe->flag & RT_PIPE_FLAG_BLOCK_WR))
|
|
||||||
{
|
{
|
||||||
level = rt_hw_interrupt_disable();
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
if (pipe->flag & RT_PIPE_FLAG_FORCE_WR)
|
return ret;
|
||||||
write_nbytes = rt_ringbuffer_put_force(&(pipe->ringbuffer),
|
}
|
||||||
buffer, size);
|
|
||||||
|
static int pipe_read(struct dfs_fd *fd, void *buf, size_t count)
|
||||||
|
{
|
||||||
|
int len = 0;
|
||||||
|
rt_pipe_t *pipe;
|
||||||
|
|
||||||
|
pipe = (rt_pipe_t *)fd->data;
|
||||||
|
|
||||||
|
/* no process has the pipe open for writing, return end-of-file */
|
||||||
|
if (pipe->writers == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
|
||||||
|
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
if (pipe->writers == 0)
|
||||||
|
{
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
len = rt_ringbuffer_get(pipe->fifo, buf, count);
|
||||||
|
|
||||||
|
if (len > 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer),
|
{
|
||||||
buffer, size);
|
if (fd->flags & O_NONBLOCK)
|
||||||
|
{
|
||||||
_rt_pipe_resume_reader(pipe);
|
len = -EAGAIN;
|
||||||
|
goto out;
|
||||||
rt_hw_interrupt_enable(level);
|
|
||||||
|
|
||||||
return write_nbytes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
thread = rt_thread_self();
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* current context checking */
|
/* wakeup writer */
|
||||||
RT_DEBUG_NOT_IN_INTERRUPT;
|
rt_wqueue_wakeup(&(pipe->writer_queue), (void*)POLLOUT);
|
||||||
|
|
||||||
do {
|
out:
|
||||||
level = rt_hw_interrupt_disable();
|
rt_mutex_release(&pipe->lock);
|
||||||
write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer), buffer, size);
|
|
||||||
if (write_nbytes == 0)
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int pipe_write(struct dfs_fd *fd, const void *buf, size_t count)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
rt_pipe_t *pipe;
|
||||||
|
int wakeup = 0;
|
||||||
|
int ret = 0;
|
||||||
|
uint8_t *pbuf;
|
||||||
|
|
||||||
|
pipe = (rt_pipe_t *)fd->data;
|
||||||
|
|
||||||
|
if (pipe->readers == 0)
|
||||||
{
|
{
|
||||||
|
ret = -EPIPE;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
if (fd->flags & O_NONBLOCK)
|
||||||
|
{
|
||||||
|
if (ret == 0)
|
||||||
|
{
|
||||||
|
ret = -EAGAIN;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rt_mutex_release(&pipe->lock);
|
||||||
|
rt_wqueue_wakeup(&(pipe->reader_queue), (void*)POLLIN);
|
||||||
/* pipe full, waiting on suspended write list */
|
/* pipe full, waiting on suspended write list */
|
||||||
rt_thread_suspend(thread);
|
rt_wqueue_wait(&(pipe->writer_queue), 0, -1);
|
||||||
/* waiting on suspended read list */
|
rt_mutex_take(&pipe->lock, -1);
|
||||||
rt_list_insert_before(&(pipe->suspended_write_list),
|
}
|
||||||
&(thread->tlist));
|
|
||||||
rt_hw_interrupt_enable(level);
|
|
||||||
|
|
||||||
rt_schedule();
|
rt_mutex_release(&pipe->lock);
|
||||||
|
if (wakeup)
|
||||||
|
{
|
||||||
|
rt_wqueue_wakeup(&(pipe->reader_queue), (void*)POLLIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
out:
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mode & 1)
|
||||||
|
{
|
||||||
|
if (rt_ringbuffer_data_len(pipe->fifo) != 0)
|
||||||
|
{
|
||||||
|
mask |= POLLIN;
|
||||||
|
}
|
||||||
|
if (pipe->writers == 0)
|
||||||
|
{
|
||||||
|
mask |= POLLHUP;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mode & 2)
|
||||||
|
{
|
||||||
|
if (rt_ringbuffer_space_len(pipe->fifo) != 0)
|
||||||
|
{
|
||||||
|
mask |= POLLOUT;
|
||||||
|
}
|
||||||
|
if (pipe->readers == 0)
|
||||||
|
{
|
||||||
|
mask |= POLLERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct dfs_file_ops pipe_fops =
|
||||||
|
{
|
||||||
|
pipe_open,
|
||||||
|
pipe_close,
|
||||||
|
pipe_ioctl,
|
||||||
|
pipe_read,
|
||||||
|
pipe_write,
|
||||||
|
RT_NULL,
|
||||||
|
RT_NULL,
|
||||||
|
RT_NULL,
|
||||||
|
pipe_poll,
|
||||||
|
};
|
||||||
|
|
||||||
|
rt_pipe_t *rt_pipe_create(const char *name)
|
||||||
|
{
|
||||||
|
rt_pipe_t *pipe;
|
||||||
|
rt_device_t dev;
|
||||||
|
|
||||||
|
pipe = rt_malloc(sizeof(rt_pipe_t));
|
||||||
|
if (pipe == RT_NULL) return RT_NULL;
|
||||||
|
|
||||||
|
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));
|
||||||
|
|
||||||
|
dev = &(pipe->parent);
|
||||||
|
dev->type = RT_Device_Class_Pipe;
|
||||||
|
|
||||||
|
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 pipe;
|
||||||
|
}
|
||||||
|
|
||||||
|
int rt_pipe_delete(const char *name)
|
||||||
|
{
|
||||||
|
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
|
else
|
||||||
{
|
{
|
||||||
_rt_pipe_resume_reader(pipe);
|
result = -1;
|
||||||
rt_hw_interrupt_enable(level);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
} while (write_nbytes == 0);
|
}
|
||||||
|
else
|
||||||
return write_nbytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
static rt_err_t rt_pipe_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
|
||||||
{
|
|
||||||
if (cmd == PIPE_CTRL_GET_SPACE && args)
|
|
||||||
*(rt_size_t*)args = rt_ringbuffer_space_len(&PIPE_DEVICE(dev)->ringbuffer);
|
|
||||||
return RT_EOK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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_ASSERT(pipe);
|
|
||||||
RT_ASSERT(buf);
|
|
||||||
|
|
||||||
/* 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, buf, size);
|
|
||||||
|
|
||||||
pipe->flag = flag;
|
|
||||||
|
|
||||||
/* 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;
|
|
||||||
|
|
||||||
return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR);
|
|
||||||
}
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
return rt_device_unregister(&pipe->parent);
|
|
||||||
}
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
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)
|
|
||||||
return -RT_ENOMEM;
|
|
||||||
|
|
||||||
/* create ring buffer of pipe */
|
|
||||||
rb_memptr = rt_malloc(size);
|
|
||||||
if (rb_memptr == RT_NULL)
|
|
||||||
{
|
{
|
||||||
rt_free(pipe);
|
result = -1;
|
||||||
return -RT_ENOMEM;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return rt_pipe_init(pipe, name, flag, rb_memptr, size);
|
return result;
|
||||||
}
|
}
|
||||||
RTM_EXPORT(rt_pipe_create);
|
|
||||||
|
|
||||||
void rt_pipe_destroy(struct rt_pipe_device *pipe)
|
int pipe(int fildes[2])
|
||||||
{
|
{
|
||||||
|
rt_pipe_t *pipe;
|
||||||
|
char dname[8];
|
||||||
|
char dev_name[32];
|
||||||
|
static int pipeno = 0;
|
||||||
|
|
||||||
|
rt_snprintf(dname, sizeof(dname), "pipe%d", pipeno++);
|
||||||
|
|
||||||
|
pipe = rt_pipe_create(dname);
|
||||||
if (pipe == RT_NULL)
|
if (pipe == RT_NULL)
|
||||||
return;
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/* un-register pipe device */
|
rt_snprintf(dev_name, sizeof(dev_name), "/dev/%s", dname);
|
||||||
rt_pipe_detach(pipe);
|
fildes[0] = open(dev_name, O_RDONLY, 0);
|
||||||
|
if (fildes[0] < 0)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/* release memory */
|
fildes[1] = open(dev_name, O_WRONLY, 0);
|
||||||
rt_free(pipe->ringbuffer.buffer_ptr);
|
if (fildes[1] < 0)
|
||||||
rt_free(pipe);
|
{
|
||||||
|
close(fildes[1]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
RTM_EXPORT(rt_pipe_destroy);
|
|
||||||
#endif /* RT_USING_HEAP */
|
int mkfifo(const char *path, mode_t mode)
|
||||||
|
{
|
||||||
|
rt_pipe_t *pipe;
|
||||||
|
|
||||||
|
pipe = rt_pipe_create(path);
|
||||||
|
if (pipe == RT_NULL)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
|
@ -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 */
|
|
||||||
|
|
|
@ -21,18 +21,31 @@
|
||||||
* Date Author Notes
|
* Date Author Notes
|
||||||
* 2012-09-30 Bernard first version.
|
* 2012-09-30 Bernard first version.
|
||||||
* 2013-05-08 Grissiom reimplement
|
* 2013-05-08 Grissiom reimplement
|
||||||
|
* 2016-08-18 heyuanjie add interface
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <rtthread.h>
|
#include <rtthread.h>
|
||||||
#include <rtdevice.h>
|
#include <rtdevice.h>
|
||||||
#include <string.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,
|
void rt_ringbuffer_init(struct rt_ringbuffer *rb,
|
||||||
rt_uint8_t *pool,
|
rt_uint8_t *pool,
|
||||||
rt_int16_t size)
|
rt_int16_t size)
|
||||||
{
|
{
|
||||||
RT_ASSERT(rb != RT_NULL);
|
RT_ASSERT(rb != RT_NULL);
|
||||||
RT_ASSERT(size > 0)
|
RT_ASSERT(size > 0);
|
||||||
|
|
||||||
/* initialize read and write index */
|
/* initialize read and write index */
|
||||||
rb->read_mirror = rb->read_index = 0;
|
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);
|
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
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
|
@ -67,7 +67,7 @@ static rt_err_t rt_watchdog_close(struct rt_device *dev)
|
||||||
}
|
}
|
||||||
|
|
||||||
static rt_err_t rt_watchdog_control(struct rt_device *dev,
|
static rt_err_t rt_watchdog_control(struct rt_device *dev,
|
||||||
rt_uint8_t cmd,
|
int cmd,
|
||||||
void *args)
|
void *args)
|
||||||
{
|
{
|
||||||
rt_watchdog_t *wtd;
|
rt_watchdog_t *wtd;
|
||||||
|
|
Loading…
Reference in New Issue