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

📄 usb_monitor.c

📁 USB-TEST是USB通信的例子
💻 C
字号:
/*********************************************************************************************
* File:		usb_monitor.c
* Author:	embest
* Desc:		Usb monitor entry point,menu,download
*			To measure up to large time, The WDT interrupt is used.
* History:	
*			purnnamu, Sep.03.2002
*				To remove the power noise in the USB signal, the unused CLKOUT0,1 is disabled
*			R.X.Huang, March 12, 2005
*				Programming modify, style of the program: nomenclature, commentary
*			Y.J.Guo, April 28, 2005
*				Modifying and reusing  of S3C2410X u24xmon
*********************************************************************************************/

/*------------------------------------------------------------------------------------------*/
/*                                     include files	                                    */
/*------------------------------------------------------------------------------------------*/
#include <string.h>

#include "2410lib.h"
#include "mmu.h"
#include "profile.h"
#include "usbmain.h"
#include "usbout.h"
#include "usblib.h"
#include "2410usb.h"

/*------------------------------------------------------------------------------------------*/
/*                                     global variables                                     */
/*------------------------------------------------------------------------------------------*/
extern char Image_RW_Limit[];
extern void (*run)(void);

volatile UINT32T downloadAddress;
volatile unsigned char *downPt;
volatile UINT32T downloadFileSize;
volatile UINT16T checkSum;
volatile unsigned int err=0;
volatile UINT32T totalDmaCount;
volatile int isUsbdSetConfiguration;

int download_run=0;
int menuUsed=0;
int consoleNum;
UINT32T *pMagicNum=(UINT32T *)Image_RW_Limit;
UINT32T tempDownloadAddress;

/*------------------------------------------------------------------------------------------*/
/*                                     function declare                                     */
/*------------------------------------------------------------------------------------------*/
void usb_isr_init(void);
void HaltUndef(void);
void HaltSwi(void);
void HaltPabort(void);
void HaltDabort(void);
void Lcd_Off(void);
void down_wait(void);
void down_menu(void);

