From 98c830f3dede0b6d6744167e49e2340ba3416088 Mon Sep 17 00:00:00 2001 From: armink Date: Wed, 24 May 2017 09:09:55 +0800 Subject: [PATCH] [BSP] Fix a bug on stm32f4xx uart rx DMA driver when first receive data filled the FIFO. --- bsp/stm32f40x/drivers/usart.c | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/bsp/stm32f40x/drivers/usart.c b/bsp/stm32f40x/drivers/usart.c index d9d0f74c6..d0ce27172 100644 --- a/bsp/stm32f40x/drivers/usart.c +++ b/bsp/stm32f40x/drivers/usart.c @@ -242,15 +242,7 @@ static void dma_uart_rx_idle_isr(struct rt_serial_device *serial) { level = rt_hw_interrupt_disable(); recv_total_index = uart->dma.setting_recv_len - DMA_GetCurrDataCounter(uart->dma.rx_stream); - if (recv_total_index >= uart->dma.last_recv_index) - { - recv_len = recv_total_index - uart->dma.last_recv_index; - } - else - { - recv_len = uart->dma.setting_recv_len - uart->dma.last_recv_index + recv_total_index; - } - + recv_len = recv_total_index - uart->dma.last_recv_index; uart->dma.last_recv_index = recv_total_index; /* enable interrupt */ rt_hw_interrupt_enable(level); @@ -269,7 +261,7 @@ static void dma_uart_rx_idle_isr(struct rt_serial_device *serial) { static void dma_rx_done_isr(struct rt_serial_device *serial) { struct stm32_uart *uart = (struct stm32_uart *) serial->parent.user_data; - rt_size_t recv_total_index, recv_len; + rt_size_t recv_len; rt_base_t level; if (DMA_GetFlagStatus(uart->dma.rx_stream, uart->dma.rx_flag) != RESET) @@ -277,17 +269,9 @@ static void dma_rx_done_isr(struct rt_serial_device *serial) /* disable interrupt */ level = rt_hw_interrupt_disable(); - recv_total_index = uart->dma.setting_recv_len - DMA_GetCurrDataCounter(uart->dma.rx_stream); - if (recv_total_index >= uart->dma.last_recv_index) - { - recv_len = recv_total_index - uart->dma.last_recv_index; - } - else - { - recv_len = uart->dma.setting_recv_len - uart->dma.last_recv_index + recv_total_index; - } - - uart->dma.last_recv_index = recv_total_index; + recv_len = uart->dma.setting_recv_len - uart->dma.last_recv_index; + /* reset last recv index */ + uart->dma.last_recv_index = 0; /* enable interrupt */ rt_hw_interrupt_enable(level); @@ -639,8 +623,8 @@ static void NVIC_Configuration(struct stm32_uart *uart) /* Enable the USART1 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = uart->irq; - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; - NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } @@ -668,7 +652,7 @@ static void DMA_Configuration(struct rt_serial_device *serial) { /* rx dma interrupt config */ NVIC_InitStructure.NVIC_IRQChannel = uart->dma.rx_irq_ch; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; - NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }