David Boyd

Prgramming HCS08 Flash - Help Needed

Discussion created by David Boyd on Jul 14, 2007
Latest reply on Jul 14, 2007 by Frank Volker
I am creating some code to save configs in Flash on a HCS908QG8.  Single byte writes are fine. I created the code from scratch, but find it is locking-up the micro.  I then lifted the code from AN2496 and I get the same result. The problem occurs as soon as I issue the program command.
Any help is appreciated.  The code is listed below.
byte_pgm:    equ    $20           ;Command to program a single byte
; bit position masks
mFCBEF:     equ   %10000000   ;flash command buffer empty flag
mFCCF:      equ   %01000000   ;flash command complete flag
mFPVIOL:    equ   %00100000   ;flash protection violation
mFACCERR:   equ   %00010000   ;flash access error
mFBLANK:    equ   %00000100   ;flash verified as all blank (erased =$ff) flag
* Flash Routines Initialisation
  lda #!45
  sta rFCDIV   ;Set the flash clock divider register to produce 186kHz clock
  ldhx #vNVStorageStart ;Set up an address pointer
  sthx wAddrFlashWrite  ; "
  lda #'a'   ;Create some test data
  sta yFlashByte  ; "
  jsr COP_Reset

ProgFlashByte lda #mFPVIOL|mFACCERR ;error flag bits to clear if set
  sta rFSTAT   ;clear any error flags
  ldhx wAddrFlashWrite  ;Get address to program
  lda yFlashByte  ;Store the byte in flash
  sta ,X   ; "
  aix #1   ;Save address of next free config space in flash
  sthx wAddrFlashWrite  ; "
  lda #byte_pgm  ;page write command
  sta rFCMD   ;initiate the programming sequence
  lda rFSTAT
  ora #mFCBEF
  sta rFSTAT   ;launch the command
         nop    ;burn bus cycs before chking status
  lda rFSTAT   ;look at status register for errors
  and #mFPVIOL|mFACCERR ;error flag bits
  bne flash_error  ;check for programming errors
pgm_loop: lda rFSTAT   ;wait around unil prog'ing complete
  and #mFCCF   ;command complete yet?
  beq pgm_loop  ;not done yet..loop until complete