/*********************************************************************************************
* name:		usb_memory_test
* func:		memory operation
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void usb_memory_test(void)
{
    int i;
    UINT32T data;
    int memError=0;
    UINT32T *pt;
    
    // memory test
    Uart_Printf("Memory Test(%xh-%xh):WR",_RAM_STARTADDRESS,(_ISR_STARTADDRESS&0xfff0000));

    pt=(UINT32T *)_RAM_STARTADDRESS;
    while((UINT32T)pt<(_ISR_STARTADDRESS&0xffff0000))
    {
		*pt=(UINT32T)pt;
		pt++;
   }

    Uart_Printf("\b\bRD");
    pt=(UINT32T *)_RAM_STARTADDRESS;
	
    while((UINT32T)pt<(_ISR_STARTADDRESS&0xffff0000))
    {
		data=*pt;
		if(data!=(UINT32T)pt)
		{
		    memError=1;
		    Uart_Printf("\b\bFAIL:0x%x=0x%x\n",i,data);
		    break;
		}
		pt++;
   }

    if(memError==0)Uart_Printf("\b\bO.K.\n");
}

/*********************************************************************************************
* name:		usb_monitor
* func:		usb operation main code
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void usb_monitor(void)
{
    char *mode;
    int i;
    UINT8T key;

    ChangeClockDivider(1,1);    // 1:2:4    
    //ChangeMPllValue(82,2,1);  //FCLK=135.0Mhz     
    //ChangeMPllValue(82,1,1);    //FCLK=180.0Mhz     
    ChangeMPllValue(161,3,1); //FCLK=202.8Mhz     
    
    Port_Init();
    
    rGPHCON = rGPHCON&~(0xf<<18)|(0x5<<18);   
    //To enhance the USB signal quality.
    //CLKOUT 0,1=OUTPUT to reduce the power consumption.
    
    usb_isr_init();
    if(*pMagicNum!=0x12345678)
    	consoleNum=0;
    else
    	consoleNum=1;
    	
    Uart_Init(0,115200);
    Uart_Select(consoleNum);

    rMISCCR=rMISCCR&~(1<<3);                                    // USBD is selected instead of USBH1 
    rMISCCR=rMISCCR&~(1<<13);                                   // USB port 1 is enabled.

    // USBD should be initialized first of all.
    isUsbdSetConfiguration=0;

#if 0
    UsbdMain(); 
    MMU_Init();                                                 //MMU should be reconfigured or turned off for the debugger, 
    //After downloading, MMU should be turned off for the MMU based program,such as WinCE.	
#else
    //MMU_EnableICache();  
    UsbdMain(); 
#endif
    Delay(0);                                                   //calibrate Delay()
    
    pISR_SWI=(_ISR_STARTADDRESS+0xf0);                          //for pSOS

    Led_Display(0x6);

#if USBDMA
    mode="DMA";
#else
    mode="Int";
#endif

    Uart_Printf("\n\n");
    Uart_Printf("+---------------------------------------------+\n");
    Uart_Printf("| S3C2410X USB Downloader ver R1.11 April/28/05 |\n");
    Uart_Printf("+---------------------------------------------+\n");
    Uart_Printf("FCLK=%dMHz,%s mode\n",FCLK/1000000,mode); 
    Uart_Printf("USB: IN_ENDPOINT:1 OUT_ENDPOINT:3\n"); 
    Uart_Printf("FORMAT: <ADDR(DATA):4>+<SIZE(n+10):4>+<DATA:n>+<CS:2>\n");
    Uart_Printf("NOTE: 1. Power off/on or press the reset button for 1 sec\n");
    Uart_Printf("         in order to get a valid USB device address.\n");
    Uart_Printf("      2. For additional menu, Press any key. \n");
    Uart_Printf("\n");

    download_run=1;                                             //The default menu is the Download & Run mode.

    while(1)
    {
    	if(menuUsed==1)down_menu();
    	down_wait();    
   }

}

/*********************************************************************************************
* name:		down_menu
* func:		download selection menu
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void down_menu(void)
{
    int i;
    UINT8T key;
    menuUsed=1;
    while(1)
    {
        Uart_Printf("\n###### Select Menu ######\n");
        //Uart_Printf(" [0] Download & Run\n");
        Uart_Printf(" [1] Download Only\n");
        Uart_Printf(" [2] Test SDRAM \n");
        Uart_Printf(" [3] Change The Console UART Ch.\n");
        key=Uart_Getch();
        
        switch(key)
        {
        //case '0':
        //    Uart_Printf("\nDownload&Run is selected.\n\n");
        //    download_run=1;
        //    return;
        case '1':
            Uart_Printf("\nDownload Only is selected.\n");
            Uart_Printf("Enter a new temporary download address(0x3...):");
            tempDownloadAddress=Uart_GetIntNum();
            download_run=0;
            Uart_Printf("The temporary download address is 0x%x.\n\n",tempDownloadAddress);
            return;
        case '2':
            Uart_Printf("\nMemory Test is selected.\n");        
	        usb_memory_test();
	        down_menu();
	    return;
            break;
        case '3':
            Uart_Printf("\nWhich UART channel do you want to use for the console?[0/1]\n");
            if(Uart_Getch()!='1')
            {
	    	*pMagicNum=0x0;
		Uart_Printf("UART ch.0 will be used for console at next boot.\n");        	    	
	   }
	    else
	    {
	        *pMagicNum=0x12345678;
 		Uart_Printf("UART ch.1 will be used for console at next boot.\n");        
                Uart_Printf("UART ch.0 will be used after long power-off.\n");
	   }
            Uart_Printf("System is waiting for a reset. Please, Reboot!!!\n");
            while(1);
            break;

        default:
            break;
	}	
   }	    

}

/*********************************************************************************************
* name:		down_wait
* func:		waiting download
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void down_wait(void)
{
    UINT32T i;
    UINT32T j;
    UINT16T cs;
    UINT32T temp;
    UINT16T dnCS;
    int first=1;
    float time;
    UINT8T tempMem[16];
    UINT8T key;
    
    checkSum=0;
    downloadAddress=(UINT32T)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    */
    /*******************************/
    j=0;

    if(isUsbdSetConfiguration==0)
    {
	Uart_Printf("USB host is not connected yet.\n");
   }

    while(downloadFileSize==0)
    {
        if(first==1 && isUsbdSetConfiguration!=0)
        {
            Uart_Printf("USB host is connected. Waiting a download.\n");
            first=0;
       }

	if(j%0x50000==0)Led_Display(0x6);
	if(j%0x50000==0x28000)Led_Display(0x9);
	j++;

	key=Uart_GetKey();
	if(key!=0)
	{
	    down_menu();
            first=1;                                            //To display the message,"USB host ...."
	}

   }

    timer_initex();      
    timer_startex();  

