📄 flash.c
字号:
/*************************************************************************/
/* */
/*************************************************************************/
#include "Main.h"
#include <math.h>
// Global variable for EEPROM Write address setting
volatile U32 gRomDstAddr ;
/*********************************************************************
* PROCEDURE : Program_AM29LV160DB
*
* This procedure can be used to write a total of 128bytes at one
* write cycle to the SST's 29EE512/29LE512/29VE512, 29EE010/29LE010
* /29VE010, 29EE020/29LE020/29VE020.
*
*********************************************************************/
void Program_AM29LV160(void *Src, void *Dst,int RcvDataSize)
{
int RomPageSize;
int i;
pB0SIZE *RomSrc;
pB0SIZE *RomDst;
RomSrc = (pB0SIZE *)Src;
RomDst = (pB0SIZE *)Dst;
gRomDstAddr = (U32)Dst ;
//AM29LV160_DisableProtection() ;
//AM29LV160_EraseFLASH();
if(AM29LV160_EmptyCheck() == EMPTY) printp("The AM29LV160 is empty !");
else AM29LV160_EraseFLASH();
//RomPageSize = (int)((float)RcvDataSize/(float)ROW_SIZE_29EE) + 1 ;
RomPageSize = (int)ceil( (float)RcvDataSize / (float)ROW_SIZE_AM29LV160 );
printp("\nProgramming EEPROM ..... \n");
for (i=0 ; i<RomPageSize ; i++)
{
AM29LV160_WriteEEPROM(RomSrc,RomDst);
RomSrc += ROW_SIZE;
RomDst += ROW_SIZE;
printp("#") ;
}
printp("\n Finished.") ;
//AM29LV160_EnableProtection() ;
}
/*********************************************************************
* PROCEDURE : SST29EE_DisableProtection()
*
* This procedure DISABLES the data protection feature. After calling
* this routine, the chip can be written without any additional commands.
*
*********************************************************************/
void AM29LV160_DisableProtection(void)
{
pB0SIZE *Temp ;
printp("\rDisable EEPROM Data Protection ..... ") ;
Temp = (pB0SIZE *)( ChipCommand1A | gRomDstAddr ) ;
*Temp = (pB0SIZE)ChipCommand1D;
Temp = (pB0SIZE *)( ChipCommand2A | gRomDstAddr ) ;
*Temp = (pB0SIZE)ChipCommand2D;
Temp = (pB0SIZE *)( ChipCommand1A | gRomDstAddr ) ;
*Temp = (pB0SIZE)ChipUnlock;
Delay_Milli_Second(50000) ;
printp("Finished.") ;
}
void detect_AM29LV160(void)
{
unsigned char a=0;
unsigned int b=0;
printp("\rDisable EEPROM Data Protection ..... ") ;
*((volatile UINT16 *) FLASH_START_ADDR+ ChipCommand1A) = ChipCommand1D;
*((volatile UINT16 *) FLASH_START_ADDR+ ChipCommand2A) = ChipCommand2D;
*((volatile UINT16 *) FLASH_START_ADDR+ ChipCommand1A) = IDChip;
a= *((pB0SIZE *)(0x5000000));
Delay_Milli_Second(50000) ;
*((volatile UINT16 *) FLASH_START_ADDR+ ChipCommand1A) = ChipCommand1D;
*((volatile UINT16 *) FLASH_START_ADDR+ ChipCommand2A) = ChipCommand2D;
*((volatile UINT16 *) FLASH_START_ADDR+ ChipCommand1A) = IDChip;
b= *((pB0SIZE *)(0x5000002));
Delay_Milli_Second(50000) ;
printp("Finished.%x %x",a,b) ;
}
/*********************************************************************
* PROCEDURE : void AM29LV160_EraseFLASH(void)
*
* Erase EEPROM operation
*
*********************************************************************/
void AM29LV160_EraseFLASH(void)
{
volatile UINT16 *flashPtr=(UINT16 *)FLASH_START_ADDR;
printp("\nErasing EEPROM ..... ") ;
//按HY29LV160整片擦除命令要求写入命令序列,以16位方式操作
*((volatile UINT16 *) FLASH_START_ADDR+ ChipCommand1A) = ChipCommand1D;
*((volatile UINT16 *) FLASH_START_ADDR+ ChipCommand2A) = ChipCommand2D;
*((volatile UINT16 *) FLASH_START_ADDR+ ChipCommand1A) = ChipECommand1;
*((volatile UINT16 *) FLASH_START_ADDR+ ChipCommand1A) = ChipCommand1D;
*((volatile UINT16 *) FLASH_START_ADDR+ ChipCommand2A) = ChipCommand2D;
*((volatile UINT16 *) FLASH_START_ADDR+ ChipCommand1A) = ChipECommand2;
//等待擦除操作完成
if(FlashStatusDetect((UINT16 *)flashPtr, 0xffff, 0x1000000) == FALSE)
printp("\nERASE ERROR!");
else printp("\nERASE success Finished.") ;
}
/*********************************************************************
* PROCEDURE : SST29EE_WriteEEPROM(RomSrc,RomDst);
*
* Write a total of 128 bytes at once write cycle
* Src : SOURCE Address containing the data
* Dst : DESTINATION Address which will be writen with the
* data passed in from ds:si
*
*********************************************************************/
void AM29LV160_WriteEEPROM(pB0SIZE *Src, pB0SIZE *Dst)
{
pB0SIZE *SourceBuf ;
pB0SIZE *DestBuf ;
unsigned int index ;
SourceBuf = (pB0SIZE *)Src;
DestBuf = (pB0SIZE *)Dst ;
//*((volatile UINT16 *) FLASH_START_ADDR+ FLASH_ADDR_UNLOCK1) = FLASH_DATA_UNLOCK1;
*((volatile UINT16 *) FLASH_START_ADDR + ChipCommand1A) = ChipCommand1D;
*((volatile UINT16 *) FLASH_START_ADDR + ChipCommand2A) = ChipCommand2D;
*((volatile UINT16 *) FLASH_START_ADDR + ChipCommand1A) = ChipUnlock;
for (index=0; index<ROW_SIZE; index++)
{
*((volatile UINT16 *) FLASH_START_ADDR + ChipCommand1A) = ChipProgram;
*DestBuf++ = *SourceBuf++ ;
Delay_Milli_Second(10) ;
}
Delay_Milli_Second(50000) ;
//Check_Toggle_Ready(Dst) ;
}
/*********************************************************************
* PROCEDURE : SST29EE_EnableProtection() ;
*
* After calling this routine, the chip cannot be written
* unless preceded by the three Byte-Load sequence.
*
*********************************************************************/
void AM29LV160_EnableProtection(void)
{
pB0SIZE *Temp ;
printp("\rEnable EEPROM Data Protection ..... ") ;
Temp = (pB0SIZE *)( ChipCommand1A | gRomDstAddr ) ;
*Temp = (pB0SIZE)ChipCommand1D;
Temp = (pB0SIZE *)( ChipCommand2A | gRomDstAddr ) ;
*Temp = (pB0SIZE)ChipCommand2D;
Temp = (pB0SIZE *)( ChipCommand1A | gRomDstAddr ) ;
*Temp = (pB0SIZE)ChipProgram;
Delay_Milli_Second(50000) ;
printp("Finished.") ;
}
/*********************************************************************
* PROCEDURE : Check_Toggle_Ready(void *Dst)
*
* During the internal write cycle, any consecutive read
* operation on DQ6 will produce alternating 0's and 1's.
* When the Write cycle is completed, DQ6 of the data will
* stop toggling After the DQ6 data bit stops toggling,
* the device is ready for next operation
*
*********************************************************************/
void Check_Toggle_Ready(pB0SIZE *Dst)
{
int Loop = TRUE ;
pB0SIZE PreData ;
pB0SIZE CurrData ;
unsigned long TimeOut = 0 ;
PreData = *Dst;
PreData = PreData & (pB0SIZE)0x40404040 ;
while((TimeOut<0x07FFFFFF) && Loop)
{
CurrData = *Dst;
CurrData = CurrData & (pB0SIZE)0x40404040;
if (PreData == CurrData) Loop = FALSE;
PreData = CurrData;
TimeOut++;
}
}
/*********************************************************************
* PROCEDURE : Check_Data_Polling
*
* Durring the internal write cycle, any attemp to read DQ7 of the byte
* loading during the page/byte load cycle will receive the complement
* of the data. Once the write cycle completed, DQ7 show true data.
* TrueData : this is the original (true) data
*
*********************************************************************/
int Check_Data_Polling(pB0SIZE *Dst, pB0SIZE TrueData)
{
int Loop = TRUE ;
pB0SIZE CurrData ;
unsigned int TimeOut = 0 ;
TrueData = TrueData & (pB0SIZE)0x80808080 ;
while (( TimeOut < 0x07FFFFFF) && (Loop == TRUE))
{
CurrData = *Dst ;
CurrData = CurrData & (pB0SIZE)0x80808080 ;
if (TrueData == CurrData) Loop = FALSE ;
TimeOut++ ;
}
if(Loop == FALSE) return SUCCESS;
else return FALSE;
}
int FlashStatusDetect(UINT16 *ptr, UINT16 Data, int TimeCounter)
{
int tmp = TimeCounter;
volatile UINT16 *p = ptr;
UINT16 current_data; //data1, data2,
current_data = Data & 0x8080;
while((*p& 0x8080) != current_data)
{
if(tmp%20000==0) printp("#");
if(tmp-- <= 0)
return FALSE;
}
return SUCCESS;
}/**/
int AM29LV160_EmptyCheck(void)
{
int i;
for(i = 0;i < 0x100000;i++)
{
if(*(int *)(i+FLASH_START_ADDR) != 0xFFFF) return TRUE;
}
return EMPTY;
}
/*********************************************************************
* PROCEDURE : Delay_Milli_Second
*********************************************************************/
void Delay_Milli_Second(int count)
{
int i ;
for (i=0; i<count ; i++) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -