串口空闲中断 +DMA中断接收
时间: 2023-09-08 08:14:02 浏览: 58
串口空闲中断和DMA中断接收是两种不同的接收方式。
串口空闲中断接收是指当串口接收到数据后,数据传输完成并且串口空闲时,触发中断来进行数据处理。这种方式比较简单,适用于数据量较小的情况。
DMA中断接收是指使用DMA控制器来进行数据传输,当DMA传输完成后触发中断进行数据处理。这种方式适用于数据量较大的情况,可以提高数据传输的效率。
下面是一个基于STM32的串口空闲中断+DMA中断接收的例子:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_conf.h"
#define USARTx USART2
#define DMAx DMA1
#define DMAx_Streamx DMA1_Stream5
#define DMAx_Streamx_IRQn DMA1_Stream5_IRQn
#define DMAx_Streamx_IRQHandler DMA1_Stream5_IRQHandler
#define BUFFER_SIZE 1024
uint8_t buffer[BUFFER_SIZE];
volatile uint16_t bufferIndex = 0;
void initUSART()
{
USART_InitTypeDef USART_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
DMA_InitTypeDef DMA_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
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);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
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(USARTx, &USART_InitStruct);
USART_ITConfig(USARTx, USART_IT_IDLE, ENABLE);
DMA_InitStruct.DMA_Channel = DMA_Channel_4;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)buffer;
DMA_InitStruct.DMA_BufferSize = BUFFER_SIZE;
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&USARTx->DR;
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_Medium;
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(DMAx_Streamx, &DMA_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = DMAx_Streamx_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
USART_Cmd(USARTx, ENABLE);
DMA_Cmd(DMAx_Streamx, ENABLE);
}
void DMAx_Streamx_IRQHandler()
{
if (DMA_GetITStatus(DMAx_Streamx, DMA_IT_TCIF5) != RESET)
{
DMA_ClearITPendingBit(DMAx_Streamx, DMA_IT_TCIF5);
DMA_Cmd(DMAx_Streamx, DISABLE);
bufferIndex = BUFFER_SIZE - DMA_GetCurrDataCounter(DMAx_Streamx);
}
}
void USART2_IRQHandler()
{
if (USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_IDLE);
DMAx_Streamx->CR &= ~(1 << 0); // Disable DMA Stream
DMA_ClearFlag(DMAx_Streamx, DMA_FLAG_TCIF5 | DMA_FLAG_HTIF5 | DMA_FLAG_TEIF5 | DMA_FLAG_DMEIF5 | DMA_FLAG_FEIF5);
DMA_Cmd(DMAx_Streamx, ENABLE);
}
}
int main()
{
initUSART();
while (1)
{
if (bufferIndex > 0)
{
// 处理接收到的数据
bufferIndex = 0;
}
}
}
```
该例子中使用DMA控制器进行数据传输,并通过串口空闲中断触发DMA传输完成中断。在DMA传输完成中断中,通过DMA_GetCurrDataCounter函数获取当前未传输的数据长度,即接收到的数据长度。在串口空闲中断中,通过禁用并重新启用DMA Stream来触发DMA传输完成中断。