開始工作不久就碰到一個flash讀寫的問題。是一塊lpc2388的芯片(arm7),
開始總是抱著一arm11的flash讀寫的方式去看數(shù)據(jù)手冊。看了好長時間都沒有一個很好的解決方發(fā)。
后來我在keil的庫文件中找到:flash的寫入方式。如下://C:KeilARMFlashLPC2888FlashPrg.c
/*
* Program Page in Flash Memory
* Parameter: adr: Page Start Address
* sz: Page Size
* buf: Page Data
* Return Value: 0 - OK, 1 - Failed
*/
int ProgramPage (unsigned long adr, unsigned long sz, unsigned char *buf) {
unsigned long i;
// Preset data latches
F_CTRL = FC_CS | FC_FUNC | FC_WEN | FC_SET_DATA;
F_CTRL = FC_CS | FC_FUNC | FC_WEN;
// Set timer register for programming
F_PROG_TIME = 800 | FPT_ENABLE;
// If size is not whole number of words,
// fill rest of last word with 0xFF
if ((sz%4) != 0) {
*((unsigned long *)buf+(sz/4)) |= or_mask[sz%4];
}
// Load data to data latches
for (i = 0; i < ((sz+3)/4); i++) {
M32(adr) = *((unsigned long *)buf);
buf += 4;
adr += 4;
}
// Load remaining bytes to full page (512) with 0xFF
for (i = ((sz+3)/4); i < (512/4); i++) {
M32(adr) = 0xFFFFFFFF;
adr += 4;
}
// Issue program command
F_CTRL = FC_CS | FC_FUNC | FC_PROTECT | FC_PROG_REQ;
while (!(F_STAT & FS_DONE)); // Wait command to finish
// Disable timer
F_PROG_TIME = 0;
return (0); // Done successfully
}
這里有很清晰的flash的寫入方式,可是讀取的方式了。
于是在看手冊想找到一個寄存器之類的,進行讀取啊?!禠PC2388中文資料》在這里居然還是沒找到,看下英文的吧,可是英文水平不咋的,還是無濟于事啊。
**Arm7(lpc2388)Flash的讀取**
1
2
后來在經(jīng)理的幫助下原來lpc2388的flash的讀寫是這么的簡單啊。(其他的單片機沒有用到,我想arm系列的單片機可能都是這樣子的了)
#define DestAddr 0x00038000 ///276k 0X0007CFFF
//就是一個flash的起始地址,
/*
recindex是一個其實地址的便宜,可以這樣理解,可以根據(jù)自己的需要便宜,可以是任意值(0~1024)具體的范圍的看數(shù)據(jù)手冊。
adata是數(shù)據(jù)的保存,就是你分配的內(nèi)存空間
*/
uint32 flash_read_256( int recindex, unsigned char *adata )
{
unsigned int addr = DestAddr + recindex*256 ;
//把地址加出來
unsigned char *intd = (unsigned char *)addr , no ;
//轉(zhuǎn)下
memcpy( adata , intd , 230 ) ;
//直接memcpy()以內(nèi)存讀取的方式把flash的數(shù)據(jù)保存到內(nèi)存中。230是你讀取的數(shù)據(jù)長度,讀取的數(shù)據(jù)長度是隨意的,可以是1個也可以使256個,只要你個的內(nèi)存可以放的下就行了
return 1 ;
}
這是寫的代碼:在下面是我自己整理的代碼
/*
* Erase Sector in Flash Memory
* Parameter: adr: Sector Address
* Return Value: 0 - OK, 1 - Failed
*/
int EraseSector (unsigned long adr) {
unsigned long n;
n = GetSecNum(adr); // Get Sector Number
IAP.cmd = 50; // Prepare Sector for Erase
IAP.par[0] = n; // Start Sector
IAP.par[1] = n; // End Sector
IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command
if (IAP.stat) return (1); // Command Failed
IAP.cmd = 52; // Erase Sector
IAP.par[0] = n; // Start Sector
IAP.par[1] = n; // End Sector
IAP.par[2] = CCLK; // CCLK in kHz
IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command
if (IAP.stat) return (1); // Command Failed
return (0); // Finished without Errors
}
/*
* Program Page in Flash Memory
* Parameter: adr: Page Start Address
* sz: Page Size
* buf: Page Data
* Return Value: 0 - OK, 1 - Failed
*/
int ProgramPage (unsigned long adr, unsigned long sz, unsigned char *buf) {
unsigned long n;
#if SET_VALID_CODE != 0 // Set valid User Code Signature
if (adr == 0) { // Check for Vector Table
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; // Signature at Reserved Vector
}
#endif
n = GetSecNum(adr); // Get Sector Number
IAP.cmd = 50; // Prepare Sector for Write
IAP.par[0] = n; // Start Sector
IAP.par[1] = n; // End Sector
IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command
if (IAP.stat) return (1); // Command Failed
IAP.cmd = 51; // Copy RAM to Flash
IAP.par[0] = adr; // Destination Flash Address
IAP.par[1] = (unsigned long)buf; // Source RAM Address
IAP.par[2] = 1024; // Fixed Page Size
IAP.par[3] = CCLK; // CCLK in kHz
IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command
if (IAP.stat) return (1); // Command Failed
return (0); // Finished without Errors
}
自由發(fā)揮的:
/*
塊的選擇
*/
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);
//STAT_ADR是需要查數(shù)據(jù)手冊來確定地址的。就是一個地址
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;
fl