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

@ -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)
{ {
@ -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();
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 */ #endif /* RT_SERIAL_USING_DMA */