From fac3c5cda1c5a48c8a8d8c5c753e3a3886ac509e Mon Sep 17 00:00:00 2001 From: chenjh Date: Mon, 7 Jun 2021 22:46:02 +0800 Subject: [PATCH] =?UTF-8?q?[serial]=20=E4=BC=98=E5=8C=96dma=E6=8E=A5?= =?UTF-8?q?=E6=94=B6=E5=A4=84=E7=90=86=E6=B5=81=E7=A8=8B=EF=BC=8C=E8=A7=A3?= =?UTF-8?q?=E8=80=A6=E9=A9=B1=E5=8A=A8=E8=B0=83=E7=94=A8=E4=B8=B2=E5=8F=A3?= =?UTF-8?q?=E6=A1=86=E6=9E=B6=E7=9A=84API=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../libraries/HAL_Drivers/drv_usart_v2.c | 42 +- bsp/stm32/stm32l475-atk-pandora/.config | 16 +- .../stm32l475-atk-pandora/project.uvprojx | 380 +++++++----------- bsp/stm32/stm32l475-atk-pandora/rtconfig.h | 10 - .../drivers/include/drivers/serial_v2.h | 14 +- components/drivers/serial/serial_v2.c | 51 +-- 6 files changed, 190 insertions(+), 323 deletions(-) diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_usart_v2.c b/bsp/stm32/libraries/HAL_Drivers/drv_usart_v2.c index 51dfc425c4..b1b224005a 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_usart_v2.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_usart_v2.c @@ -342,37 +342,42 @@ static void dma_recv_isr(struct rt_serial_device *serial, rt_uint8_t isr_flag) { struct stm32_uart *uart; rt_base_t level; - rt_uint16_t recv_len = 0; + rt_size_t recv_len, counter; RT_ASSERT(serial != RT_NULL); uart = rt_container_of(serial, struct stm32_uart, serial); - struct rt_serial_rx_fifo *rx_fifo; - rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx; - RT_ASSERT(rx_fifo != RT_NULL); - level = rt_hw_interrupt_disable(); - rt_uint16_t index = __HAL_DMA_GET_COUNTER(&(uart->dma_rx.handle)); + recv_len = 0; + counter = __HAL_DMA_GET_COUNTER(&(uart->dma_rx.handle)); + switch (isr_flag) { - - case UART_RX_DMA_IT_TC_FLAG: - if(index >= uart->dma_rx.remaining_cnt) - recv_len = serial->config.rx_bufsz + uart->dma_rx.remaining_cnt - index; + case UART_RX_DMA_IT_IDLE_FLAG: + if (counter <= uart->dma_rx.remaining_cnt) + recv_len = uart->dma_rx.remaining_cnt - counter; + else + recv_len = serial->config.rx_bufsz + uart->dma_rx.remaining_cnt - counter; break; case UART_RX_DMA_IT_HT_FLAG: - case UART_RX_DMA_IT_IDLE_FLAG: - if(index < uart->dma_rx.remaining_cnt) - recv_len = uart->dma_rx.remaining_cnt - index; + if (counter < uart->dma_rx.remaining_cnt) + recv_len = uart->dma_rx.remaining_cnt - counter; break; + case UART_RX_DMA_IT_TC_FLAG: + if(counter >= uart->dma_rx.remaining_cnt) + recv_len = serial->config.rx_bufsz + uart->dma_rx.remaining_cnt - counter; + default: break; } - uart->dma_rx.remaining_cnt = index; - rt_serial_update_write_index(&(rx_fifo->rb), recv_len); + if (recv_len) + { + uart->dma_rx.remaining_cnt = counter; + rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8)); + } rt_hw_interrupt_enable(level); } @@ -444,7 +449,6 @@ static void uart_isr(struct rt_serial_device *serial) && (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_IDLE) != RESET)) { dma_recv_isr(serial, UART_RX_DMA_IT_IDLE_FLAG); - rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND); __HAL_UART_CLEAR_IDLEFLAG(&uart->handle); } #endif @@ -1051,9 +1055,7 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) struct stm32_uart *uart; RT_ASSERT(huart != NULL); uart = (struct stm32_uart *)huart; - dma_recv_isr(&uart->serial, UART_RX_DMA_IT_TC_FLAG); - rt_hw_serial_isr(&uart->serial, RT_SERIAL_EVENT_RX_DMADONE); } /** @@ -1068,15 +1070,13 @@ void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart) struct stm32_uart *uart; RT_ASSERT(huart != NULL); uart = (struct stm32_uart *)huart; - dma_recv_isr(&uart->serial, UART_RX_DMA_IT_HT_FLAG); - rt_hw_serial_isr(&uart->serial, RT_SERIAL_EVENT_RX_DMADONE); } /** * @brief HAL_UART_TxCpltCallback * @param huart: UART handle - * @note This callback can be called by two functions, first in UART_EndTransmit_IT when + * @note This callback can be called by two functions, first in UART_EndTransmit_IT when * UART Tx complete and second in UART_DMATransmitCplt function in DMA Circular mode. * @retval None */ diff --git a/bsp/stm32/stm32l475-atk-pandora/.config b/bsp/stm32/stm32l475-atk-pandora/.config index 365e154c17..3420b2d793 100644 --- a/bsp/stm32/stm32l475-atk-pandora/.config +++ b/bsp/stm32/stm32l475-atk-pandora/.config @@ -197,21 +197,7 @@ CONFIG_RT_USING_PIN=y # CONFIG_RT_USING_RYM is not set # CONFIG_RT_USING_ULOG is not set # CONFIG_RT_USING_UTEST is not set -CONFIG_RT_USING_RT_LINK=y -CONFIG_RT_LINK_USING_SF_CRC=y -# CONFIG_RT_LINK_USING_HW_CRC is not set - -# -# rt-link hardware device configuration -# -CONFIG_RT_LINK_HW_DEVICE_NAME="uart2" -CONFIG_RT_LINK_USING_UART=y - -# -# rt link debug option -# -# CONFIG_USING_RT_LINK_DEBUG is not set -# CONFIG_USING_RT_LINK_HW_DEBUG is not set +# CONFIG_RT_USING_RT_LINK is not set # CONFIG_RT_USING_LWP is not set # diff --git a/bsp/stm32/stm32l475-atk-pandora/project.uvprojx b/bsp/stm32/stm32l475-atk-pandora/project.uvprojx index 1a187748bd..6089d4c371 100644 --- a/bsp/stm32/stm32l475-atk-pandora/project.uvprojx +++ b/bsp/stm32/stm32l475-atk-pandora/project.uvprojx @@ -1,7 +1,10 @@ + 2.1 +
### uVision Project, (C) Keil Software
+ rt-thread @@ -13,31 +16,31 @@ STM32L475VETx STMicroelectronics - Keil.STM32L4xx_DFP.2.0.0 + Keil.STM32L4xx_DFP.2.2.0 http://www.keil.com/pack IRAM(0x20000000,0x00018000) IRAM2(0x10000000,0x00008000) IROM(0x08000000,0x00080000) CPUTYPE("Cortex-M4") FPU2 CLOCK(12000000) ELITTLE - - + + UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0STM32L4xx_512 -FS08000000 -FL080000 -FP0($$Device:STM32L475VETx$CMSIS\Flash\STM32L4xx_512.FLM)) 0 $$Device:STM32L475VETx$Drivers\CMSIS\Device\ST\STM32L4xx\Include\stm32l4xx.h - - - - - - - - - + + + + + + + + + $$Device:STM32L475VETx$CMSIS\SVD\STM32L4x5.svd 0 0 - - - - - + + + + + 0 0 @@ -59,8 +62,8 @@ 0 0 - - + + 0 0 0 @@ -69,8 +72,8 @@ 0 0 - - + + 0 0 0 @@ -80,14 +83,14 @@ 1 0 fromelf --bin !L --output rtthread.bin - + 0 0 0 0 0 - + 0 @@ -101,8 +104,8 @@ 0 0 3 - - + + 1 @@ -136,10 +139,10 @@ 1 BIN\UL2CM3.DLL "" () - - - - + + + + 0 @@ -172,7 +175,7 @@ 0 0 "Cortex-M4" - + 0 0 0 @@ -181,6 +184,7 @@ 0 0 2 + 0 1 0 8 @@ -304,7 +308,7 @@ 0x8000 - + 1 @@ -331,10 +335,10 @@ 0 0 - + USE_HAL_DRIVER, __RTTHREAD__, STM32L475xx - - .;applications;..\..\..\libcpu\arm\common;..\..\..\libcpu\arm\cortex-m4;..\..\..\components\drivers\include;..\..\..\components\drivers\include;..\..\..\components\drivers\include;board;board\CubeMX_Config\Inc;board\ports;..\libraries\HAL_Drivers;..\libraries\HAL_Drivers\config;..\..\..\components\finsh;.;..\..\..\include;..\..\..\components\libc\compilers\common;..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Inc;..\libraries\STM32L4xx_HAL\CMSIS\Device\ST\STM32L4xx\Include;..\libraries\STM32L4xx_HAL\CMSIS\Include + + .;applications;..\..\..\libcpu\arm\common;..\..\..\libcpu\arm\cortex-m4;..\..\..\components\drivers\include;..\..\..\components\drivers\include;..\..\..\components\drivers\include;board;board\CubeMX_Config\Inc;board\ports;..\libraries\HAL_Drivers;..\libraries\HAL_Drivers\config;..\..\..\components\finsh;.;..\..\..\include;..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Inc;..\libraries\STM32L4xx_HAL\CMSIS\Device\ST\STM32L4xx\Include;..\libraries\STM32L4xx_HAL\CMSIS\Include @@ -349,10 +353,10 @@ 0 0 - - - - + + + + @@ -364,13 +368,13 @@ 0 0x08000000 0x20000000 - + .\board\linker_scripts\link.sct - - - - - + + + + + @@ -383,39 +387,36 @@ 1 applications\main.c + + uart_sample.c + 1 + .\applications\uart_sample.c + CPU - backtrace.c + showmem.c 1 - ..\..\..\libcpu\arm\common\backtrace.c + ..\..\..\libcpu\arm\common\showmem.c - - div0.c 1 ..\..\..\libcpu\arm\common\div0.c - - - showmem.c + backtrace.c 1 - ..\..\..\libcpu\arm\common\showmem.c + ..\..\..\libcpu\arm\common\backtrace.c - - cpuport.c 1 ..\..\..\libcpu\arm\cortex-m4\cpuport.c - - context_rvds.S 2 @@ -431,57 +432,41 @@ 1 ..\..\..\components\drivers\misc\pin.c - - - serial.c + serial_v2.c 1 - ..\..\..\components\drivers\serial\serial.c + ..\..\..\components\drivers\serial\serial_v2.c - - completion.c 1 ..\..\..\components\drivers\src\completion.c - - - dataqueue.c + ringbuffer.c 1 - ..\..\..\components\drivers\src\dataqueue.c + ..\..\..\components\drivers\src\ringbuffer.c - - - pipe.c + waitqueue.c 1 - ..\..\..\components\drivers\src\pipe.c + ..\..\..\components\drivers\src\waitqueue.c - - ringblk_buf.c 1 ..\..\..\components\drivers\src\ringblk_buf.c - - - ringbuffer.c + dataqueue.c 1 - ..\..\..\components\drivers\src\ringbuffer.c + ..\..\..\components\drivers\src\dataqueue.c - - - waitqueue.c + pipe.c 1 - ..\..\..\components\drivers\src\waitqueue.c + ..\..\..\components\drivers\src\pipe.c - - workqueue.c 1 @@ -493,40 +478,30 @@ Drivers - board.c + stm32l4xx_hal_msp.c 1 - board\board.c + board\CubeMX_Config\Src\stm32l4xx_hal_msp.c - - - stm32l4xx_hal_msp.c + board.c 1 - board\CubeMX_Config\Src\stm32l4xx_hal_msp.c + board\board.c - - startup_stm32l475xx.s 2 ..\libraries\STM32L4xx_HAL\CMSIS\Device\ST\STM32L4xx\Source\Templates\arm\startup_stm32l475xx.s - - drv_gpio.c 1 ..\libraries\HAL_Drivers\drv_gpio.c - - - drv_usart.c + drv_usart_v2.c 1 - ..\libraries\HAL_Drivers\drv_usart.c + ..\libraries\HAL_Drivers\drv_usart_v2.c - - drv_common.c 1 @@ -542,19 +517,15 @@ 1 ..\..\..\components\finsh\shell.c - - - cmd.c + msh.c 1 - ..\..\..\components\finsh\cmd.c + ..\..\..\components\finsh\msh.c - - - msh.c + cmd.c 1 - ..\..\..\components\finsh\msh.c + ..\..\..\components\finsh\cmd.c @@ -562,96 +533,65 @@ Kernel - clock.c + idle.c 1 - ..\..\..\src\clock.c + ..\..\..\src\idle.c - - - components.c + kservice.c 1 - ..\..\..\src\components.c + ..\..\..\src\kservice.c - - device.c 1 ..\..\..\src\device.c - - - - idle.c - 1 - ..\..\..\src\idle.c - - - - ipc.c + clock.c 1 - ..\..\..\src\ipc.c + ..\..\..\src\clock.c - - - irq.c + mempool.c 1 - ..\..\..\src\irq.c + ..\..\..\src\mempool.c - - - kservice.c + scheduler.c 1 - ..\..\..\src\kservice.c + ..\..\..\src\scheduler.c - - mem.c 1 ..\..\..\src\mem.c - - - mempool.c + components.c 1 - ..\..\..\src\mempool.c + ..\..\..\src\components.c - - - object.c + ipc.c 1 - ..\..\..\src\object.c + ..\..\..\src\ipc.c - - - scheduler.c + irq.c 1 - ..\..\..\src\scheduler.c + ..\..\..\src\irq.c - - - signal.c + object.c 1 - ..\..\..\src\signal.c + ..\..\..\src\object.c - - thread.c 1 ..\..\..\src\thread.c - - timer.c 1 @@ -659,172 +599,124 @@ - - libc - - - time.c - 1 - ..\..\..\components\libc\compilers\common\time.c - - - Libraries - system_stm32l4xx.c + stm32l4xx_hal_cryp.c 1 - ..\libraries\STM32L4xx_HAL\CMSIS\Device\ST\STM32L4xx\Source\Templates\system_stm32l4xx.c + ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_cryp.c - - - stm32l4xx_hal.c + stm32l4xx_hal_uart.c 1 - ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal.c + ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_uart.c - - - stm32l4xx_hal_comp.c + stm32l4xx_hal_usart.c 1 - ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_comp.c + ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_usart.c - - stm32l4xx_hal_cortex.c 1 ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_cortex.c - - - stm32l4xx_hal_crc.c + stm32l4xx_hal_gpio.c 1 - ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_crc.c + ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_gpio.c - - - stm32l4xx_hal_crc_ex.c + stm32l4xx_hal_dma.c 1 - ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_crc_ex.c + ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_dma.c - - - stm32l4xx_hal_cryp.c + stm32l4xx_hal_pwr.c 1 - ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_cryp.c + ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_pwr.c - - - stm32l4xx_hal_cryp_ex.c + stm32l4xx_hal_rcc_ex.c 1 - ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_cryp_ex.c + ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_rcc_ex.c - - - stm32l4xx_hal_dma.c + stm32l4xx_hal_rcc.c 1 - ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_dma.c + ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_rcc.c - - stm32l4xx_hal_dma_ex.c 1 ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_dma_ex.c - - stm32l4xx_hal_exti.c 1 ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_exti.c - - - stm32l4xx_hal_pwr.c + stm32l4xx_hal_crc.c 1 - ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_pwr.c + ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_crc.c - - - stm32l4xx_hal_pwr_ex.c + stm32l4xx_hal.c 1 - ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_pwr_ex.c + ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal.c - - - stm32l4xx_hal_rcc.c + stm32l4xx_hal_usart_ex.c 1 - ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_rcc.c + ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_usart_ex.c - - - stm32l4xx_hal_rcc_ex.c + stm32l4xx_hal_uart_ex.c 1 - ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_rcc_ex.c + ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_uart_ex.c - - - stm32l4xx_hal_rng.c + stm32l4xx_hal_crc_ex.c 1 - ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_rng.c + ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_crc_ex.c - - - stm32l4xx_hal_gpio.c + stm32l4xx_hal_comp.c 1 - ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_gpio.c + ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_comp.c - - - stm32l4xx_hal_uart.c + stm32l4xx_hal_pwr_ex.c 1 - ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_uart.c + ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_pwr_ex.c - - - stm32l4xx_hal_uart_ex.c + stm32l4xx_hal_rng.c 1 - ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_uart_ex.c + ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_rng.c - - - stm32l4xx_hal_usart.c + system_stm32l4xx.c 1 - ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_usart.c + ..\libraries\STM32L4xx_HAL\CMSIS\Device\ST\STM32L4xx\Source\Templates\system_stm32l4xx.c - - - stm32l4xx_hal_usart_ex.c + stm32l4xx_hal_cryp_ex.c 1 - ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_usart_ex.c + ..\libraries\STM32L4xx_HAL\STM32L4xx_HAL_Driver\Src\stm32l4xx_hal_cryp_ex.c + - - - + + + +
diff --git a/bsp/stm32/stm32l475-atk-pandora/rtconfig.h b/bsp/stm32/stm32l475-atk-pandora/rtconfig.h index 178f69b3ec..5f3f8a812b 100644 --- a/bsp/stm32/stm32l475-atk-pandora/rtconfig.h +++ b/bsp/stm32/stm32l475-atk-pandora/rtconfig.h @@ -111,16 +111,6 @@ /* Utilities */ -#define RT_USING_RT_LINK -#define RT_LINK_USING_SF_CRC - -/* rt-link hardware device configuration */ - -#define RT_LINK_HW_DEVICE_NAME "uart2" -#define RT_LINK_USING_UART - -/* rt link debug option */ - /* RT-Thread Utestcases */ diff --git a/components/drivers/include/drivers/serial_v2.h b/components/drivers/include/drivers/serial_v2.h index 18187bc0f5..51306a4bb4 100644 --- a/components/drivers/include/drivers/serial_v2.h +++ b/components/drivers/include/drivers/serial_v2.h @@ -112,7 +112,7 @@ struct serial_configure }; /* - * Serial FIFO mode + * Serial Receive FIFO mode */ struct rt_serial_rx_fifo { @@ -128,6 +128,9 @@ struct rt_serial_rx_fifo rt_uint8_t buffer[]; }; +/* + * Serial Transmit FIFO mode + */ struct rt_serial_tx_fifo { struct rt_ringbuffer rb; @@ -174,15 +177,6 @@ struct rt_uart_ops rt_uint32_t tx_flag); }; -rt_size_t rt_serial_get_linear_buffer(struct rt_ringbuffer *rb, - rt_uint8_t **ptr); - -rt_size_t rt_serial_update_read_index(struct rt_ringbuffer *rb, - rt_uint16_t read_index); - -rt_size_t rt_serial_update_write_index(struct rt_ringbuffer *rb, - rt_uint16_t write_index); - void rt_hw_serial_isr(struct rt_serial_device *serial, int event); rt_err_t rt_hw_serial_register(struct rt_serial_device *serial, diff --git a/components/drivers/serial/serial_v2.c b/components/drivers/serial/serial_v2.c index f41cdcd8f9..1019b73e67 100644 --- a/components/drivers/serial/serial_v2.c +++ b/components/drivers/serial/serial_v2.c @@ -157,7 +157,7 @@ static int serial_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req) rx_fifo = (struct rt_serial_rx_fifo*) serial->serial_rx; level = rt_hw_interrupt_disable(); - + if (rt_ringbuffer_data_len(&rx_fifo->rb)) mask |= POLLIN; rt_hw_interrupt_enable(level); @@ -180,8 +180,8 @@ const static struct dfs_file_ops _serial_fops = }; #endif -rt_size_t rt_serial_get_linear_buffer(struct rt_ringbuffer *rb, - rt_uint8_t **ptr) +static rt_size_t rt_serial_get_linear_buffer(struct rt_ringbuffer *rb, + rt_uint8_t **ptr) { rt_size_t size; @@ -206,8 +206,8 @@ rt_size_t rt_serial_get_linear_buffer(struct rt_ringbuffer *rb, return rb->buffer_size - rb->read_index; } -rt_size_t rt_serial_update_read_index(struct rt_ringbuffer *rb, - rt_uint16_t read_index) +static rt_size_t rt_serial_update_read_index(struct rt_ringbuffer *rb, + rt_uint16_t read_index) { rt_size_t size; @@ -219,7 +219,7 @@ rt_size_t rt_serial_update_read_index(struct rt_ringbuffer *rb, /* no data */ if (size == 0) return 0; - + /* less data */ if(size < read_index) read_index = size; @@ -239,8 +239,8 @@ rt_size_t rt_serial_update_read_index(struct rt_ringbuffer *rb, return read_index; } -rt_size_t rt_serial_update_write_index(struct rt_ringbuffer *rb, - rt_uint16_t write_index) +static rt_size_t rt_serial_update_write_index(struct rt_ringbuffer *rb, + rt_uint16_t write_index) { rt_uint16_t size; RT_ASSERT(rb != RT_NULL); @@ -499,7 +499,7 @@ static rt_size_t _serial_fifo_tx_blocking_buf(struct rt_device *dev, while (size) { - /* Copy one piece of data into the ringbuffer at a time + /* Copy one piece of data into the ringbuffer at a time * until the length of the data is equal to size */ tx_fifo->put_size = rt_ringbuffer_put(&(tx_fifo->rb), (rt_uint8_t *)buffer + offset, @@ -629,7 +629,7 @@ static rt_err_t rt_serial_tx_enable(struct rt_device *dev, if (optmode == RT_SERIAL_TX_BLOCKING_BUFFER) { /* If use RT_SERIAL_TX_BLOCKING_BUFFER, the ringbuffer is initialized */ - tx_fifo = (struct rt_serial_tx_fifo *) rt_malloc + tx_fifo = (struct rt_serial_tx_fifo *) rt_malloc (sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz); RT_ASSERT(tx_fifo != RT_NULL); @@ -641,9 +641,9 @@ static rt_err_t rt_serial_tx_enable(struct rt_device *dev, } else { - /* If not use RT_SERIAL_TX_BLOCKING_BUFFER, + /* If not use RT_SERIAL_TX_BLOCKING_BUFFER, * the control() API is called to configure the serial device */ - tx_fifo = (struct rt_serial_tx_fifo*) rt_malloc + tx_fifo = (struct rt_serial_tx_fifo*) rt_malloc (sizeof(struct rt_serial_tx_fifo)); RT_ASSERT(tx_fifo != RT_NULL); @@ -665,14 +665,14 @@ static rt_err_t rt_serial_tx_enable(struct rt_device *dev, /* When using RT_SERIAL_TX_NON_BLOCKING, ringbuffer needs to be initialized, * and initialize the tx_fifo->activated value is RT_FALSE. */ - tx_fifo = (struct rt_serial_tx_fifo *) rt_malloc + tx_fifo = (struct rt_serial_tx_fifo *) rt_malloc (sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz); RT_ASSERT(tx_fifo != RT_NULL); tx_fifo->activated = RT_FALSE; tx_fifo->put_size = 0; - rt_ringbuffer_init(&(tx_fifo->rb), - tx_fifo->buffer, + rt_ringbuffer_init(&(tx_fifo->rb), + tx_fifo->buffer, serial->config.tx_bufsz); serial->serial_tx = tx_fifo; @@ -712,7 +712,7 @@ static rt_err_t rt_serial_rx_enable(struct rt_device *dev, return -RT_EINVAL; } - dev->read = _serial_poll_rx; + dev->read = _serial_poll_rx; dev->open_flag |= RT_SERIAL_RX_BLOCKING; return RT_EOK; } @@ -892,7 +892,7 @@ static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag) dev->open_flag |= RT_SERIAL_TX_BLOCKING; /* set steam flag */ - if ((oflag & RT_DEVICE_FLAG_STREAM) || + if ((oflag & RT_DEVICE_FLAG_STREAM) || (dev->open_flag & RT_DEVICE_FLAG_STREAM)) dev->open_flag |= RT_DEVICE_FLAG_STREAM; @@ -903,7 +903,7 @@ static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag) /* initialize the Tx structure according to open flag */ if (serial->serial_tx == RT_NULL) - rt_serial_tx_enable(dev, dev->open_flag & + rt_serial_tx_enable(dev, dev->open_flag & (RT_SERIAL_TX_BLOCKING | RT_SERIAL_TX_NON_BLOCKING)); return RT_EOK; @@ -925,10 +925,10 @@ static rt_err_t rt_serial_close(struct rt_device *dev) /* this device has more reference count */ if (dev->ref_count > 1) return -RT_ERROR; /* Disable serial receive mode. */ - rt_serial_rx_disable(dev, dev->open_flag & + rt_serial_rx_disable(dev, dev->open_flag & (RT_SERIAL_RX_BLOCKING | RT_SERIAL_RX_NON_BLOCKING)); /* Disable serial tranmit mode. */ - rt_serial_tx_disable(dev, dev->open_flag & + rt_serial_tx_disable(dev, dev->open_flag & (RT_SERIAL_TX_BLOCKING | RT_SERIAL_TX_NON_BLOCKING)); /* Call the control() API to close the serial device */ @@ -978,7 +978,7 @@ static rt_err_t rt_serial_control(struct rt_device *dev, } /* set serial configure */ serial->config = *pconfig; - serial->ops->configure(serial, + serial->ops->configure(serial, (struct serial_configure *) args); } @@ -994,7 +994,7 @@ static rt_err_t rt_serial_control(struct rt_device *dev, } #ifdef RT_USING_DEVICE_OPS -const static struct rt_device_ops serial_ops = +const static struct rt_device_ops serial_ops = { rt_serial_init, rt_serial_open, @@ -1070,9 +1070,14 @@ void rt_hw_serial_isr(struct rt_serial_device *serial, int event) rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx; RT_ASSERT(rx_fifo != RT_NULL); + /* If the event is RT_SERIAL_EVENT_RX_IND, rx_length is equal to 0 */ + rx_length = (event & (~0xff)) >> 8; + + if (rx_length) + rt_serial_update_write_index(&(rx_fifo->rb), rx_length); + /* Get the length of the data from the ringbuffer */ rx_length = rt_ringbuffer_data_len(&rx_fifo->rb); - if (rx_length == 0) break; if (serial->parent.open_flag & RT_SERIAL_RX_BLOCKING) -- GitLab