STM32F407使用标准库,串口USART2通过DMA使用乒乓缓冲来接收和发送数据
时间: 2023-06-26 12:06:21 浏览: 243
【STM32】标准库-USART-DMA收发数据-串口空闲-仿printf发送-双缓冲
好的,这是一个比较常见的应用场景。我可以给你提供一些思路和代码片段来实现这个功能。
1. 配置USART2串口
首先,你需要使用标准库函数来配置USART2串口。你需要设置波特率、数据位、停止位、校验位等参数。下面是一个示例代码:
```c
#include "stm32f4xx.h"
// 配置USART2串口
void USART2_Init(void)
{
USART_InitTypeDef USART_InitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_InitStruct.USART_BaudRate = 115200;
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);
}
```
2. 配置DMA
接下来,你需要配置DMA。DMA可以帮助你实现高效的数据传输。你需要配置DMA的通道、数据传输方向、数据缓冲区地址等参数。下面是一个示例代码:
```c
#include "stm32f4xx.h"
// 配置DMA
void DMA_Init(void)
{
DMA_InitTypeDef DMA_InitStruct;
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)buffer1;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStruct.DMA_BufferSize = 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);
DMA_Cmd(DMA1_Stream5, ENABLE);
}
```
在这个示例中,我们使用了DMA1的Stream5通道,将数据从USART2的DR寄存器传输到缓冲区buffer1中。我们设置了循环模式,这样当缓冲区满了之后就会自动从缓冲区开头重新开始存储数据。
3. 配置乒乓缓冲区
为了避免在数据传输期间出现数据冲突,你需要使用乒乓缓冲区来处理数据。乒乓缓冲区就是在两个缓冲区之间进行切换,当一个缓冲区被DMA传输完毕后,另一个缓冲区就会被用来存储新的数据。下面是一个示例代码:
```c
#include "stm32f4xx.h"
#define BUFFER_SIZE 1024
uint8_t buffer1[BUFFER_SIZE];
uint8_t buffer2[BUFFER_SIZE];
volatile uint8_t *pRX = buffer1;
volatile uint8_t *pTX = buffer2;
// USART2 DMA发送数据
void USART2_DMA_Send(uint8_t *pData, uint16_t Length)
{
while(DMA_GetFlagStatus(DMA1_Stream6, DMA_FLAG_TCIF6) == RESET);
DMA_ClearFlag(DMA1_Stream6, DMA_FLAG_TCIF6);
DMA_Cmd(DMA1_Stream6, DISABLE);
DMA_SetCurrDataCounter(DMA1_Stream6, Length);
DMA_MemoryTargetConfig(DMA1_Stream6, (uint32_t)pData, DMA_Memory_0);
DMA_Cmd(DMA1_Stream6, ENABLE);
}
// USART2 DMA接收数据
void USART2_DMA_Receive(void)
{
if(DMA_GetFlagStatus(DMA1_Stream5, DMA_FLAG_TCIF5) != RESET)
{
DMA_ClearFlag(DMA1_Stream5, DMA_FLAG_TCIF5);
// 处理接收到的数据
// ...
if(pRX == buffer1)
{
// 切换到buffer2
DMA_MemoryTargetConfig(DMA1_Stream5, (uint32_t)pTX, DMA_Memory_0);
pRX = buffer2;
pTX = buffer1;
}
else
{
// 切换到buffer1
DMA_MemoryTargetConfig(DMA1_Stream5, (uint32_t)pTX, DMA_Memory_0);
pRX = buffer1;
pTX = buffer2;
}
// 重新启动DMA传输
DMA_Cmd(DMA1_Stream5, ENABLE);
}
}
```
在这个示例中,我们使用了两个缓冲区buffer1和buffer2,并且定义了两个指针pRX和pTX来指向当前使用的缓冲区。当DMA传输完成后,我们会检查当前使用的缓冲区,并且切换到另一个缓冲区来存储新的数据。然后重新启动DMA传输,以便继续接收数据。
4. 发送和接收数据
最后,你需要编写代码来发送和接收数据。你可以使用USART_SendData函数来发送数据,使用USART_ReceiveData函数来接收数据。下面是一个示例代码:
```c
#include "stm32f4xx.h"
#define BUFFER_SIZE 1024
uint8_t buffer1[BUFFER_SIZE];
uint8_t buffer2[BUFFER_SIZE];
volatile uint8_t *pRX = buffer1;
volatile uint8_t *pTX = buffer2;
// 配置USART2串口
void USART2_Init(void)
{
USART_InitTypeDef USART_InitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_InitStruct.USART_BaudRate = 115200;
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);
}
// 配置DMA
void DMA_Init(void)
{
DMA_InitTypeDef DMA_InitStruct;
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)buffer1;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStruct.DMA_BufferSize = 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);
DMA_Cmd(DMA1_Stream5, ENABLE);
}
// USART2 DMA发送数据
void USART2_DMA_Send(uint8_t *pData, uint16_t Length)
{
while(DMA_GetFlagStatus(DMA1_Stream6, DMA_FLAG_TCIF6) == RESET);
DMA_ClearFlag(DMA1_Stream6, DMA_FLAG_TCIF6);
DMA_Cmd(DMA1_Stream6, DISABLE);
DMA_SetCurrDataCounter(DMA1_Stream6, Length);
DMA_MemoryTargetConfig(DMA1_Stream6, (uint32_t)pData, DMA_Memory_0);
DMA_Cmd(DMA1_Stream6, ENABLE);
}
// USART2 DMA接收数据
void USART2_DMA_Receive(void)
{
if(DMA_GetFlagStatus(DMA1_Stream5, DMA_FLAG_TCIF5) != RESET)
{
DMA_ClearFlag(DMA1_Stream5, DMA_FLAG_TCIF5);
// 处理接收到的数据
// ...
if(pRX == buffer1)
{
// 切换到buffer2
DMA_MemoryTargetConfig(DMA1_Stream5, (uint32_t)pTX, DMA_Memory_0);
pRX = buffer2;
pTX = buffer1;
}
else
{
// 切换到buffer1
DMA_MemoryTargetConfig(DMA1_Stream5, (uint32_t)pTX, DMA_Memory_0);
pRX = buffer1;
pTX = buffer2;
}
// 重新启动DMA传输
DMA_Cmd(DMA1_Stream5, ENABLE);
}
}
int main(void)
{
USART2_Init();
DMA_Init();
while(1)
{
// 发送数据
uint8_t data[] = "Hello World!";
USART2_DMA_Send(data, sizeof(data));
// 接收数据
USART2_DMA_Receive();
}
}
```
这个示例代码中,我们在主循环中不断发送和接收数据。当需要发送数据时,我们调用USART2_DMA_Send函数,并传入要发送的数据和数据长度。当需要接收数据时,我们调用USART2_DMA_Receive函数。在接收到数据后,我们会对数据进行处理,并且切换到另一个缓冲区来存储新的数据。
阅读全文