Programming, actually erasing, Flash from within a program

Discussion created by MIKE YETSKO on Feb 21, 2006
Latest reply on Feb 25, 2006 by Samiran Joseph
I have no trouble programming the flash from CW and the debugger.  But I'm trying to use one of the multiple pages of the flash as a 'data' page.
Anyway, I did my own little flash file system that looks for a 'token frame' I place at the beginning of a segment of the flash.  I'm careful about not switching out my code page to switch in the data page.
So...  I can write data to my data page.  I can transfer data in and out of the page.  I can erase data (I zero it by an 'overwrite' since my 'frames' are much smaller than
the flash erase size) and everything works.
Except erase.  I can't get the erase to work.  I'm using the exact same routine to issue the command to the flash that I use to write data or to zero data.  But the erase just seems to hang.
Here's what I do.
   I read FSTAT to make sure there are no bits in 30h for errors,
       If so, I write the bits back to clear them
   I put a 0000h at location 0 in the 512 byte block I want to erase.
   I put a 40h into the FCMD register
   I disable ints (to make sure I'm quick on the status check
   I put an 80h into FSTAT to clear CBEIF
   I then wait for FSTAT to clear on a read
   I enable ints at this point
   I then wait for CBEIF to set again indicating the command is 'taken'
This works for writing (with valid data and a 20h command, of course)  but for some reason it won't erase.  I've tried putting a check for CCIF at the end, but
it doesn't get that far.  I fail at the checking for CBEIF to clear after I kick the bit
(the 5th step above) to tell the chip to take the command.  It doens't seem to
want to take the command, it just hangs in my loop to check:
   brset  FSTAT, #$80, *
Thing is, if I stop the CPU with the debugger, I'm seeing a C1 in FSTAT.  Could
it be taking it and coming back that quick?  Should I be checking it that way?