当前位置: 代码迷 >> 综合 >> USART+DMA传输+串口空闲中断
  详细解决方案

USART+DMA传输+串口空闲中断

热度:38   发布时间:2023-11-28 05:55:29.0

USART是一种异步通信,不带有时钟同步信号,而且是全双工,允许数据同时在两个方向上传输。

而DMA就是在I/O设备和内存的数据传输的时候,数据搬运工作全部交给DMA控制器,而CPU不再参与任何与数据搬运相关的事情,这样CPU就可以去处理别的数据。

对于这个USART+DMA传输+串口空闲中断就极大的减少CPU的消耗,可以用CPU去做更多的事情。

废话不都说,上代码,代码是stm32ZGT6的芯片上的代码

#include "sys.h"
#include "usart.h"	  
#include "dma.h"
#include "led.h"#if 1
#pragma import(__use_no_semihosting)             struct __FILE 
{ int handle; }; FILE __stdout;       void _sys_exit(int x) 
{ x = x; 
} int fputc(int ch, FILE *f)
{      while((USART1->SR&0X40)==0);  USART1->DR = (u8) ch;      return ch;
}
#endif /* 
int fputc(int ch, FILE *f)
{USART_SendData(USART1, (uint8_t) ch);while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}	return ch;
}
int GetKey (void)  { while (!(USART1->SR & USART_FLAG_RXNE));return ((int)(USART1->DR & 0x1FF));
}
*/#if EN_USART1_RX   //??????????????u8 USART_RX_BUF[USART_REC_LEN];  u16 rx_len;
u8 i;void uart_init(u32 bound)
{GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);	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);//??????GPIOA.9GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_Init(GPIOA, &GPIO_InitStructure);//??????GPIOA.10  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			NVIC_Init(&NVIC_InitStructure);	USART_InitStructure.USART_BaudRate = bound;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_Init(USART1, &USART_InitStructure); USART_ITConfig(USART1,USART_IT_IDLE, ENABLE);USART_Cmd(USART1, ENABLE);                 }void USART1_IRQHandler(void)                	
{	
#if SYSTEM_SUPPORT_OS 		//????SYSTEM_SUPPORT_OS???????ò?è???§??OS.OSIntEnter();    
#endifif(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)  {i=USART1->SR;i=USART1->DR;	USART_ClearITPendingBit(USART1,USART_IT_IDLE);MYDMA_Enable(DMA1_Channel5);	LED1=!LED1;		} 
#if SYSTEM_SUPPORT_OS 	OSIntExit();  											 
#endif
} 
#endif	

