加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
serialX.c 47.30 KB
一键复制 编辑 原始数据 按行查看 历史
thewon 提交于 2023-08-26 16:23 . compatibility processing
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2006-03-13 bernard first version
* 2012-05-15 lgnq modified according bernard's implementation.
* 2012-05-28 bernard code cleanup
* 2012-11-23 bernard fix compiler warning.
* 2013-02-20 bernard use RT_SERIAL_RB_BUFSZ to define
* the size of ring buffer.
* 2014-07-10 bernard rewrite serial framework
* 2014-12-31 bernard use open_flag for poll_tx stream mode.
* 2015-05-19 Quintin fix DMA tx mod tx_dma->activated flag !=RT_FALSE BUG
* in open function.
* 2015-11-10 bernard fix the poll rx issue when there is no data.
* 2016-05-10 armink add fifo mode to DMA rx when serial->config.bufsz != 0.
* 2017-01-19 aubr.cool prevent change serial rx bufsz when serial is opened.
* 2017-11-07 JasonJia fix data bits error issue when using tcsetattr.
* 2017-11-15 JasonJia fix poll rx issue when data is full.
* add TCFLSH and FIONREAD support.
* 2018-12-08 Ernest Chen add DMA choice
* 2020-09-14 WillianChan add a line feed to the carriage return character
* when using interrupt tx
* 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)
* 2022-04-10 THEWON serialX first version
* 2022-06-08 THEWON add No TX Empty interrupt support
* 2023-02-15 THEWON add init ops
* 2023-08-19 THEWON blocking timeout
*/
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#ifdef RT_USING_SERIAL
#define DBG_TAG "UART"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#ifdef RT_USING_POSIX_STDIO
#if RTTHREAD_VERSION <= RT_VERSION_CHECK(4, 0, 3)
#include <dfs_posix.h>
#include <dfs_poll.h>
#else
#include <dfs_file.h>
#include <fcntl.h>
#include <poll.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#endif
#ifdef RT_USING_POSIX_TERMIOS
#if RTTHREAD_VERSION <= RT_VERSION_CHECK(4, 0, 3)
#include <posix_termios.h>
#else
#include <termios.h>
#endif
#endif
/* 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:
LOG_D("fops open: O_RDONLY!");
flags = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_RDONLY;
break;
case O_WRONLY:
LOG_D("fops open: O_WRONLY!");
flags = RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_WRONLY;
break;
case O_RDWR:
LOG_D("fops open: O_RDWR!");
flags = RT_DEVICE_FLAG_RDWR
| RT_DEVICE_FLAG_INT_RX
| RT_DEVICE_FLAG_INT_TX;
break;
default:
LOG_E("fops open: unknown mode - %d!", fd->flags & O_ACCMODE);
break;
}
if ((fd->flags & O_NONBLOCK) != 0) {
flags |= RT_DEVICE_OFLAG_NONBLOCKING;
}
ret = rt_device_open(device, flags);
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;
}
static int serial_fops_close(struct dfs_fd *fd)
{
rt_device_t device;
device = (rt_device_t)fd->data;
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;
int flags = 0;
rt_device_t device;
flags = fd->flags & O_ACCMODE;
if (flags == O_WRONLY) {
return -EIO;
}
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)
{
int flags = 0;
rt_device_t device;
flags = fd->flags & O_ACCMODE;
if (flags == O_RDONLY) {
return -EIO;
}
device = (rt_device_t)fd->data;
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)
{
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_fifo* rx_fifo;
rt_poll_add(&(device->wait_queue), req);
rx_fifo = (struct rt_serial_fifo*) serial->serial_rx;
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))
{
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,
serial_fops_flush, /* flush */
RT_NULL, /* lseek */
RT_NULL, /* getdents */
serial_fops_poll,
};
#endif
/**
* Calculate fifo data length.
*
* @param fifo the data fifo of serial device
*
* @return length
*/
rt_inline rt_size_t _serial_fifo_calc_data_len(struct rt_serial_fifo *fifo)
{
rt_size_t size;
if (fifo->put_index == fifo->get_index) {
size = (fifo->is_full == RT_FALSE) ? 0 : fifo->buf_sz;
} else if (fifo->put_index > fifo->get_index) {
size = fifo->put_index - fifo->get_index;
} else {
size = fifo->buf_sz - (fifo->get_index - fifo->put_index);
}
return size;
}
rt_inline void _serial_fifo_push_data(struct rt_serial_fifo *fifo, rt_uint8_t ch)
{
fifo->buffer[fifo->put_index] = ch;
fifo->put_index += 1;
if (fifo->put_index >= fifo->buf_sz) fifo->put_index = 0;
}
rt_inline rt_uint8_t _serial_fifo_pop_data(struct rt_serial_fifo *fifo)
{
rt_uint8_t ch;
ch = fifo->buffer[fifo->get_index];
fifo->get_index += 1;
if (fifo->get_index >= fifo->buf_sz) fifo->get_index = 0;
return ch;
}
/*
* Serial poll routines
*/
rt_inline int _serial_poll_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
{
int ch;
int size;
RT_ASSERT(serial != RT_NULL);
size = length;
while (length)
{
ch = serial->ops->getc(serial);
if (ch == -1) break;
*data = ch;
data++; length--;
}
return size - length;
}
rt_inline int _serial_poll_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
{
int size;
rt_uint8_t last_char = 0;
RT_ASSERT(serial != RT_NULL);
size = length;
while (length)
{
/*
* to be polite with serial console add a line feed
* to the carriage return character
*/
if (*data == '\n' &&
(serial->parent.open_flag & RT_DEVICE_FLAG_STREAM) == RT_DEVICE_FLAG_STREAM &&
last_char != '\r')
{
serial->ops->putc(serial, '\r');
last_char = 0;
} else if (*data == '\r') {
last_char = '\r';
} else {
last_char = 0;
}
serial->ops->putc(serial, *data);
data++; length--;
}
return size - length;
}
/*
* Serial interrupt routines
*/
rt_inline int _serial_fifo_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
{
rt_size_t len, size;
struct rt_serial_fifo* rx_fifo;
rt_base_t level;
RT_ASSERT(serial != RT_NULL);
if (length == 0) return 0;
rx_fifo = (struct rt_serial_fifo*) serial->serial_rx;
/* disable interrupt */
level = rt_hw_interrupt_disable();
// serial->ops->disable_interrupt(serial);
len = _serial_fifo_calc_data_len(rx_fifo);
if ((len == 0) && // non-blocking io mode
(serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) == RT_DEVICE_OFLAG_NONBLOCKING) {
/* enable interrupt */
rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
return 0;
}
if ((len == 0) && // blocking io mode
(serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) != RT_DEVICE_OFLAG_NONBLOCKING) {
rt_err_t ret;
do {
/* enable interrupt */
rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
#ifndef RT_SERIAL_USE_EVENT
ret = rt_completion_wait(&(serial->completion_rx), serial->timeout_tick);
#else
ret = rt_event_recv(serial->rx_done, RT_SERIAL_EVENT_RXDONE, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, serial->timeout_tick, RT_NULL);
#endif
if (ret == RT_EOK) {
} else if (ret == -RT_ETIMEOUT) {
return -RT_ETIMEOUT;
} else {
return 0;
}
/* disable interrupt */
level = rt_hw_interrupt_disable();
// serial->ops->disable_interrupt(serial);
len = _serial_fifo_calc_data_len(rx_fifo);
} while(len == 0);
}
if (len > length) {
len = length;
}
/* read from software FIFO */
for (size = 0; size < len; size++) {
/* otherwise there's the data: */
*data = _serial_fifo_pop_data(rx_fifo);
data++;
}
rx_fifo->is_full = RT_FALSE;
/* enable interrupt */
rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
return size;
}
rt_inline int _serial_int_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
{
rt_size_t len, length_t, size;
struct rt_serial_fifo *tx_fifo;
rt_base_t level;
rt_uint8_t ch = 0;
static rt_uint8_t last_char = 0;
RT_ASSERT(serial != RT_NULL);
if (length == 0) return 0;
tx_fifo = (struct rt_serial_fifo*) serial->serial_tx;
size = 0;
do {
length_t = length - size;
/* disable interrupt */
level = rt_hw_interrupt_disable();
// serial->ops->disable_interrupt(serial);
len = tx_fifo->buf_sz - _serial_fifo_calc_data_len(tx_fifo);
if ((len == 0) && // non-blocking io mode
(serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) == RT_DEVICE_OFLAG_NONBLOCKING) {
/* enable interrupt */
rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
break;
}
if ((len == 0) && // blocking io mode
(serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) != RT_DEVICE_OFLAG_NONBLOCKING) {
rt_err_t ret;
/* enable interrupt */
rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
#ifndef RT_SERIAL_USE_EVENT
ret = rt_completion_wait(&(serial->completion_tx), serial->timeout_tick);
#else
ret = rt_event_recv(serial->tx_done, RT_SERIAL_EVENT_TXDONE, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, serial->timeout_tick, RT_NULL);
#endif
if (ret == RT_EOK) {
continue;
} else if (ret == -RT_ETIMEOUT) {
return -RT_ETIMEOUT;
} else {
return size;
}
}
if (len > length_t) {
len = length_t;
}
/* copy to software FIFO */
while (len > 0) {
/*
* to be polite with serial console add a line feed
* to the carriage return character
*/
if (*data == '\n' &&
(serial->parent.open_flag & RT_DEVICE_FLAG_STREAM) == RT_DEVICE_FLAG_STREAM &&
last_char != '\r') {
_serial_fifo_push_data(tx_fifo, '\r');
last_char = '\r';
if (len == 1) {
break;
} else {
len--;
}
} else if (*data == '\r') {
last_char = '\r';
} else {
last_char = 0;
}
_serial_fifo_push_data(tx_fifo, *data);
data++; len--; size++;
}
/* if the next position is read index, discard this 'read char' */
if (tx_fifo->put_index == tx_fifo->get_index) {
tx_fifo->is_full = RT_TRUE;
}
// 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);
#endif
/* enable interrupt */
rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
} while(size < length);
return size;
}
#ifdef RT_SERIAL_USING_DMA
/*
* Serial DMA routines
*/
rt_inline int _serial_dma_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
{
rt_size_t len, length_t, size, i;
struct rt_serial_fifo *tx_fifo;
rt_base_t level;
rt_uint8_t ch;
static rt_uint8_t last_char = 0;
RT_ASSERT(serial != RT_NULL);
if (length == 0) return 0;
tx_fifo = (struct rt_serial_fifo*) serial->serial_tx;
size = 0;
do {
length_t = length - size;
/* disable interrupt */
level = rt_hw_interrupt_disable();
// serial->ops->disable_interrupt(serial);
len = tx_fifo->buf_sz - _serial_fifo_calc_data_len(tx_fifo);
if ((len == 0) && // non-blocking io mode
(serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) == RT_DEVICE_OFLAG_NONBLOCKING) {
/* enable interrupt */
rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
break;
}
if ((len == 0) && // blocking io mode
(serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) != RT_DEVICE_OFLAG_NONBLOCKING) {
rt_err_t ret;
/* enable interrupt */
rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
#ifndef RT_SERIAL_USE_EVENT
ret = rt_completion_wait(&(serial->completion_tx), serial->timeout_tick);
#else
ret = rt_event_recv(serial->tx_done, RT_SERIAL_EVENT_TXDONE, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, serial->timeout_tick, RT_NULL);
#endif
if (ret == RT_EOK) {
continue;
} else if (ret == -RT_ETIMEOUT) {
return -RT_ETIMEOUT;
} else {
return size;
}
}
if (len > length_t) {
len = length_t;
}
/* copy to software FIFO */
while (len > 0) {
/*
* to be polite with serial console add a line feed
* to the carriage return character
*/
if (*data == '\n' &&
(serial->parent.open_flag & RT_DEVICE_FLAG_STREAM) == RT_DEVICE_FLAG_STREAM &&
last_char != '\r') {
_serial_fifo_push_data(tx_fifo, '\r');
last_char = '\r';
if (len == 1) {
break;
} else {
len--;
}
} else if (*data == '\r') {
last_char = '\r';
} else {
last_char = 0;
}
_serial_fifo_push_data(tx_fifo, *data);
data++; len--; size++;
}
/* if the next position is read index, discard this 'read char' */
if (tx_fifo->put_index == tx_fifo->get_index) {
tx_fifo->is_full = RT_TRUE;
}
/* enable interrupt */
rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
// TODO: start tx
/* disable interrupt */
level = rt_hw_interrupt_disable();
// serial->ops->disable_interrupt(serial);
if (serial->ops->is_dma_txing(serial) == RT_FALSE) {
/* calucate fifo data size */
len = _serial_fifo_calc_data_len(tx_fifo);
if (len > RT_SERIAL_DMA_BUFSZ) {
len = RT_SERIAL_DMA_BUFSZ;
}
/* read from software FIFO */
for (i = 0; i < len; i++) {
/* pop one byte data */
ch = _serial_fifo_pop_data(tx_fifo);
serial->serial_dma_tx[i] = ch;
}
tx_fifo->is_full = RT_FALSE;
serial->ops->start_dma_tx(serial, serial->serial_dma_tx, len);
}
/* enable interrupt */
rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
} while(size < length);
return size;
}
#endif /* RT_SERIAL_USING_DMA */
/* RT-Thread Device Interface */
/*
* This function initializes serial device.
*/
static rt_err_t rt_serial_init(struct rt_device *dev)
{
rt_err_t result = RT_EOK;
struct rt_serial_device *serial;
RT_ASSERT(dev != RT_NULL);
serial = (struct rt_serial_device *)dev;
/* initialize rx/tx */
serial->serial_rx = RT_NULL;
serial->serial_tx = RT_NULL;
/* initialize hardware */
if (serial->ops->init)
result = serial->ops->init(serial);
return result;
}
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;
RT_ASSERT(dev != RT_NULL);
serial = (struct rt_serial_device *)dev;
LOG_D("open serial device: 0x%08x with open flag: 0x%04x",
dev, oflag);
/* check device flag with the open flag */
if ((oflag & RT_DEVICE_FLAG_DMA_RX) && !(dev->flag & RT_DEVICE_FLAG_DMA_RX))
return -RT_EIO;
if ((oflag & RT_DEVICE_FLAG_DMA_TX) && !(dev->flag & RT_DEVICE_FLAG_DMA_TX))
return -RT_EIO;
if ((oflag & RT_DEVICE_FLAG_INT_RX) && !(dev->flag & RT_DEVICE_FLAG_INT_RX))
return -RT_EIO;
if ((oflag & RT_DEVICE_FLAG_INT_TX) && !(dev->flag & RT_DEVICE_FLAG_INT_TX))
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 */
dev->open_flag = oflag & 0xff;
if (oflag & RT_DEVICE_FLAG_INT_RX)
{
/* initialize the Rx/Tx structure according to open flag */
if (serial->serial_rx == RT_NULL)
{
struct rt_serial_fifo* rx_fifo;
rx_fifo = (struct rt_serial_fifo*)rt_malloc(sizeof(struct rt_serial_fifo) +
serial->bufsz);
RT_ASSERT(rx_fifo != RT_NULL);
rx_fifo->buf_sz = serial->bufsz;
rx_fifo->buffer = (rt_uint8_t*) (rx_fifo + 1);
rt_memset(rx_fifo->buffer, 0, rx_fifo->buf_sz);
rx_fifo->put_index = 0;
rx_fifo->get_index = 0;
rx_fifo->is_full = RT_FALSE;
serial->serial_rx = rx_fifo;
}
dev->open_flag |= RT_DEVICE_FLAG_INT_RX;
serial->_cb_rx = _serial_fifo_rx;
#ifndef RT_SERIAL_USE_EVENT
rt_completion_init(&(serial->completion_rx));
#else
serial->rx_done = rt_event_create("rx_done", RT_IPC_FLAG_PRIO);
#endif
/* configure low level device */
serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX);
}
#ifdef RT_SERIAL_USING_DMA
else if (oflag & RT_DEVICE_FLAG_DMA_RX)
{
/* initialize the Rx/Tx structure according to open flag */
if (serial->serial_rx == RT_NULL)
{
struct rt_serial_fifo* rx_fifo;
rx_fifo = (struct rt_serial_fifo*)rt_malloc(sizeof(struct rt_serial_fifo) +
serial->bufsz);
RT_ASSERT(rx_fifo != RT_NULL);
rx_fifo->buf_sz = serial->bufsz;
rx_fifo->buffer = (rt_uint8_t*) (rx_fifo + 1);
rt_memset(rx_fifo->buffer, 0, rx_fifo->buf_sz);
rx_fifo->put_index = 0;
rx_fifo->get_index = 0;
rx_fifo->is_full = RT_FALSE;
serial->serial_rx = rx_fifo;
}
dev->open_flag |= RT_DEVICE_FLAG_DMA_RX;
serial->dma_idx_rx = 0;
serial->_cb_rx = _serial_fifo_rx;
#ifndef RT_SERIAL_USE_EVENT
rt_completion_init(&(serial->completion_rx));
#else
serial->rx_done = rt_event_create("rx_done", RT_IPC_FLAG_PRIO);
#endif
/* configure fifo address and length to low level device */
serial->ops->control(serial, RT_DEVICE_CTRL_CONFIG, (void *) RT_DEVICE_FLAG_DMA_RX);
}
#endif /* RT_SERIAL_USING_DMA */
else
{
serial->serial_rx = RT_NULL;
serial->_cb_rx = _serial_poll_rx;
}
if (oflag & RT_DEVICE_FLAG_INT_TX)
{
if (serial->serial_tx == RT_NULL)
{
struct rt_serial_fifo *tx_fifo;
tx_fifo = (struct rt_serial_fifo*)rt_malloc(sizeof(struct rt_serial_fifo) +
serial->bufsz);
RT_ASSERT(tx_fifo != RT_NULL);
tx_fifo->buf_sz = serial->bufsz;
tx_fifo->buffer = (rt_uint8_t*) (tx_fifo + 1);
rt_memset(tx_fifo->buffer, 0, tx_fifo->buf_sz);
tx_fifo->put_index = 0;
tx_fifo->get_index = 0;
tx_fifo->is_full = RT_FALSE;
#ifndef RT_SERIAL_USE_EVENT
rt_completion_init(&(serial->completion_tx));
#else
serial->tx_done = rt_event_create("tx_done", RT_IPC_FLAG_PRIO);
#endif
serial->serial_tx = tx_fifo;
}
dev->open_flag |= RT_DEVICE_FLAG_INT_TX;
/* configure low level device */
// serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_TX);
serial->_cb_tx = _serial_int_tx;
}
#ifdef RT_SERIAL_USING_DMA
else if (oflag & RT_DEVICE_FLAG_DMA_TX)
{
if (serial->serial_tx == RT_NULL)
{
struct rt_serial_fifo *tx_fifo;
tx_fifo = (struct rt_serial_fifo*)rt_malloc(sizeof(struct rt_serial_fifo) +
serial->bufsz);
RT_ASSERT(tx_fifo != RT_NULL);
tx_fifo->buf_sz = serial->bufsz;
tx_fifo->buffer = (rt_uint8_t*) (tx_fifo + 1);
rt_memset(tx_fifo->buffer, 0, tx_fifo->buf_sz);
tx_fifo->put_index = 0;
tx_fifo->get_index = 0;
tx_fifo->is_full = RT_FALSE;
#ifndef RT_SERIAL_USE_EVENT
rt_completion_init(&(serial->completion_tx));
#else
serial->tx_done = rt_event_create("tx_done", RT_IPC_FLAG_PRIO);
#endif
serial->serial_tx = tx_fifo;
}
dev->open_flag |= RT_DEVICE_FLAG_DMA_TX;
serial->_cb_tx = _serial_dma_tx;
/* configure low level device */
serial->ops->control(serial, RT_DEVICE_CTRL_CONFIG, (void *)RT_DEVICE_FLAG_DMA_TX);
}
#endif /* RT_SERIAL_USING_DMA */
else
{
serial->serial_tx = RT_NULL;
serial->_cb_tx = _serial_poll_tx;
}
serial->ops->control(serial, RT_DEVICE_CTRL_OPEN, (void *)0);
serial->timeout_tick = RT_WAITING_FOREVER;
/* set stream flag */
dev->open_flag |= stream_flag;
return RT_EOK;
}
static rt_err_t rt_serial_close(struct rt_device *dev)
{
struct rt_serial_device *serial;
RT_ASSERT(dev != RT_NULL);
serial = (struct rt_serial_device *)dev;
serial->ops->control(serial, RT_DEVICE_CTRL_CLOSE, RT_NULL);
if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
{
struct rt_serial_fifo *rx_fifo;
/* configure low level device */
serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_FLAG_INT_RX);
dev->open_flag &= ~RT_DEVICE_FLAG_INT_RX;
rx_fifo = (struct rt_serial_fifo *)serial->serial_rx;
rt_free(rx_fifo);
serial->serial_rx = RT_NULL;
#ifndef RT_SERIAL_USE_EVENT
rt_completion_init(&(serial->completion_rx));
#else
rt_event_delete(serial->rx_done);
#endif
}
#ifdef RT_SERIAL_USING_DMA
else if (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
{
struct rt_serial_fifo *rx_fifo;
/* configure low level device */
serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *) RT_DEVICE_FLAG_DMA_RX);
dev->open_flag &= ~RT_DEVICE_FLAG_DMA_RX;
rx_fifo = (struct rt_serial_fifo *)serial->serial_rx;
rt_free(rx_fifo);
serial->serial_rx = RT_NULL;
#ifndef RT_SERIAL_USE_EVENT
rt_completion_init(&(serial->completion_rx));
#else
rt_event_delete(serial->rx_done);
#endif
}
#endif /* RT_SERIAL_USING_DMA */
if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
{
struct rt_serial_fifo *tx_fifo;
/* configure low level device */
serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_FLAG_INT_TX);
dev->open_flag &= ~RT_DEVICE_FLAG_INT_TX;
tx_fifo = (struct rt_serial_fifo *)serial->serial_tx;
rt_free(tx_fifo);
serial->serial_tx = RT_NULL;
#ifndef RT_SERIAL_USE_EVENT
rt_completion_init(&(serial->completion_tx));
#else
rt_event_delete(serial->tx_done);
#endif
}
#ifdef RT_SERIAL_USING_DMA
else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
{
struct rt_serial_fifo *tx_fifo;
/* configure low level device */
serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *) RT_DEVICE_FLAG_DMA_TX);
dev->open_flag &= ~RT_DEVICE_FLAG_DMA_TX;
tx_fifo = (struct rt_serial_fifo *)serial->serial_tx;
rt_free(tx_fifo);
serial->serial_tx = RT_NULL;
#ifndef RT_SERIAL_USE_EVENT
rt_completion_init(&(serial->completion_tx));
#else
rt_event_delete(serial->tx_done);
#endif
}
#endif /* RT_SERIAL_USING_DMA */
dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
return RT_EOK;
}
#if RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 0, 0)
static rt_ssize_t rt_serial_read(struct rt_device *dev,
#else
static rt_size_t rt_serial_read(struct rt_device *dev,
#endif
rt_off_t pos,
void *buffer,
rt_size_t size)
{
struct rt_serial_device *serial;
RT_ASSERT(dev != RT_NULL);
if (size == 0) return 0;
serial = (struct rt_serial_device *)dev;
return serial->_cb_rx(serial, (rt_uint8_t *)buffer, size);
}
#if RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 0, 0)
static rt_ssize_t rt_serial_write(struct rt_device *dev,
#else
static rt_size_t rt_serial_write(struct rt_device *dev,
#endif
rt_off_t pos,
const void *buffer,
rt_size_t size)
{
struct rt_serial_device *serial;
RT_ASSERT(dev != RT_NULL);
if (size == 0) return 0;
serial = (struct rt_serial_device *)dev;
return serial->_cb_tx(serial, (const rt_uint8_t *)buffer, size);
}
static rt_err_t rt_serial_flush(struct rt_device *dev)
{
struct rt_serial_device *serial;
rt_size_t len;
struct rt_serial_fifo *tx_fifo, *rx_fifo;
rt_base_t level;
RT_ASSERT(dev != RT_NULL);
serial = (struct rt_serial_device *)dev;
if((dev->open_flag & RT_DEVICE_FLAG_INT_RX)
#ifdef RT_SERIAL_USING_DMA
|| (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
#endif /* RT_SERIAL_USING_DMA */
) {
rx_fifo = (struct rt_serial_fifo*) serial->serial_rx;
level = rt_hw_interrupt_disable();
rx_fifo->get_index = rx_fifo->put_index = 0;
rx_fifo->is_full = RT_FALSE;
rt_hw_interrupt_enable(level);
}
if ((dev->open_flag & RT_DEVICE_FLAG_INT_TX)
#ifdef RT_SERIAL_USING_DMA
|| (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
#endif /* RT_SERIAL_USING_DMA */
) {
tx_fifo = (struct rt_serial_fifo*) serial->serial_tx;
while(1) {
/* disable interrupt */
level = rt_hw_interrupt_disable();
// serial->ops->disable_interrupt(serial);
len = _serial_fifo_calc_data_len(tx_fifo);
if (len == 0) {
/* enable interrupt */
rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
break;
} else {
/* enable interrupt */
rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
#ifndef RT_SERIAL_USE_EVENT
rt_completion_wait(&(serial->completion_tx), RT_WAITING_FOREVER);
#else
rt_event_recv(serial->tx_done, RT_SERIAL_EVENT_TXDONE, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, RT_NULL);
#endif
}
}
if (dev->open_flag & RT_DEVICE_FLAG_INT_TX) {
serial->ops->flush(serial);
}
#ifdef RT_SERIAL_USING_DMA
else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX) {
while (serial->ops->is_dma_txing(serial) == RT_TRUE);
}
#endif /* RT_SERIAL_USING_DMA */
} else {
serial->ops->flush(serial);
}
return RT_EOK;
}
#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;
}
static void _tc_flush(struct rt_serial_device *serial, int queue)
{
rt_base_t level;
int ch = -1;
struct rt_serial_fifo *rx_fifo = RT_NULL;
struct rt_device *device = RT_NULL;
RT_ASSERT(serial != RT_NULL);
device = &(serial->parent);
rx_fifo = (struct rt_serial_fifo *) serial->serial_rx;
switch(queue)
{
case TCIFLUSH:
case TCIOFLUSH:
RT_ASSERT(rx_fifo != RT_NULL);
if((device->open_flag & RT_DEVICE_FLAG_INT_RX) || (device->open_flag & RT_DEVICE_FLAG_DMA_RX))
{
RT_ASSERT(RT_NULL != rx_fifo);
level = rt_hw_interrupt_disable();
rx_fifo->get_index = rx_fifo->put_index;
rx_fifo->is_full = RT_FALSE;
rt_hw_interrupt_enable(level);
}
else
{
while (1)
{
ch = serial->ops->getc(serial);
if (ch == -1) break;
}
}
break;
case TCOFLUSH:
break;
}
}
#endif
static rt_err_t rt_serial_control(struct rt_device *dev,
int cmd,
void *args)
{
rt_err_t ret = RT_EOK;
struct rt_serial_device *serial;
RT_ASSERT(dev != RT_NULL);
serial = (struct rt_serial_device *)dev;
switch (cmd)
{
case RT_DEVICE_CTRL_SUSPEND:
/* suspend device */
dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
break;
case RT_DEVICE_CTRL_RESUME:
/* resume device */
dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
break;
case RT_DEVICE_CTRL_BLOCKING:
if (((rt_uint32_t)args & RT_DEVICE_OFLAG_NONBLOCKING) == RT_DEVICE_OFLAG_NONBLOCKING) {
dev->open_flag |= RT_DEVICE_OFLAG_NONBLOCKING;
} else {
dev->open_flag &= ~RT_DEVICE_OFLAG_NONBLOCKING;
}
break;
case RT_DEVICE_CTRL_CONFIG:
if (args)
{
struct serial_configure *pconfig = (struct serial_configure *) args;
/* serial device has been opened, to configure it */
ret = serial->ops->configure(serial, pconfig);
if (ret == RT_EOK) {
/* set serial configure */
serial->config = *pconfig;
}
}
break;
case RT_DEVICE_CTRL_TIMEOUT:
{
rt_tick_t timeout_tick = (rt_tick_t)args;
serial->timeout_tick = timeout_tick;
}
break;
#ifdef RT_USING_POSIX_STDIO
#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;
switch (tio->c_cflag & CSIZE)
{
case CS5:
config.data_bits = DATA_BITS_5;
break;
case CS6:
config.data_bits = DATA_BITS_6;
break;
case CS7:
config.data_bits = DATA_BITS_7;
break;
default:
config.data_bits = DATA_BITS_8;
break;
}
if (tio->c_cflag & CSTOPB) config.stop_bits = STOP_BITS_2;
else config.stop_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:
{
int queue = (int)args;
_tc_flush(serial, queue);
}
break;
case TCXONC:
break;
#endif /*RT_USING_POSIX_TERMIOS*/
case TIOCSWINSZ:
{
struct winsize* p_winsize;
p_winsize = (struct winsize*)args;
rt_kprintf("\x1b[8;%d;%dt", p_winsize->ws_col, p_winsize->ws_row);
}
break;
case TIOCGWINSZ:
{
struct winsize* p_winsize;
p_winsize = (struct winsize*)args;
if(rt_thread_self() != rt_thread_find("tshell"))
{
/* only can be used in tshell thread; otherwise, return default size */
p_winsize->ws_col = 80;
p_winsize->ws_row = 24;
}
else
{
#define _TIO_BUFLEN 20
char _tio_buf[_TIO_BUFLEN];
unsigned char cnt1, cnt2, cnt3, i;
char row_s[4], col_s[4];
char *p;
rt_memset(_tio_buf, 0, _TIO_BUFLEN);
/* send the command to terminal for getting the window size of the terminal */
rt_kprintf("\033[18t");
/* waiting for the response from the terminal */
i = 0;
while(i < _TIO_BUFLEN)
{
_tio_buf[i] = getchar();
if(_tio_buf[i] != 't')
{
i ++;
}
else
{
break;
}
}
if(i == _TIO_BUFLEN)
{
/* buffer overloaded, and return default size */
p_winsize->ws_col = 80;
p_winsize->ws_row = 24;
break;
}
/* interpreting data eg: "\033[8;1;15t" which means row is 1 and col is 15 (unit: size of ONE character) */
rt_memset(row_s,0,4);
rt_memset(col_s,0,4);
cnt1 = 0;
while(_tio_buf[cnt1] != ';' && cnt1 < _TIO_BUFLEN)
{
cnt1++;
}
cnt2 = ++cnt1;
while(_tio_buf[cnt2] != ';' && cnt2 < _TIO_BUFLEN)
{
cnt2++;
}
p = row_s;
while(cnt1 < cnt2)
{
*p++ = _tio_buf[cnt1++];
}
p = col_s;
cnt2++;
cnt3 = rt_strlen(_tio_buf) - 1;
while(cnt2 < cnt3)
{
*p++ = _tio_buf[cnt2++];
}
/* load the window size date */
p_winsize->ws_col = atoi(col_s);
p_winsize->ws_row = atoi(row_s);
#undef _TIO_BUFLEN
}
p_winsize->ws_xpixel = 0;/* unused */
p_winsize->ws_ypixel = 0;/* unused */
}
break;
case FIONREAD:
{
rt_size_t recved = 0;
rt_base_t level;
level = rt_hw_interrupt_disable();
recved = _serial_fifo_calc_data_len(serial->serial_rx);
rt_hw_interrupt_enable(level);
*(rt_size_t *)args = recved;
}
break;
#endif /*RT_USING_POSIX_STDIO*/
default :
/* control device */
ret = serial->ops->control(serial, cmd, args);
break;
}
return ret;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops serial_ops =
{
.init = rt_serial_init,
.open = rt_serial_open,
.close = rt_serial_close,
.read = rt_serial_read,
.write = rt_serial_write,
.flush = rt_serial_flush,
.control = rt_serial_control
};
#endif
/*
* serial register
*/
rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
const char *name,
rt_uint32_t flag,
void *data)
{
rt_err_t ret;
struct rt_device *device;
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
RT_ASSERT(serial != RT_NULL);
serial->config = config;
serial->bufsz = RT_SERIAL_FIFO_BUFSZ;
serial->timeout_tick = RT_WAITING_FOREVER;
device = &(serial->parent);
device->type = RT_Device_Class_Char;
device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL;
#ifdef RT_USING_DEVICE_OPS
device->ops = &serial_ops;
#else
device->init = rt_serial_init;
device->open = rt_serial_open;
device->close = rt_serial_close;
device->read = rt_serial_read;
device->write = rt_serial_write;
device->flush = rt_serial_flush;
device->control = rt_serial_control;
#endif
device->user_data = data;
/* register a character device */
ret = rt_device_register(device, name, flag);
#if defined(RT_USING_POSIX_STDIO)
/* set fops */
device->fops = &_serial_fops;
#endif
return ret;
}
/* ISR for serial interrupt */
void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
{
switch (event & 0xff)
{
case RT_SERIAL_EVENT_RX_IND:
{
int ch = -1;
struct rt_serial_fifo* rx_fifo;
/* interrupt mode receive */
rx_fifo = (struct rt_serial_fifo*)serial->serial_rx;
#if defined(RT_SERIAL_HARD_FIFO)
while (1)
#endif
{
ch = serial->ops->getc(serial);
#if defined(RT_SERIAL_HARD_FIFO)
if (ch == -1) break;
#endif
/* if fifo is full, discard one byte first */
if (rx_fifo->is_full == RT_TRUE) {
rx_fifo->get_index += 1;
if (rx_fifo->get_index >= rx_fifo->buf_sz) rx_fifo->get_index = 0;
}
/* push a new data */
_serial_fifo_push_data(rx_fifo, ch);
/* if put index equal to read index, fifo is full */
if (rx_fifo->put_index == rx_fifo->get_index)
{
rx_fifo->is_full = RT_TRUE;
}
}
#ifndef RT_SERIAL_USE_EVENT
rt_completion_done(&(serial->completion_rx));
#else
rt_event_send(serial->rx_done, RT_SERIAL_EVENT_RXDONE);
#endif
/* invoke callback */
if (serial->parent.rx_indicate != RT_NULL) {
rt_size_t rx_length;
/* get rx length */
rx_length = _serial_fifo_calc_data_len(rx_fifo);
if (rx_length) {
serial->parent.rx_indicate(&serial->parent, rx_length);
}
}
}
break;
case RT_SERIAL_EVENT_TX_DONE:
{
#if defined(RT_SERIAL_HARD_FIFO)
rt_size_t fifo_sz;
#endif
rt_size_t len;
rt_uint8_t ch = 0;
struct rt_serial_fifo *tx_fifo;
tx_fifo = (struct rt_serial_fifo*) serial->serial_tx;
/* calucate fifo data size */
len = _serial_fifo_calc_data_len(tx_fifo);
if (len == 0) {
// TODO: stop tx
serial->ops->stop_tx(serial);
#ifndef RT_SERIAL_USE_EVENT
rt_completion_done(&(serial->completion_tx));
#else
rt_event_send(serial->tx_done, RT_SERIAL_EVENT_TXDONE);
#endif
/* invoke callback */
if (serial->parent.tx_complete != RT_NULL) {
serial->parent.tx_complete(&serial->parent, (void*)len);
}
break;
}
#if defined(RT_SERIAL_HARD_FIFO)
fifo_sz = event >> 8;
if (len > fifo_sz) {
len = fifo_sz;
}
/* read from software FIFO */
while (len > 0) {
/* pop one byte data */
ch = _serial_fifo_pop_data(tx_fifo);
serial->ops->putc(serial, ch);
len--;
}
#else
/* pop one byte data */
ch = _serial_fifo_pop_data(tx_fifo);
serial->ops->putc(serial, ch);
#endif
tx_fifo->is_full = RT_FALSE;
}
break;
#ifdef RT_SERIAL_USING_DMA
case RT_SERIAL_EVENT_RX_DMADONE:
{
int dma_idx, ch = -1;
struct rt_serial_fifo* rx_fifo;
dma_idx = event >> 8;
if (serial->dma_idx_rx == dma_idx) break;
rx_fifo = (struct rt_serial_fifo*)serial->serial_rx;
while (serial->dma_idx_rx != dma_idx) {
ch = serial->serial_dma_rx[serial->dma_idx_rx];
/* if fifo is full, discard one byte first */
if (rx_fifo->is_full == RT_TRUE) {
rx_fifo->get_index += 1;
if (rx_fifo->get_index >= rx_fifo->buf_sz) rx_fifo->get_index = 0;
}
/* push a new data */
_serial_fifo_push_data(rx_fifo, ch);
/* if put index equal to read index, fifo is full */
if (rx_fifo->put_index == rx_fifo->get_index)
{
rx_fifo->is_full = RT_TRUE;
}
serial->dma_idx_rx++;
if (serial->dma_idx_rx == RT_SERIAL_DMA_BUFSZ) {
serial->dma_idx_rx = 0;
}
}
serial->dma_idx_rx = dma_idx;
#ifndef RT_SERIAL_USE_EVENT
rt_completion_done(&(serial->completion_rx));
#else
rt_event_send(serial->rx_done, RT_SERIAL_EVENT_RXDONE);
#endif
/* invoke callback */
if (serial->parent.rx_indicate != RT_NULL) {
rt_size_t rx_length;
/* get rx length */
rx_length = _serial_fifo_calc_data_len(rx_fifo);
if (rx_length) {
serial->parent.rx_indicate(&serial->parent, rx_length);
}
}
}
break;
case RT_SERIAL_EVENT_TX_DMADONE:
{
int i;
rt_size_t len;
rt_uint8_t ch = 0;
struct rt_serial_fifo *tx_fifo;
tx_fifo = (struct rt_serial_fifo*) serial->serial_tx;
/* calucate fifo data size */
len = _serial_fifo_calc_data_len(tx_fifo);
if (len == 0) {
// TODO: stop tx
serial->ops->stop_dma_tx(serial);
#ifndef RT_SERIAL_USE_EVENT
rt_completion_done(&(serial->completion_tx));
#else
rt_event_send(serial->tx_done, RT_SERIAL_EVENT_TXDONE);
#endif
/* invoke callback */
if (serial->parent.tx_complete != RT_NULL) {
serial->parent.tx_complete(&serial->parent, (void*)len);
}
break;
}
if (len > RT_SERIAL_DMA_BUFSZ) {
len = RT_SERIAL_DMA_BUFSZ;
}
/* read from software FIFO */
for (i = 0; i < len; i++) {
/* pop one byte data */
ch = _serial_fifo_pop_data(tx_fifo);
serial->serial_dma_tx[i] = ch;
}
tx_fifo->is_full = RT_FALSE;
serial->ops->start_dma_tx(serial, serial->serial_dma_tx, len);
}
break;
#endif /* RT_SERIAL_USING_DMA */
}
}
#endif /* RT_USING_SERIAL */
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化