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

📄 usb.c

📁 优龙开发板YL2410的bios完整代码
💻 C
字号:
#include "def.h"
#include "2410addr.h"
#include "2410lib.h"
#include "timer.h"

#include "option.h"
#include "2410usb.h"
#include "usblib.h"
#include "usbsetup.h"
#include "usbout.h"
#include "usbin.h"

//======================================================
volatile U32 downloadAddress = 0;
volatile U8 *downPt;
volatile U32 downloadFileSize = 0;
volatile U32 totalDmaCount;
volatile U16 checkSum;

volatile int isUsbdSetConfiguration;

//======================================================
void __irq IsrUsbd(void)
{
    U8 usbdIntpnd, epIntpnd;
    U8 saveIndexReg = rINDEX_REG;
    usbdIntpnd = rUSB_INT_REG;
    epIntpnd = rEP_INT_REG;

	//	printf("usbd i %x\n", usbdIntpnd);
    if(usbdIntpnd&SUSPEND_INT)
    {
    	rUSB_INT_REG=SUSPEND_INT;    	
    }
    if(usbdIntpnd&RESUME_INT)
    {
    	rUSB_INT_REG=RESUME_INT;    	
    }
    if(usbdIntpnd&RESET_INT)
    {
    	    	
    	//ResetUsbd();
    	ReconfigUsbd();

    	rUSB_INT_REG=RESET_INT;  //RESET_INT should be cleared after ResetUsbd().   	

        //PrepareEp1Fifo(); 
    }

    if(epIntpnd&EP0_INT)
    {
		rEP_INT_REG=EP0_INT;  
    	Ep0Handler();
    }
    if(epIntpnd&EP1_INT)
    {
    	rEP_INT_REG=EP1_INT;  
    	Ep1Handler();
    }

    if(epIntpnd&EP2_INT)
    {
    	rEP_INT_REG=EP2_INT;  
    	//Ep2Handler();
    }

    if(epIntpnd&EP3_INT)
    {
    	rEP_INT_REG=EP3_INT;
    	Ep3Handler();
    }

    if(epIntpnd&EP4_INT)
    {
    	rEP_INT_REG=EP4_INT;    		
    	//Ep4Handler();
    }

    ClearPending(BIT_USBD);	 
    
    rINDEX_REG=saveIndexReg;
}

U32 UsbState;
U32 UsbInLength;
U8 *UsbTxAddr;

void UsbdInit(U8 fun)
{
	isUsbdSetConfiguration = 0;
	ChangeUPllValue(40, 4, 1);  //UCLK=48Mhz     
	InitDescriptorTable(fun);
	ReconfigUsbd();
//	PrepareEp1Fifo();

	UsbState = 0;
}

//======================================================
void WaitDownload(void);

void UsbMain(void)
{	
	U8 fun;
	
	rGPHCON = rGPHCON&~(0xf<<18)|(0x5<<18);
    //To enhance the USB signal quality.
    //CLKOUT 0,1=OUTPUT to reduce the power consumption.	

	rGPGCON &= 0xfff3ffff;	//GPG9 input
/*	puts("Please select usb function\n1:download\n2:usb test\n");
	while(1)
	{
		fun = '1';//getch();
		if((fun=='1')||(fun=='2'))
		{
			fun -= '0';
			break;
		}	
		 
	}*/
	fun = 1;
	UsbdInit(fun);	
		
	Delay(1000);
	rGPGCON |= 0x00040000;	
	rGPGDAT |= 0x0200;		//GPG9 ouput 1

	pISR_USBD =(unsigned)IsrUsbd;
	ClearPending(BIT_USBD);
	EnableIrq(BIT_USBD);
	
	WaitDownload();
				
	DisableIrq(BIT_USBD);

	rGPGCON &= 0xfff3ffff;	//GPG9 input
}

U32 UsbPoll(void);

/*************************************************************/
static __inline void cpu_arm920_cache_clean_invalidate_all(void)
{
	__asm{
		mov	r1, #0		
		mov	r1, #7 << 5			  	/* 8 segments */
cache_clean_loop1:		
		orr	r3, r1, #63UL << 26	  	/* 64 entries */
cache_clean_loop2:	
		mcr	p15, 0, r3, c7, c14, 2	/* clean & invalidate D index */
		subs	r3, r3, #1 << 26
		bcs	cache_clean_loop2		/* entries 64 to 0 */
		subs	r1, r1, #1 << 5
		bcs	cache_clean_loop1		/* segments 7 to 0 */
		mcr	p15, 0, r1, c7, c5, 0	/* invalidate I cache */
		mcr	p15, 0, r1, c7, c10, 4	/* drain WB */
	}
}
void cache_clean_invalidate(void)
{
	cpu_arm920_cache_clean_invalidate_all();
}