u8 ReceiveBuff[RECEIVE_BUF_SIZE];
u8 SendBuff[SEND_BUF_SIZE];	
const u8 TEXT_TO_SEND[]={"ALIENTEK WarShip STM32F1 DMA ???????é"};int main(void){	 u16 i;u8 t=0;u8 j,mask=0;delay_init();	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);uart_init(115200);	 	LED_Init();		  		LCD_Init();			KEY_Init();			Usart_Tx_Config();Usart_Rx_Config();//MYDMA_Config(DMA1_Channel4,(u32)&USART1->DR,(u32)SendBuff,SEND_BUF_SIZE);//DMA1?¨??4,???è??????1,?????÷??SendBuff,?¤??SEND_BUF_SIZE.//MYDMA_Config(DMA1_Channel5,(u32)&USART1->DR,(u32)ReceiveBuff,RECEIVE_BUF_SIZE);//DMA?¨??5?????è????5?????ü?÷receive,?¤??8200POINT_COLOR=RED;LCD_ShowString(30,50,200,16,16,"WarShip STM32");	LCD_ShowString(30,70,200,16,16,"DMA TEST");	LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");LCD_ShowString(30,110,200,16,16,"2015/1/15");	LCD_ShowString(30,130,200,16,16,"KEY0:Start");j=sizeof(TEXT_TO_SEND);	   for(i=0;i<SEND_BUF_SIZE;i++){if(t>=j){if(mask){SendBuff[i]=0x0a;t=0;}else {SendBuff[i]=0x0d;mask++;}	}else{mask=0;SendBuff[i]=TEXT_TO_SEND[t];t++;}    	   }		 POINT_COLOR=BLUE;  i=0;while(1){t=KEY_Scan(0);if(t==KEY0_PRES){LCD_ShowString(30,150,200,16,16,"Start Transimit....");LCD_ShowString(30,170,200,16,16,"   %");//????°?·???	USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);MYDMA_Enable(DMA1_Channel4);}i++;delay_ms(10);if(i==20){LED0=!LED0;i=0;}		   }
}
#ifndef __USART_H
#define __USART_H
#include "stdio.h"	
#include "sys.h" #define USART_REC_LEN  			200  	
#define EN_USART1_RX 			1		extern u8  USART_RX_BUF[USART_REC_LEN]; 
extern u16 USART_RX_STA;         	void uart_init(u32 bound);
#endif
#include "dma.h"
#include "lcd.h"DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef	NVIC_InitStructure;u16 DMA1_MEM_LEN;void Usart_Tx_Config()				
{RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);DMA_DeInit(DMA1_Channel4);DMA1_MEM_LEN=SEND_BUF_SIZE;DMA_InitStructure.DMA_PeripheralBaseAddr=(u32)&USART1->DR;DMA_InitStructure.DMA_MemoryBaseAddr=(u32)SendBuff;DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST;DMA_InitStructure.DMA_BufferSize=(u32)SEND_BUF_SIZE;DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte;	DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;	DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Byte;	DMA_InitStructure.DMA_Mode=DMA_Mode_Normal;	DMA_InitStructure.DMA_Priority=DMA_Priority_VeryHigh;	DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;	DMA_Init(DMA1_Channel4,&DMA_InitStructure);		NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel4_IRQn;		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;			NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;				NVIC_Init(&NVIC_InitStructure);									DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);				USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);DMA_Cmd(DMA1_Channel4,ENABLE);							
}void Usart_Rx_Config()				
{RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);DMA_DeInit(DMA1_Channel5);DMA1_MEM_LEN=RECEIVE_BUF_SIZE;DMA_InitStructure.DMA_PeripheralBaseAddr=(u32)&USART1->DR;DMA_InitStructure.DMA_MemoryBaseAddr=(u32)ReceiveBuff;DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST;DMA_InitStructure.DMA_BufferSize=(u32)RECEIVE_BUF_SIZE;DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte;	DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;	DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Byte;	DMA_InitStructure.DMA_Mode=DMA_Mode_Normal;	DMA_InitStructure.DMA_Priority=DMA_Priority_VeryHigh;	DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;	DMA_Init(DMA1_Channel5,&DMA_InitStructure);		USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);DMA_Cmd(DMA1_Channel5,ENABLE);							
}void MYDMA_Enable(DMA_Channel_TypeDef*DMA_CHx)
{DMA_Cmd(DMA_CHx,DISABLE); DMA_SetCurrDataCounter(DMA_CHx,DMA1_MEM_LEN);DMA_Cmd(DMA_CHx,ENABLE);
}void DMA1_Channel4_IRQHandler(void)
{if(DMA_GetFlagStatus(DMA1_FLAG_TC4)!=RESET){DMA_ClearFlag(DMA1_FLAG_TC4);DMA_Cmd(DMA1_Channel4,DISABLE);DMA_ClearITPendingBit(DMA1_FLAG_TC4);	LCD_ShowNum(30,170,100,3,16);LCD_ShowString(30,150,200,16,16,"Transimit Finished!");}
}
#ifndef __DMA_H
#define	__DMA_H	   
#include "sys.h"#define SEND_BUF_SIZE 8200	
#define RECEIVE_BUF_SIZE  8200extern u8 ReceiveBuff[RECEIVE_BUF_SIZE];
extern u8 SendBuff[SEND_BUF_SIZE];	void Usart_Tx_Config(void);
void Usart_Rx_Config(void);void MYDMA_Enable(DMA_Channel_TypeDef*DMA_CHx);#endif