Copying one flash block to other.

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

Copying one flash block to other.

1,219 Views
luck
Contributor III

hi.

 

I know its very basic that i m asking here.But i m new in woking with firmwares.

 

1. so here i wanted to know how can i copy a block of flash to another block. i m working on MC9S12XDT controller

 

2. lets say i wanted to copy "E9 - EF" flash block to "F9 - FF" flash block .how can i do that. Here E9 -EF and F9 - FF are PPAGE registers numbers or name.

 

3 .how large data i can copy at one time before restarting the watchdog timer.

 

4. Do i need to set or reset any flash registers.

 

5. please provide any other info which i need to know regarding above query.

 

thanks

luck

Labels (1)
6 Replies

1,000 Views
Lundin
Senior Contributor IV

This isn't "very basic", it is a rather advanced topic.

I'm assuming that you want the program to do this, in runtime. As far as a flash programming firmware is concerned, the flash is divided into banks, which are in turn divided into pages, which are in turn divided into segments, where each segment is the minimum amount of flash memory that you can erase before writing to it. The nature of flash memory is such that you must always erase a cell before writing new data to it.

How to do the actual flash erasing/programming is described in the manual and various app notes. There is no doubt some examples on this site, too.

The main problem you are facing is that the code that executes the actual flash erasing/programming cannot be located in the same flash _bank_ as you are currently accessing. If you attempt this, you'll get strange errors where the program freezes etc.

I don't know how many banks there are in your particular S12 derivate, if there are more than 1, you should be able to place your flash erase/program code in the bank that isn't programmed. If you need the code to program any bank in the MCU, you will have to duplicate it for each bank.

An alternative is to execute the code from EEPROM, since you have it available. The last resort is messy and possibly unsafe (but preached by the Freescale app notes), and that is to copy-down the flash programming code into RAM and temporarily execute it from there.

The amount of data you can copy before refreshing the watchdog depends on what flash pre-scaler clock you are using. The timing is specified in the manual, if I remember correctly, the flash must have a clock between 150-200 kHz. And of course, it depends on what watchdog time you have set, as well.

1,000 Views
luck
Contributor III

Thanks for reply.

(FIRST of all   E9 in my first post is E4 and same way F9 is F4)

Now Actually this whole operation is performed by boot bootloader.

and bootloader is stored in the lower half or in the last half of FF Page which is last flash page.

and thats why before copying data from (E4 to EF pages) i am clearing flash pages from (F4 to F8). and for FF i am clearing only half of the page so that the bootloader which is performing this task will not get removed.

after this i am copying the data byte by byte  from E48000- E4BFFF( 8000 and BFFF are the flash window start and end address)  to F48000-F4BFFF. and same goes on for E5, E6 ,E7 upto EE.

for EF to FF  i am only copying half data. since on other half of FF the bootloader is present which is performing these task.

so now i wanted to ask, am i doing the copy in the correct way by copying the data byte by byte .

if i am doing it wrong please suggest how can i perform the copy in the correct way.

i am also restarting the watch dog timer after every byte copy.

thanks.

0 Kudos

1,000 Views
Lundin
Senior Contributor IV

"i am clearing only half of the page so that the bootloader which is performing this task will not get removed"

As I already explained, you cannot run code from the same flash bank that you are currently erasing/programming (unless Freescale suddenly made this possible through some undocumented feature). So what you are trying will simply not work.

I don't know the flash layout for this particular S12 version. I assume that you know which pages that are located in the same banks?

0 Kudos

1,000 Views
luck
Contributor III

Hi Daniel

above are the flash layout S12x which i am using.

i did a search online on freescale about the above issue that how can i erase a page in a block of flash by the code which is also stored in the same flash.

so i found something like PIC(place independent code) code.

so is it possible that , we can first store a copy of code in RAM and using this PIC thing use the code in RAM which will erase the flash.

I dont know about this PIC thing or PIC segment so if you have any idea on this please share.

Thanks.

0 Kudos

1,000 Views
Lundin
Senior Contributor IV

Position-independent code as such, has nothing to do with this issue. The reason you can't execute code from the same block being written to is a memory hardware limitation, which you cannot overcome.

The sensible thing to do is to store the actual flash erase/programming code in duplicates over several blocks. Or execute that code from RAM, as explained in some app note. But you should always run code from flash if possible, executing code from RAM is a last resort "dirty hack", since it is considered unsafe.

Do create duplicates of your code, you may have to disassemble it and store the op codes in an array of bytes.

0 Kudos

1,000 Views
luck
Contributor III


HC12x has 512 Kbytes of flash.

Has 4 128 Kbytes of blocks.

Each block has 8 16Kbytes of pages.

Block 1 - has pages from E0 to E8.

Block 2 - has pages from E9 to EF

Block 3  -has pages from F0 to F8

Block 4 - has pages from F9 to FF

0 Kudos