static __inline void cpu_arm920_tlb_invalidate_all(void)
{
	__asm{
		mov	r0, #0
		mcr	p15, 0, r0, c7, c10, 4	/* drain WB */
		mcr	p15, 0, r0, c8, c7, 0	/* invalidate I & D TLBs */
	}
}

void tlb_invalidate(void)
{
	cpu_arm920_tlb_invalidate_all();
}

void disable_irq(void);

void call_linux(U32 a0, U32 a1, U32 a2)
{
	int i, j;
	void (*goto_start)(U32, U32);
	cache_clean_invalidate();
	tlb_invalidate();	

//	DisableInt();
	disable_irq();
	//If write-back is used,the DCache should be cleared.
	for(i=0; i<64; i++)
		for(j=0; j<8; j++)
			MMU_CleanInvalidateDCacheIndex((i<<26)|(j<<5));
	__asm {
		mov	r0, #0
		mcr	p15, 0, r0, c7, c10, 4	// drain WB
	}
	MMU_DisableDCache();
	MMU_DisableICache();
	MMU_InvalidateICache();
	MMU_DisableMMU();
	MMU_InvalidateTLB();

/*	__asm{
//		mov	r0, a0//%0
//		mov	r1, a1//%1
//		mov	r2, a2//%2
		mov	ip, #0
		mcr	p15, 0, ip, c13, c0, 0	// zero PID 
		mcr	p15, 0, ip, c7, c7, 0	// invalidate I,D caches
		mcr	p15, 0, ip, c7, c10, 4	// drain write buffer
		mcr	p15, 0, ip, c8, c7, 0	// invalidate I,D TLBs 
		mrc	p15, 0, ip, c1, c0, 0	// get control register 
		bic	ip, ip, #0x0001			// disable MMU 
		mcr	p15, 0, ip, c1, c0, 0	// write control register 
		//mov	pc, r2
		//nop
		//nop
		// no outpus 
		//: "r" (a0), "r" (a1), "r" (a2)
	}
*/
//	SetClockDivider(1, 1);
//	SetSysFclk(FCLK_200M);		//start kernel, use 200M
//	Delay(1000);
//	disable_irq();
	goto_start = (void (*)(U32, U32))a2;
	(*goto_start)(a0, a1);	
}

