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

📄 main.c

📁 EP9315的wince下载程序。download.exe
💻 C
字号:
//****************************************************************************
//
// MAIN.C - The architecture for program the program via serial, ethernet or
//          usb slave chip.
//
// Copyright (c) 2006 Cirrus Logic, Inc.
//
//****************************************************************************
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

#include "ep93xx.h"
#include "serial.h"
#include "timer.h"

#include "flash.h"
#include "intel.h"
#include "amd.h"

#include "spi.h"
#include "at25f1024.h"
#include "sst25vf020.h"

//****************************************************************************
// USE_SEMIHOSTING
// Macro definitions to handle using semihosted prints as your console
//****************************************************************************
#define USE_SEMIHOSTING

#ifdef USE_SEMIHOSTING

#define CHECK_SEMIHOSTING()
#define PRINT_MSG(msg) printf(msg)
#define GET_MSG(msg) gets(msg)

#else

#define CHECK_SEMIHOSTING() __use_no_semihosting_swi()

#endif

#define _DBG


#define EEPROM_MAX 8

static struct eeprom eeprom_ops[EEPROM_MAX]={
    {ATMEL_ID, ATMEL_AT25F1024, init_at25f1024, query_at25f1024, erase_at25f1024, program_at25f1024,read_at25f1024},
    {SST_ID, SST_SST25VF020, init_sst25vf020, query_sst25vf020, erase_sst25vf020, program_sst25vf020,read_sst25vf020},
    {SST_ID, SST_SST25VF040B, init_sst25vf020, query_sst25vf040, erase_sst25vf040, program_sst25vf020,read_sst25vf020} 
    };


struct FlashOps sFlashOps[]={
    // AMD operation function pointer! 
    {
        AmdFlashQuery,
        AmdFlashErase,
        AmdFlashProgram
    },

    // INTEL operation function pointer! 
    {
        IntelFlashQuery,
        IntelFlashErase,
        IntelFlashProgram
    },
    
    // Add your operation function pointer!


    // The last one indicate the termination
    {
        0,
        0,
        0
    }
};

//****************************************************************************
// Function:    flash_operate()
// Description: Erase and Program the nor flash.
//****************************************************************************
void flash_operate(void)
{
    struct FlashInfo sFlashInfo;
    struct CFIQuery sQuery;
    struct FlashOps *spFlashOps = sFlashOps;
    unsigned char *pBuffer;
    long iOffSet, iFileSize, iCheckSum;
    char data[1024];
    int index = 0;
    char cErase=0;

    if ((in(0x8093009C) & BIT7))
    {
        // The system is boot with 32bit width.
        sFlashInfo.ByteWidth = 2;
        sFlashInfo.FlashBase = 0x60000000;
        sFlashInfo.pQuery = &sQuery;
    }
    else
    {
        // The system is boot with 16bit width.
        sFlashInfo.ByteWidth = 1;
        sFlashInfo.FlashBase = 0x60000000;
        sFlashInfo.pQuery = &sQuery;
    }

    if(!sFlashOps[0].FlashQuery(&sFlashInfo))
    {
        // The flash is amd compatible serial.
        spFlashOps = &sFlashOps[0];
        
#ifdef _DBG 
        iOffSet = 0x00;     
        iFileSize = 0x20000;
        for(index=0; index<1024; index++)
        {
            data[index] = 0xaa;
        }
        spFlashOps->FlashErase(&sFlashInfo, iOffSet, iFileSize);
        spFlashOps->FlashProgram(&sFlashInfo, iOffSet, (void *)data, 1024);
#endif

        Serial_SendChar('A');
        Serial_SendChar((sFlashInfo.ManufactureId)&0xff);
        Serial_SendChar((sFlashInfo.ManufactureId>>8)&0xff);
        Serial_SendChar((sFlashInfo.DeviceId[0])&0xff);
        Serial_SendChar((sFlashInfo.DeviceId[0]>>8)&0xff);
    } else if(!sFlashOps[1].FlashQuery(&sFlashInfo))
    {
        // The flash is intel compatible serial.
        spFlashOps = &sFlashOps[1];
        
#ifdef _DBG 
        iOffSet = 0x00;     
        iFileSize = 0x20000;
        for(index=0; index<1024; index++)
        {
            data[index] = 0xaa;
        }
        spFlashOps->FlashErase(&sFlashInfo, iOffSet, iFileSize);
        spFlashOps->FlashProgram(&sFlashInfo, iOffSet, (void *)data, 1024);
#endif

        Serial_SendChar('I');
        Serial_SendChar((sFlashInfo.ManufactureId)&0xff);
        Serial_SendChar((sFlashInfo.ManufactureId>>8)&0xff);
        Serial_SendChar((sFlashInfo.DeviceId[0])&0xff);
        Serial_SendChar((sFlashInfo.DeviceId[0]>>8)&0xff);
        
    } else
    {
        Serial_SendChar('X');
    }
    
    //
    // Get the image offset and size.
    //
    pBuffer = (unsigned char *)&iOffSet;
    pBuffer[0] = Serial_GetChar();
    pBuffer[1] = Serial_GetChar();
    pBuffer[2] = Serial_GetChar();
    pBuffer[3] = Serial_GetChar();

    pBuffer = (unsigned char *)&iFileSize;
    pBuffer[0] = Serial_GetChar();
    pBuffer[1] = Serial_GetChar();
    pBuffer[2] = Serial_GetChar();
    pBuffer[3] = Serial_GetChar();

    cErase = Serial_GetChar();
    //
    // Begin to erase the flash.
    // Send '!' if flash has been erased!
    //
    if(iOffSet>sQuery.DeviceSize)
    {
    	Serial_SendChar('%');
    	return;
    }
    
    if((iFileSize+iOffSet)>sQuery.DeviceSize)
    {
    	Serial_SendChar('&');
    	return;
    }
    
    if(cErase=='R')
    {
    	if(!spFlashOps->FlashErase(&sFlashInfo, iOffSet, iFileSize))
        {
            Serial_SendChar('!');
        }
        else
        {
            Serial_SendChar('=');
        }
    }
    
    //
    // Begin to receive the data and write to flash.
    //
    do
    {
        do
        {
            for(index=0; index<1024; index++)
            {
                data[index] = Serial_GetChar();
            }

            pBuffer = (unsigned char *)&iCheckSum;
            pBuffer[0] = Serial_GetChar();
            pBuffer[1] = Serial_GetChar();
            pBuffer[2] = Serial_GetChar();
            pBuffer[3] = Serial_GetChar();

            for(index=0; index<1024; index++)
            {
                iCheckSum -= data[index];
            }
            if (iCheckSum != 0) Serial_SendChar('@');

        } while(iCheckSum != 0);

        if(spFlashOps->FlashProgram(&sFlashInfo, iOffSet, (void *)data, 1024)!=0)
        {
            //
            //program fail
            //
	    Serial_SendChar('^');
	}
	else
	{	
	    //
	    //program sucessfully
	    //
	    Serial_SendChar('#');
	    iOffSet += 1024;
            iFileSize -= 1024;    
	}
	
        //
        // Receive the next packet.
        //
        //Serial_SendChar('#');
        //iOffSet += 1024;
        //iFileSize -= 1024;
    } while ( iFileSize > 0);

    //
    // Send '*' to exit.
    //
    Serial_SendChar('*');
}



//****************************************************************************
// Function:    eeprom_operate()
// Description: Erase and Program the eeprom.
//****************************************************************************
void eeprom_operate(int iRead)
{   
    int OffSet, FileSize, CheckSum;
    int temp,tmp;
    int i,j,k;
    unsigned char data[1024];
    int index = 0;
    unsigned char *pBuffer;
    int found_eeprom = EEPROM_MAX + 1;
    unsigned long ulRet=0;
    char cErase=0;

     for(i=0; i<EEPROM_MAX; i++)
      { 
        eeprom_ops[i].init();  
        tmp = eeprom_ops[i].query(eeprom_ops[i].manufacture_id,eeprom_ops[i].device_id);
        if (1 == tmp) 
           {
             found_eeprom = i;
             if(i==0)
                 Serial_SendChar('O');
             else if(i==1)
                 Serial_SendChar('S');
             else if(i==2)
                 Serial_SendChar('S');                 
             break;
           }  
            
      }
    
         
    if ( (EEPROM_MAX + 1) == found_eeprom)
       {
         Serial_SendChar('X');
       }
     
    //
    // Get the image offset and size.
    //
    pBuffer = (unsigned char *)&OffSet;
    pBuffer[0] = Serial_GetChar();
    pBuffer[1] = Serial_GetChar();
    pBuffer[2] = Serial_GetChar();
    pBuffer[3] = Serial_GetChar();

    pBuffer = (unsigned char *)&FileSize;
    pBuffer[0] = Serial_GetChar();
    pBuffer[1] = Serial_GetChar();
    pBuffer[2] = Serial_GetChar();
    pBuffer[3] = Serial_GetChar();


    if(iRead==0)
    {
        cErase = Serial_GetChar();
    }
    //
    //erase eeprom
    //
   if((iRead==0)&&(cErase=='R'))
   {
    	temp = eeprom_ops[found_eeprom].erase(OffSet,FileSize);
    }
    else
    {
    	temp = 1;	
    }
    
    //
    //sucess
    //
    if ( 1 == temp) 
    {
    	Serial_SendChar('!');
    }// image +offset >eeprom size
    else if(0 == temp)
    {
    	Serial_SendChar('&');
    }//offset>eeprom size
    else if(2 == temp)
    {
    	Serial_SendChar('%');
    	
    }

    if(iRead==1)
    {
    	k=FileSize;
    	for(j=0; j<FileSize; j+=1024)
    	{
		if(FileSize>1024)
		{
			k=FileSize-1024;
			i=1024;
		}
		else
		{
			i=k;	
		}
		
	        for(index=0; index<i; index=index+4)
        	{
 			ulRet = eeprom_ops[found_eeprom].read((OffSet+j+index),data,1024);
            		Serial_SendChar(ulRet&0xff);
            		Serial_SendChar((ulRet>>8)&0xff);
            		Serial_SendChar((ulRet>>16)&0xff);
            		Serial_SendChar((ulRet>>24)&0xff);
          		
       	 	}
       	}
       	
	//
    	// Send '*' to exit.
    	//
    	Serial_SendChar('*');
    	return;
        
    }
        
    //
    // Begin to receive the data and write
    //
    for(j=0; j<FileSize; j+=1024)
    {
        for(index=0; index<1024; index++)
        {
            data[index] = Serial_GetChar();
        }

        pBuffer = (unsigned char *)&CheckSum;
        pBuffer[0] = Serial_GetChar();
        pBuffer[1] = Serial_GetChar();
        pBuffer[2] = Serial_GetChar();                                                                                                                                                                           
        pBuffer[3] = Serial_GetChar();


        for(index=0; index<1024; index++)
        {
            temp = data[index] & 0xff;
            CheckSum -= temp;
        }

        if (CheckSum !=0) Serial_SendChar('@');

        if(eeprom_ops[found_eeprom].program((OffSet + j ),data,1024)==-1)
        {
            //
            //program fail
            //
	    Serial_SendChar('^');
	    j = j-1024;
	}
	else
	{	
	    //
	    //program sucessfully
	    //
	    Serial_SendChar('#');
        
	}
        //Serial_SendChar('#');
    }

    //
    // Send '*' to exit.
    //
    Serial_SendChar('*');
}



//****************************************************************************
// func: main()
// C function entry.
//****************************************************************************
int main()
{
    char cChar;

    //
    // Reinitialize the uart 1 to 115200 baud rate and no fifo
    //
    Serial_Init(3, 0);
    enable_debugtimer();

    while(1)
    {
        //
        // Read the character from the serial port.
        //
        cChar = Serial_GetChar();

        switch (cChar)
        {
            case '-':
                //
                // Program the nor FLASH.
                //
                clr(PEDR, 0x3);
                Serial_SendChar('?');
                break;

            case 'F':
                //
                // Program the nor FLASH.
                //
                clr(PEDR, 0x3);
                flash_operate();
                //while(1)
                //{
                //    set(PEDR, 0x1);
                //    delay_msec(500);
                //    clr(PEDR, 0x1);
                //    delay_msec(500);
                //}
                break;

            case 'S':
                //
                // Program the spi EEPROM.
                //
                clr(PEDR, 0x3);
                eeprom_operate(0);
                //while(1)
                //{
                //   set(PEDR, 0x1);
                //    delay_msec(500);
                //    clr(PEDR, 0x1);
                //    delay_msec(500);
                //}
                break;
                
            case 'R':
                //
                // Program the spi EEPROM.
                //
                clr(PEDR, 0x3);
                eeprom_operate(1);
                //while(1)
                //{
                //    set(PEDR, 0x1);
                //    delay_msec(500);
                //    clr(PEDR, 0x1);
                //    delay_msec(500);
                //}
                break;
            default:
                //
                // No command.
                //
                //while(1)
                //{
                //    set(PEDR, 0x1);
                //    delay_msec(500);
                //    clr(PEDR, 0x1);
                //    delay_msec(500);
                //}                
                break;
        }
    }

    return 0;

}

⌨️ 快捷键说明

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