STM32F103基于DMA接收不定幀長(zhǎng)USART數(shù)據(jù)
掃描二維碼
隨時(shí)隨地手機(jī)看文章
DMA是一種不使用CPU而將數(shù)據(jù)從一片地址空間復(fù)制到另一片地址空間的總線,這樣就減少了CPU的負(fù)擔(dān),使其能夠更加專注于數(shù)據(jù)運(yùn)算。為了能夠減少CPU的負(fù)擔(dān),DMA應(yīng)該采取中斷方式而非查詢模式。但是非常不幸的是,STM32F103只為DMA提供了三種中斷:半步中斷、完成中斷和錯(cuò)誤中斷。如果UART接收的是定幀長(zhǎng)的數(shù)據(jù),則可以開啟DMA半步中斷,并且目標(biāo)地址長(zhǎng)度為幀長(zhǎng)兩倍。這樣每接收完一幀進(jìn)一次中斷,進(jìn)行某些操作,是很理想的。然而當(dāng)遇到如同GPS一樣不定幀長(zhǎng)的數(shù)據(jù)時(shí),如果仍用半步中斷則難以確定目標(biāo)地址的長(zhǎng)度。所以在此放棄使用DMA的中斷,轉(zhuǎn)而使用的是另一種比較特別的中斷:UART空閑中斷。
先來(lái)介紹一下UART空閑中斷。UART常用的接收中斷響應(yīng)有:接收數(shù)據(jù) 就緒可讀中斷RXNE(這是最常用的)、數(shù)據(jù)溢出中斷(ORE)、奇偶校驗(yàn)錯(cuò)中斷(PE)和空閑中斷(IDLE)??臻e中斷是指當(dāng)總線檢測(cè)到一幀發(fā)完后, 總線空閑則會(huì)將此位置一,如果USART_CR1中的IDLEIE為’1’,則產(chǎn)生中斷??臻e中斷有兩個(gè)比較有意思的特點(diǎn):
1、清零方式:軟件清零,先讀USART_SR,然后讀USART_DR
2、直到RXNE被置一后IDLE才能被重讀置一,即IDLE被置一并軟件清零后,只有之后再次接收到數(shù)據(jù),IDLE才能被置一。這樣就防止了總線長(zhǎng)時(shí)間空閑而多次引發(fā)空閑中斷。
好的廢話不多說(shuō)上例程。
以下都是通過(guò)DMA接收GPS串口的子模塊程序
#define UART_RX_LEN128
static char Uart_Rx[UART_RX_LEN];//GPS接收數(shù)據(jù)
void GPS_Init(void)
{
RCC_Configuration();//時(shí)鐘打開
GPIO_Configuration();//GPIO配置
DMA_Configuration();//DMA配置
UART_Configuration();//UART配置
NVIC_Configuration();//中斷優(yōu)先級(jí)配置
}
void RCC_Configuration(void)
{
//打開串口對(duì)應(yīng)的外設(shè)時(shí)鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
//啟動(dòng)DMA時(shí)鐘
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
//DMA1通道5配置
DMA_DeInit(DMA1_Channel5);
//外設(shè)地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);
//內(nèi)存地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Uart_Rx;
//dma傳輸方向單向
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
//設(shè)置DMA在傳輸時(shí)緩沖區(qū)的長(zhǎng)度
DMA_InitStructure.DMA_BufferSize = UART_RX_LEN;
//設(shè)置DMA的外設(shè)遞增模式,一個(gè)外設(shè)
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//設(shè)置DMA的內(nèi)存遞增模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//外設(shè)數(shù)據(jù)字長(zhǎng)
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//內(nèi)存數(shù)據(jù)字長(zhǎng)
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
//設(shè)置DMA的傳輸模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//設(shè)置DMA的優(yōu)先級(jí)別
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
//設(shè)置DMA的2個(gè)memory中的變量互相訪問(wèn)
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5,&DMA_InitStructure);
//使能通道5
DMA_Cmd(DMA1_Channel5,ENABLE);
}
void UART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
//初始化參數(shù)
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_InitStructure.USART_BaudRate = 9600;
//初始化串口
USART_Init(USART1,&USART_InitStructure);
//中斷配置
USART_ITConfig(USART1,USART_IT_TC,DISABLE);
USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);
USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);
//采用DMA方式接收
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
//配置UART1中斷
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//通道設(shè)置為串口1中斷
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;//中斷占先等級(jí)0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//中斷響應(yīng)優(yōu)先級(jí)0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//打開中斷
NVIC_Init(&NVIC_InitStructure);
}
void USART1_IRQHandler(void)
{
uint32_t Length = 0;//數(shù)據(jù)長(zhǎng)度
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
DMA_Cmd(DMA1_Channel5,DISABLE);
Length = USART1->SR;
Length = USART1->DR; //清USART_IT_IDLE標(biāo)志
Length = UART_RX_LEN - DMA_GetCurrDataCounter(DMA1_Channel5);
//設(shè)置傳輸數(shù)據(jù)長(zhǎng)度
DMA1_Channel5->CNDTR = UART_RX_LEN;//重裝填,并讓接收地址偏址從0開始
DMA_Cmd(DMA1_Channel5, ENABLE);//處理完,重開DMA
}
__nop();
}
void GPS_Cmd(FunctionalState NewState)
{
USART_Cmd(USART1, NewState);
}
以下是調(diào)用函數(shù),一般在main函數(shù)中
GPS_Init();
GPS_Cmd(ENABLE);