#if USBDMA    

    rINTMSK &=~(BIT_DMA2);  

    ClearEp3OutPktReady(); 
    	// indicate the first packit is processed.
    	// has been delayed for DMA2 cofiguration.

    if(downloadFileSize>EP3_PKT_SIZE)
    {
        if(downloadFileSize<=(0x80000))
        {
      	    ConfigEp3DmaMode(downloadAddress+EP3_PKT_SIZE-8,downloadFileSize-EP3_PKT_SIZE);	
  	    //wait until DMA reload occurs.
	    while((rDSTAT2&0xfffff)==0);

      	    //will not be used.
       	    rDIDST2=(downloadAddress+downloadFileSize-EP3_PKT_SIZE);  
       	    rDIDSTC2=(0<<1)|(0<<0);  
	    rDCON2=rDCON2&~(0xfffff)|(0);        	    
		}
		else
		{
			ConfigEp3DmaMode(downloadAddress+EP3_PKT_SIZE-8,0x80000-EP3_PKT_SIZE);
			//wait until DMA reload occurs.
			while((rDSTAT2&0xfffff)==0);
	      	    
			if(downloadFileSize>(0x80000*2))                    //for 1st autoreload
			{
				rDIDST2=(downloadAddress+0x80000-8);                //for 1st autoreload.
				rDIDSTC2=(0<<1)|(0<<0);  
				rDCON2=rDCON2&~(0xfffff)|(0x80000);        	  
	
				while(rEP3_DMA_TTC<0xfffff)
				{
					rEP3_DMA_TTC_L=0xff; 
					rEP3_DMA_TTC_M=0xff;
					rEP3_DMA_TTC_H=0xf;
				}
			}	
	 	    else
	 	    {
	 	        rDIDST2=(downloadAddress+0x80000-8);                //for 1st autoreload.
	      	        rDIDSTC2=(0<<1)|(0<<0);  
	 	        rDCON2=rDCON2&~(0xfffff)|(downloadFileSize-0x80000); 		
	
		  		while(rEP3_DMA_TTC<0xfffff)
		  		{
		  		    rEP3_DMA_TTC_L=0xff; 
		  		    rEP3_DMA_TTC_M=0xff;
		  		    rEP3_DMA_TTC_H=0xf;
		  		}
			}
		}
 		totalDmaCount=0;
	}
    else
	{
		totalDmaCount=downloadFileSize;
	}
#endif

    Uart_Printf("\nNow, Downloading [ADDRESS:%xh,TOTAL:%d]\n",
    		downloadAddress,downloadFileSize);
    Uart_Printf("RECEIVED FILE SIZE:%8d",0);

#if USBDMA    
    j=0x10000;

    while(1)
	{
    	if( (rDCDST2-(UINT32T)downloadAddress+8)>=j)
		{
			Uart_Printf("\b\b\b\b\b\b\b\b%8d",j);
			j+=0x10000;
		}
		if(totalDmaCount>=downloadFileSize)break;
	}

#else
    j=0x10000;

    while(((UINT32T)downPt-downloadAddress)<(downloadFileSize-8))
    {
	if( ((UINT32T)downPt-downloadAddress)>=j)
	{
	    Uart_Printf("\b\b\b\b\b\b\b\b%8d",j);
   	    j+=0x10000;
	}
   }
#endif
    time=timer_stopex();
    Uart_Printf("\b\b\b\b\b\b\b\b%8d",downloadFileSize);	
    Uart_Printf("(%5.1fKB/S,%3.1fS)\n",(float)(downloadFileSize/time/1000.),time);
    
#if USBDMA    
    /*******************************/
    /*     Verify check sum        */
    /*******************************/

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

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

    i=(downloadAddress+downloadFileSize-10)&0xfffffffc;
    j=(downloadAddress+downloadFileSize-10);
    while(i<j)
    {
  	cs+=*((UINT8T *)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	  

    dnCS=*((unsigned char *)(downloadAddress+downloadFileSize-8-2))+
	(*( (unsigned char *)(downloadAddress+downloadFileSize-8-1) )<<8);

    if(checkSum!=dnCS)
    {
	Uart_Printf("Checksum Error!!! MEM:%x DN:%x\n",checkSum,dnCS);
	return;
   }

    Uart_Printf("Download O.K.\n\n");
    Uart_TxEmpty(consoleNum);


    if(download_run==1)
    {
        rINTMSK = BIT_ALLMSK;
    	run=(void (*)(void))DOWNLOAD_ADDRESS;
		run();
   }
}

/*********************************************************************************************
* name:		usb_isr_init
* func:		initialize interrupt settings for usb
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void usb_isr_init(void)
{

#if 1
    pISR_USBD =(unsigned)IsrUsbd;
    pISR_DMA2 =(unsigned)IsrDma2;
#else
    pISR_IRQ =(unsigned)IsrUsbd;	
    	//Why doesn't it receive the big file if use this. (???)
    	//It always stops when 327680 bytes are received.
#endif    
    ClearPending(BIT_DMA2);
    ClearPending(BIT_USBD);
    //rINTMSK&=~(BIT_USBD);  
   
    //pISR_FIQ,pISR_IRQ must be initialized
}


⌨️ 快捷键说明

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