MC9S08SH8 Flash Program & Erase

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

MC9S08SH8 Flash Program & Erase

2,663 Views
PabloA
Contributor II

Hi there, I need your help!

I need to program some bytes in the Flash Sector of my SH8 in Burst mode but I can't understand how to do this.

In the flash section of the SH8 Data Sheet I found all the information about how to program in Burst mode with even an flow chart but it don't explain where is the Data Buffer and Address to be written in the flash sector.

I made code for MCHC08 microcontrollers for this aplication and the address and data had an specific place in RAM.

 

 

1. The first thing to do is set the internal Clock divider register (FCDIV). OK

2. Clear any error flag (FACCERR). OK

3. Wait until Command Buffer is empty. OK

4. Write to Flash to Buffer Adress and Data. ¿?

     Where is this Buffer and Address Data? How can I specify the address and Data to be written in Flash?

5. Write Command. OK

6. Write 1 to FCBEF to Launch Command. OK

7. Test FACCERR and FPVIO for errors. OK

8. Wait for Flash Command Complete Flag FCCF. OK

 

 

Well, I know it should be simple but where is the explanation.

 

Thahks a lot for your time.

 

Pablo Suárez from Buenos Aires.

Labels (1)
0 Kudos
16 Replies

1,276 Views
oskaratk
Contributor I

Pablo,

 

as you wrote

>>

I made code for MCHC08 microcontrollers for this aplication and the address and data had an specific place in RAM.

<<

The you probably know about AN3942, doOnStack etc...

 

I myself am trying to figure out how to program a few byte in a HCS08AW ...

 

Oskar

0 Kudos

1,276 Views
PabloA
Contributor II

Oskar,

 

No, I didn't know about this AN. But I think it will be usefull.

 

Thank you.

0 Kudos

1,276 Views
tonyp
Senior Contributor II

 


PabloA wrote: 

4. Write to Flash to Buffer Address and Data. ¿?

     Where is this Buffer and Address Data? How can I specify the address and Data to be written in Flash?


The word buffer in that sentence is a verb.  It says you need to write the value to the actual Flash address you want to program.  It is the act of writing that buffers (verb) both the address and the data used by the programming operation.  A simple assignment is enough to buffer both address and data.  In assembly, a simple 'sta Flash, where Flash is the actual address to burn, is enough.

 

0 Kudos

1,276 Views
PabloA
Contributor II

tonyp, thank you for your reply.

 

Ok, is big change from the old way.

 So, I have to write the Byte Data directly in the flash memory (Like: LDHX   $E000 | LDA  #$80 |  STA 0,X ) and then set the write command, write 1 to FCBEF, and so on..., Byte by Byte until complete a row of 64. Is that right?

 

Once again thank you for your time, and sorry about my english.

0 Kudos

1,276 Views
tonyp
Senior Contributor II

 


PabloA wrote:

 

 So, I have to write the Byte Data directly in the flash memory (Like: LDHX   $E000 | LDA  #$80 |  STA 0,X ) and then set the write command, write 1 to FCBEF, and so on..., Byte by Byte until complete a row of 64. Is that right?


Correct, except you don't necessarily have to write a complete row in one go, if your data isn't big enough to fill that area.  You may write a few bytes at one time, and then write some more bytes at a later time.  It is important that the programming code runs from a separate memory (usually RAM).

 

 

The application note mentioned (AN3942) should help you, whether you program in C or Assembly.

0 Kudos

1,276 Views
bigmac
Specialist III

Hello Pablo,

 

I might suggest that, try to avoid using burst mode, but program your data on a byte-by-byte basis.  This will result in simpler code, particularly for the portion of code which requires to be RAM resident.  If you are erasing the flash sector immediately prior to programming, the period required for this operation will usually be far greater than the total programming period, so little benefit would be gained by use of burst mode, with its greater complexity.  If not using burst mode, the size of a flash row is not significant, since you would be programming individual bytes.

 

I usually do not use the "doonstack" method for the RAM resident code, but would generally choose to allocate a small fixed block of RAM for this purpose (less than 20 bytes) to give further code simplication.

 

Some assembly code to demonstrate this process is attached.  I also have a C version of this code, if needed.

 

Regards,

Mac

 

0 Kudos

1,276 Views
David199
Contributor I

Hello bigmac, I need the code to assemble. Might upload it again? to the SE8 is the same code? thank you

0 Kudos

1,276 Views
danielhembree
Contributor III

bigmac wrote:

Hello Pablo,

I might suggest that, try to avoid using burst mode, but program your data on a byte-by-byte basis.  This will result in simpler code, particularly for the portion of code which requires to be RAM resident.  If you are erasing the flash sector immediately prior to programming, the period required for this operation will usually be far greater than the total programming period, so little benefit would be gained by use of burst mode, with its greater complexity.  If not using burst mode, the size of a flash row is not significant, since you would be programming individual bytes.

I usually do not use the "doonstack" method for the RAM resident code, but would generally choose to allocate a small fixed block of RAM for this purpose (less than 20 bytes) to give further code simplication.

Some assembly code to demonstrate this process is attached.  I also have a C version of this code, if needed.

Regards,

Mac

Mac, I realize this is an older thread, but do you still have this code?

Thanks!

Dan


0 Kudos

1,276 Views
bigmac
Specialist III

Hello Dan,

I assume that you are referring to the C coded version.  I think that the attached code is what was previously posed, but has somehow been removed.

Let me know if you require the assembly coded version.

Regards,

Mac

0 Kudos

1,276 Views
danielhembree
Contributor III

Mac,

I've been struggling through this code. I can follow it through in the simulation, but want to bring it into my demo board so I can do something with it. When I try to load it into the board The code does not pause at the top of the code as it normally does. When I replace the .PRN file with one from another project it loads, but I believe the copy to RAM part of the code is adversely affected. I opened the one that came with your code next to the one from another project and I see many differences. I don't know much about the contents of these .PRN file to know what to do. I suspect the "Relocate to 0x1000" portion is key, but don't know what else might need to change, or what changing this will have to the rest of the code.

Can you lend some direction?

I greatly appreciate your help!!

Dan

0 Kudos

1,276 Views
danielhembree
Contributor III

Well, I think I figured out the correct changes to the PRN file and got the code to load and run. The copy to RAM portion is working as well, but I am still unable to write a value to a Flash location. I'm assuming the line:

PTFD &= ~0x01

is intended to indicate an error in writing to flash by writing a 0 to Port F bit 0, which, in my case, conveniently illuminates an LED on my demo board. If I examine the FSTAT register immediately after stepping past that line I see no errors flagged. I have tried writing to a variety of flash locations but still have no success. My Fbus is 4.19MHz and FDIV is being written with 0x13. My code is below:

#include <hidef.h>

#include "derivative.h"

#include "S08_Flash.h"

#define LED1 PTFD_PTFD0

#define LED2 PTFD_PTFD1

long Flash_mem = 0xF800;                    // Defines the location in flash memory to erase/write

byte Flash_contents @ 0xF800;

byte data = 0xda;                        // changed from ' '

char *Flash_byte;

int i;

int n;

void main(void)

{

   // I added this code:

   ICGC1 = 0x38;                        // Configure Fbus = 4.19MHz

   ICGC2 = 0x00;

  

  

   /* Port F Bit 1 active when error occurs */   

   PTFDD |= 0x03;

   PTFD |= 0x03;

   EnableInterrupts;

   flash_init();

 

   copyinRAM();                       /* Copy flash routine to RAM */

 

   if (flash_erase( Flash_mem))          /* Erase flash sector - change from 0xF800 */

      PTFD &= ~0x01;

   if (flash_program( Flash_mem, data))  /* Program single byte - changed from 0xF800*/

      PTFD &= ~0x01;

 

 

    for( ; ; ) {

    for(i=0;i<= Flash_contents;i++);    //This portion of code will flash LED1 at a rate

        for(n=0; n<2; n++);                //tied to the value of the data value written to

                                        //the Flash location. It is a simple form of

    LED1 =~ LED1;                        //user feedback.

      

    __RESET_WATCHDOG();

  }  /* Loop always */

}

THANKS!


0 Kudos

1,276 Views
pavel_sadek
NXP Employee
NXP Employee

Hi all,

there are few approaches to do the programming of Flash on S08.

The simplest one is described in HCS08QRUG document page 93

it occupies some space in RAM as routines are hardcoded as data array there, however its use is really simple

to program byte call

Program(int Address, unsigned char data)

to erase page call 

Erase(int Address)

http://cache.freescale.com/files/microcontrollers/doc/user_guide/HCS08QRUG.pdf?fsrch=1&sr=1

Pavel

0 Kudos

1,276 Views
oskaratk
Contributor I

Mac,

 

if you would not mind to post the C code.

 

For me way easier to read ( ans hopefully understand )

 

Thanks

Oskar

 

0 Kudos

1,276 Views
bigmac
Specialist III

Hello Oskar,

 

Attached is a C code version.  In this instance, the RAM resident function is located at the bottom of the stack segment, and would be copied there immediately prior to using the erase or programming functions.  The modified PRM file is also shown to explicitly set up the stack segment.

 

A single function provides for both sector erase and byte programming.  There are macros provided for each operation.

 

Regards,

Mac

 

0 Kudos

1,276 Views
oskaratk
Contributor I

Mac,

 

thanks a lot !

 

Oskar

0 Kudos

1,276 Views
PabloA
Contributor II

Thank you very much everybody. It was very friendly.

 

Regards!

0 Kudos