/*************************************************************/
void WaitDownload(void)
{
    U16 UsbConnected = 0;
    U32 i;
    U32 j;
    U16 cs;
    U16 dnCS;
    U32 temp;            
	U8 tempMem[16];    
    
    checkSum = 0;            
    downloadAddress=(U32)tempMem; 		//_RAM_STARTADDRESS; 
    downPt=(unsigned char *)downloadAddress;	//This address is used for receiving first 8 byte.
    downloadFileSize = 0;    
    
#if 0
    MMU_DisableICache(); 
        //For multi-ICE. 
        //If ICache is not turned-off, debugging is started with ICache-on.
#endif

    /*******************************/
    /*    Test program download    */
    /*******************************/
    
    puts("\nUSB download file, press Esc key to exit\n");
/*    
	//	while(!isUsbdSetConfiguration)
    //	{
    //		if(getkey()==0x1b)
    //		return;    		
	//	}	    	
    //	puts("USB connected\n");
    	
    	DisableIrq(BIT_USBD);
    	DisableIrq(BIT_DMA2);
    	
    	while(!UsbPoll())
    	{
	    	if(getkey()==0x1b)
	    	{	    	
		    	puts("USB download aborted.\n");
		    	return;    	
		    }
	    }
    	
    	DbgOut("End, Received %dBytes\n",(U32)downPt-downloadAddress);
    	checkSum=checkSum - *((unsigned char *)(downloadAddress+downloadFileSize-8-2))
				- *( (unsigned char *)(downloadAddress+downloadFileSize-8-1) );	
    	goto PollRxEnd;
*/    
	j=0;   

	while(downloadFileSize==0)
	{
	//	if(j%0x50000==0)
	//		Led_Display(0x1);
	//	if(j%0x50000==0x28000)
	//		Led_Display(0x0);
		j++;
		if(getkey()==0x1b)
		{	
			puts("USB download aborted.\n");			
			return;
		}
		
		if(isUsbdSetConfiguration)
		{
			if(!UsbConnected)
				puts("Now USB is connected.");
			UsbConnected = 1;			
		}		
    }  

#if USBDMA     
    ClearEp3OutPktReady();	//中断处理读完第一个包后未清OutPktReady位,在此清掉

    if(downloadFileSize>EP3_PKT_SIZE)
    {
        if(downloadFileSize<=(0x80000))
      	    ConfigEp3DmaMode(downloadAddress+EP3_PKT_SIZE-8,downloadFileSize-EP3_PKT_SIZE);		
      	else
			ConfigEp3DmaMode(downloadAddress+EP3_PKT_SIZE-8,0x80000-EP3_PKT_SIZE);		
 		totalDmaCount=0;
    }
    else					// downloadFileSize < EP3_PKT_SIZE    
		totalDmaCount=downloadFileSize;		    
#endif

	printf("\nNow, Downloading [ADDRESS:%xh,TOTAL:%d]\n",
			downloadAddress, downloadFileSize);	

	j=0x80000;
	
#if USBDMA    
    while(totalDmaCount<downloadFileSize)
    {
    	if((rDCDST2-(U32)downloadAddress+8)>=j)
		{
		/*	static int led = 0x0;
			
			led ^=2;
			Led_Display(led);*/
	    	putch('d');	    	
   	    	j+=0x80000;
		}		
		if(getkey()==0x1b)
		{
			int i;
			
			ConfigEp3IntMode();
			DisableIrq(BIT_DMA2);
			DbgOut("\nReceive process aborted.\n");
			DbgOut("Received %d bytes\n", totalDmaCount);
			for(i=0;i<48;i++)
			{
				printf("0x%x = %x\n", downloadAddress+i*0x100000, *(U32 *)(downloadAddress+i*0x100000));
			}
			return;
		}
    }
#else
    while(((U32)downPt-downloadAddress)<(downloadFileSize-8))
    {
		if(((U32)downPt-downloadAddress)>=j)
		{
			static int led = 0x0;
			
			led ^= 2;
			Led_Display(led);
	    	putch('i');
	   	    j+=0x80000;
		}
		if(getkey()==0x1b)
		{
			int i;			
			
			DbgOut("\nReceive process aborted.\n");
			DbgOut("Received %d bytes\n", (U32)downPt-downloadAddress);			
			for(i=0;i<48;i++)
			{
				printf("0x%x = %x\n", downloadAddress+i*0x100000, *(U32 *)(downloadAddress+i*0x100000));
			}			
			return;
		}
    }
#endif    
    
#if USBDMA    
    /*******************************/
    /*     Verify check sum        */
    /*******************************/

    printf("Now, Checksum calculation\n");

    cs=0;    
    i=(downloadAddress);
    j=(downloadAddress+downloadFileSize-10)&0xfffffffc;
    while(i<j)
    {
    	temp=*((U32 *)i);
    	i+=4;
    	cs+=(U16)(temp&0xff);
    	cs+=(U16)((temp&0xff00)>>8);
    	cs+=(U16)((temp&0xff0000)>>16);
    	cs+=(U16)((temp&0xff000000)>>24);
    }

    i=(downloadAddress+downloadFileSize-10)&0xfffffffc;
    j=(downloadAddress+downloadFileSize-10);
    while(i<j)
    {
	  	cs+=*((U8 *)i++);
    }
    
    checkSum = cs;
#else
	//checkSum was calculated including dnCS. So, dnCS should be subtracted.
	checkSum=checkSum - *((unsigned char *)(downloadAddress+downloadFileSize-8-2))
				- *( (unsigned char *)(downloadAddress+downloadFileSize-8-1) );	
#endif	  
//PollRxEnd:
    dnCS=*((unsigned char *)(downloadAddress+downloadFileSize-8-2))+
	(*((unsigned char *)(downloadAddress+downloadFileSize-8-1))<<8);

    if(checkSum!=dnCS)
    {
		printf("Checksum Error!!! MEM:%x DN:%x\n",checkSum,dnCS);
		return;
    }
	
	DisableIrq(BIT_USBD);
	
	printf("Download O.K.\n");
    Uart_TxEmpty(0);

	wait_print_end();
{
/*	U8 extra_info[256];
	U16 extra_len;
	
	extra_len = *(U16 *)downloadAddress;
	if(extra_len>=sizeof(extra_info)) {
		printf("Extra info length too long > %d\n", sizeof(extra_info)-1);
		return;
	}

	for(i=0; i<extra_len; i++)
		extra_info[i] = *(U8 *)(downloadAddress+i);
	extra_info[i] = 0;
	
	downloadFileSize -= extra_len+10;
	for(i=0; i<downloadFileSize; i++)
		*(U8 *)(downloadAddress+i) = *(U8 *)(downloadAddress+i+extra_len);
	
	printf("%s\n", extra_info+sizeof(extra_len));*/
	downloadFileSize -= 10;
}	
	puts("\nDo you want to run? [y/n] : ");
	
		rGPGCON &= 0xfff3ffff;	//GPG9 input
		
	while(1) {		
		U8 key;
		
		key = getch();
		if(key=='y'||key=='Y') {		
			putch(key);
			putch('\n');		
		
			rINTMSK = BIT_ALLMSK;
			Delay(2000);
			call_linux(0, 193, downloadAddress);
		/*	{
				void (*fun)(void) = (void (*)(void))downloadAddress;
				(*fun)();
			}*/
    	}
    	if(key=='n'||key=='N') {    	
    		putch(key);
    		putch('\n');
    		break;
    	}
    }
}

⌨️ 快捷键说明

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