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

📄 am29lv800.c

📁 smdk40100 40mhz test code
💻 C
字号:
#include "..\include\k401.h"
#include "..\include\k401lib.h"
#include "..\include\def.h"

void AM29LV800_StartCmd(U16 cmd);
static void SectorProg_InputTargetAddr(void);
static void ChipProg_InputTargetAddr(void);
int AM29LV800_CheckID(void);
int AM29LV800_LoadData(void);
int AM29LV800_ChipErase(void);
int AM29LV800_SectorErase(U32 targetAddr);
int AM29LV800_BlankCheck(U32 targetAddr, U32 targetSize);
int AM29LV800_Program(U32 targetAddr, U32 limit, U32 srcAddr);
int AM29LV800_Verification(U32 targetAddr, U32 limit, U32 srcAddr);
void AM29LV800_SectorProg(void);
void AM29LV800_ChipProg(void);
int _WAIT(void);

#define _WR(addr,data)	*((U16 *)(addr<<1))=(U16)data
#define _RD(addr)	( *((U16 *)(addr<<1)) )
#define _RESET()	_WR(0x0,0xf0f0)
#define BADDR2WADDR(addr)   (addr>>1)

#define BaseAddress 0x8000000

U32 downloadAddress;
U32 downloadProgramSize;
U32 srcAddress;
U32 srcOffset;
U32 targetAddress;
U32 targetSize;

void *flashfunc[][2]=
{
    (void *)AM29LV800_SectorProg,	"Sector Program    ",
    (void *)AM29LV800_ChipProg,		"Chip Programming  ",
    0,0
};

void ProgramAM29LV800(void)
{
    int i;
    int temp0=0;
    int temp1=0;

    downloadAddress=(unsigned)malloc(0x400000);
    if(downloadAddress==0)return;

    temp0='q'-'a';
    temp1='Q'-'a';

    Uart_Printf("KS17C40100 Flash(AM29LV800BB) Download Program\n");
    Uart_Printf("Download the data to SDRAM by UART\n");
    if(!AM29LV800_LoadData())
    {
	Uart_Printf("CheckSum Error!!!");
	return;
    }
    Uart_Printf("\nDownload O.K.\n");

    while(1)
    {
	i=0;

	Uart_Printf("=========================================================================\n");
	while(1)
        {
	    Uart_Printf("%c: %s",'a'+i,flashfunc[i][1]);
	    i++;
	    if((int)(flashfunc[i][0])==0){Uart_Printf("\nq: Return to Main Menu\n");break;}
	    if((i%4)==0)Uart_Printf("\n");
	}
	Uart_Printf("=========================================================================\n");
    	Uart_Printf("Select the function to test?");
	i=Uart_Getch()-'a';
	
	if(i==temp0 || i==temp1) break;
        else
	{
	    Uart_Printf("\n");
	    if(i>=0 && (i<(sizeof(flashfunc)/8)) ) 
		( (void (*)(void)) (flashfunc[i][0]) )();
	}
    }
}

void AM29LV800_ChipProg(void)
{
    Uart_Printf("Flash full Programming is started!!!!\n");

    //Uart_Printf("Setting the Source Address, the Targat Address, the Target Size.\n");
    ChipProg_InputTargetAddr();
    srcAddress=downloadAddress+4;

    Uart_Printf("[ID Check]\n");
    if(!AM29LV800_CheckID())
    {
	Uart_Printf("ID Check Error!!!\n");
	return;
    }

    Uart_Printf("[Flash full Erase]\n");
    if(!AM29LV800_ChipErase())
    {
	Uart_Printf("Flash Erase Error!!!\n");
	return;
    }
    Uart_Printf("Flash Erase OK!!!\n");

    Uart_Printf("[Blank Ckeck] : ");
    if(!AM29LV800_BlankCheck(targetAddress, targetSize))
	Uart_Printf("Blank Check Error!!!\n");
    else Uart_Printf("Blank Check OK!!!\n");

    Uart_Printf("[Programming] : ");
    AM29LV800_Program(targetAddress, targetSize, (srcAddress+srcOffset));

    Uart_Printf("[Verification] : ");
    if(!AM29LV800_Verification(targetAddress, targetSize, (srcAddress+srcOffset)))
    {
	Uart_Printf("Flash Verification Error!!!\n");
	return;
    }

    Uart_Printf("Flash Programming completed!!!\n");
    Uart_Printf("Press 'RESET' button in SMDK40100 board....");
    while(1);
}

void AM29LV800_SectorProg(void)
{
    Uart_Printf("Flash full Programming is started!!!!\n");

    Uart_Printf("Setting the Source Address, the Targat Address, the Target Size.\n");
    SectorProg_InputTargetAddr();
    srcAddress=downloadAddress+4;

    Uart_Printf("[ID Check]\n");
    if(!AM29LV800_CheckID())
    {
	Uart_Printf("ID Check Error!!!\n");
	return;
    }

    Uart_Printf("[Sector Erase]\n");
    if(!AM29LV800_SectorErase(targetAddress))
    {
	Uart_Printf("Flash Erase Error!!!\n");
	return;
    }

    Uart_Printf("[Blank Ckeck]\n");
    if(!AM29LV800_BlankCheck(targetAddress, targetSize))
    {
	Uart_Printf("Blank Check Error!!!\n");
	return;
    }

    Uart_Printf("[Programming]\n");
    AM29LV800_Program(targetAddress, targetSize, (srcAddress+srcOffset));

    Uart_Printf("[Verification]\n");
    if(!AM29LV800_Verification(targetAddress, targetSize, (srcAddress+srcOffset)))
    {
	Uart_Printf("Flash Verification Error!!!\n");
	return;
    }

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

void AM29LV800_StartCmd(U16 cmd)
{
    _WR(0x555,0xaaaa);
    _WR(0x2aa,0x5555);
    _WR(0x555,cmd);
}

int AM29LV800_CheckID(void)
{
    U16 manId,devId;

    _RESET();

    AM29LV800_StartCmd(0x9090);
    manId=_RD(0x0);

    _RESET();

    AM29LV800_StartCmd(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;
}

int AM29LV800_LoadData(void)
{
    int i;
    U16 checkSum=0,dnCS;
    U32 fileSize=10;
    U8 *downPt;

    downPt=(U8 *)downloadAddress;
   
    Uart_Printf("downloadAddress=%x\n",downloadAddress);

    Uart_Printf("Download the plain binary file(.BHC) to be written\n");
    Uart_Printf(".BHC file format: <n+6>(4)+(n)+CS(2)\n");
    Uart_Printf("To transmit .BHC file    : wkocm2 xxx.BHC /2 /g /d:1\n");
    Uart_Printf("Or, to transmit .BIN file: wkocm2 xxx.BIN /2 /d:1\n");
    Uart_Printf("Download methods: COM:8Bit,NP,1STOP\n");
 
    Uart_Printf("STATUS:");

    RdURXH();			//To remove overrun error state.

    i=0;
    while(i<fileSize)
    {
	while(!(rUSTAT&0x20));
	*(downPt+i)=RdURXH();
	if(i==3)
	{
	    fileSize=*((U8 *)(downloadAddress+0))+
		(*((U8 *)(downloadAddress+1))<<8)+
		(*((U8 *)(downloadAddress+2))<<16)+
		(*((U8 *)(downloadAddress+3))<<24);
	}
	
	if((i%1000)==0)WrUTXH('#');
	i++;
    }

    downloadProgramSize=fileSize-6;

    for(i=4;i<(fileSize-2);i++)
    {
	checkSum+=*((U8 *)(i+downloadAddress));
    }

    dnCS=*((U8 *)(downloadAddress+fileSize-2))+
         (*( (U8 *)(downloadAddress+fileSize-1) )<<8);

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

static void SectorProg_InputTargetAddr(void)
{
    Uart_Printf("[AM29LV800BB Writing Program]\n");

    Uart_Printf("CAUTION: Check AM29LV800BB BYTE#(47) pin is connected to VDD.\n");
    Uart_Printf("         On some SMDK40100 B/D,the BYTE# pin is connected by JP9.\n"); 

    Uart_Printf("Source size:0h~%xh\n",downloadProgramSize);

    Uart_Printf("Available Target/Source Address:\n"); 
    Uart_Printf("    0h, 4000h, 6000h, 8000h,10000h,20000h,30000h,40000h,50000h,60000h\n");
    Uart_Printf("70000h,80000h,90000h,a0000h,b0000h,c0000h,d0000h,e0000h,f0000h\n");

    Uart_Printf("Input source offset:");
    srcOffset=Uart_GetIntNum();
    Uart_Printf("Input target address among above addresses:");
    targetAddress=Uart_GetIntNum()+BaseAddress;
    
    if(targetAddress<(0x4000+BaseAddress))targetSize=0x4000;
    else if(targetAddress<(0x6000+BaseAddress))targetSize=0x2000;
    else if(targetAddress<(0x8000+BaseAddress))targetSize=0x2000;
    else if(targetAddress<(0x10000+BaseAddress))targetSize=0x8000; 
    else targetSize=0x10000;

    Uart_Printf("source offset=0x%x\n",srcOffset);
    Uart_Printf("target address=0x%x\n",targetAddress);
    Uart_Printf("target block size=0x%x\n",targetSize);
}

static void ChipProg_InputTargetAddr(void)
{
    Uart_Printf("[AM29LV800BB Writing Program]\n");

    Uart_Printf("CAUTION: Check AM29LV800BB BYTE#(47) pin is connected to VDD.\n");
    Uart_Printf("         On some SMDK40100 B/D,the BYTE# pin is connected by JP9.\n"); 

    //Uart_Printf("Source size:0h~%xh\n",downloadProgramSize);

    srcOffset=0x0;
    targetAddress=BaseAddress;
    targetSize=downloadProgramSize;

    //Uart_Printf("source offset=0x%x\n",srcOffset);
    //Uart_Printf("target address=0x%x\n",targetAddress);
    //Uart_Printf("target block size=0x%x\n",targetSize);
}

int AM29LV800_ChipErase(void)
{
    int isYes=0;

    Uart_Printf("Chip Erase is started!!! : ");

    _RESET();

    AM29LV800_StartCmd(0x8080);

    _WR(0x555,0xaaaa);
    _WR(0x2aa,0x5555);
    _WR(0x555,0x1010);

    isYes = _WAIT();

    _RESET();

    if(isYes==1) return 1;
    else return 0;
}

int AM29LV800_SectorErase(U32 targetAddr)
{
    int isYes=0;

    Uart_Printf("Sector Erase is started!\n");

    _RESET();

    AM29LV800_StartCmd(0x8080);
    _WR(0x555,0xaaaa);
    _WR(0x2aa,0x5555);
    _WR(BADDR2WADDR(targetAddr),0x3030);

    isYes = _WAIT();

    _RESET();

    if(isYes==1) return 1;
    else return 0; 
}

int AM29LV800_BlankCheck(U32 targetAddr, U32 targetSize)
{
    int i,j;
    for(i=0;i<targetSize;i+=2)
    {
	j=*((U16 *)(i+targetAddr));
	if( j!=0xffff) return 0;
    }
    return 1;
}

int AM29LV800_Program(U32 targetAddr, U32 limit, U32 srcAddr)
{
    volatile U16 *tempPt;
    int i;
    U16 data;

    for(i=0; i<limit; i+=2)
    {
	tempPt=(volatile U16 *)(targetAddr+i);
	data=*((U16 *)(srcAddr+i));

	AM29LV800_StartCmd(0xa0a0);
	*tempPt=data;

	_WAIT();

	if((i%0x1000)==0)Uart_Printf("%x ",i);
    }
    Uart_Printf("End of the data writing!!!\n");

    _RESET();
}

int AM29LV800_Verification(U32 targetAddr, U32 limit, U32 srcAddr)
{
    int i=0;
    volatile U16 *tempPt;
    volatile U16 data;

    //Uart_Printf("Verifying Start. : ");

    for(i=0; i<limit; i+=2)
    {
	tempPt=(volatile U16 *)(targetAddr+i);
	data=*((U16 *)(srcAddr+i));

	if(*(tempPt) != data)
	{
	    Uart_Printf("Error Address=0x%x Rael Data=0x%x Error Data=0x%x\n",tempPt,*tempPt,data);
	    //return 0;
	}
	if((i%0x1000)==0)Uart_Printf("%x ",i);
    }
    Uart_Printf("verifying OK!!!\n");
    return 1;
}

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

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

    while(1)
    {
	flashStatus=*((volatile U16 *)0x8000000);
	if( (old&0x40) == (flashStatus&0x40) )break;
	if( flashStatus&0x20 )
	{
	    old=*((volatile U16 *)0x0);
	    flashStatus=*((volatile U16 *)0x0);
	    if( (old&0x40) == (flashStatus&0x40) )
		return 1;
	    else return 0;
	}
	old=flashStatus;
    }
    return 1;
}

⌨️ 快捷键说明

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