How can i write multiple data to MC9S08SH8 flash's?

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

How can i write multiple data to MC9S08SH8 flash's?

3,212 Views
Brodek
Contributor II
Hi,
I've some problem to write datas to flash of the SH8. I've made this function to write one byte in to the flash on a specified (empty) address:
 
unsigned int write_flash(unsigned int indirizzo,byte dato){  
    while(!FSTAT_FCBEF);  //wait if there's another command in progress
    if(FSTAT_FACCERR)     
      FSTAT_FACCERR = 1;
   
    (*((volatile unsigned char *)(indirizzo))) = dato;    //write dato in the location defined by indirizzo
    
    FCMD = 0x20;          // write byte command
    FSTAT_FCBEF = 1; //Send Command
   
    _asm NOP;        //Wait 4 cycles
    _asm NOP;
    _asm NOP;
    _asm NOP;
    _asm NOP;
    while(!FSTAT_FCCF);
    return(indirizzo);           //return indirizzo to verify if the write location is correct
   
}
 
In the main program I want to made a firmware to write once the flash to store some important data like firmware version and some costant, that are useful for the execution of the main alghoritm and then I want to protect this data forever.
 
My problem is that: when i call multiple times the write flash function, only the firts call work fine.
 
 Example:
 
 address = write_flash(0xEA01,0x10); //this instruction produce the wanted effect
 address = write_flash(0xEA02,0x01); // this instruction doesn't work       
 address = write_flash(0xEA03,0x03); // and also this doesb't work
 
 
How can I write multiple locations of the flash in sequence, for instance with a for cycle.
 
 
Thak for your help,
                        Walter
   
 
Labels (1)
0 Kudos
8 Replies

644 Views
JimDon
Senior Contributor III
Are you executing the code from RAM? You can only run the flash code from RAM.
Do have interrupts disabled? If not your code could be crashing after the first write.

Once things are right, your code should work.

Here is one of many threads that shows how to do this.
0 Kudos

644 Views
Brodek
Contributor II
But when the code is executed it isn't executed from RAM normally?
 
this i how I call the function in the main program:
 
 
void main(void) { 
  MCU_init(); /* call Device Initialization */

  address = write_flash(0xEA01,0x10);
    
  address = write_flash(0xEA02,0x01);       
   
  [...]
 
};    
 
 
 
Actually I don't use any interrupt in the code.
 
0 Kudos

644 Views
allawtterb
Contributor IV


Brodek wrote:
But when the code is executed it isn't executed from RAM normally?
 

Code is exectued from where ever the program counter is set to, but normally all your code is located in flash and there is where the program counter is set to.  With the code Jim has in the above link he places the code that must be run from RAM in the array PGM.


Brodek wrote:
void main(void) { 
  MCU_init(); /* call Device Initialization */

  address = write_flash(0xEA01,0x10);
    
  address = write_flash(0xEA02,0x01);       
   
  [...]
 
};    

Your code can not be called this way, Jim's code can because the crucial part is placed in RAM.  When erasing/writing to flash you can not execute code out of flash.
0 Kudos

644 Views
Brodek
Contributor II
Ok! Now, I've followed your suggestions and I've tried to write in RAM the critical part of code like in the JimD example. The result is this:
 
Code:
 
[variable declaration, library call, ....]
volatile unsigned char PGM[6]  = {  0xf7,      // sta ,X FSTAT  0x44,      // lsra -  delay and convert to FCCF bit0xf5,      // Bit ,X       FSTAT0x27,0xfd, // BEQ *-10x81       // RTS}; byte Flash_Erase_Page(word page) {       asm {            TPA         ; Get status to A            PSHA        ; Save current status              SEI         ; Disable interrupts            LDA  #0x30            STA  FSTAT  ; Clear FACCERR & FPVIOL flags            LDHX  page            STA ,X      ; Save the data            LDA  #$40   ; Erase command            STA  FCMD            LDA  #FSTAT_FCBEF_MASK      LDHX @FSTAT             JSR  PGM           PULA        ; Restore previous status            TAP         }           return (FSTAT & 0x30);  }byte Flash_Program_Byte(word address, byte data) {        asm{            TPA              PSHA        ; Save current status              SEI         ; Disable interrupts            LDA  #0x30            STA  FSTAT  ; Clear FACCERR & FPVIOL flags            LDHX  address            LDA  data            STA ,X      ; Save the data            LDA  #$20   ; Burn command            STA  FCMD            LDA  #FSTAT_FCBEF_MASK            LDHX @FSTAT             JSR   PGM            PULA        ; Restore previous status            TAP         }            return (FSTAT & 0x30);  } void MCU_init(void); /* Device initialization function declaration */void main(void) {MCU_init(); /* call Device Initialization */  (void) Flash_Erase_Page(0xEA00);(void) Flash_Program_Byte(0xEA00,0x01);        (void) Flash_Program_Byte(0xEA01,0x02);(void) Flash_Program_Byte(0xEA02,0x03);
[......]
};    

So i write the firmware on the SH8 and tried to run the code with debugger step-to-step and also in one shot, but after the execution if i watch the flash memory at the specified address nothing was writed\erased.
 
Thank for your precious help.
 
Walter
0 Kudos

644 Views
JimDon
Senior Contributor III
How do you know it did not work?
TrueTime will not update you view of memory when it is flash unless you set  "re-fresh memory when halting " in the "Debugging Memory Map" for that block. Also check "no memory access when running".

The "Debugging Memory Map" dialog is Main  Menu, 4th item from the left.

The debugger normally has no reason the re-read flash since it never changes.

0 Kudos

644 Views
Brodek
Contributor II
I always do "refresh" of mem manually after a breakpoint, but the problem was that I called write function before MCU  init function....
 
Now I've call the function after MCU_init and it's working!!
 
Thank a lot for your help my saver!!! :smileyvery-happy:
 
Walter
 
 
Ps: I'm very for my poor english and for my stupid error!
0 Kudos

644 Views
lumenshroom
Contributor I

Thank you for that code Brodek. Was hurting my head for a while, so many other people have the same problem. Then you realise you have to run from RAM, and setting that up is a headache with pragmas and memory mapping and etc; and then you discover a cool use of the volatile keyword, and a few bytes of handy code.

 

Thank you again.

:smileyhappy:

 

0 Kudos

644 Views
JimDon
Senior Contributor III
We ALL make silly mistakes from time to time.
This version only uses 6 bytes of ram, which on an SH with only 512 bytes of ram is meaningful.
You might move the flash page you are using to the 0xFC00 as your code may hit where you are writing to.

0 Kudos