STM32F407 使用标准库usart2通过DMA进行数据的接收,接收缓冲区使用乒乓缓存
时间: 2023-06-25 11:03:26 浏览: 181
使用DMA进行数据接收可以大大提高数据传输效率,同时使用乒乓缓存可以有效避免数据丢失。下面是使用标准库usart2通过DMA进行数据接收的步骤:
1. 配置GPIO引脚。将USART2的TX引脚(PA2)设置为复用功能,将RX引脚(PA3)设置为输入模式。
```
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/* Enable USART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* Configure USART Tx and Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
```
2. 配置USART2。设置波特率、数据位、停止位等参数。
```
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
```
3. 配置DMA。设置DMA传输方向、传输数据大小、缓存地址等参数。
```
DMA_InitTypeDef DMA_InitStructure;
/* Enable DMA clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
/* Configure DMA for USART2 Rx */
DMA_DeInit(DMA1_Stream5);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) &rx_buffer[0];
DMA_InitStructure.DMA_BufferSize = BUFFER_SIZE;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &USART2->DR;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream5, &DMA_InitStructure);
```
4. 开始DMA传输。启动USART2的DMA接收,并打开USART2接收中断。
```
USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
```
5. 在USART2接收中断中处理数据。注意使用乒乓缓存时需要对缓存进行切换。
```
void USART2_IRQHandler(void)
{
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
/* Disable USART2 RX interrupt */
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
/* Switch buffer */
if (current_buffer == 0)
{
current_buffer = 1;
next_buffer = 0;
}
else
{
current_buffer = 0;
next_buffer = 1;
}
/* Clear DMA transfer complete flag */
DMA_ClearFlag(DMA1_Stream5, DMA_FLAG_TCIF5);
/* Start DMA transfer to next buffer */
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) &rx_buffer[next_buffer][0];
DMA_Init(DMA1_Stream5, &DMA_InitStructure);
DMA_Cmd(DMA1_Stream5, ENABLE);
/* Enable USART2 RX interrupt */
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
}
}
```
阅读全文