Hello,
Firstly on the reset issue -
A sector erase will take 25-35 milliseconds, depending on the flash clock frequency. If your external watchdog does not have a greater timeout period, you would normally need to reset the watchdog timer within the wait loop of your HighVoltage sub-routine. Temporarily disabling the watchdog should also work, but resetting the watchdog within the loop should be simpler.
For the case of a MCU with a single flash array, as you are using, I think it is rather dangerous to have any possibility of a non-specific "call back" routine executing from within the HighVoltage routine, even it can be disabled with a null address. Personally, I would totally remove the code from the routine (which will also reduce the size of this RAM based routine). The wait loop can be further simplified by using a single test to see whether any one of the three FSTAT flags is set, and exiting the routine if so. Whether the exit was due to an error condition can then be determined from outside this sub-routine. The error flags would also be cleared from outside. The following code should considerably reduce the amount of RAM storage required.
HighVoltage:
ldhx #SGF_FSTAT ; load FSTAT address
lda #SGF_FSTAT_FCBEF ; A <- $80
sta ,x ; launch command
waitFCCF: reset_watchdog ; Macro for external watchdog reset
lda ,x ; check flags in FSTAT
bit #(SGF_FSTAT_FCCF|SGF_FSTAT_FPVIOL|SGF_FSTAT_ACCERR)
beq waitFCCF
rts ; Current FSTAT value returned in ACC
But this probably does not help with your current problem.
I do not pretend to understand the complexities of your EEPROM emulation process. However the primary contender for the write problem is probably where indexed addressing has been used to define a flash address location, and you have a wayward H-register value. It is quite possible that the H-regiister is being corrupted only one percent of the time. You will need to double check your code for this possibility.
I assume that, for the code over-write to occur, that you do not have this portion of flash write protected. Whenever using flash routines within a program, it is essential that the remainder of the flash containing the program code be write protected.
Regards,
Mac
Message Edited by bigmac on
2008-05-18 12:34 PM