Flash programming (HCS08AW60)

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

Flash programming (HCS08AW60)

1,638 Views
ssinfod
Contributor IV
Hi,
 
I am currently testing a function which writes a byte in the flash memory of the MCU (HCS08AW60).
 
I would like to know if I have to erase the sector before programming the byte for a second time.
 
Example :
  unsigned char fdata = 0;
  unsigned int faddress = 0x870;
 
  fdata = flash_sector_erase(faddress); // Erase page
  delay(1000);
  
  fdata = flash_read(faddress);         // read operation (0xFF)
  delay(1000);
 
  //1st Write
  fdata = 'U';
  fdata = flash_write(faddress, fdata); // write operation
  delay(1000);
 
  // Read
  fdata = flash_read(faddress);         // read operation ( 'U')
  delay(1000);
 
  // 2nd Write
  fdata = 'A';
  fdata = flash_write(faddress, fdata); // write operation
  delay(1000);
 
  // Read
  fdata = flash_read(faddress);         // read operation ('A')
  delay(1000);
 
It seems I can write the byte at the same address (0x870) a second time without having to erase the sector. Is that true ? Do I have the erase the sector before programming any bytes ?
 
I also tried it without erasing the sector at all and it works.
 
Thanks
 
 
 
Labels (1)
0 Kudos
3 Replies

373 Views
ssinfod
Contributor IV
My flash programming routines are based on example from the forums..
 
File.c
 
//==============================================================================
// Write a byte into the specified FLASH address
//==============================================================================
char flash_write(unsigned int address, unsigned char data)
{
  unsigned char *pointer;
  pointer = (char*) address;
  FlashProg(pointer,data);   // call the FLASH programming function
  if (FSTAT_FACCERR) data=1; else data=0;
  if (FSTAT_FPVIOL) data|=2;
  return(data);
}
//==============================================================================
// Erase a sector of the FLASH memory
//==============================================================================
unsigned char flash_sector_erase(unsigned int address)
{
  unsigned char *pointer, res;
  pointer = (char*) address;
  FlashErase(pointer);
  if (FSTAT_FACCERR) res=1; else res=0;
  if (FSTAT_FPVIOL) res|=2;
  return(res);
}
 
File.asm
 
;*******************************************************************************
;
;*******************************************************************************
FlashErase:
        psha                                                                    ;adjust sp for DoOnStack entry
        lda #(mFPVIOL+mFACCERR)                                                 ;mask
        sta FSTAT                                                               ;abort any command and clear errors
        lda #mPageErase                                                         ;mask pattern for page erase command
        bsr DoOnStack                                                           ;finish command from stack-based sub
        ais #1                                                                  ;deallocate data location from stack
        rts
;*******************************************************************************
;
;*******************************************************************************
FlashProg:
        psha                                                                    ;temporarily save entry data
        lda #(mFPVIOL+mFACCERR)                                                 ;mask
        sta FSTAT                                                               ;abort any command and clear errors
        lda #mByteProg                                                          ;mask pattern for byte prog command
        bsr DoOnStack                                                           ;execute prog code from stack RAM
        ais #1                                                                  ;deallocate data location from stack
        rts
...
 
0 Kudos

373 Views
peg
Senior Contributor IV
Hi ssinfod2,

You have to erase first, or at least start with $FF.
Programming can only change selected 1's to 0's.
Erasing makes all bits at once 1.
You can actually programme a byte twice if you only want to make more of the remaining 1's a 0, but it is strongly discouraged by Freescale.
Sometimes if monitoring with CW it can look like you have programmed a byte when in fact you haven't. Power cycle and check again.

0 Kudos

373 Views
ssinfod
Contributor IV

Ok , now I understand better.

Thanks for your answer.



Message Edited by ssinfod2 on 2008-03-30 06:58 AM
0 Kudos