separate init & configure of uart ops

This commit is contained in:
thewon86 2023-02-21 16:20:46 +08:00 committed by chinky
parent 2a1a014d10
commit 2a55b6144f
5 changed files with 154 additions and 67 deletions

View File

@ -65,7 +65,7 @@ menuconfig RT_USING_SERIAL
and RT_SERIAL_FIFO_BUFSZ >= 2*RT_SERIAL_DMA_BUFSZ and RT_SERIAL_FIFO_BUFSZ >= 2*RT_SERIAL_DMA_BUFSZ
config RT_SERIAL_HARD_FIFO config RT_SERIAL_HARD_FIFO
bool "Set Hard FIFO buffer size" bool "Using Hard FIFO"
default n default n
help help
Useful only if the chip supported Useful only if the chip supported

View File

@ -5,7 +5,9 @@
* *
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
* 2022-04-10 THEWON first version * 2022-04-10 THEWON serialX first version
* 2022-06-08 THEWON add No TX Empty interrupt support
* 2023-02-15 THEWON add init ops
*/ */
#ifndef __SERIALX_H__ #ifndef __SERIALX_H__
@ -124,7 +126,7 @@ struct serial_configure
rt_uint32_t parity :2; rt_uint32_t parity :2;
rt_uint32_t bit_order :1; rt_uint32_t bit_order :1;
rt_uint32_t invert :1; rt_uint32_t invert :1;
rt_uint32_t bufsz :16; rt_uint32_t bufsz :16; // unused, move it to struct rt_serial_device
rt_uint32_t flowcontrol :1; rt_uint32_t flowcontrol :1;
rt_uint32_t reserved :5; rt_uint32_t reserved :5;
}; };
@ -149,6 +151,7 @@ struct rt_serial_device
const struct rt_uart_ops *ops; const struct rt_uart_ops *ops;
struct serial_configure config; struct serial_configure config;
rt_uint32_t bufsz;
void *serial_rx; void *serial_rx;
void *serial_tx; void *serial_tx;
@ -176,6 +179,8 @@ typedef struct rt_serial_device rt_serial_t;
*/ */
struct rt_uart_ops struct rt_uart_ops
{ {
int (*init)(struct rt_serial_device *serial);
rt_err_t (*configure)(struct rt_serial_device *serial, struct serial_configure *cfg); rt_err_t (*configure)(struct rt_serial_device *serial, struct serial_configure *cfg);
rt_err_t (*control)(struct rt_serial_device *serial, int cmd, void *arg); rt_err_t (*control)(struct rt_serial_device *serial, int cmd, void *arg);
@ -183,7 +188,12 @@ struct rt_uart_ops
int (*getc)(struct rt_serial_device *serial); int (*getc)(struct rt_serial_device *serial);
int (*flush)(struct rt_serial_device *serial); int (*flush)(struct rt_serial_device *serial);
#if defined (RT_SERIAL_NO_TXEIT)
rt_bool_t (*is_int_txing)(struct rt_serial_device *serial);
void (*start_tx)(struct rt_serial_device *serial, rt_uint8_t ch);
#else
void (*start_tx)(struct rt_serial_device *serial); void (*start_tx)(struct rt_serial_device *serial);
#endif
void (*stop_tx)(struct rt_serial_device *serial); void (*stop_tx)(struct rt_serial_device *serial);
#ifdef RT_SERIAL_USING_DMA #ifdef RT_SERIAL_USING_DMA
@ -204,4 +214,3 @@ rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
void *data); void *data);
#endif #endif

View File

