UART使用DMA和循环队列接收数据的代码
时间: 2024-05-01 08:20:35 浏览: 88
以下是使用DMA和循环队列接收UART数据的示例代码:
```c
#include "stm32f4xx_hal.h"
#define UART_RX_BUF_SIZE 256
UART_HandleTypeDef huart;
DMA_HandleTypeDef hdma_usart_rx;
uint8_t uart_rx_buf[UART_RX_BUF_SIZE];
volatile uint16_t uart_rx_head = 0;
volatile uint16_t uart_rx_tail = 0;
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if (huart->Instance == USART1)
{
/* Peripheral clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**USART1 GPIO Configuration
PB6 ------> USART1_TX
PB7 ------> USART1_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USART1 DMA Init */
/* USART1_RX Init */
hdma_usart_rx.Instance = DMA2_Stream2;
hdma_usart_rx.Init.Channel = DMA_CHANNEL_4;
hdma_usart_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_usart_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_usart_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_usart_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_usart_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_usart_rx.Init.Mode = DMA_CIRCULAR;
hdma_usart_rx.Init.Priority = DMA_PRIORITY_LOW;
hdma_usart_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_usart_rx) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(huart, hdmarx, hdma_usart_rx);
/* USART1 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1)
{
// Move tail pointer to next position
uart_rx_tail = (uart_rx_tail + 1) % UART_RX_BUF_SIZE;
}
}
void USART1_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart);
}
void uart_init(void)
{
huart.Instance = USART1;
huart.Init.BaudRate = 115200;
huart.Init.WordLength = UART_WORDLENGTH_8B;
huart.Init.StopBits = UART_STOPBITS_1;
huart.Init.Parity = UART_PARITY_NONE;
huart.Init.Mode = UART_MODE_TX_RX;
huart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart) != HAL_OK)
{
Error_Handler();
}
}
void dma_init(void)
{
__HAL_RCC_DMA2_CLK_ENABLE();
HAL_NVIC_SetPriority(DMA2_Stream2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA2_Stream2_IRQn);
}
void DMA2_Stream2_IRQHandler(void)
{
HAL_DMA_IRQHandler(&hdma_usart_rx);
}
void start_uart_dma_rx(void)
{
HAL_UART_Receive_DMA(&huart, uart_rx_buf, UART_RX_BUF_SIZE);
}
uint8_t uart_rx_buf_pop(void)
{
uint8_t data = uart_rx_buf[uart_rx_head];
uart_rx_head = (uart_rx_head + 1) % UART_RX_BUF_SIZE;
return data;
}
uint16_t uart_rx_buf_size(void)
{
if (uart_rx_head <= uart_rx_tail)
{
return uart_rx_tail - uart_rx_head;
}
else
{
return UART_RX_BUF_SIZE - (uart_rx_head - uart_rx_tail);
}
}
int main(void)
{
HAL_Init();
uart_init();
dma_init();
start_uart_dma_rx();
while (1)
{
if (uart_rx_buf_size() > 0)
{
uint8_t data = uart_rx_buf_pop();
// Process received data
}
}
}
```
在上述代码中,我们使用 HAL 库来初始化 UART 和 DMA。我们使用循环队列来存储接收到的 UART 数据,这样可以避免数据丢失和覆盖。我们还实现了一个简单的 API 来访问循环队列中的数据。
在 `main()` 函数中,我们不断检查循环队列中是否有接收到的数据,并进行处理。当收到新数据时,我们将它从循环队列中弹出并进行处理。
阅读全文