开始工作不久就碰到一个flash读写的问题。是一块lpc2388的芯片(arm7),
开始总是抱着一arm11的flash读写的方式去看数据手册。看了好长时间都没有一个很好的解决方发。
后来我在keil的库文件中找到:flash的写入方式。如下://C:KeilARMFlashLPC2888FlashPrg.c
int ProgramPage (unsigned long adr, unsigned long sz, unsigned char *buf) {
unsigned long i;
F_CTRL = FC_CS | FC_FUNC | FC_WEN | FC_SET_DATA;
F_CTRL = FC_CS | FC_FUNC | FC_WEN;
F_PROG_TIME = 800 | FPT_ENABLE;
if ((sz%4) != 0) {
*((unsigned long *)buf+(sz/4)) |= or_mask[sz%4];
}
for (i = 0; i < ((sz+3)/4); i++) {
M32(adr) = *((unsigned long *)buf);
buf += 4;
adr += 4;
}
for (i = ((sz+3)/4); i < (512/4); i++) {
M32(adr) = 0xFFFFFFFF;
adr += 4;
}
F_CTRL = FC_CS | FC_FUNC | FC_PROTECT | FC_PROG_REQ;
while (!(F_STAT & FS_DONE));
F_PROG_TIME = 0;
return (0);
}
这里有很清晰的flash的写入方式,可是读取的方式了。
于是在看手册想找到一个寄存器之类的,进行读取啊。《LPC2388中文资料》在这里居然还是没找到,看下英文的吧,可是英文水平不咋的,还是无济于事啊。
**Arm7(lpc2388)Flash的读取**
后来在经理的帮助下原来lpc2388的flash的读写是这么的简单啊。(其他的单片机没有用到,我想arm系列的单片机可能都是这样子的了)
#define DestAddr 0x00038000
uint32 flash_read_256( int recindex, unsigned char *adata )
{
unsigned int addr = DestAddr + recindex*256 ;
unsigned char *intd = (unsigned char *)addr , no ;
memcpy( adata , intd , 230 ) ;
return 1 ;
}
这是写的代码:在下面是我自己整理的代码
int EraseSector (unsigned long adr) {
unsigned long n;
n = GetSecNum(adr);
IAP.cmd = 50;
IAP.par[0] = n;
IAP.par[1] = n;
IAP_Call (&IAP.cmd, &IAP.stat);
if (IAP.stat) return (1);
IAP.cmd = 52;
IAP.par[0] = n;
IAP.par[1] = n;
IAP.par[2] = CCLK;
IAP_Call (&IAP.cmd, &IAP.stat);
if (IAP.stat) return (1);
return (0);
}
int ProgramPage (unsigned long adr, unsigned long sz, unsigned char *buf) {
unsigned long n;
#if SET_VALID_CODE != 0
if (adr == 0) {
n = *((unsigned long *)(buf + 0x00)) +
*((unsigned long *)(buf + 0x04)) +
*((unsigned long *)(buf + 0x08)) +
*((unsigned long *)(buf + 0x0C)) +
*((unsigned long *)(buf + 0x10)) +
*((unsigned long *)(buf + 0x14)) +
*((unsigned long *)(buf + 0x18));
*((unsigned long *)(buf + 0x1C)) = 0 - n;
}
#endif
n = GetSecNum(adr);
IAP.cmd = 50;
IAP.par[0] = n;
IAP.par[1] = n;
IAP_Call (&IAP.cmd, &IAP.stat);
if (IAP.stat) return (1);
IAP.cmd = 51;
IAP.par[0] = adr;
IAP.par[1] = (unsigned long)buf;
IAP.par[2] = 1024;
IAP.par[3] = CCLK;
IAP_Call (&IAP.cmd, &IAP.stat);
if (IAP.stat) return (1);
return (0);
}
自由发挥的:
int SelSector(unsigned char sec1,unsigned char sec2)
{
flash_paramin[0] = IAP_SELECTOR;
flash_paramin[1] = sec1;
flash_paramin[2] = sec2;
(*(void(*)())STAT_ADR)(flash_paramin,flash_paramout);
return(flash_paramout[0]);
}
Int EraseSector(unsigned int sec1,unsigned int sec2)
{
flash_paramin[0] = IAP_ERASESECTOR;
flash_paramin[1] = sec1;
flash_paramin[2] = sec2;
flash_paramin[3] = IAP_FCCLK;
(*(void(*)())STAT_ADR)(flash_paramin,flash_paramout);
return(flash_paramout[0]);
}
int RamToFlash(unsigned int dst, unsigned int src, unsigned int no)
{
flash_paramin[0] = IAP_RAMTOFLASH;
flash_paramin[1] = dst;
flash_paramin[2] = src;
flash_paramin[3] = no;
flash_paramin[4] = IAP_FCCLK;
(*(void(*)())STAT_ADR)(flash_paramin,flash_paramout);
return(flash_paramout[0]);
}
int flash_Compare(unsigned int dst, unsigned int src, unsigned int no)
{
flash_paramin[0] = IAP_COMPARE;
flash_paramin[1] = dst;
flash_paramin[2] = src;
flash_paramin[3] = no;
(*(void(*)())STAT_ADR)(flash_paramin,flash_paramout);
return( flash_paramout[0]);
}
unsigned long GetSecNum_6208 (unsigned long adr, unsigned char *firstflag)
{
unsigned long n ;
*firstflag = 0 ;
n = (adr >> 12) & 0x7F;
if (n >= 0x78) {
n -= 0x62;
if( (adr & 0xFFF) == 0 ) {
*firstflag = 1 ;
}
}
else if (n >= 0x08) {
n = 0x07 + (n >> 3);
if( (adr & 0x7FFF) == 0 ) {
*firstflag = 1 ;
}
}
return (n);
}
对于写入,可以直接看keil的库文件的写法。
no = GetSecNum( addr, &firstflag );
if( firstflag ) {
recode =SelSector( no , no);
if(0 != recode) SerOutput("flash_SelSector 1 error
");
recode = EraseSector( no , no );
if(0 != recode) SerOutput("flash_EraseSector error
");
}
recode = Compare( addr, (uint32)&flash_SendData, FLASH_SEG_SIZE);
if( 0 != recode ){
no = GetSecNum( addr );
recode = SelSector( no, no);
if(0 != recode) SerOutput("flash_SelSector 2 error
");
recode = RamToFlash( addr , (uint32)&flash_SendData, FLASH_SEG_SIZE);
if(0 != recode) SerOutput("flash_RamToFlash error
");
SerOutput("flash_Compare 1 ERROR
");
}
else SerOutput("flash_Compare 1 SUCCESS
");
以上内容全属原作者经验所结