serialX 更新到上游 2ee235299b9891

This commit is contained in:
chinky 2024-08-23 18:54:15 +08:00
parent c137034490
commit 5b5fce1f39
6 changed files with 78 additions and 19 deletions

View File

@ -14,6 +14,8 @@ if GetDepend(['RT_USING_PIN']):
if GetDepend(['RT_USING_SERIAL']):
if GetDepend(['RT_USING_SERIAL_V2']):
src += ['drv_usart_v2.c']
elif GetDepend(['RT_USING_SERIAL_X']):
src += ['drv_usartX.c']
else:
src += ['drv_usart.c']

View File

@ -720,7 +720,7 @@ static void stm32_dma_rx_config(struct rt_serial_device *serial)
/* Transfer error in reception process */
RT_ASSERT(0);
}
CLEAR_BIT(uart->handle.Instance->CR3, USART_CR3_EIE);
// CLEAR_BIT(uart->handle.Instance->CR3, USART_CR3_EIE);
__HAL_UART_ENABLE_IT(&(uart->handle), UART_IT_IDLE);
/* DMA irq should set in DMA TX mode, or HAL_UART_TxCpltCallback function will not be called */

View File

@ -67,6 +67,7 @@
#endif
#if RT_SERIAL_FIFO_BUFSZ < (RT_SERIAL_DMA_BUFSZ*2)
#undef RT_SERIAL_FIFO_BUFSZ
#define RT_SERIAL_FIFO_BUFSZ RT_SERIAL_DMA_BUFSZ*2
#endif
@ -147,19 +148,20 @@ struct rt_serial_fifo
struct rt_serial_device
{
struct rt_device parent;
struct rt_device parent;
const struct rt_uart_ops *ops;
struct serial_configure config;
rt_uint32_t bufsz;
const struct rt_uart_ops *ops;
struct serial_configure config;
rt_uint32_t bufsz;
rt_tick_t timeout_tick; /**< timeout tick */
void *serial_rx;
void *serial_tx;
#ifdef RT_SERIAL_USING_DMA
rt_size_t dma_idx_rx;
rt_uint8_t serial_dma_rx[RT_SERIAL_DMA_BUFSZ];
rt_uint8_t serial_dma_tx[RT_SERIAL_DMA_BUFSZ];
rt_size_t dma_idx_rx;
rt_uint8_t serial_dma_rx[RT_SERIAL_DMA_BUFSZ];
rt_uint8_t serial_dma_tx[RT_SERIAL_DMA_BUFSZ];
#endif
cb_serial_rx _cb_rx;

View File

@ -12,6 +12,10 @@ menuconfig RT_USING_SERIAL
bool "RT_USING_SERIAL_V1"
config RT_USING_SERIAL_V2
bool "RT_USING_SERIAL_V2"
config RT_USING_SERIAL_X
bool "RT_USING_SERIAL"
help
A serial driver that supporting True NonBlock
endchoice
config RT_SERIAL_USING_DMA
bool "Enable serial DMA mode"
@ -19,6 +23,34 @@ menuconfig RT_USING_SERIAL
config RT_SERIAL_RB_BUFSZ
int "Set RX buffer size"
depends on !RT_USING_SERIAL_V2
depends on !(RT_USING_SERIAL_V2 || RT_USING_SERIAL_X)
default 64
if RT_USING_SERIAL_X
config RT_SERIAL_FIFO_BUFSZ
int "Set SerialX FIFO buffer size"
default 128
help
suggestion: RT_SERIAL_FIFO_BUFSZ == 2^N
and RT_SERIAL_FIFO_BUFSZ >= 2*RT_SERIAL_DMA_BUFSZ
config RT_SERIAL_DMA_BUFSZ
int "Set SerialX DMA buffer size"
depends on RT_SERIAL_USING_DMA
default 64
help
suggestion: RT_SERIAL_DMA_BUFSZ == 2^N
and RT_SERIAL_FIFO_BUFSZ >= 2*RT_SERIAL_DMA_BUFSZ
config RT_SERIAL_HARD_FIFO
bool "Using Hard FIFO"
default n
help
Useful only if the chip supported
eg: NUC970 N9H30 serial chips
The UART1/2/4/6/8/10 is built-in with a 64-byte transmitter FIFO (TX_FIFO)
and a 64-byte receiver FIFO (RX_FIFO),
and the UART0/3/5/7/9 are equipped 16-byte transmitter FIFO (TX_FIFO)
and 16-byte receiver FIFO (RX_FIFO).
endif
endif

View File

@ -29,6 +29,7 @@
* 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>
@ -385,11 +386,13 @@ rt_inline int _serial_fifo_rx(struct rt_serial_device *serial, rt_uint8_t *data,
// serial->ops->enable_interrupt(serial);
#ifndef RT_SERIAL_USE_EVENT
ret = rt_completion_wait(&(serial->completion_rx), RT_WAITING_FOREVER);
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, RT_WAITING_FOREVER, RT_NULL);
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 || ret == -RT_ETIMEOUT) {
if (ret == RT_EOK) {
} else if (ret == -RT_ETIMEOUT) {
return -RT_ETIMEOUT;
} else {
return 0;
}
@ -460,12 +463,14 @@ rt_inline int _serial_int_tx(struct rt_serial_device *serial, const rt_uint8_t *
rt_hw_interrupt_enable(level);
// serial->ops->enable_interrupt(serial);
#ifndef RT_SERIAL_USE_EVENT
ret = rt_completion_wait(&(serial->completion_tx), RT_WAITING_FOREVER);
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, RT_WAITING_FOREVER, RT_NULL);
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 || ret == -RT_ETIMEOUT) {
if (ret == RT_EOK) {
continue;
} else if (ret == -RT_ETIMEOUT) {
return -RT_ETIMEOUT;
} else {
return size;
}
@ -568,12 +573,14 @@ rt_inline int _serial_dma_tx(struct rt_serial_device *serial, const rt_uint8_t *
// serial->ops->enable_interrupt(serial);
#ifndef RT_SERIAL_USE_EVENT
ret = rt_completion_wait(&(serial->completion_tx), RT_WAITING_FOREVER);
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, RT_WAITING_FOREVER, RT_NULL);
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 || ret == -RT_ETIMEOUT) {
if (ret == RT_EOK) {
continue;
} else if (ret == -RT_ETIMEOUT) {
return -RT_ETIMEOUT;
} else {
return size;
}
@ -838,6 +845,8 @@ static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
serial->ops->control(serial, RT_DEVICE_CTRL_OPEN, (void *)0);
serial->timeout_tick = RT_WAITING_FOREVER;
/* set stream flag */
dev->open_flag |= stream_flag;
@ -940,7 +949,11 @@ static rt_err_t rt_serial_close(struct rt_device *dev)
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)
@ -955,7 +968,11 @@ static rt_ssize_t rt_serial_read(struct rt_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)
@ -1174,6 +1191,12 @@ static rt_err_t rt_serial_control(struct rt_device *dev,
}
}
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:
@ -1406,7 +1429,7 @@ rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
struct serial_configure uart_conf = RT_SERIAL_CONFIG_DEFAULT;
rt_memcpy(&serial->config, &uart_conf, sizeof(uart_conf));
serial->bufsz = uart_conf.bufsz;
serial->bufsz = RT_SERIAL_FIFO_BUFSZ;
device = &(serial->parent);