📄 rwflash.c
字号:
/*--------------------------------------------------------------------------
eeprom.c
Header file for eeprom emulation using boot (secondary) flash
11/2002 Ver 0.1 - Initial Version
Copyright (c) 2002 ST Microelectronics
This example demo code is provided as is and has no warranty,
implied or otherwise. You are free to use/modify any of the provided
code at your own risk in your applications with the expressed limitation
of liability (see below) so long as your product using the code contains
at least one uPSD products (device).
LIMITATION OF LIABILITY: NEITHER STMicroelectronics NOR ITS VENDORS OR
AGENTS SHALL BE LIABLE FOR ANY LOSS OF PROFITS, LOSS OF USE, LOSS OF DATA,
INTERRUPTION OF BUSINESS, NOR FOR INDIRECT, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES OF ANY KIND WHETHER UNDER THIS AGREEMENT OR
OTHERWISE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
--------------------------------------------------------------------------*/
#include "include.h"
xdata PSD_REGS PSD8xx_reg _at_ PSD_REG_ADDR; // Define PSD registers at address "csiop" space
#define NVM_DATA_POLL 0x80 // flash status "data poll" bit at DQ7
#define NVM_DATA_TOGGLE 0x40 // flash status "toggle poll" bit at DQ6
#define NVM_ERROR 0x20 // flash status "error" bit at DQ5
#define FLASH_COMMON_XAAA (volatile unsigned char xdata *) 0x0AAA
#define FLASH_COMMON_X555 (volatile unsigned char xdata *) 0x0555
/***** Eeprom_Sector_Erase_Start*****/
// Start Erases one main flash sector.
// Accepts sector number (0-3).
// Returns 0 for successful erasure. If error, returns 1.
#if 0
unsigned char flash_erase_sector(
volatile unsigned char xdata* flash_bulk_erase_address)
{
unsigned char done;
unsigned char poll;
unsigned char error;
unsigned char err;
done = FALSE;
err = FALSE;
// Note: the following constants (FLASH_COMMON_XXXX)
// are declared type volatile in the header file
// so they are not optimized away by the compiler
PSD8xx_reg.PAGE |= (1<<4);
*(FLASH_COMMON_X555) = 0xAA; // unlock main flash, write 0xAA to addess 0xX555
*(FLASH_COMMON_XAAA) = 0x55; // unlock main flash, write 0x55 to addess 0xXAAA
*(FLASH_COMMON_X555) = 0x80; // write 0x80 command to erase entire chip
*(FLASH_COMMON_X555) = 0xAA; // continue unlock sequence
*(FLASH_COMMON_XAAA) = 0x55; // continue unlock sequence
//*(FLASH_COMMON_X555) = 0x10; // write 0x10 command to complete erase command
*(flash_bulk_erase_address) = 0x30; // write 0x30 to sector address to erase
do // now use dat polling method to verify successful erase
{
poll = *(flash_bulk_erase_address); // read flash status from any address
// within the defined flash address space
error = poll & NVM_ERROR; // save timeout error bit at DQ5
poll = poll & NVM_DATA_POLL; // look at D7
if (poll == NVM_DATA_POLL) // compare DQ7
done = TRUE; // bulk erase OK,
// indicate successful exit criteria
else if (error == NVM_ERROR) // check for timeout error
err = TRUE; // indicate timeout error occurred
} while((done == FALSE) && (err == FALSE));
if (err == TRUE) // make sure timeout error and dat poll didn't
// occur simultaneously
{
poll = *(flash_bulk_erase_address); // Read flash status again
poll = poll & NVM_DATA_POLL; // get DQ7 of poll byte read from flash
if (poll == NVM_DATA_POLL) // compare DQ7
done = TRUE; // the flash erased OK at the same
// time timout error occured, indicate successful
// exit criteria
*(FLASH_COMMON_X555) = 0xF0; // reset the flash array (short reset instruction)
// now delay 3 msec per dat sheet
}
PSD8xx_reg.PAGE&=(~(1<<4));
if(done==0)
return 1;
return 0;
// return(done); // a successful flash erase returns 1, timeout error returns 0
}
#endif
unsigned char flash_erase_chip( )
{
unsigned char done;
unsigned char poll;
unsigned char error;
unsigned char err;
unsigned char xdata* flash_bulk_erase_address;
done = FALSE;
err = FALSE;
flash_bulk_erase_address = 0;
// Note: the following constants (FLASH_COMMON_XXXX)
// are declared type volatile in the header file
// so they are not optimized away by the compiler
PSD8xx_reg.PAGE |= (1<<4);
*(FLASH_COMMON_X555) = 0xAA; // unlock main flash, write 0xAA to addess 0xX555
*(FLASH_COMMON_XAAA) = 0x55; // unlock main flash, write 0x55 to addess 0xXAAA
*(FLASH_COMMON_X555) = 0x80; // write 0x80 command to erase entire chip
*(FLASH_COMMON_X555) = 0xAA; // continue unlock sequence
*(FLASH_COMMON_XAAA) = 0x55; // continue unlock sequence
*(FLASH_COMMON_X555) = 0x10; // write 0x10 command to complete erase command
//*(flash_bulk_erase_address) = 0x30; // write 0x30 to sector address to erase
do // now use dat polling method to verify successful erase
{
poll = *(flash_bulk_erase_address); // read flash status from any address
// within the defined flash address space
error = poll & NVM_ERROR; // save timeout error bit at DQ5
poll = poll & NVM_DATA_POLL; // look at D7
if (poll == NVM_DATA_POLL) // compare DQ7
done = TRUE; // bulk erase OK,
// indicate successful exit criteria
else if (error == NVM_ERROR) // check for timeout error
err = TRUE; // indicate timeout error occurred
} while((done == FALSE) && (err == FALSE));
if (err == TRUE) // make sure timeout error and dat poll didn't
// occur simultaneously
{
poll = *(flash_bulk_erase_address); // Read flash status again
poll = poll & NVM_DATA_POLL; // get DQ7 of poll byte read from flash
if (poll == NVM_DATA_POLL) // compare DQ7
done = TRUE; // the flash erased OK at the same
// time timout error occured, indicate successful
// exit criteria
*(FLASH_COMMON_X555) = 0xF0; // reset the flash array (short reset instruction)
// now delay 3 msec per dat sheet
}
PSD8xx_reg.PAGE&=(~(1<<4));
if(done==0)
return 1;
return 0;
// return(done); // a successful flash erase returns 1, timeout error returns 0
}
unsigned char flash_write_with_poll(uint addr, uchar dat)
{
unsigned char done;
unsigned char error;
unsigned char err;
unsigned char poll;
done = FALSE;
err = FALSE;
// Note: the following constants (FLASH_COMMON_XXXX)
// are declared type volatile in the header file
// so they are not optimized away by the compiler
*(FLASH_COMMON_X555) = 0xAA; // unlock main flash, write 0xAA to addess 0xX555
*(FLASH_COMMON_XAAA) = 0x55; // unlock main flash, write 0x55 to addess 0xXAAA
*(FLASH_COMMON_X555) = 0xA0; // write 0xA0 command to program
XBYTE[addr] =dat; // write byte to flash
dat = dat & NVM_DATA_POLL; // get bit DQ7 of original dat
do // now use dat polling method to verify successful write
{
poll = XBYTE[addr]; // Read the location that was just programmed
error = poll & NVM_ERROR; // save timeout error bit at DQ5
poll = poll & NVM_DATA_POLL; // get DQ7 of poll byte read from flash
if (dat == poll) // compare DQ7
done = TRUE; // dat byte programmed into flash OK,
// indicate successful exit criteria
else if (error == NVM_ERROR ) // check for timeout error
err = TRUE; // indicate timeout error occurred
} while((done == FALSE) && (err == FALSE));
if (err == TRUE) // make sure timeout error and dat poll didn't
{
poll = XBYTE[addr]; // Read location in flash again
poll = poll & NVM_DATA_POLL; // get DQ7 of poll byte read from flash
if (dat == poll) // compare DQ7
done = TRUE; // dat byte programmed into flash OK at the same
// time timout error occured, indicate successful
// exit criteria
*(FLASH_COMMON_X555) = 0xF0; // reset the flash array (short reset instruction)
// now delay 3 msec per dat sheet
}
if(done==0)
return 1;
return 0;
//return(done); // a successful flash write returns 1, timeout error returns 0
}
/*==================================================================
函数功能:写主FLASH字节
入口参数:which -- 扇区选择:0--7;
Address -- 扇区内地址;
Bdata -- 字节数据;
返回参数: 0:正确,1:错误
==================================================================*/
uchar WriteMainFlash(uchar which,uint len, uint Address,uchar *wdata)
{
uint i;
uint addr = Address;
uchar error;
uchar page;
page = PSD8xx_reg.PAGE;
PSD8xx_reg.PAGE &= 0xf8;
PSD8xx_reg.PAGE |= which;
PSD8xx_reg.PAGE|=(1<<4);
for(i=0;i<len;i++)
{
error = flash_write_with_poll(addr,*wdata);
if (error)
break;
addr++;
wdata++;
}
PSD8xx_reg.PAGE = page;
return error;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -