Hello,
Perhaps you should monitor the SRS register to ascertain the source of your unexpected resets. If you are attempting to erase the last sector, where the interrupt vectors are located, this would be problematic.
You do not say which MCU type you are using? The majority of 9s08 derivatives have a single flash array, so that a portion of the erase/programming code needs to be executed from RAM, as Kef has suggested. The following function would be executed from RAM, when flash memory becomes inaccessible. The code that preceeds, and follows the call of this function can be located within flash.
#define EXIT_MASK FSTAT_FACCERR_MASK | FSTAT_FPVIOL_MASK | FSTAT_FCCF_MASK#define ERR_MASK FSTAT_FACCERR_MASK | FSTAT_FPVIOL_MASK/*****************************************************************************/#pragma CODE_SEG FLASH_ROUTINES/* This function is executed from RAM, after a flash copy is transferred. Requires 13 bytes, plus __RESET_WATCHDOG() macro size. */void flash_cmd( void){ FSTAT = FSTAT_FCBEF_MASK; // Clear FCBEF bit - activate command do { __RESET_WATCHDOG(); } while ((FSTAT & EXIT_MASK) == 0); // Wait for command completion or error}#pragma CODE_SEG DEFAULTSince the function will exit for either command completion, or for an error state, the presence of an error is easily tested using(FSTAT & ERR_MASK). Here is an example of the flash based function that I have previoulsy used for both sector erase and single byte programming.
// Process flash commandbyte flash_proc( byte *faddr, byte data, byte fcommand){ CRITICAL(); // Disable interrupts FSTAT = ERR_MASK; // Clear FACCERR & PVIOL flags, if set *faddr = data; // Write data to flash location FCMD = fcommand; // Enter command flash_cmd(); // Execute command (from RAM) CRITICAL_RESTORE(); // Restore previous interrupt status return (FSTAT & ERR_MASK); // Return FACCERR & PVIOL error status}
If you are using one of the few devices that contain two separate flash arrays, e.g. 'DZ60, the code for programming one array could be located within the other flash array, in lieu of being transferred to RAM.
Regards,
Mac