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

📄 sst39lf160.c

📁 Samsung S3c2440主板 Flash编程烧写软件
💻 C
字号:
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\def.h"


int SST39LF160_ProgFlash(U32 realAddr,U16 data);
void SST39LF160_EraseSector(U32 Sector);
void SST39LF160_EraseBlock(U32 Block);
void SST39LF160_EraseChip(void);
int SST39LF160_CheckId(void);
int SST39LF160BlankCheck(U32 Addr, U32 Size);
static int _WAIT(void);
extern void Led_Display(int LedStatus);

// Because S3C44B0X is connected to AM29LV800B,
// the addr parameter has to be a WORD address, so called in AMD specification.

#define _WR(addr,data)	*((U16 *)(addr<<1))=(U16)data	//the addr should be shifted
#define _RD(addr)	( *((U16 *)(addr<<1)) )	
#define _RESET()	_WR(0x0,0xf0)		
#define BADDR2WADDR(addr)   (addr/*>>1*/)			
extern U32 downloadAddress;
extern U32 downloadProgramSize;
extern U32 srcOffset;
extern U32 targetSize;

U32 targetAddress;
#define WordsPerSector (2048)
#define BytesPerSector (WordsPerSector*2)
#define BytesPerBlock  (BytesPerSector*16)
#define srcAddress 0xc100000
#define Block2BeWritten 480

void ProgramSST39LF160(void)
{
    U32 i;

    /*Uart_Printf("[Check SST39LF160]\n");
    if(!SST39LF160_CheckId())
    {
	Uart_Printf("ID Check Error!!!\n");
	return;
    }
    */
    
    Uart_Printf("\nErase the sector:0x%x.\n",targetAddress);
    
    targetAddress = BytesPerSector * 0;//==0x80000:512kbytes;

    //SST39LF160_EraseChip();
    for(i=0; i<31; i++) SST39LF160_EraseBlock(i);

    if(!SST39LF160BlankCheck(targetAddress, BytesPerSector * Block2BeWritten/*512*/))    {
	    Uart_Printf("After SST39LF160_EraseChip(), Blank Check Error!!!\n");
	    //return;
    }

    Uart_Printf("\nStart of the data writing.\n");

    for(i=0;i<BytesPerSector*Block2BeWritten;i+=2) 
    {
        SST39LF160_ProgFlash(  i+targetAddress, * ((U16*)(srcAddress+i) ) );
		if( (i / 3000)%4 == 0 ) Led_Display(1);
		else if( (i / 3000)%4 == 1 ) Led_Display(2);
		else if( (i / 3000)%4 == 2 ) Led_Display(4);
		else if( (i / 3000)%4 == 3 ) Led_Display(8);
    }

    Uart_Printf("\nEnd of the data writing!!!\n");

    _RESET();

    Uart_Printf("\nVerifying Start.\n");
    for(i=0;i<BytesPerSector*Block2BeWritten;i+=2) 
    {
        if(*( (U16 *)(i+targetAddress) ) != (U16)* ((U16*)(srcAddress+i) )   )
	    {    
	        Uart_Printf("%x=verify error\n",i+targetAddress);
	        //return;
            break;
	    }

        if((i%0x1000)==0)    Uart_Printf("%x ",i);
    }

    Uart_Printf("\nVerifying End!!!\n");

	Led_Display(0xf);
	Beep(0x1);
	Led_Display(0);
	Beep(0x0);

	( (void (*)(void)) (0x000000) )();

    Uart_Printf("Do you want another programming without additional download? [y/n]\n");
    if(Uart_Getch()=='y')
	    ProgramSST39LF160();
}

int SST39LF160_CheckId(void)
{
    U16 manId,devId;

    _RESET();
    
    _WR(0x555,0xaaaa);
    _WR(0x2aa,0x5555);
    _WR(0x555,0x9090);
    manId=_RD(0x0);

    _WR(0x555,0xaaaa);
    _WR(0x2aa,0x5555);
    _WR(0x555,0x9090);
    devId=_RD(0x1);

    _RESET();   

    Uart_Printf("Manufacture ID=%4x(0x0001), Device ID(0x225B)=%4x\n",manId,devId);

    if(manId==0x0001 && devId==0x225b)
	return 1;
    else 
	return 0;
}




void SST39LF160_EraseSector(U32 Sector)
{
        Uart_Printf("Sector Erase is started!\n");
        
        Sector = BADDR2WADDR( Sector<<11 );

        _RESET();

        _WR(0x5555,0xaa);
        _WR(0x2aaa,0x55);
        _WR(0x5555,0x80);
        _WR(0x5555,0xaa);
        _WR(0x2aaa,0x55);
        _WR(Sector,0x30);
        _WAIT();
        _RESET();
}


void SST39LF160_EraseBlock(U32 Block)
{
        Uart_Printf("Block Erase is started!\n");
        
        Block = BADDR2WADDR( Block<<15 );

        _RESET();

        _WR(0x5555,0xaa);
        _WR(0x2aaa,0x55);
        _WR(0x5555,0x80);
        _WR(0x5555,0xaa);
        _WR(0x2aaa,0x55);
        _WR(Block,0x50);
        _WAIT();
        _RESET();
}

void SST39LF160_EraseChip(void){

        Uart_Printf("Chip Erase is started!\n");
        
        _RESET();

        _WR(0x5555,0xaa);
        _WR(0x2aaa,0x55);
        _WR(0x5555,0x80);
        _WR(0x5555,0xaa);
        _WR(0x2aaa,0x55);
        _WR(0x5555,0x10);
        _WAIT();
        _RESET();
}

int SST39LF160BlankCheck(U32 Addr, U32 Size)
{
    U32 i,j;
    for(i=0;i<Size;i+=2)
    {
	    j=*((U16 *)(i+Addr));
	    if( j!=0xffff)
	    {
			Led_Display(0xf);
	        Uart_Printf("Blank Check Error:%x=%x\n",(i + Addr),j);
	        return 0;
	    }
		if( (i / 3000)%4 == 0 ) Led_Display(1);
		else if( (i / 3000)%4 == 1 ) Led_Display(2);
		else if( (i / 3000)%4 == 2 ) Led_Display(4);
		else if( (i / 3000)%4 == 3 ) Led_Display(8);
    }
    return 1;
}




static int _WAIT(void) //Check if the bit6 toggle ends.
{
    volatile U16 flashStatus,old;

    old=*((volatile U16 *)0x0);

    while(1)
    {
	    flashStatus=*((volatile U16 *)0x0);
	    if( (old&0x40) == (flashStatus&0x40) )
	        break;
	    old=flashStatus;
    }
    //Uart_Printf("!\n");
    return 1;
}




int SST39LF160_ProgFlash(U32 realAddr,U16 data)
{
	volatile U16 *tempPt;
        int temp,count=0;
        tempPt=(volatile U16 *)realAddr;
        _WR(0x5555,0xaa);
        _WR(0x2aaa,0x55);
        _WR(0x5555,0xa0);
        *tempPt=data;
        
	return _WAIT();
/*        while(1)
	{
	    temp=*tempPt;
            if(temp==data || count==100)break;
	    if(temp&0x20)
	    {
		count++;
	    }
        }
        if(count>0) Uart_Printf("Time out is occurred at %x\n",realAddress);
*/
}

⌨️ 快捷键说明

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