📄 mx1611drv.c
字号:
#include <comsub.h>
#if ( _FLASH_TYPE==MX1611 )
#include <flash.h>
#define TimeOut 0x2100
#define CmdOffset1 0xDFC0AAAA
#define CmdOffset2 0xDFC05554
#define PageSize 64
extern void DisInt();
extern void EnInt();
BOOL Chip_Erase_Op(void)
{
int loopCounter = 0;
BOOL TimeOutFlag = 0;
BOOL EraseOKFlag = 1; // clear the erase ok flag to be TRUE
WORD *FlashAddr;
BYTE statusReg;
// write Chip_Erase_command ;
FlashAddr = CmdOffset1; // write 6 commands to enter Chip Erase operation
*FlashAddr = 0xAA;
FlashAddr = CmdOffset2;
*FlashAddr = 0x55;
FlashAddr = CmdOffset1;
*FlashAddr = 0x80;
FlashAddr = CmdOffset1;
*FlashAddr = 0xAA;
FlashAddr = CmdOffset2;
*FlashAddr = 0x55;
FlashAddr = CmdOffset1;
*FlashAddr = 0x10;
while(1) // set a loop to check the flash's WSM till ready or system time out
{
FlashAddr = CmdOffset1; // Switch to read status mode
*FlashAddr = 0xAA;
FlashAddr = CmdOffset2;
*FlashAddr = 0x55;
FlashAddr = CmdOffset1;
*FlashAddr = 0x70;
statusReg = *FlashAddr; // read status from flash
if((statusReg & 0x80) == 0x80) // Is the Q7(ready/busy bit) ready or not ?
{
if((statusReg & 0x20) == 0x00) // It is successfully erased if Q5(erase flag) is equal to ˉ 0ˇ
break; // exit chip erase polling loop
else
{
EraseOKFlag = FALSE; // if Q5 is ˉ1ˇ, then the chip erase was failed
FlashAddr = CmdOffset1; // write Clear status command to clear status Reg.
*FlashAddr = 0xAA;
FlashAddr = CmdOffset2;
*FlashAddr = 0x55;
FlashAddr = CmdOffset1;
*FlashAddr = 0x50;
break;
}
}
delay(1); // the delay time is adjustable and deponed on the system clock
if(TimeOut < loopCounter++) // The number for timeout must be set bigger than Max. of the spec.
{ // Basically, this time out loop to prevent system hang up is no necessary,
EraseOKFlag = FALSE; // since the flash already provides the Q7 ready/busy bit for that.
FlashAddr = CmdOffset1; // write Clear status command to clear status Reg.
*FlashAddr = 0xAA;
FlashAddr = CmdOffset2;
*FlashAddr = 0x55;
FlashAddr = CmdOffset1;
*FlashAddr = 0x50;
break;
}
}
FlashAddr = CmdOffset1; // RESET command to reset flash to read operation
*FlashAddr = 0xAA;
FlashAddr = CmdOffset2;
*FlashAddr = 0x55;
FlashAddr = CmdOffset1;
*FlashAddr = 0xF0;
return (EraseOKFlag); // report the final result to main program
} /* end of Chip_erase_Op */
//////
BOOL Page_Program_Op(int PageNum, WORD *PageData)
{
int i;
int loopCounter = 0;
BOOL TimeOutFlag = 0;
BOOL ProgramOKFlag = 1; // clear the program ok flag to be TRUE
WORD *FlashAddr;
// WORD aaa;
BYTE statusReg;
DisInt(); // Disable system's Interrupt;
// System interrupts might cause page data downloaded flow to be improperly
// ended. Disable all system interrupts before going to page program operation.
// write Program_command
FlashAddr = CmdOffset1; // write 3 commands to enter program operation
*FlashAddr = 0xAA;
FlashAddr = CmdOffset2;
*FlashAddr = 0x55;
FlashAddr = CmdOffset1;
*FlashAddr = 0xA0;
for(i=0; i<PageSize; i++) // set a loop to down load whole page data into flashˇs buffer
{
/* Load whole page data into flash's buffer follow the write Program
cmd and the successive data must be loaded within 30us from
the rising edge of /WE to falling edge of /WE */
FlashAddr = 0xDFC00000 + PageNum * 128 + i*2;
// PRINTF("Write to Flashaddr = %x \n",FlashAddr);
*FlashAddr = *(PageData + i);
//aaa= *(PageData + i);
//PRINTF("PageData=%x \n",aaa);
}
EnInt(); // Enable system's Interrupt;
// wait 100us for closing page data load window and entering Program operation
for (i=0;i<10000;i++)
{}
while(1) // set a loop to check the flash's WSM till ready or system time out
{
FlashAddr = CmdOffset1; // Switch to read status mode
*FlashAddr = 0xAA;
FlashAddr = CmdOffset2;
*FlashAddr = 0x55;
FlashAddr = CmdOffset1;
*FlashAddr = 0x70;
statusReg = *FlashAddr; // read status from flash
if((statusReg & 0x80) == 0x80) // Is the Q7(ready/busy-bit) ready ?
{
if((statusReg & 0x10) == 0x00) // It is successful to program if Q4(Program flag) is equal to ˉ 0ˇ
break; // exit program polling loop
else
{
ProgramOKFlag = FALSE; // if Q4 is ˉ1ˇ, then program was failed */
FlashAddr = CmdOffset1; // write Clear status command to clear status Reg.
*FlashAddr = 0xAA;
FlashAddr = CmdOffset2;
*FlashAddr = 0x55;
FlashAddr = CmdOffset1;
*FlashAddr = 0x50;
break;
}
}
delay(1); // the delay time is adjustable and deponed on the system clock
if(TimeOut < loopCounter++) // The number for timeout must be set bigger than Max. of the spec.
{ // Basically, this time out loop to prevent system hang up is no necessary,
ProgramOKFlag = FALSE; // since the flash already provides the Q7 Ready/busy bit for that.
FlashAddr = CmdOffset1; // write Clear status command to clear status Reg.
*FlashAddr = 0xAA;
FlashAddr = CmdOffset2;
*FlashAddr = 0x55;
FlashAddr = CmdOffset1;
*FlashAddr = 0x50;
break;
}
}
FlashAddr = CmdOffset1; // RESET command to reset flash to read operation
*FlashAddr = 0xAA;
FlashAddr = CmdOffset2;
*FlashAddr = 0x55;
FlashAddr = CmdOffset1;
*FlashAddr = 0xF0;
return (ProgramOKFlag); // report the final result to main program
} /* end of Page_Program_Op */
#if 0
BOOL Page_Program_Op(int PageNum, WORD *PageData)
{
return 1 ;
} /* end of Page_Program_Op */
#endif
#endif /*#if ( _FLASH_TYPE==MX1611 )*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -