?? flash.c
字號(hào):
/************************************************************************* * $File name : flash.C * * $Create date(YYYY-MM-DD) : 2002-12-05 * * $Version : 1.0A * * * * $All copyrights reserved by GSL ( 2002 ) * * * * $Writen by : Eric wang * * * * History: 2002-12-05 addrs for word * *************************************************************************/#include "flash.h"#ifndef COMPLETE{ #define COMPLETE 0}#ifndef FAIL{ #define FAIL 1}#define FLASH_ROM 0x00800000#define ADDR_UNLOCK0 0x00800555#define ADDR_UNLOCK1 0x008002AA#define COM_UNLOCK0 0x00AA#define COM_UNLOCK1 0x0055#define MBM29LV160TE//#define MBM29LV160BE#ifdef MBM29LV160TE unsigned long const BlockAddress[] = { 0x800000, //block0 0x808000, //block1 0x810000, //block2 0x818000, //block3 0x820000, //block4 0x828000, //block5 0x830000, //block6 0x838000, //block7 0x840000, //block8 0x848000, //block9 0x850000, //block10 0x858000, //block11 0x860000, //block12 0x868000, //block13 0x870000, //block14 0x878000, //block15 0x880000, //block16 0x888000, //block17 0x890000, //block18 0x898000, //block19 0x8a0000, //block20 0x8a8000, //block21 0x8b0000, //block22 0x8b8000, //block23 0x8c0000, //block24 0x8c8000, //block25 0x8d0000, //block26 0x8d8000, //block27 0x8e0000, //block28 0x8e8000, //block29 0x8f0000, //block30 0x8f8000, //block31 0x8fc000, //block32 0x8fd000, //block33 0x8fe000 //block34 };#endif#ifdef MBM29LV160BE unsigned long const BlockAddress[] = { 0x800000, //block0 0x802000, //block1 0x803000, //block2 0x804000, //block3 0x808000, //block4 0x810000, //block5 0x818000, //block6 0x820000, //block7 0x828000, //block8 0x830000, //block9 0x838000, //block10 0x840000, //block11 0x848000, //block12 0x850000, //block13 0x858000, //block14 0x860000, //block15 0x868000, //block16 0x870000, //block17 0x878000, //block18 0x880000, //block19 0x888000, //block20 0x890000, //block21 0x898000, //block22 0x8a0000, //block23 0x8a8000, //block24 0x8b0000, //block25 0x8b8000, //block26 0x8c0000, //block27 0x8c8000, //block28 0x8d0000, //block29 0x8d8000, //block30 0x8e0000, //block31 0x8e8000, //block32 0x8f0000, //block33 0x8f8000 //block34 };#endifvoid FlashReset(void){ unsigned short *address; address = (unsigned short *)FLASH_ROM; *address = 0x00F0;}/*************************************************************** *Purpose Read flash identifier code * *Prototype void ReadFlashID(unsigned short *buf) * *Parameters buf : buf to save flash memory identifier codes * * as manufacturing code device code,etc. * *Remarks * ***************************************************************/void ReadFlashID(unsigned short *buf){ unsigned short Manu_code = 0; unsigned short Dev_code = 0; unsigned short *address; unsigned short *tmp_buf = buf; // autoselect command address = (unsigned short *)ADDR_UNLOCK0; *address = COM_UNLOCK0; address = (unsigned short *)ADDR_UNLOCK1; *address = COM_UNLOCK1; address = (unsigned short *)ADDR_UNLOCK0; *address = 0x0090; // get id address = (unsigned short *)FLASH_ROM; Manu_code = *address; address++; Dev_code = *address; *tmp_buf++ = Manu_code; *tmp_buf = Dev_code; FlashReset (); }/******************************************************** *Notice: * * Parameter: addr : 0x000000 - 0x100000 * * size : 8k, 2*4k, 16k, 31 * 32K in word * * Return : * * COMPLETE = SUCCESS TO ERASE * * FAIL = FAILED TO ERASE * ********************************************************/short EraseFlashChip(void){ unsigned short *address; unsigned char Data = 0; // chip erase command address = (unsigned short *)ADDR_UNLOCK0; *address = COM_UNLOCK0; address = (unsigned short *)ADDR_UNLOCK1; *address = COM_UNLOCK1; address = (unsigned short *)ADDR_UNLOCK0; *address = 0x0080; *address = COM_UNLOCK0; address = (unsigned short *)ADDR_UNLOCK1; *address = COM_UNLOCK1; address = (unsigned short *)ADDR_UNLOCK0; *address = 0x0010; // /data polling address = (unsigned short *)FLASH_ROM; Data = (unsigned char)(*address); while ((Data&0x80) != 0x80) // DQ7 == 1 erase complete { if ((Data&0x20) == 0x20) // DQ5 == 1 erase exceed timing limits { Data = (unsigned char)(*address); if ((Data&0x80) == 0x80) { break; } else { return FAIL; } } Data = (unsigned char)(*address); } Data = (unsigned char)(*address); if (Data == 0xFF) { return COMPLETE; } else { return FAIL; }}/*********************************************************************Purpose Erase flash data in block size **Prototype short EraseFlash(unsigned long addr, unsigned long size)* **Parameters addr : starting address ** size : size of data erase from flash in word. ** The size should be equal unit size. ** (64K in block size in flash memory) **Result On success operation return 0,else return 1. **Remarks **********************************************************************/short EraseFlash(unsigned long addr, unsigned long size){ unsigned short *address; unsigned char Data = 0; unsigned long era_addrs[35]; unsigned long end_addr; unsigned char era_no = 1; unsigned char i; for (i=0; i<35; i++) { era_addrs[i] = 0; } end_addr = addr + size; // get erase address era_addrs[0] = addr; for (i=0; i<35; i++) { if (BlockAddress[i] <= addr) { continue; } if ((BlockAddress[i]>addr) && (BlockAddress[i]<end_addr)) { era_addrs[era_no] = BlockAddress[i]; era_no++; continue; } if (BlockAddress[i] >= end_addr) { break; } } // sectors erase command address = (unsigned short *)ADDR_UNLOCK0; *address = COM_UNLOCK0; address = (unsigned short *)ADDR_UNLOCK1; *address = COM_UNLOCK1; address = (unsigned short *)ADDR_UNLOCK0; *address = 0x0080; *address = COM_UNLOCK0; address = (unsigned short *)ADDR_UNLOCK1; *address = COM_UNLOCK1; for (i=0; i<era_no; i++) { address = (unsigned short *)era_addrs[i]; *address = 0x0030; } // /data polling address = (unsigned short *)addr; Data = (unsigned char)(*address); while ((Data&0x80) != 0x80) // DQ7 == 1 erase complete { if ((Data&0x20) == 0x20) // DQ5 == 1 erase exceed timing limits { Data = (unsigned char)(*address); if ((Data&0x80) == 0x80) { break; } else { return FAIL; } } Data = (unsigned char)(*address); } Data = (unsigned char)(*address); if (Data == 0xFF) { return COMPLETE; } else { return FAIL; }}/******************************************************************** *Purpose Read data into flash * *Prototype void ReadFlash(unsigned long addr, unsigned long size, * * unsigned short *buf) * *Parameters addr : starting address * * size : size of data read from flash in word * * buf : buffer to store data read from flash * *Result * *Remarks * ********************************************************************/void ReadFlash(unsigned long addr, unsigned long size, unsigned short *buf){ unsigned short *address; unsigned short *tmp_buf = buf; unsigned long i; // read command address = (unsigned short *)ADDR_UNLOCK0; *address = COM_UNLOCK0; address = (unsigned short *)ADDR_UNLOCK1; *address = COM_UNLOCK1; address = (unsigned short *)ADDR_UNLOCK0; *address = 0x00F0; // get data address = (unsigned short *)addr; for (i=0; i<size; i++) { *tmp_buf++ = *address++; } } /**********************************************************************Purpose Write data into flash **Prototype short WriteFlash(unsigned long addr, unsigned long size, * unsigned short *buf) **Parameters addr : starting address ** size : size of buffer write to flash in word ** buf : data to write into flash **Result On success operation return 0, else return 1. **Remarks ***********************************************************************/short WriteFlash(unsigned long addr, unsigned long size, unsigned short *buf){ unsigned short *address; unsigned short *tmp_buf = buf; unsigned char Data; unsigned long i; // set fast mode address = (unsigned short *)ADDR_UNLOCK0; *address = COM_UNLOCK0; address = (unsigned short *)ADDR_UNLOCK1; *address = COM_UNLOCK1; address = (unsigned short *)ADDR_UNLOCK0; *address = 0x0020; // fast program address = (unsigned short *)addr; for (i=0; i<size; i++) { *address = 0x00A0; *address = *tmp_buf; Data = (unsigned char)(*address); while (Data != (*tmp_buf)) // DQ7 == data erase complete { if ((Data&0x20) == 0x20) // DQ5 == 1 erase exceed timing limits { Data = (unsigned char)(*address); if (Data == (*tmp_buf)) { break; } else { return FAIL; } } Data = (unsigned char)(*address); } address++; tmp_buf++; } // reset fast mode address = (unsigned short *)FLASH_ROM; *address = 0x0090; *address = 0x00F0; return COMPLETE; } short WriteFlashCode(unsigned long addr, unsigned short value){ unsigned short *address; unsigned char Data; // program command address = (unsigned short *)ADDR_UNLOCK0; *address = COM_UNLOCK0; address = (unsigned short *)ADDR_UNLOCK1; *address = COM_UNLOCK1; address = (unsigned short *)ADDR_UNLOCK0; *address = 0x00A0; // write data address = (unsigned short *)addr; *address = value; // /data polling Data = (unsigned char)(*address); while (Data != value) // DQ7 == data erase complete { if ((Data&0x20) == 0x20) // DQ5 == 1 erase exceed timing limits { Data = (unsigned char)(*address); if (Data == value) { break; } else { return FAIL; } } Data = (unsigned char)(*address); } return COMPLETE;}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -