Thanks peg and mac.
My first try was to put feed dog interrupt routing into RAM, and expected the interrupt can still happen and feed the watchdog during Flash accessing. It didn't work because of the reason presented by peg. So I had to try another way.
The good thing is, I found the exactly place where access Flash routing is waiting. The following code is generated by freescale beekit, located in file NV_FlashHAL.c.
/***********************************************************/
static bool_t NvHalExecuteFlashCmd
(
uint8_t data, /* IN: Data to be written */
void *pDestination, /* IN: Address where data has to be written */
uint8_t flashCommand, /* IN: Flash command to be executed */
uint8_t loopIndex /* IN: Initial loop index. */
)
{
/* Wait for flash to be ready for a command. */
while ( !( FSTAT & FCCF ))
{/* Do nothing for a while. */}
/* Clear all errors. */
FSTAT = FPVIOL | FACCERR;
for (;/* Do it until a break point is reached. */; )
{
/* Write data to flash. This is required by the flash controller, even */
/* for commands that don't modify data. */
*( unsigned char * ) pDestination = data;
/* Load command register with flash command. */
FCMD = flashCommand;
/* Launch the command. */
FSTAT = FCBEF;
/* Get the next byte form the logical sector buffer. */
data = maNvSectorBuffer[ ++loopIndex ];
if ( loopIndex >= sizeof( maNvSectorBuffer ))
{
/* If we are done get out */
break;
}
/* Wait for the command buffer to clear. */
while ( !( FSTAT & FCBEF ))
{/* Do nothing for a while. */}
}/* for (;; ) */
/* Wait for the command to complete. */
while ( !( FSTAT & FCCF ))
{/* Do nothing for a while. */}
if ( FSTAT & ( FPVIOL | FACCERR ))
{
/* If somethig got wrong, the set and return error. */
return FALSE; /* An error bit is set. */
}
/* We are totally done return success. */
return TRUE;
}/* NvHalExecuteFlashCmd() */
/***********************************************************/
Notice there are three while loops with "/* Do nothing for a while. */" in them. As mac said, adding "SIMRS = 0x01;" to feed dog there will keep system alive. It solves the problem.
Again, thanks for all replies!
Charlie.