STM32使用串口1配合DMA接收不定長(zhǎng)數(shù)據(jù),減輕CPU載荷 http://www.openedv.com/thread-63849-1-1.html
實(shí)現(xiàn)思路:采 用STM32F103的串口1,并配置成空閑中斷模式且使能DMA接收,并同時(shí)設(shè)置接收緩沖區(qū)和初始化DMA。那么初始化完成之后,當(dāng)外部給單片機(jī)發(fā)送數(shù) 據(jù)的時(shí)候,假設(shè)這幀數(shù)據(jù)長(zhǎng)度是100個(gè)字節(jié),那么在單片機(jī)接收到一個(gè)字節(jié)的時(shí)候并不會(huì)產(chǎn)生串口中斷,而是DMA在后臺(tái)把數(shù)據(jù)默默地搬運(yùn)到你指定的緩沖區(qū)里 面。當(dāng)整幀數(shù)據(jù)發(fā)送完畢之后串口才會(huì)產(chǎn)生一次中斷,此時(shí)可以利用DMA_GetCurrDataCounter();函數(shù)計(jì)算出本次的數(shù)據(jù)接受長(zhǎng)度,從而進(jìn)行數(shù)據(jù)處理。
關(guān)鍵代碼分析:usart.H#ifndef __USART_H#define __USART_H#include "stdio.h"#include "sys.h" #define DMA_Rec_Len 200 //定義一個(gè)長(zhǎng)度為200個(gè)字節(jié)的數(shù)據(jù)緩沖區(qū)。(建議定義的長(zhǎng)度比你可能接收到的最長(zhǎng)單幀數(shù)據(jù)長(zhǎng)度長(zhǎng)?。?/span>void uart_init(u32 bound);void MYDMA_Enable(DMA_Channel_TypeDef*DMA_CHx);#endifusart.C//初始化IO 串口1 //bound:波特率void uart_init(u32 bound){ //GPIO端口設(shè)置 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; DMA_InitTypeDef DMA_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA時(shí)鐘 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA傳輸 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2時(shí)鐘 USART_DeInit(USART1); //復(fù)位串口1 //USART1_TX PA.9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽輸出 GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA9 //USART1_RX A.10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入 GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA10 //Usart1 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//搶占優(yōu)先級(jí)3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子優(yōu)先級(jí)3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根據(jù)指定的參數(shù)初始化VIC寄存器 //USART 初始化設(shè)置 USART_InitStructure.USART_BaudRate = bound;//一般設(shè)置為9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字長(zhǎng)為8位數(shù)據(jù)格式 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一個(gè)停止位 USART_InitStructure.USART_Parity = USART_Parity_No;//無(wú)奇偶校驗(yàn)位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無(wú)硬件數(shù)據(jù)流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發(fā)模式 USART_Init(USART1, &USART_InitStructure); //初始化串口 USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//開啟空閑中斷 USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); //使能串口1 DMA接收 USART_Cmd(USART1, ENABLE); //使能串口 //相應(yīng)的DMA配置 DMA_DeInit(DMA1_Channel5); //將DMA的通道5寄存器重設(shè)為缺省值 串口1對(duì)應(yīng)的是DMA通道5 DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1->DR; //DMA外設(shè)ADC基地址 DMA_InitStructure.DMA_MemoryBaseAddr = (u32)DMA_Rece_Buf; //DMA內(nèi)存基地址 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //數(shù)據(jù)傳輸方向,從外設(shè)讀取發(fā)送到內(nèi)存 DMA_InitStructure.DMA_BufferSize = DMA_Rec_Len; //DMA通道的DMA緩存的大小 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外設(shè)地址寄存器不變 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //內(nèi)存地址寄存器遞增 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //數(shù)據(jù)寬度為8位 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //數(shù)據(jù)寬度為8位 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //工作在正常緩存模式 DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //DMA通道 x擁有中優(yōu)先級(jí) DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA通道x沒(méi)有設(shè)置為內(nèi)存到內(nèi)存?zhèn)鬏?/span> DMA_Init(DMA1_Channel5, &DMA_InitStructure); //根據(jù)DMA_InitStruct中指定的參數(shù)初始化DMA的通道 DMA_Cmd(DMA1_Channel5, ENABLE); //正式驅(qū)動(dòng)DMA傳輸}//串口中斷函數(shù)void USART1_IRQHandler(void) //串口1中斷服務(wù)程序{ if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET) //接收中斷(接收到的數(shù)據(jù)必須是0x0d 0x0a結(jié)尾) { USART_ReceiveData(USART1);//讀取數(shù)據(jù) 注意:這句必須要,否則不能夠清除中斷標(biāo)志位。 Usart1_Rec_Cnt = DMA_Rec_Len-DMA_GetCurrDataCounter(DMA1_Channel5); //算出接本幀數(shù)據(jù)長(zhǎng)度 //***********幀數(shù)據(jù)處理函數(shù)************// printf ("The lenght:%d\r\n",Usart1_Rec_Cnt); printf ("The data:\r\n"); Usart1_Send(DMA_Rece_Buf,Usart1_Rec_Cnt); printf ("\r\nOver! \r\n"); //*************************************// USART_ClearITPendingBit(USART1, USART_IT_IDLE); //清除中斷標(biāo)志 MYDMA_Enable(DMA1_Channel5); //恢復(fù)DMA指針,等待下一次的接收 } }
這種方式和傳統(tǒng)的uart接收中斷里面處理數(shù)據(jù)(解析協(xié)議的比較):
//普通方式
uart_rcv_irq()
{
DISABLE_UARTX
if G_Counter >= MAXLEN
clear buffer and counter;
else
G_Buffer[G_Counter]= GetData(UARTX);
G_Counter++;
if OK==unpack(G_Buffer,G_Counter)
clear buffer and counter;
set flag;
ENABLE_UARTX
}
//idle中斷 + dma方式
G_Buffer
G_Counter
G_DMARcvBuffer
uart_idle_irq()
{
G_Counter += MAXLEN - DMAGetCurDataCounter(DMAx);
copy G_DMARcvBuffer to G_Buffer;
if OK==unpack(G_Buffer,G_Counter)
clear buffers and counter;
set flag;
USART_ClearITPendingBit(USARTx, USART_IT_IDLE);
}
DMAx_OVERFLOW_IRQHandler()
{
G_Counter += MAXLEN - DMAGetCurDataCounter(DMAx);
copy G_DMARcvBuffer to G_Buffer;
if OK==unpack(G_Buffer,G_Counter)
clear buffers and counter;
set flag;
RESET DMA
}
聯(lián)系客服