STM32F407使用标准库,usart2通过DMA进行数据的接收,接收缓冲区使用乒乓缓存的实例
时间: 2023-06-24 13:07:52 浏览: 141
下面是一个使用标准库,usart2通过DMA进行数据接收,并使用乒乓缓存的实例代码:
首先需要初始化USART2和DMA:
```c
#include "stm32f4xx.h"
#include <stdio.h>
#define RX_BUFFER_SIZE 64
uint8_t rxBuffer1[RX_BUFFER_SIZE];
uint8_t rxBuffer2[RX_BUFFER_SIZE];
uint8_t* currentRxBuffer;
void initUSART2(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
DMA_InitTypeDef DMA_InitStruct;
/* Enable clock for GPIOA and USART2 */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* Configure PA2 and PA3 for USART2 TX and RX */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Connect PA2 to USART2_TX */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
/* Connect PA3 to USART2_RX */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
/* Configure USART2 */
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStruct);
USART_Cmd(USART2, ENABLE);
/* Configure DMA */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
DMA_InitStruct.DMA_Channel = DMA_Channel_4;
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR;
DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)rxBuffer1;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStruct.DMA_BufferSize = RX_BUFFER_SIZE;
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
DMA_InitStruct.DMA_Priority = DMA_Priority_High;
DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream5, &DMA_InitStruct);
/* Enable DMA interrupts */
DMA_ITConfig(DMA1_Stream5, DMA_IT_TC, ENABLE);
/* Configure NVIC for DMA interrupts */
NVIC_InitStruct.NVIC_IRQChannel = DMA1_Stream5_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
/* Start DMA */
DMA_Cmd(DMA1_Stream5, ENABLE);
/* Set currentRxBuffer to rxBuffer1 */
currentRxBuffer = rxBuffer1;
}
```
在DMA中断服务程序中,需要判断当前DMA传输的缓冲区,并根据情况切换到另一个缓冲区:
```c
void DMA1_Stream5_IRQHandler(void)
{
if (DMA_GetITStatus(DMA1_Stream5, DMA_IT_TCIF5) != RESET)
{
DMA_ClearITPendingBit(DMA1_Stream5, DMA_IT_TCIF5);
/* Switch to the other buffer */
if (currentRxBuffer == rxBuffer1)
{
DMA1_Stream5->M0AR = (uint32_t)rxBuffer2;
currentRxBuffer = rxBuffer2;
}
else
{
DMA1_Stream5->M0AR = (uint32_t)rxBuffer1;
currentRxBuffer = rxBuffer1;
}
}
}
```
在主函数中,可以使用`currentRxBuffer`指向当前接收到的数据缓冲区:
```c
int main(void)
{
initUSART2();
while (1)
{
/* Process received data from currentRxBuffer */
}
}
```
阅读全文