Programming, actually erasing, Flash from within a program

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Programming, actually erasing, Flash from within a program

7,872 Views
mke_et
Contributor IV
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?
 
Mike
 
Labels (1)
0 Kudos
Reply
7 Replies

1,413 Views
samiran_cj
Contributor I
Im glad you solved it.

As far as I remember there are erase commands also available. Both for sector and page and whole flash.

Documents are scarce though. In one experiment even erased my code while it was running and locked the chip. What a bother that was. :smileyvery-happy:

Message Edited by _sam_ on 02-25-200604:34 AM

0 Kudos
Reply

1,413 Views
sjmelnikoff
Contributor III

Don't confuse flash pages/banks (logical blocks of 16Kb) and flash blocks (physical blocks of 64Kb, each with its own control registers). Code in one block cannot write or erase another location in the same block.

You need to check the BKSEL bit(s) in FCNFG. The exact details depend on the size of the flash in your chip. Make sure that you have the correct documentation for the flash module (e.g. S12FTS128KV2 for HCS12 with 128Kb).

Steve Melnikoff.

0 Kudos
Reply

1,413 Views
mke_et
Contributor IV
Well, got it.  Thanks to everyone.  Seems I was going over a boundary and when that happens...
 
 
0 Kudos
Reply

1,413 Views
mke_et
Contributor IV
Hmm, I AM erasing to the last sector of the page... 
 
You know, I could just turn off my page, then turn it back on and see...
 
Mike
 
0 Kudos
Reply

1,413 Views
samiran_cj
Contributor I
Had a hanging problem similar to this once on a return instruction of a flash programming routine.
I cant recall if it exactly was during an erase.

Sometimes on a boundary, you might want to double check the code page register hasnt rolled over to point to an invalid code instruction after the erase.
Or alternatively change the memory model might help.

Hope this helps...
0 Kudos
Reply

1,413 Views
mke_et
Contributor IV
Well, I actually picked my pages carefully.
 
I'm using 3F as my boot code, vectors, and main loader area.
 
Then I use 3E as my 'interface/driver' area, and it appears down at 4000 and is always visible.
 
Then all the other pages are 'programs' that appear on a menu that is managed by the code on page 3F.  The code finds all the programs and puts a list on the LCD.  The user then selects from the list, and the appropriate pages is then 'swapped in' at 8000 and run. 
 
One of the programs accesses an external device and picks up lots of data from
that device.  What I did is write a small Flash File System that is 'managed' inside the program, but puts all the drivers for it on page 3E.  The calls to the drivers on page 3E 'swap out' the program page and swap in the data page (which happens to be 38) to do any reads or writes.  All transfers take place through an SRAM buffer area.
 
Everything works.  Well, at least to the point I'm at so far.
 
To get started, I just defined a 'dummy' data page when I built the code.  From my program I can erase entries on the page (I overwrite them with 00 fields) and I can add new data.  I can walk the data and really do anything I want. 
 
Well, mostly.
 
First off, I cannot get the CodeWarrior debugger to even show the flash pages.  It will show what is there when I program the part, but it just will not update on the PC window through the BDM.  However, I wrote a little 'flash walker' that shows what's there, so I KNOW it's working right.
 
That aside, I can read, write, and zero the flash data area.  No problems.  (Well, I had a few, but I managed to get around them.)  What I CANNOT seem to do is erase the page.  I have to erase half a page, and I do it by issuing a series of erase commands to 512byte blocks. 
 
Actually, that works.
 
But once erased, I have to then put a 'frame header' at the start of the area, that way my program knows where the valid data is.  (Think of it as a 'boot sector' on a floppy that describes the parameters of the floppy.)  When I go to write it after an erase, it hangs. 
 
If I try to 'single step' the flash routines, EVERYTHING hangs.  The only way I've gotten anything to work is to put 'tracer code' in there that lets me know what is going on and how far it gets.
 
At this point, if CW worked, I think I'd be much further along.  But even still, I SUSPECT that my erase is actually either leaving the flash in a state that touching it to try to program it gets it 'out of sequence' or it's just not done yet.  And I think that when I try to single step CW, the BDM accessing the flash, or trying to, is also messing things up.
 
0 Kudos
Reply

1,413 Views
Nabla69
Contributor V

Hi Mike,

I don't think it's really a matter of being in the same page you erase.

When you do flash programming you need to execute your code from outside the physical memory block you want to change.
You need to check how many physical blocks your device has and execute the code from elswhere. When you program and erase, the charge pump has to put high voltage on the memory array to change the status of the cells.

The solution working all the time is to execute your flash routines from RAM. So you can use them on any flash location whithout having to worry about blocks.

Cheers,
Alvin.

0 Kudos
Reply