/*---------------------------------------------------------
* read/write flash
* Parameters: cmd: the command
* can be: FLASH_READ
* FLASH_WRITE
* FLASH_ERASE
* pDat: the input and output register address, target type is 'protect_reg'
*
* e.g. protect_reg dat;
* dat.addr = 0x1800; //IAP addr
* dat.dat = 0x55; //IAP dat
* OperateFlash(FLASH_WRITE, &dat); //0x55----write to--->0x1800
* Return Value: none
*/
void OperateFlash(const lock_reg_cmd cmd, protect_reg *pDat)
{
BOOL tmp = EA;
EA = 0;
switch(cmd)
{
//======================= READ FLASH =======================
case FLASH_READ:
case FLASH_WRITE:
case FLASH_ERASE:
IFADRH = pDat->addr>>8;
IFADRL = pDat->addr&0xff;
IFD = pDat->dat;
IFMT = cmd;
if(UNLOCK_NUMBER == unlockRegVar) //safety
{
ISPCR |= 0x80; //enable ISP
SCMD = 0x46;
if(UNLOCK_NUMBER == unlockRegVar) //safety
{
SCMD = 0xb9; //start to operate
}
}
pDat->dat = IFD;
break;
default:
break;
}
LOCKREG();
ISPCR = 0x00;
IFD = 0xff; //for safety
EA = tmp;
} |