⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dma.c

📁 基于一款32位嵌入式微处理器的DMAIP的测试代码
💻 C
字号:
/*************************************************************************************
*	Copyright (c) 2005 by National ASIC System Engineering Research Center.
*	PROPRIETARY RIGHTS of ASIC are involved in the subject matter of this 
*	material.  All manufacturing, reproduction, use, and sales rights 
*	pertaining to this subject matter are governed by the license agreement.
*	The recipient of this software implicitly accepts the terms of the license.
*
*	File Name: dma_lab.c
*
*	File Description:
*			This file give a simple test of Garfield DMA module, such as esram<->SDram
*		and nand<->SDram
*			
*	Function Description:
*		1.void InitDma(void)
*			Initlization DMA status registers;
*
*		2.STATUS DmaTransMtoM(U32 srcadd, U32 destadd, U32 srcwidth, U32 destwidth, U32 burstsize, U32 sum)
*			Memory to memory trans function.
*				Parameters Notes:
*					sourcewidth:	byte/halfword/word
*					destwidth:		byte/halfword/word
*					burstsize:		1,4,8,16
*					sum:	 		number of to be transed data,by source data width
*
*		3.STATUS DMAAPB(U32 sourceadd, U32 destadd, U32 source, U32 dest, U32 dmaccontrol, U32 srcwidth, U32 destwidth, U32 burstsize, U32 sum)
*  			This function realize the trans between Memory and APH devices	
*				Parameters Notes:
*					source and dest:	DMAMEM/DMAUSB/DMAAC97/DMASPI/DMANAND/DMAMMC		
*					dmaccontrol:   		action controller
*					dmaccontrol:		1(off) or 0(on)
*					sourcewidth:		byte/halfword/word
*					destwidth:			byte/halfword/word
*					burstsize:			1,4,8,16
*					sum:	 			number of to be transed data,by source data width
*						
*					note:		when source is memory,destination must be periphery  
*
*		4.void DMADataDefine(U32 beginadd, U32 num)  
*			This function define an area with byte writing into the head of "beginadd",and the write
*         number will be "num" 
*   
*		5.STATUS DMADataCheck(U32 beginadd, U32 num)    
*			This function check the area which head address is "beginadd",and check num is "num" bytes.
*
*
*
*
*
*
*
*
*
*
*
*	Created by Wuer 
**************************************************************************************/

#include "garfield.h"
extern int print(char *string);

U32 SOURCE = 0x1fff3800;								/* 数据存放源地址 */							
U32 DEST = 0x30002000;									/* 数据读出目标地址 */
U32 TESTNUM = 0x200;									/* 定义数据块大小 */							
char ch = 0xaa;											/* 数据内容 */


int PRINT(char *string)
{
   #ifndef USB_ICE
   printf(string);
   #else
   print(string);
   #endif
   return 0;
}


int main(void)
{
	char *s1,*s2;
	s1="____Error in  DMA lab!!!\n";
	s2="Succeeded in DMA lab!!!!\n";
	if(E_OK != DMA_M_M())
		 PRINT(s1);  //printf("____Error in  DMA lab!!!\n");
	else  PRINT(s2);  //printf("Succeeded in DMA lab!!!!\n");
	
		
	while(1);
	return 1;
}



STATUS DMA_M_M(void)
{
	char *s1,*s2;
	s1="____Some data is different from original data!\n";
	s2="ALL ok!\n";
	DataDefine(SOURCE, ch, TESTNUM);						/* 初始化一块数据块 */			
	clear(DEST, TESTNUM);									/* 将需要读出数据存放地址清零 */
							
	DmaTransMtoM(SOURCE, DEST, 32, 32, 4, (TESTNUM/4)); 	/* 用DMA通道0传输到目的地址 */
	
	if(E_OK != check(DEST, SOURCE, TESTNUM))				/* 检查目标内存中的数据是否与写入数据相同 */	
	{
		//printf("____Some data is different from original data!\n");
		PRINT(s1);
		return ~E_OK;
	}else	PRINT(s2);
	//printf("ALL ok!\n"); 
		
	return E_OK;
}


void InitDma(void)    
{
    *(RP)DMACIntErrClr     	 =0xFFFFFFFF;					/* 清除传输错误中断状态 */
	*(RP)DMACIntTCClear	     =0xFFFFFFFF;					/* 清除传输完成中断状态 */
	*(RP)DMACIntErrClr       =0x00000000;					/* 初始化传输错误中断状态 */
	*(RP)DMACIntTCClear      =0x00000000;					/* 初始化传输完成中断状态 */

	return ;

}



STATUS DmaTransMtoM(U32 srcadd, U32 destadd, U32 srcwidth, U32 destwidth, U32 burstsize, U32 sum)
{
											/*	Parameters Notes:
												sourcewidth:	byte/halfword/word
												destwidth:		byte/halfword/word
												burstsize:		1,4,8,16
												channelnum:   	0-5 general channel
												sum:	 		number of to be transed data,by source data width
										
												note:			destsize is omitted,because sourcewidth*sourcesize
												  				must=destwidth*destsize
                                       		 */
	
	U32		i=0;
	U32		j=0;
	U32		k=0;			
	
	*(RP)DMACC0SrcAddr = srcadd;
	*(RP)DMACC0DestAddr = destadd;
	
	i = (sum << 2) + DMASouraddInc + (DMADstaddInc<<1);  			//12th and 13th bit set to 1:srcadd and destadd are increasing during transaction;
	j = i << 12;												//set trans number,10th--21bit,(<4k)
																//21th--10th bits determine total transcation data size;
	
	
	if 	((srcwidth == 32)&&(destwidth == 32)&&(burstsize == 4))		
		k = 0x49b;
	else if ((srcwidth == 32)&&(destwidth == 32)&&(burstsize == 8))	
		k = 0x4ad;
	else if ((srcwidth == 32)&&(destwidth == 32)&&(burstsize == 16))
		k = 0x4bf;
	else if ((srcwidth == 32)&&(destwidth == 16)&&(burstsize == 8))
		k = 0x2bd;
	else if ((srcwidth == 32)&&(destwidth == 16)&&(burstsize == 4))
		k = 0x2ab;
	else if ((srcwidth == 32)&&(destwidth == 8)&&(burstsize == 4))
		k = 0x0bb;
	else if ((srcwidth == 16)&&(destwidth == 32)&&(burstsize == 16))
		k = 0x46f;
	else if ((srcwidth == 16)&&(destwidth == 32)&&(burstsize == 8))
	  	k = 0x45d;
	else if ((srcwidth == 16)&&(destwidth == 16)&&(burstsize == 4))				
		k = 0x25b;
	else if ((srcwidth == 16)&&(destwidth == 16)&&(burstsize == 8))
		k = 0x26d;
	else if ((srcwidth == 16)&&(destwidth == 16)&&(burstsize == 16))			
		k = 0x27f;	
	else if ((srcwidth == 16)&&(destwidth == 8)&&(burstsize == 4))			
		k = 0x06b;			
	else if ((srcwidth == 16)&&(destwidth == 8)&&(burstsize == 8))
		k = 0x07d;
	else if ((srcwidth == 8)&&(destwidth == 32)&&(burstsize == 16))			
		k = 0x41f;
	else if ((srcwidth == 8)&&(destwidth == 16)&&(burstsize == 16))			
		k = 0x22f;
	else if ((srcwidth == 8)&&(destwidth == 16)&&(burstsize == 8))			
		k = 0x21d;
	else if ((srcwidth == 8)&&(destwidth == 8)&&(burstsize == 4))			
		k = 0x01b;
	else if ((srcwidth == 8)&&(destwidth == 8)&&(burstsize == 8))			
		k = 0x02d;
	else if ((srcwidth == 8)&&(destwidth == 8)&&(burstsize == 16))			
		k = 0x03f;							
	
	                                                				//19 transcation patterns altogether,change with sourcewith,destwith,
	                                                				//sourcesize and destsize.
	                                                				//0th --7th bits determine which pattern is to be used;
	                                                				
	i = j + k;														//Set the control register;
	*(RP)DMACC0Control = i;                    				
	                                                				
	*(RP)DMACC0Configuration = 0x19;								//Channel enable: memory-->memory,mask error interrupt and accomplishment
											       				 	// interrupt;DMAC control;transcation begin now;
			
	i = *(RP)DMACC0Configuration;									//judge the channel finish the action or not yet.
	while( (i & 0x1) != 0x0){ 
		i = *(RP)DMACC0Configuration;
	}
	
	return E_OK;
}


STATUS DMAAPB(U32 sourceadd, U32 destadd, U32 source, U32 dest, U32 dmaccontrol, U32 srcwidth, U32 destwidth, U32 burstsize, U32 sum)
{
								/*	Parameters Notes:
										source and dest:	DMAMEM/DMAUSB/DMAAC97/DMASPI/DMANAND/DMAMMC		
										dmaccontrol:   		action controller
										dmaccontrol:		1(off) or 0(on)
										destwidth:			byte/halfword/word
										burstsize:			1,4,8,16
										sum:	 			number of to be transed data,by source data width
					
										note:		when source is memory,destination must be periphery  
								*/
											

	U32	i=0;
	U32 j=0;
	U32 k=0;
	U32 l=0;
	U32 m=0;
	
	*(RP)DMACC0SrcAddr = source;
	*(RP)DMACC0DestAddr = dest;

	if(source == 0)
		i = 1;
	else i = 2;
	j = (sum << 2) + i;  	
	l = j << 12;											
															
	
	if 	((srcwidth == 0x32)&&(destwidth == 0x32)&&(burstsize == 0x4))		
		k = 0x49b;
	else if ((srcwidth == 0x32)&&(destwidth == 0x32)&&(burstsize == 0x8))	
		k = 0x4ad;
	else if ((srcwidth == 0x32)&&(destwidth == 0x32)&&(burstsize == 0x16))
		k = 0x4bf;
	else if ((srcwidth == 0x32)&&(destwidth == 0x16)&&(burstsize == 0x8))
		k = 0x2bd;
	else if ((srcwidth == 0x32)&&(destwidth == 0x16)&&(burstsize == 0x4))
		k = 0x2ab;
	else if ((srcwidth == 0x32)&&(destwidth == 0x8)&&(burstsize == 0x4))
		k = 0x0bb;
	else if ((srcwidth == 0x16)&&(destwidth == 0x32)&&(burstsize == 0x16))
		k = 0x46f;
	else if ((srcwidth == 0x16)&&(destwidth == 0x32)&&(burstsize == 0x8))
	  	k = 0x45d;
	else if ((srcwidth == 0x16)&&(destwidth == 0x16)&&(burstsize == 0x4))				
		k = 0x25b;
	else if ((srcwidth == 0x16)&&(destwidth == 0x16)&&(burstsize == 0x8))
		k = 0x26d;
	else if ((srcwidth == 0x16)&&(destwidth == 0x16)&&(burstsize == 0x16))			
		k = 0x27f;	
	else if ((srcwidth == 0x16)&&(destwidth == 0x8)&&(burstsize == 0x4))			
		k = 0x06b;			
	else if ((srcwidth == 0x16)&&(destwidth == 0x8)&&(burstsize == 0x8))
		k = 0x07d;
	else if ((srcwidth == 0x8)&&(destwidth == 0x32)&&(burstsize == 0x16))			
		k = 0x41f;
	else if ((srcwidth == 0x8)&&(destwidth == 0x16)&&(burstsize == 0x16))			
		k = 0x22f;
	else if ((srcwidth == 0x8)&&(destwidth == 0x16)&&(burstsize == 0x8))			
		k = 0x21d;
	else if ((srcwidth == 0x8)&&(destwidth == 0x8)&&(burstsize == 0x4))			
		k = 0x01b;
	else if ((srcwidth == 0x8)&&(destwidth == 0x8)&&(burstsize == 0x8))			
		k = 0x02d;
	else if ((srcwidth == 0x8)&&(destwidth == 0x8)&&(burstsize == 0x16))			
		k = 0x03f;							
	
	                                                				//19 transcation patterns altogether,change with sourcewith,destwith,
	                                                				//sourcesize and destsize.
	                                                				//0th --7th bits determine which pattern is to be used;
	                                                				
	m = l + k;														//Set the control register;
	*(RP)DMACC0Control = m;                    				
	          
	          
	i =  (dest << 11) + (source << 7) + (dmaccontrol << 6) + (i << 1) + 1;                                           				
	*(RP)DMACC0Configuration = i	;								//Channel enable: memory-->memory,mask error interrupt and accomplishment
											       				 	// interrupt;DMAC control;transcation begin now;
			
	i = *(RP)DMACC0Configuration;									//judge the channel finish the action or not yet.
	while( (i & 0x1) != 0x0){ 
		i = *(RP)DMACC0Configuration;
	}
	
	

	return E_OK;
	
}





STATUS check(U32 head1, U32 head2, U32 num)
{
	RP8 p1, p2;
	char data;
	char *s;
	s="the copyed data is not the orignal one!!\n";
	p1 = (RP8)head1;
	p2 = (RP8)head2;
	
	
	while(num-->0)
	{
		data = *((RP8)p1++);
		
		if(data!= *((RP8)p2++))
		{
			//printf("the copyed data is not the orignal one!!\n");
			PRINT(s);
			return ~E_OK;
		}
	}
	
	return E_OK;
}

void clear(U32 head, U32 num)
{
	U32 i = 0;
	for(i=0; i<num; i++)
	{
		*(RP8)head++ = (U8)0;
	}
	
	return;
}

void DataDefine(U32 head, char data, U32 num)
{
	U32 i = 0;
	for(i=0; i<num; i++)
	{
		*(RP8)head++ = data;
	}
	
	return;
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -