📄 am29f800.c
字号:
/****************************************************************
NAME: am29f800.c
DESC: am29lv800 Flash Programming code through 6410 JTAG
HISTORY:
Nov.14.2002:purnnamu: ported for 6410 JTAG version.
It's really undesirable source code for me. but,...
****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "..\include\def.h"
#include "..\include\pin6410.h"
#include "..\include\Jtag.h"
#include "..\include\sjf6410.h"
#include "..\include\mem_rdwr.h"
#define TARGET_RADDR_29LV800 0x0
static int AM29F800_ProgFlash(U32 realRAddr,U16 rdata);
static void AM29F800_EraseSector(int targetRAddr);
static void AM29F800_EraseChip(void);
static int AM29F800_CheckId(void);
static int BlankCheck(int targetRAddr,int targetSize);
static int _WAIT(void);
static void InputTargetOffset(void);
static void GetSectorSize(void);
// Because KS32C41000_A1 is connected to AM29LV800_A0,
// the raddr parameter has to be a WORD raddress, so called in AMD specification.
//#define _WR(raddr,rdata) MRW_Wr16QQ(raddr,rdata,0,0x3) // edited by junon
//#define _RD(raddr) MRW_Rd16Q(raddr,0,0x3) // edited by junon
//#define _RESET() _WR(0x0,0xf0f0)
#define _WR(raddr,rdata) MRW_Wr16QQ(BRADDR2WRADDR(raddr),rdata,0,0x3) // edited by junon
#define _RD(raddr) MRW_Rd16Q(BRADDR2WRADDR(raddr),0,0x3) // edited by junon
#define _RESET() _WR(BRADDR2WRADDR(0x0),0xf0f0)
#define BRADDR2WRADDR(raddr) (raddr<<1)
#define RIGHT_RADDR(raddr) (raddr>>1)
//If you want to access a real raddress, use BRADDR2WRADDR macro.
//For example, MRW_Wr16Q(BRADDR2WRADDR(realRAddr),rdata,0,0x3)....
static U32 srcRAddress;
static U32 srcOffset;
static U32 targetRAddress;
static U32 targetOffset;
static U32 targetSize;
static U32 sectorOffset;
static U32 sectorSize;
static U32 totalSize;
void ProgramAM29F800(void)
{
U32 i;
printf("\n[AM29F800 Writing Program]\n");
printf("NOTE: AM29LV800BB needs 4 step sequences for 1 half-word rdata.\n");
printf(" So,the program time is twice of Starata flash(2 step sequences).\n");
MRW_JtagInit();
printf("[Check AM29LV800]\n");
if(!AM29F800_CheckId())
{
printf("ID Check Error!!!\n");
return;
}
// AM29F800_EraseChip();
targetSize=imageSize;
srcRAddress=(U32)malloc(targetSize+4);
if(srcRAddress==0)
return;
LoadImageFile((U8 *)srcRAddress,targetSize);
srcOffset=0;
targetRAddress=TARGET_RADDR_29LV800;
InputTargetOffset();
totalSize=0;
sectorOffset=targetOffset;
printf("\n");
while(totalSize<imageSize)
{
GetSectorSize(); //sectorSize is updated.
printf("Erase the sector:0x%x.\n",targetRAddress+sectorOffset);
AM29F800_EraseSector(targetRAddress+sectorOffset);
printf("Start of the sector rdata writing.\n");
for(i=0x0 ; i<sectorSize ; i+=2)
{
AM29F800_ProgFlash(targetRAddress+sectorOffset+i,*( (U16 *)(srcRAddress+srcOffset+i) ) );
if((i%0x100)==0)
printf("%x ",i);
totalSize+=2;
if(totalSize>=targetSize)break;
}
printf("\nEnd of the sector rdata writing!!!\n\n");
_RESET();
sectorOffset+=sectorSize;
srcOffset+=sectorSize;
}
/*
printf("\nVerifying Start.\n");
for(i=0x0;i<targetSize;i+=2)
{
if(*( (U16 *)(i+targetRAddress+targetOffset) )!=*( (U16 *)(srcRAddress+i) ) )
{
printf("%x=verify error\n",i+targetRAddress+targetOffset);
return;
}
if((i%0x1000)==0)
printf("%x ",i);
}
printf("\nVerifying End!!!\n");
*/
}
static void InputTargetOffset(void)
{
printf("\nImage Size:0h~%xh\n",targetSize);
printf("\nAvailable Target Offset:\n");
printf(" 0x0, 0x4000, 0x6000, 0x8000,0x10000,0x20000,0x30000,0x40000,\n");
printf("0x50000,0x60000,0x70000,0x80000,0x90000,0xa0000,0xb0000,0xc0000,\n");
printf("0xd0000,0xe0000,0xf0000\n");
printf("Input target offset:");
scanf("%x",&targetOffset);
}
static void GetSectorSize(void)
{
if(sectorOffset<0x4000){sectorSize=0x4000;}
else if(sectorOffset<0x6000)sectorSize=0x2000;
else if(sectorOffset<0x8000)sectorSize=0x2000;
else if(sectorOffset<0x10000)sectorSize=0x8000;
else sectorSize=0x10000;
printf("SectorOffset=0x%x\n",sectorOffset);
printf("SectorSize =0x%x\n",sectorSize);
}
static int AM29F800_CheckId(void)
{
U16 manId,devId;
_RESET();
_WR(0x555,0xaaaa);
_WR(0x2aa,0x5555);
_WR(0x555,0x9090);
manId=_RD(0x0);
_RESET(); // New 5V AM29F800 needs this command.
_WR(0x555,0xaaaa);
_WR(0x2aa,0x5555);
_WR(0x555,0x9090);
devId=_RD(0x1);
_RESET();
printf("Manufacture ID(0x0001)=0x%4x, Device ID(0x225B)=0x%4x\n",manId,devId);
if(manId==0x0001 && devId==0x225b)
return 1;
else
return 0;
}
void AM29F800_EraseChip(void)
{
printf("Chip Erase is started!\n");
_RESET();
_WR(0x555,0xaaaa);
_WR(0x2aa,0x5555);
_WR(0x555,0x8080);
_WR(0x555,0xaaaa);
_WR(0x2aa,0x5555);
_WR(0x555,0x1010);
_WAIT();
_RESET();
}
void AM29F800_EraseSector(int targetRAddr)
{
printf("Sector Erase is started!\n");
_RESET();
_WR(0x555,0xaaaa);
_WR(0x2aa,0x5555);
_WR(0x555,0x8080);
_WR(0x555,0xaaaa);
_WR(0x2aa,0x5555);
_WR(RIGHT_RADDR(targetRAddr),0x3030);
_WAIT();
_RESET();
}
int _WAIT(void) //Check if the bit6 toggle ends.
{
volatile U16 flashStatus,old;
old=_RD(0x0);
while(1)
{
flashStatus=_RD(0x0);
if( (old&0x40) == (flashStatus&0x40) )
break;
if( flashStatus&0x20 )
{
//printf("[DQ5=1:%x]\n",flashStatus);
old=_RD(0x0);
flashStatus=_RD(0x0);
if( (old&0x40) == (flashStatus&0x40) )
return 0;
else
return 1;
}
//printf(".");
old=flashStatus;
}
//printf("!\n");
return 1;
}
int AM29F800_ProgFlash(U32 realRAddr,U16 rdata)
{
_WR(0x555,0xaaaa);
_WR(0x2aa,0x5555);
_WR(0x555,0xa0a0);
_WR(RIGHT_RADDR(realRAddr),rdata);
//return _WAIT(); //not needed at JTAG access
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -