STM32 串口空闲中断DMA接收
时间: 2023-07-23 19:06:00 浏览: 166
在STM32中,可以使用空闲中断和DMA接收来实现串口数据的接收。下面是一个基本的流程:
1. 配置串口的硬件参数,包括波特率、数据位、停止位、校验等。
2. 配置DMA通道,使其能够接收串口数据并将其存储到指定的缓冲区中。
3. 配置空闲中断,当串口接收到数据后触发,并在中断处理函数中进行处理。
4. 在主程序中启动DMA传输和空闲中断,使其能够接收串口数据并进行处理。
下面是一个简单的例子,用于演示如何使用空闲中断和DMA接收来实现串口数据的接收:
```c
#include "stm32f4xx.h"
#include <stdio.h>
#define BUFFER_SIZE 256
volatile uint8_t buffer[BUFFER_SIZE];
volatile uint16_t buffer_head = 0;
volatile uint16_t buffer_tail = 0;
void USART2_IRQHandler(void)
{
if (USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)
{
// 空闲中断触发,表示接收到了完整的数据帧
DMA_Cmd(DMA1_Stream5, DISABLE);
uint16_t len = BUFFER_SIZE - DMA_GetCurrDataCounter(DMA1_Stream5);
buffer_tail = (buffer_head + len) % BUFFER_SIZE;
DMA_SetCurrDataCounter(DMA1_Stream5, BUFFER_SIZE);
DMA_Cmd(DMA1_Stream5, ENABLE);
USART_ClearITPendingBit(USART2, USART_IT_IDLE);
}
}
int main(void)
{
// 初始化串口2
USART_InitTypeDef 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_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);
USART_Cmd(USART2, ENABLE);
// 初始化DMA1 Stream5,用于接收串口2数据
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Stream5);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->DR);
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = BUFFER_SIZE;
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_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream5, &DMA_InitStructure);
// 启动DMA传输和空闲中断
DMA_Cmd(DMA1_Stream5, ENABLE);
NVIC_EnableIRQ(USART2_IRQn);
while (1)
{
if (buffer_head != buffer_tail)
{
// 有数据需要处理,这里只是简单地将其打印出来
uint16_t len = (buffer_tail > buffer_head) ? (buffer_tail - buffer_head) : (BUFFER_SIZE - buffer_head);
printf("%.*s", len, buffer + buffer_head);
buffer_head = buffer_tail;
}
}
}
```
在上面的代码中,我们将DMA设置为循环模式,这样可以不断地接收串口数据。当空闲中断触发时,说明接收到了完整的数据帧,此时我们停止DMA传输,并记录下接收到的数据的长度。然后将缓冲区的读指针向后移动相应的长度,以便下一次处理。在主程序中,我们不断检查缓冲区的读指针和写指针是否相同,如果不同,则说明有新的数据需要处理,这里只是简单地将其打印出来,实际应用中可以根据需要进行其他的处理。
阅读全文