From 385d0206884594e36bfc08fba19ab405860a2415 Mon Sep 17 00:00:00 2001 From: talpachen Date: Sun, 20 Dec 2020 17:39:15 +0800 Subject: [PATCH] optimize usart rx operation --- .../vllink_lite/usrapp_usbd_vllinklite.c | 2 +- .../driver/GigaDevice/GD32E10X/common/usart.c | 40 +++++++++++-------- .../driver/GigaDevice/GD32F3X0/common/usart.c | 40 +++++++++++-------- 3 files changed, 47 insertions(+), 35 deletions(-) diff --git a/source/project_vllink_lite/vllink_lite/usrapp_usbd_vllinklite.c b/source/project_vllink_lite/vllink_lite/usrapp_usbd_vllinklite.c index 49968344..a8b0a186 100644 --- a/source/project_vllink_lite/vllink_lite/usrapp_usbd_vllinklite.c +++ b/source/project_vllink_lite/vllink_lite/usrapp_usbd_vllinklite.c @@ -40,7 +40,7 @@ typedef struct usbd_vllinklite_t { vk_usbd_cdcacm_t param; vsf_block_stream_t ext2usb; vsf_block_stream_t usb2ext; - uint8_t ext2usb_buf[(APP_CFG_CDCEXT_PKT_SIZE + 4) * 6]; + uint8_t ext2usb_buf[(APP_CFG_CDCEXT_PKT_SIZE + 4) * 4]; uint8_t usb2ext_buf[(APP_CFG_CDCEXT_PKT_SIZE + 4) * 3]; } cdcext; #endif diff --git a/source/vsf_private/hal/driver/GigaDevice/GD32E10X/common/usart.c b/source/vsf_private/hal/driver/GigaDevice/GD32E10X/common/usart.c index 20aa44c9..1217ccf4 100644 --- a/source/vsf_private/hal/driver/GigaDevice/GD32E10X/common/usart.c +++ b/source/vsf_private/hal/driver/GigaDevice/GD32E10X/common/usart.c @@ -30,6 +30,10 @@ typedef struct usart_control_t { void *param; uint8_t tx_buff[USART_BUFF_SIZE]; uint8_t rx_buff[USART_BUFF_SIZE * 2]; +#if USART_STREAM_ENABLE + uint8_t stream_rx_buff[USART_BUFF_SIZE]; + uint8_t stream_rx_size; +#endif uint8_t rx_buff_w_pos; // Non-DMA RX uint8_t rx_buff_r_pos; uint8_t tx_size; @@ -267,6 +271,7 @@ uint32_t vsfhal_usart_config(enum usart_idx_t idx, uint32_t baudrate, uint32_t m USART_CTL0(usartx) &= ~USART_CTL0_UEN; USART_BAUD(usartx) = (temp + baudrate - 1) / baudrate; + USART_RT(usartx) = min(0xffffff, baudrate * 2 / 10000); // 200us USART_CTL0(usartx) |= USART_CTL0_UEN; } else { USART_CTL0(usartx) = 0; @@ -277,7 +282,7 @@ uint32_t vsfhal_usart_config(enum usart_idx_t idx, uint32_t baudrate, uint32_t m USART_CTL2(usartx) = (mode >> 4) & (USART_CTL2_CTSEN | USART_CTL2_RTSEN | USART_CTL2_HDEN | USART_CTL2_ERRIE); USART_CTL3(usartx) = ((mode >> 8) & (USART_CTL3_RINV | USART_CTL3_TINV | USART_CTL3_DINV | USART_CTL3_MSBF)) | USART_CTL3_RTEN | USART_CTL3_RTIE; - USART_RT(usartx) = 20; + USART_RT(usartx) = min(0xffffff, baudrate * 2 / 10000); // 200us switch (idx) { #if USART0_ENABLE @@ -585,25 +590,18 @@ static void stream_dorx(usart_control_t *ctrl, vsf_stream_t *stream) enum usart_idx_t idx = ((uint32_t)ctrl - (uint32_t)usart_control) / sizeof(usart_control_t); if (stream->op == &vsf_fifo_stream_op) { - uint8_t buf[USART_BUFF_SIZE]; - size = min(vsfhal_usart_rx_get_data_size(idx), USART_BUFF_SIZE); - if (size) { - size = vsfhal_usart_rx_bytes(idx, buf, size); - if (size && VSF_STREAM_IS_RX_CONNECTED(stream)) - VSF_STREAM_WRITE(stream, buf, size); - } + size = ctrl->stream_rx_size; + if (size && VSF_STREAM_IS_RX_CONNECTED(stream)) + VSF_STREAM_WRITE(stream, ctrl->stream_rx_buff, size); } else if (stream->op == &vsf_block_stream_op) { uint8_t *buf; - size = VSF_STREAM_GET_WBUF(stream, &buf); - if (size) { - size = vsfhal_usart_rx_bytes(idx, buf, size); - if (size && VSF_STREAM_IS_RX_CONNECTED(stream)) - VSF_STREAM_WRITE(stream, buf, size); - } else { - uint8_t discard[USART_BUFF_SIZE]; - vsfhal_usart_rx_bytes(idx, discard, USART_BUFF_SIZE); + size = min(VSF_STREAM_GET_WBUF(stream, &buf), ctrl->stream_rx_size); + if (size && VSF_STREAM_IS_RX_CONNECTED(stream)) { + memcpy(buf, ctrl->stream_rx_buff, size); + VSF_STREAM_WRITE(stream, buf, size); } } + ctrl->stream_rx_size = 0; } enum { @@ -667,7 +665,15 @@ static void stream_ontx(void *param) static void stream_onrx(void *param) { usart_control_t *ctrl = param; - vsf_eda_post_evt(&ctrl->eda, VSF_EVT_RX_STREAM_ONRX); + if (ctrl->stream_rx_size == 0) { + uint32_t size; + enum usart_idx_t idx = ((uint32_t)ctrl - (uint32_t)usart_control) / sizeof(usart_control_t); + size = min(vsfhal_usart_rx_get_data_size(idx), USART_BUFF_SIZE); + if (size) { + ctrl->stream_rx_size = vsfhal_usart_rx_bytes(idx, ctrl->stream_rx_buff, size); + vsf_eda_post_evt(&ctrl->eda, VSF_EVT_RX_STREAM_ONRX); + } + } } static void vsfhal_usart_stream_fini(enum usart_idx_t idx) diff --git a/source/vsf_private/hal/driver/GigaDevice/GD32F3X0/common/usart.c b/source/vsf_private/hal/driver/GigaDevice/GD32F3X0/common/usart.c index 275cbf6e..f2bab31d 100644 --- a/source/vsf_private/hal/driver/GigaDevice/GD32F3X0/common/usart.c +++ b/source/vsf_private/hal/driver/GigaDevice/GD32F3X0/common/usart.c @@ -30,6 +30,10 @@ typedef struct usart_control_t { void *param; uint8_t tx_buff[USART_BUFF_SIZE]; uint8_t rx_buff[USART_BUFF_SIZE * 2]; +#if USART_STREAM_ENABLE + uint8_t stream_rx_buff[USART_BUFF_SIZE]; + uint8_t stream_rx_size; +#endif uint8_t rx_buff_w_pos; // Non-DMA RX uint8_t rx_buff_r_pos; uint8_t tx_size; @@ -179,6 +183,7 @@ uint32_t vsfhal_usart_config(enum usart_idx_t idx, uint32_t baudrate, uint32_t m USART_CTL0(usartx) &= ~USART_CTL0_UEN; USART_BAUD(usartx) = (temp + baudrate - 1) / baudrate; + USART_RT(usartx) = min(0xffffff, baudrate * 2 / 10000); // 200us USART_CTL0(usartx) |= USART_CTL0_UEN; } else { USART_CTL0(usartx) = 0; @@ -188,7 +193,7 @@ uint32_t vsfhal_usart_config(enum usart_idx_t idx, uint32_t baudrate, uint32_t m USART_CTL1(usartx) = ((mode << 8) & USART_CTL1_STB) | USART_CTL1_RTEN; USART_CTL1(usartx) |= mode & (USART_CTL1_MSBF | USART_CTL1_DINV | USART_CTL1_TINV | USART_CTL1_RINV); USART_CTL2(usartx) = (mode >> 4) & (USART_CTL2_CTSEN | USART_CTL2_RTSEN | USART_CTL2_HDEN); - USART_RT(usartx) = 20; + USART_RT(usartx) = min(0xffffff, baudrate * 2 / 10000); // 200us switch (idx) { #if USART0_ENABLE @@ -419,25 +424,18 @@ static void stream_dorx(usart_control_t *ctrl, vsf_stream_t *stream) enum usart_idx_t idx = ((uint32_t)ctrl - (uint32_t)usart_control) / sizeof(usart_control_t); if (stream->op == &vsf_fifo_stream_op) { - uint8_t buf[USART_BUFF_SIZE]; - size = min(vsfhal_usart_rx_get_data_size(idx), USART_BUFF_SIZE); - if (size) { - size = vsfhal_usart_rx_bytes(idx, buf, size); - if (size && VSF_STREAM_IS_RX_CONNECTED(stream)) - VSF_STREAM_WRITE(stream, buf, size); - } + size = ctrl->stream_rx_size; + if (size && VSF_STREAM_IS_RX_CONNECTED(stream)) + VSF_STREAM_WRITE(stream, ctrl->stream_rx_buff, size); } else if (stream->op == &vsf_block_stream_op) { uint8_t *buf; - size = VSF_STREAM_GET_WBUF(stream, &buf); - if (size) { - size = vsfhal_usart_rx_bytes(idx, buf, size); - if (size && VSF_STREAM_IS_RX_CONNECTED(stream)) - VSF_STREAM_WRITE(stream, buf, size); - } else { - uint8_t discard[USART_BUFF_SIZE]; - vsfhal_usart_rx_bytes(idx, discard, USART_BUFF_SIZE); + size = min(VSF_STREAM_GET_WBUF(stream, &buf), ctrl->stream_rx_size); + if (size && VSF_STREAM_IS_RX_CONNECTED(stream)) { + memcpy(buf, ctrl->stream_rx_buff, size); + VSF_STREAM_WRITE(stream, buf, size); } } + ctrl->stream_rx_size = 0; } enum { @@ -501,7 +499,15 @@ static void stream_ontx(void *param) static void stream_onrx(void *param) { usart_control_t *ctrl = param; - vsf_eda_post_evt(&ctrl->eda, VSF_EVT_RX_STREAM_ONRX); + if (ctrl->stream_rx_size == 0) { + uint32_t size; + enum usart_idx_t idx = ((uint32_t)ctrl - (uint32_t)usart_control) / sizeof(usart_control_t); + size = min(vsfhal_usart_rx_get_data_size(idx), USART_BUFF_SIZE); + if (size) { + ctrl->stream_rx_size = vsfhal_usart_rx_bytes(idx, ctrl->stream_rx_buff, size); + vsf_eda_post_evt(&ctrl->eda, VSF_EVT_RX_STREAM_ONRX); + } + } } static void vsfhal_usart_stream_fini(enum usart_idx_t idx)