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) \
&& !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"
/* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */
#error "Please define at least one BSP_USING_UARTx"
/* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */
#endif
#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
enum
@ -56,25 +56,25 @@ enum
static struct stm32_uart_config uart_config[] =
{
#ifdef BSP_USING_UART1
UART1_CONFIG,
UART1_CONFIG,
#endif
#ifdef BSP_USING_UART2
UART2_CONFIG,
UART2_CONFIG,
#endif
#ifdef BSP_USING_UART3
UART3_CONFIG,
UART3_CONFIG,
#endif
#ifdef BSP_USING_UART4
UART4_CONFIG,
UART4_CONFIG,
#endif
#ifdef BSP_USING_UART5
UART5_CONFIG,
UART5_CONFIG,
#endif
#ifdef BSP_USING_UART6
UART6_CONFIG,
UART6_CONFIG,
#endif
#ifdef BSP_USING_LPUART1
LPUART1_CONFIG,
LPUART1_CONFIG,
#endif
};
@ -247,7 +247,7 @@ static void uart_isr(struct rt_serial_device *serial)
/* UART in mode Receiver -------------------------------------------------*/
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);
}
@ -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)
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);
{
rt_uint32_t tmpreg= 0x00U;
rt_uint32_t tmpreg = 0x00U;
#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*/
SET_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)
{
struct rt_serial_device *serial;
struct stm32_uart *uart;
rt_size_t recv_len;
rt_base_t level;
RT_ASSERT(huart != NULL);
uart = (struct stm32_uart *)huart;
serial = &uart->serial;
dma_isr(&uart->serial);
}
level = rt_hw_interrupt_disable();
recv_len = serial->config.bufsz - uart->dma.last_index;
uart->dma.last_index = 0;
rt_hw_interrupt_enable(level);
if (recv_len)
{
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8));
}
/**
* @brief Rx Half transfer completed callback
* @param huart: UART handle
* @note This example shows a simple way to report end of DMA Rx Half transfer,
* and you can add your own implementation.
* @retval None
*/
void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
{
struct stm32_uart *uart;
RT_ASSERT(huart != NULL);
uart = (struct stm32_uart *)huart;
dma_isr(&uart->serial);
}
#endif /* RT_SERIAL_USING_DMA */
@ -666,20 +702,20 @@ int rt_hw_usart_init(void)
uart_obj[i].serial.config = config;
#if defined(RT_SERIAL_USING_DMA)
if(uart_obj[i].uart_dma_flag)
if (uart_obj[i].uart_dma_flag)
{
/* register UART device */
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
,&uart_obj[i]);
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
, &uart_obj[i]);
}
else
#endif
{
/* 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
,&uart_obj[i]);
, &uart_obj[i]);
}
RT_ASSERT(result == RT_EOK);
}