Merge pull request #2644 from zhouchuanfu/stm32_uart

[bsp][stm32] 增加uart的dma半中断接收方式。
This commit is contained in:
Bernard Xiong 2019-05-18 14:57:19 +08:00 committed by GitHub
commit c8efc0032d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 86 additions and 50 deletions

View File

@ -20,12 +20,12 @@
#if !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && !defined(BSP_USING_UART3) \ #if !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && !defined(BSP_USING_UART3) \
&& !defined(BSP_USING_UART4) && !defined(BSP_USING_UART5) && !defined(BSP_USING_UART6) && !defined(BSP_USING_LPUART1) && !defined(BSP_USING_UART4) && !defined(BSP_USING_UART5) && !defined(BSP_USING_UART6) && !defined(BSP_USING_LPUART1)
#error "Please define at least one BSP_USING_UARTx" #error "Please define at least one BSP_USING_UARTx"
/* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */ /* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */
#endif #endif
#ifdef RT_SERIAL_USING_DMA #ifdef RT_SERIAL_USING_DMA
static void stm32_dma_config(struct rt_serial_device *serial); static void stm32_dma_config(struct rt_serial_device *serial);
#endif #endif
enum enum
@ -56,25 +56,25 @@ enum
static struct stm32_uart_config uart_config[] = static struct stm32_uart_config uart_config[] =
{ {
#ifdef BSP_USING_UART1 #ifdef BSP_USING_UART1
UART1_CONFIG, UART1_CONFIG,
#endif #endif
#ifdef BSP_USING_UART2 #ifdef BSP_USING_UART2
UART2_CONFIG, UART2_CONFIG,
#endif #endif
#ifdef BSP_USING_UART3 #ifdef BSP_USING_UART3
UART3_CONFIG, UART3_CONFIG,
#endif #endif
#ifdef BSP_USING_UART4 #ifdef BSP_USING_UART4
UART4_CONFIG, UART4_CONFIG,
#endif #endif
#ifdef BSP_USING_UART5 #ifdef BSP_USING_UART5
UART5_CONFIG, UART5_CONFIG,
#endif #endif
#ifdef BSP_USING_UART6 #ifdef BSP_USING_UART6
UART6_CONFIG, UART6_CONFIG,
#endif #endif
#ifdef BSP_USING_LPUART1 #ifdef BSP_USING_LPUART1
LPUART1_CONFIG, LPUART1_CONFIG,
#endif #endif
}; };
@ -247,7 +247,7 @@ static void uart_isr(struct rt_serial_device *serial)
/* UART in mode Receiver -------------------------------------------------*/ /* UART in mode Receiver -------------------------------------------------*/
if ((__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET) && if ((__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET) &&
(__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_RXNE) != RESET)) (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_RXNE) != RESET))
{ {
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND); rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
} }
@ -312,6 +312,42 @@ static void uart_isr(struct rt_serial_device *serial)
} }
} }
#ifdef RT_SERIAL_USING_DMA
static void dma_isr(struct rt_serial_device *serial)
{
struct stm32_uart *uart;
rt_size_t recv_total_index, recv_len;
rt_base_t level;
RT_ASSERT(serial != RT_NULL);
uart = (struct stm32_uart *) serial->parent.user_data;
RT_ASSERT(uart != RT_NULL);
if ((__HAL_DMA_GET_IT_SOURCE(&(uart->dma.handle), DMA_IT_TC) != RESET) ||
(__HAL_DMA_GET_IT_SOURCE(&(uart->dma.handle), DMA_IT_HT) != RESET))
{
level = rt_hw_interrupt_disable();
recv_total_index = serial->config.bufsz - __HAL_DMA_GET_COUNTER(&(uart->dma.handle));
if (recv_total_index == 0)
{
recv_len = serial->config.bufsz - uart->dma.last_index;
}
else
{
recv_len = recv_total_index - uart->dma.last_index;
}
uart->dma.last_index = recv_total_index;
rt_hw_interrupt_enable(level);
if (recv_len)
{
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8));
}
}
}
#endif
#if defined(BSP_USING_UART1) #if defined(BSP_USING_UART1)
void USART1_IRQHandler(void) void USART1_IRQHandler(void)
{ {
@ -498,9 +534,9 @@ static void stm32_dma_config(struct rt_serial_device *serial)
LOG_D("%s dma config start", uart->config->name); LOG_D("%s dma config start", uart->config->name);
{ {
rt_uint32_t tmpreg= 0x00U; rt_uint32_t tmpreg = 0x00U;
#if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) \ #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) \
|| defined(SOC_SERIES_STM32L0) || defined(SOC_SERIES_STM32L0)
/* enable DMA clock && Delay after an RCC peripheral clock enabling*/ /* enable DMA clock && Delay after an RCC peripheral clock enabling*/
SET_BIT(RCC->AHBENR, uart->config->dma_rx->dma_rcc); SET_BIT(RCC->AHBENR, uart->config->dma_rx->dma_rcc);
tmpreg = READ_BIT(RCC->AHBENR, uart->config->dma_rx->dma_rcc); tmpreg = READ_BIT(RCC->AHBENR, uart->config->dma_rx->dma_rcc);
@ -590,25 +626,25 @@ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
*/ */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{ {
struct rt_serial_device *serial;
struct stm32_uart *uart; struct stm32_uart *uart;
rt_size_t recv_len;
rt_base_t level;
RT_ASSERT(huart != NULL); RT_ASSERT(huart != NULL);
uart = (struct stm32_uart *)huart; uart = (struct stm32_uart *)huart;
serial = &uart->serial; dma_isr(&uart->serial);
}
level = rt_hw_interrupt_disable(); /**
* @brief Rx Half transfer completed callback
recv_len = serial->config.bufsz - uart->dma.last_index; * @param huart: UART handle
uart->dma.last_index = 0; * @note This example shows a simple way to report end of DMA Rx Half transfer,
* and you can add your own implementation.
rt_hw_interrupt_enable(level); * @retval None
if (recv_len) */
{ void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8)); {
} struct stm32_uart *uart;
RT_ASSERT(huart != NULL);
uart = (struct stm32_uart *)huart;
dma_isr(&uart->serial);
} }
#endif /* RT_SERIAL_USING_DMA */ #endif /* RT_SERIAL_USING_DMA */
@ -666,20 +702,20 @@ int rt_hw_usart_init(void)
uart_obj[i].serial.config = config; uart_obj[i].serial.config = config;
#if defined(RT_SERIAL_USING_DMA) #if defined(RT_SERIAL_USING_DMA)
if(uart_obj[i].uart_dma_flag) if (uart_obj[i].uart_dma_flag)
{ {
/* register UART device */ /* register UART device */
result = rt_hw_serial_register(&uart_obj[i].serial,uart_obj[i].config->name, result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].config->name,
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX| RT_DEVICE_FLAG_DMA_RX RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX
,&uart_obj[i]); , &uart_obj[i]);
} }
else else
#endif #endif
{ {
/* register UART device */ /* register UART device */
result = rt_hw_serial_register(&uart_obj[i].serial,uart_obj[i].config->name, result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].config->name,
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX
,&uart_obj[i]); , &uart_obj[i]);
} }
RT_ASSERT(result == RT_EOK); RT_ASSERT(result == RT_EOK);
} }