@ -27,6 +27,8 @@
* 2020-12-14 Meco Man implement function of setting window's size(TIOCSWINSZ) * 2020-12-14 Meco Man implement function of setting window's size(TIOCSWINSZ)
* 2021-08-22 Meco Man implement function of getting window's size(TIOCGWINSZ) * 2021-08-22 Meco Man implement function of getting window's size(TIOCGWINSZ)
* 2022-04-10 THEWON serialX first version * 2022-04-10 THEWON serialX first version
* 2022-06-08 THEWON add No TX Empty interrupt support
* 2023-02-15 THEWON add init ops
*/ */
#include <rthw.h> #include <rthw.h>
@ -39,14 +41,15 @@
#define DBG_LVL DBG_INFO #define DBG_LVL DBG_INFO
#include <rtdbg.h> #include <rtdbg.h>
static rt_err_t rt_serial_flush(struct rt_device *dev);
#ifdef RT_USING_POSIX_STDIO #ifdef RT_USING_POSIX_STDIO
#include <dfs_posix.h>
#if RTTHREAD_VERSION <= RT_VERSION_CHECK(4, 0, 3) #if RTTHREAD_VERSION <= RT_VERSION_CHECK(4, 0, 3)
#include <dfs_posix.h>
#include <dfs_poll.h> #include <dfs_poll.h>
#else #else
#include <dfs_file.h>
#include <fcntl.h>
#include <poll.h> #include <poll.h>
#include <sys/errno.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#endif #endif
@ -92,21 +95,30 @@ static int serial_fops_open(struct dfs_fd *fd)
break; break;
case O_WRONLY: case O_WRONLY:
LOG_D("fops open: O_WRONLY!"); LOG_D("fops open: O_WRONLY!");
flags = RT_DEVICE_FLAG_WRONLY; flags = RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_WRONLY;
break; break;
case O_RDWR: case O_RDWR:
LOG_D("fops open: O_RDWR!"); LOG_D("fops open: O_RDWR!");
flags = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_RDWR; flags = RT_DEVICE_FLAG_RDWR
| RT_DEVICE_FLAG_INT_RX
| RT_DEVICE_FLAG_INT_TX;
break; break;
default: default:
LOG_E("fops open: unknown mode - %d!", fd->flags & O_ACCMODE); LOG_E("fops open: unknown mode - %d!", fd->flags & O_ACCMODE);
break; break;
} }
if ((fd->flags & O_ACCMODE) != O_WRONLY) if ((fd->flags & O_NONBLOCK) != 0) {
rt_device_set_rx_indicate(device, serial_fops_rx_ind); flags |= RT_DEVICE_OFLAG_NONBLOCKING;
}
ret = rt_device_open(device, flags); ret = rt_device_open(device, flags);
if (ret == RT_EOK) return 0; if (ret == RT_EOK) {
if ((fd->flags & O_ACCMODE) != O_WRONLY) {
rt_device_set_rx_indicate(device, serial_fops_rx_ind);
}
return 0;
}
return ret; return ret;
} }
@ -117,7 +129,6 @@ static int serial_fops_close(struct dfs_fd *fd)
device = (rt_device_t)fd->data; device = (rt_device_t)fd->data;
rt_device_set_rx_indicate(device, RT_NULL);
rt_device_close(device); rt_device_close(device);
return 0; return 0;
@ -142,8 +153,14 @@ static int serial_fops_ioctl(struct dfs_fd *fd, int cmd, void *args)
static int serial_fops_read(struct dfs_fd *fd, void *buf, size_t count) static int serial_fops_read(struct dfs_fd *fd, void *buf, size_t count)
{ {
int size = 0; int size = 0;
int flags = 0;
rt_device_t device; rt_device_t device;
flags = fd->flags & O_ACCMODE;
if (flags == O_WRONLY) {
return -EIO;
}
device = (rt_device_t)fd->data; device = (rt_device_t)fd->data;
do do
@ -166,12 +183,26 @@ static int serial_fops_read(struct dfs_fd *fd, void *buf, size_t count)
static int serial_fops_write(struct dfs_fd *fd, const void *buf, size_t count) static int serial_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
{ {
int flags = 0;
rt_device_t device; rt_device_t device;
flags = fd->flags & O_ACCMODE;
if (flags == O_RDONLY) {
return -EIO;
}
device = (rt_device_t)fd->data; device = (rt_device_t)fd->data;
return rt_device_write(device, -1, buf, count); return rt_device_write(device, -1, buf, count);
} }
static int serial_fops_flush(struct dfs_fd *fd)
{
rt_device_t device;
device = (rt_device_t)fd->data;
return rt_device_flush(device);
}
static int serial_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req) static int serial_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
{ {
int mask = 0; int mask = 0;
@ -188,7 +219,7 @@ static int serial_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
flags = fd->flags & O_ACCMODE; flags = fd->flags & O_ACCMODE;
if (flags == O_RDONLY || flags == O_RDWR) if (flags == O_RDONLY || flags == O_RDWR)
{ {
register rt_base_t level; rt_base_t level;
struct rt_serial_fifo* rx_fifo; struct rt_serial_fifo* rx_fifo;
rt_poll_add(&(device->wait_queue), req); rt_poll_add(&(device->wait_queue), req);
@ -197,7 +228,9 @@ static int serial_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
if ((rx_fifo->get_index != rx_fifo->put_index) || (rx_fifo->get_index == rx_fifo->put_index && rx_fifo->is_full == RT_TRUE)) if ((rx_fifo->get_index != rx_fifo->put_index) || (rx_fifo->get_index == rx_fifo->put_index && rx_fifo->is_full == RT_TRUE))
{
mask |= POLLIN; mask |= POLLIN;
}
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
} }
@ -211,7 +244,7 @@ const static struct dfs_file_ops _serial_fops =
serial_fops_ioctl, serial_fops_ioctl,
serial_fops_read, serial_fops_read,
serial_fops_write, serial_fops_write,
RT_NULL, /* flush */ serial_fops_flush, /* flush */
RT_NULL, /* lseek */ RT_NULL, /* lseek */
RT_NULL, /* getdents */ RT_NULL, /* getdents */
serial_fops_poll, serial_fops_poll,
@ -225,9 +258,9 @@ const static struct dfs_file_ops _serial_fops =
* *
* @return length * @return length
*/ */
rt_inline rt_size_t _serial_fifo_calc_data_len(struct rt_serial_fifo *fifo) rt_inline rt_ssize_t _serial_fifo_calc_data_len(struct rt_serial_fifo *fifo)
{ {
rt_size_t size; rt_ssize_t size;
if (fifo->put_index == fifo->get_index) { if (fifo->put_index == fifo->get_index) {
size = (fifo->is_full == RT_FALSE) ? 0 : fifo->buf_sz; size = (fifo->is_full == RT_FALSE) ? 0 : fifo->buf_sz;
} else if (fifo->put_index > fifo->get_index) { } else if (fifo->put_index > fifo->get_index) {
@ -322,7 +355,7 @@ rt_inline int _serial_fifo_rx(struct rt_serial_device *serial, rt_uint8_t *data,
{ {
rt_size_t len, size; rt_size_t len, size;
struct rt_serial_fifo* rx_fifo; struct rt_serial_fifo* rx_fifo;
register rt_base_t level; rt_base_t level;
RT_ASSERT(serial != RT_NULL); RT_ASSERT(serial != RT_NULL);
@ -332,6 +365,7 @@ rt_inline int _serial_fifo_rx(struct rt_serial_device *serial, rt_uint8_t *data,
/* disable interrupt */ /* disable interrupt */
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
// serial->ops->disable_interrupt(serial);
len = _serial_fifo_calc_data_len(rx_fifo); len = _serial_fifo_calc_data_len(rx_fifo);
@ -339,6 +373,7 @@ rt_inline int _serial_fifo_rx(struct rt_serial_device *serial, rt_uint8_t *data,
(serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) == RT_DEVICE_OFLAG_NONBLOCKING) { (serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) == RT_DEVICE_OFLAG_NONBLOCKING) {
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
return 0; return 0;
} }
if ((len == 0) && // blocking io mode if ((len == 0) && // blocking io mode
@ -347,6 +382,7 @@ rt_inline int _serial_fifo_rx(struct rt_serial_device *serial, rt_uint8_t *data,
do { do {
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
#ifndef RT_SERIAL_USE_EVENT #ifndef RT_SERIAL_USE_EVENT
ret = rt_completion_wait(&(serial->completion_rx), RT_WAITING_FOREVER); ret = rt_completion_wait(&(serial->completion_rx), RT_WAITING_FOREVER);
@ -355,11 +391,12 @@ rt_inline int _serial_fifo_rx(struct rt_serial_device *serial, rt_uint8_t *data,
#endif #endif
if (ret == RT_EOK || ret == -RT_ETIMEOUT) { if (ret == RT_EOK || ret == -RT_ETIMEOUT) {
} else { } else {
return size; return 0;
} }
/* disable interrupt */ /* disable interrupt */
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
// serial->ops->disable_interrupt(serial);
len = _serial_fifo_calc_data_len(rx_fifo); len = _serial_fifo_calc_data_len(rx_fifo);
} while(len == 0); } while(len == 0);
@ -380,6 +417,7 @@ rt_inline int _serial_fifo_rx(struct rt_serial_device *serial, rt_uint8_t *data,
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
return size; return size;
} }
@ -388,8 +426,9 @@ rt_inline int _serial_int_tx(struct rt_serial_device *serial, const rt_uint8_t *
{ {
rt_size_t len, length_t, size; rt_size_t len, length_t, size;
struct rt_serial_fifo *tx_fifo; struct rt_serial_fifo *tx_fifo;
register rt_base_t level; rt_base_t level;
rt_uint8_t last_char = 0; rt_uint8_t ch = 0;
static rt_uint8_t last_char = 0;
RT_ASSERT(serial != RT_NULL); RT_ASSERT(serial != RT_NULL);
@ -402,6 +441,7 @@ rt_inline int _serial_int_tx(struct rt_serial_device *serial, const rt_uint8_t *
length_t = length - size; length_t = length - size;
/* disable interrupt */ /* disable interrupt */
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
// serial->ops->disable_interrupt(serial);
len = tx_fifo->buf_sz - _serial_fifo_calc_data_len(tx_fifo); len = tx_fifo->buf_sz - _serial_fifo_calc_data_len(tx_fifo);
@ -409,6 +449,7 @@ rt_inline int _serial_int_tx(struct rt_serial_device *serial, const rt_uint8_t *
(serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) == RT_DEVICE_OFLAG_NONBLOCKING) { (serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) == RT_DEVICE_OFLAG_NONBLOCKING) {
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
break; break;
} }
@ -417,6 +458,7 @@ rt_inline int _serial_int_tx(struct rt_serial_device *serial, const rt_uint8_t *
rt_err_t ret; rt_err_t ret;
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
#ifndef RT_SERIAL_USE_EVENT #ifndef RT_SERIAL_USE_EVENT
ret = rt_completion_wait(&(serial->completion_tx), RT_WAITING_FOREVER); ret = rt_completion_wait(&(serial->completion_tx), RT_WAITING_FOREVER);
#else #else
@ -443,7 +485,12 @@ rt_inline int _serial_int_tx(struct rt_serial_device *serial, const rt_uint8_t *
last_char != '\r') { last_char != '\r') {
_serial_fifo_push_data(tx_fifo, '\r'); _serial_fifo_push_data(tx_fifo, '\r');
last_char = 0; last_char = '\r';
if (len == 1) {
break;
} else {
len--;
}
} else if (*data == '\r') { } else if (*data == '\r') {
last_char = '\r'; last_char = '\r';
} else { } else {
@ -461,10 +508,18 @@ rt_inline int _serial_int_tx(struct rt_serial_device *serial, const rt_uint8_t *
} }
// TODO: start tx // TODO: start tx
#if defined (RT_SERIAL_NO_TXEIT)
if (serial->ops->is_int_txing != RT_NULL && serial->ops->is_int_txing(serial) == RT_FALSE) {
ch = _serial_fifo_pop_data(tx_fifo);
serial->ops->start_tx(serial, ch);
}
#else
serial->ops->start_tx(serial); serial->ops->start_tx(serial);
#endif
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
} while(size < length); } while(size < length);
return size; return size;
@ -478,8 +533,9 @@ rt_inline int _serial_dma_tx(struct rt_serial_device *serial, const rt_uint8_t *
{ {
rt_size_t len, length_t, size, i; rt_size_t len, length_t, size, i;
struct rt_serial_fifo *tx_fifo; struct rt_serial_fifo *tx_fifo;
register rt_base_t level; rt_base_t level;
rt_uint8_t last_char = 0, ch; rt_uint8_t ch;
static rt_uint8_t last_char = 0;
RT_ASSERT(serial != RT_NULL); RT_ASSERT(serial != RT_NULL);
@ -492,6 +548,7 @@ rt_inline int _serial_dma_tx(struct rt_serial_device *serial, const rt_uint8_t *
length_t = length - size; length_t = length - size;
/* disable interrupt */ /* disable interrupt */
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
// serial->ops->disable_interrupt(serial);
len = tx_fifo->buf_sz - _serial_fifo_calc_data_len(tx_fifo); len = tx_fifo->buf_sz - _serial_fifo_calc_data_len(tx_fifo);
@ -499,6 +556,7 @@ rt_inline int _serial_dma_tx(struct rt_serial_device *serial, const rt_uint8_t *
(serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) == RT_DEVICE_OFLAG_NONBLOCKING) { (serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) == RT_DEVICE_OFLAG_NONBLOCKING) {
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
break; break;
} }
@ -507,6 +565,7 @@ rt_inline int _serial_dma_tx(struct rt_serial_device *serial, const rt_uint8_t *
rt_err_t ret; rt_err_t ret;
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
#ifndef RT_SERIAL_USE_EVENT #ifndef RT_SERIAL_USE_EVENT
ret = rt_completion_wait(&(serial->completion_tx), RT_WAITING_FOREVER); ret = rt_completion_wait(&(serial->completion_tx), RT_WAITING_FOREVER);
@ -534,7 +593,12 @@ rt_inline int _serial_dma_tx(struct rt_serial_device *serial, const rt_uint8_t *
last_char != '\r') { last_char != '\r') {
_serial_fifo_push_data(tx_fifo, '\r'); _serial_fifo_push_data(tx_fifo, '\r');
last_char = 0; last_char = '\r';
if (len == 1) {
break;
} else {
len--;
}
} else if (*data == '\r') { } else if (*data == '\r') {
last_char = '\r'; last_char = '\r';
} else { } else {
@ -553,10 +617,12 @@ rt_inline int _serial_dma_tx(struct rt_serial_device *serial, const rt_uint8_t *
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
// TODO: start tx // TODO: start tx
/* disable interrupt */ /* disable interrupt */
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
// serial->ops->disable_interrupt(serial);
if (serial->ops->is_dma_txing(serial) == RT_FALSE) { if (serial->ops->is_dma_txing(serial) == RT_FALSE) {
/* calucate fifo data size */ /* calucate fifo data size */
len = _serial_fifo_calc_data_len(tx_fifo); len = _serial_fifo_calc_data_len(tx_fifo);
@ -577,6 +643,7 @@ rt_inline int _serial_dma_tx(struct rt_serial_device *serial, const rt_uint8_t *
} }
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
} while(size < length); } while(size < length);
return size; return size;
@ -599,9 +666,9 @@ static rt_err_t rt_serial_init(struct rt_device *dev)
serial->serial_rx = RT_NULL; serial->serial_rx = RT_NULL;
serial->serial_tx = RT_NULL; serial->serial_tx = RT_NULL;
/* apply configuration */ /* initialize hardware */
if (serial->ops->configure) if (serial->ops->init)
result = serial->ops->configure(serial, &serial->config); result = serial->ops->init(serial);
return result; return result;
} }
@ -641,9 +708,9 @@ static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
struct rt_serial_fifo* rx_fifo; struct rt_serial_fifo* rx_fifo;
rx_fifo = (struct rt_serial_fifo*)rt_malloc(sizeof(struct rt_serial_fifo) + rx_fifo = (struct rt_serial_fifo*)rt_malloc(sizeof(struct rt_serial_fifo) +
serial->config.bufsz); serial->bufsz);
RT_ASSERT(rx_fifo != RT_NULL); RT_ASSERT(rx_fifo != RT_NULL);
rx_fifo->buf_sz = serial->config.bufsz; rx_fifo->buf_sz = serial->bufsz;
rx_fifo->buffer = (rt_uint8_t*) (rx_fifo + 1); rx_fifo->buffer = (rt_uint8_t*) (rx_fifo + 1);
rt_memset(rx_fifo->buffer, 0, rx_fifo->buf_sz); rt_memset(rx_fifo->buffer, 0, rx_fifo->buf_sz);
rx_fifo->put_index = 0; rx_fifo->put_index = 0;
@ -672,9 +739,9 @@ static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
struct rt_serial_fifo* rx_fifo; struct rt_serial_fifo* rx_fifo;
rx_fifo = (struct rt_serial_fifo*)rt_malloc(sizeof(struct rt_serial_fifo) + rx_fifo = (struct rt_serial_fifo*)rt_malloc(sizeof(struct rt_serial_fifo) +
serial->config.bufsz); serial->bufsz);
RT_ASSERT(rx_fifo != RT_NULL); RT_ASSERT(rx_fifo != RT_NULL);
rx_fifo->buf_sz = serial->config.bufsz; rx_fifo->buf_sz = serial->bufsz;
rx_fifo->buffer = (rt_uint8_t*) (rx_fifo + 1); rx_fifo->buffer = (rt_uint8_t*) (rx_fifo + 1);
rt_memset(rx_fifo->buffer, 0, rx_fifo->buf_sz); rt_memset(rx_fifo->buffer, 0, rx_fifo->buf_sz);
rx_fifo->put_index = 0; rx_fifo->put_index = 0;
@ -710,9 +777,9 @@ static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
struct rt_serial_fifo *tx_fifo; struct rt_serial_fifo *tx_fifo;
tx_fifo = (struct rt_serial_fifo*)rt_malloc(sizeof(struct rt_serial_fifo) + tx_fifo = (struct rt_serial_fifo*)rt_malloc(sizeof(struct rt_serial_fifo) +
serial->config.bufsz); serial->bufsz);
RT_ASSERT(tx_fifo != RT_NULL); RT_ASSERT(tx_fifo != RT_NULL);
tx_fifo->buf_sz = serial->config.bufsz; tx_fifo->buf_sz = serial->bufsz;
tx_fifo->buffer = (rt_uint8_t*) (tx_fifo + 1); tx_fifo->buffer = (rt_uint8_t*) (tx_fifo + 1);
rt_memset(tx_fifo->buffer, 0, tx_fifo->buf_sz); rt_memset(tx_fifo->buffer, 0, tx_fifo->buf_sz);
tx_fifo->put_index = 0; tx_fifo->put_index = 0;
@ -739,9 +806,9 @@ static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
struct rt_serial_fifo *tx_fifo; struct rt_serial_fifo *tx_fifo;
tx_fifo = (struct rt_serial_fifo*)rt_malloc(sizeof(struct rt_serial_fifo) + tx_fifo = (struct rt_serial_fifo*)rt_malloc(sizeof(struct rt_serial_fifo) +
serial->config.bufsz); serial->bufsz);
RT_ASSERT(tx_fifo != RT_NULL); RT_ASSERT(tx_fifo != RT_NULL);
tx_fifo->buf_sz = serial->config.bufsz; tx_fifo->buf_sz = serial->bufsz;
tx_fifo->buffer = (rt_uint8_t*) (tx_fifo + 1); tx_fifo->buffer = (rt_uint8_t*) (tx_fifo + 1);
rt_memset(tx_fifo->buffer, 0, tx_fifo->buf_sz); rt_memset(tx_fifo->buffer, 0, tx_fifo->buf_sz);
tx_fifo->put_index = 0; tx_fifo->put_index = 0;
@ -784,7 +851,6 @@ static rt_err_t rt_serial_close(struct rt_device *dev)
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
serial = (struct rt_serial_device *)dev; serial = (struct rt_serial_device *)dev;
rt_serial_flush(dev);
serial->ops->control(serial, RT_DEVICE_CTRL_CLOSE, RT_NULL); serial->ops->control(serial, RT_DEVICE_CTRL_CLOSE, RT_NULL);
if (dev->open_flag & RT_DEVICE_FLAG_INT_RX) if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
@ -874,7 +940,7 @@ static rt_err_t rt_serial_close(struct rt_device *dev)
return RT_EOK; return RT_EOK;
} }
static rt_size_t rt_serial_read(struct rt_device *dev, static rt_ssize_t rt_serial_read(struct rt_device *dev,
rt_off_t pos, rt_off_t pos,
void *buffer, void *buffer,
rt_size_t size) rt_size_t size)
@ -889,7 +955,7 @@ static rt_size_t rt_serial_read(struct rt_device *dev,
return serial->_cb_rx(serial, (rt_uint8_t *)buffer, size); return serial->_cb_rx(serial, (rt_uint8_t *)buffer, size);
} }
static rt_size_t rt_serial_write(struct rt_device *dev, static rt_ssize_t rt_serial_write(struct rt_device *dev,
rt_off_t pos, rt_off_t pos,
const void *buffer, const void *buffer,
rt_size_t size) rt_size_t size)
@ -909,7 +975,7 @@ static rt_err_t rt_serial_flush(struct rt_device *dev)
struct rt_serial_device *serial; struct rt_serial_device *serial;
rt_size_t len; rt_size_t len;
struct rt_serial_fifo *tx_fifo, *rx_fifo; struct rt_serial_fifo *tx_fifo, *rx_fifo;
register rt_base_t level; rt_base_t level;
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
@ -937,16 +1003,19 @@ static rt_err_t rt_serial_flush(struct rt_device *dev)
while(1) { while(1) {
/* disable interrupt */ /* disable interrupt */
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
// serial->ops->disable_interrupt(serial);
len = _serial_fifo_calc_data_len(tx_fifo); len = _serial_fifo_calc_data_len(tx_fifo);
if (len == 0) { if (len == 0) {
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
break; break;
} else { } else {
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
#ifndef RT_SERIAL_USE_EVENT #ifndef RT_SERIAL_USE_EVENT
rt_completion_wait(&(serial->completion_tx), RT_WAITING_FOREVER); rt_completion_wait(&(serial->completion_tx), RT_WAITING_FOREVER);
#else #else
@ -960,8 +1029,11 @@ static rt_err_t rt_serial_flush(struct rt_device *dev)
} }
#ifdef RT_SERIAL_USING_DMA #ifdef RT_SERIAL_USING_DMA
else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX) { else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX) {
while (serial->ops->is_dma_txing(serial) == RT_TRUE);
} }
#endif /* RT_SERIAL_USING_DMA */ #endif /* RT_SERIAL_USING_DMA */
} else {
serial->ops->flush(serial);
} }
return RT_EOK; return RT_EOK;
@ -1018,7 +1090,7 @@ static int _get_baudrate(speed_t speed)
static void _tc_flush(struct rt_serial_device *serial, int queue) static void _tc_flush(struct rt_serial_device *serial, int queue)
{ {
register rt_base_t level; rt_base_t level;
int ch = -1; int ch = -1;
struct rt_serial_fifo *rx_fifo = RT_NULL; struct rt_serial_fifo *rx_fifo = RT_NULL;
struct rt_device *device = RT_NULL; struct rt_device *device = RT_NULL;
@ -1093,15 +1165,13 @@ static rt_err_t rt_serial_control(struct rt_device *dev,
if (args) if (args)
{ {
struct serial_configure *pconfig = (struct serial_configure *) args; struct serial_configure *pconfig = (struct serial_configure *) args;
if (pconfig->bufsz != serial->config.bufsz && serial->parent.ref_count)
{ /* serial device has been opened, to configure it */
/*can not change buffer size*/ ret = serial->ops->configure(serial, pconfig);
return RT_EBUSY; if (ret == RT_EOK) {
}
/* set serial configure */ /* set serial configure */
serial->config = *pconfig; serial->config = *pconfig;
/* serial device has been opened, to configure it */ }
serial->ops->configure(serial, pconfig);
} }
break; break;
#ifdef RT_USING_POSIX_STDIO #ifdef RT_USING_POSIX_STDIO
@ -1290,7 +1360,7 @@ static rt_err_t rt_serial_control(struct rt_device *dev,
case FIONREAD: case FIONREAD:
{ {
rt_size_t recved = 0; rt_size_t recved = 0;
register rt_base_t level; rt_base_t level;
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
recved = _serial_fifo_calc_data_len(serial->serial_rx); recved = _serial_fifo_calc_data_len(serial->serial_rx);
@ -1312,13 +1382,13 @@ static rt_err_t rt_serial_control(struct rt_device *dev,
#ifdef RT_USING_DEVICE_OPS #ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops serial_ops = const static struct rt_device_ops serial_ops =
{ {
rt_serial_init, .init = rt_serial_init,
rt_serial_open, .open = rt_serial_open,
rt_serial_close, .close = rt_serial_close,
rt_serial_read, .read = rt_serial_read,
rt_serial_write, .write = rt_serial_write,
rt_serial_control, .flush = rt_serial_flush,
rt_serial_flush, .control = rt_serial_control
}; };
#endif #endif
@ -1334,6 +1404,9 @@ rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
struct rt_device *device; struct rt_device *device;
RT_ASSERT(serial != RT_NULL); RT_ASSERT(serial != RT_NULL);
serial->config = RT_SERIAL_CONFIG_DEFAULT;
serial->bufsz = RT_SERIAL_FIFO_BUFSZ;
device = &(serial->parent); device = &(serial->parent);
device->type = RT_Device_Class_Char; device->type = RT_Device_Class_Char;
@ -1377,9 +1450,14 @@ void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
/* interrupt mode receive */ /* interrupt mode receive */
rx_fifo = (struct rt_serial_fifo*)serial->serial_rx; rx_fifo = (struct rt_serial_fifo*)serial->serial_rx;
while (1) { #if defined(RT_SERIAL_HARD_FIFO)
while (1)
#endif
{
ch = serial->ops->getc(serial); ch = serial->ops->getc(serial);
#if defined(RT_SERIAL_HARD_FIFO)
if (ch == -1) break; if (ch == -1) break;
#endif
/* if fifo is full, discard one byte first */ /* if fifo is full, discard one byte first */
if (rx_fifo->is_full == RT_TRUE) { if (rx_fifo->is_full == RT_TRUE) {
@ -1467,10 +1545,10 @@ void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
int dma_idx, ch = -1; int dma_idx, ch = -1;
struct rt_serial_fifo* rx_fifo; struct rt_serial_fifo* rx_fifo;
if (serial->dma_idx_rx == dma_idx) break;
dma_idx = event >> 8; dma_idx = event >> 8;
if (serial->dma_idx_rx == dma_idx) break;
rx_fifo = (struct rt_serial_fifo*)serial->serial_rx; rx_fifo = (struct rt_serial_fifo*)serial->serial_rx;
while (serial->dma_idx_rx != dma_idx) { while (serial->dma_idx_rx != dma_idx) {
@ -1557,4 +1635,3 @@ void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
} }
} }
#endif /* RT_USING_SERIAL */ #endif /* RT_USING_SERIAL */

View File

@ -519,6 +519,11 @@ rt_size_t rt_device_write(rt_device_t dev,
const void *buffer, const void *buffer,
rt_size_t size); rt_size_t size);
rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg); rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg);
rt_err_t rt_device_flush(rt_device_t dev);
#ifdef RT_USING_DM
rt_err_t rt_device_bind_driver(rt_device_t device, rt_driver_t driver, void *node);
rt_device_t rt_device_create_since_driver(rt_driver_t drv,int device_id);
rt_err_t rt_device_probe_and_init(rt_device_t device);
/**@}*/ /**@}*/
#endif #endif

View File

@ -403,8 +403,7 @@ rt_err_t rt_device_flush(rt_device_t dev)
if (dev->ref_count == 0) if (dev->ref_count == 0)
{ {
rt_set_errno(-RT_ERROR); return -RT_ERROR;
return 0;
} }
/* call device_write interface */ /* call device_write interface */
@ -413,10 +412,7 @@ rt_err_t rt_device_flush(rt_device_t dev)
return device_flush(dev); return device_flush(dev);
} }
/* set error code */ return -RT_ENOSYS;
rt_set_errno(-RT_ENOSYS);
return 0;
} }
RTM_EXPORT(rt_device_flush); RTM_EXPORT(rt_device_flush);