Simplified Flash Erase and Write Functions for HCS08GT32

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

Simplified Flash Erase and Write Functions for HCS08GT32

6,156 Views
Gcoakley
Contributor I
Hello All,
 
I am trying to simplify the slfprg-s08gbgt.asm bootloader code to two basic functions: erase and write.  So far, I have not had much luck at this and the functions do not appear to change anything in memory.  I have attached what I have at the bottom of this posting.  Any help would be appreciated.  If someone has already done this, could you please post your solution.  Thank you.
 
-gcoakley
 
 
 
EraseFlashPage:
     LDHX    Address
     STHX    ADRS
     
     lda  #(mFPVIOL+mFACCERR) ;mask
  sta  FSTAT     ;abort any command and clear errors
  mov  #EraseSubSize, STAT ;length of flash erase routine to STAT
  tsx
  sthx STACK
  ldhx  #EraseSubEnd-1   ;point at last byte to move to stack
DoOnStack2:
  lda  ,x     ;read from flash
  psha      ;move onto stack
  aix  #-1    ;next byte to move
  dbnz STAT, DoOnStack2
EraseSub2:  
  ldhx  ADRS    ;get flash address
  sta  0,x    ;write to flash; latch addr and data
  lda  #mPageErase  ;get flash command
  sta  FCMD    ;write the flash command
  lda  #mFCBEF   ;mask to initiate command
  sta  FSTAT    ;[pwpp] register command
  nop      ;[p] want min 4~ from w cycle to r
ChkDoneErase2:
  lda  FSTAT    ;[prpp] so FCCF is valid
  lsla      ;FCCF now in MSB
  bpl  ChkDoneErase2  ;loop if FCCF = 0
   ldhx STACK
  txs
  rts
 
 
WriteFlashPage:
     LDHX  Address
     STHX  ADRS
     LDA   ALEN                ;load length
     STA   STAT
     STA   LEN
     LDHX  #Array              ;load array address
     STHX  ADRR
     
WR_DATA_L2:
     AIX     #1                ;add 1 to index register
     DBNZ    STAT,WR_DATA_L2   ;decrement & branch if not zero
     
     lda  #(mFPVIOL+mFACCERR) ;mask
     sta  FSTAT           ;abort any command and clear errors
     
     mov  #ProgSubSize, STAT ;length of flash prog routine to STAT
   tsx
   sthx STACK
   ldhx  #ProgSubEnd-1      ;point at last byte to move to stack
 DoOnStack3:
  lda  ,x     ;read from flash
  psha      ;move onto stack
  aix  #-1    ;next byte to move
  dbnz STAT, DoOnStack3
ProgSub2:  
  lda  FSTAT    ;check FCBEF
  and  #mFCBEF   ;mask it
  beq  ProgSub2   ;loop if not empty
  
  ldhx ADRR
  lda  0,x
  aix  #1
  sthx ADRR
  
  ldhx  ADRS    ;get flash address
  sta  0,x    ;write to flash; latch addr and data
  aix  #1
  sthx ADRS
  
  lda  #mBurstProg  ;get flash command
  sta  FCMD    ;write the flash command
  lda  #mFCBEF   ;mask to initiate command
  sta  FSTAT    ;[pwpp] register command
  dbnz LEN,ProgSub2  ;all bytes in a row?
ChkDoneProg2:
  lda  FSTAT    ;[prpp] so FCCF is valid
  lsla      ;FCCF now in MSB
  bpl  ChkDoneProg2  ;loop if FCCF = 0
  
  ldhx STACK
  txs
   rts
Labels (1)
0 Kudos
Reply
3 Replies

1,407 Views
ok2ucx
Contributor IV
Hallo,
 
maybe one little thing overlooked - the Flash module needs (before programming) the proper clock to be programmed - writting into FCDIV register - just like what bootloader does during its initialization:
 
     LDA    FSTAT
     ORA    #mFACCERR                   
     STA    FSTAT     ; clear any FACCERR flag

     LDA    #%00001101  ; div by 13 to fit into 150-200kHz Flash clock!
     STA  FCDIV
 
The best (and original) source of these routines is the S08 core reference manual (HCS08RM.pdf if I remember well) available from all S08 summary pages. All details explained there.
 
Hope it helps,
regards Pavel, ok2ucx (AN2295 developper :smileyhappy:
 
Freescale Roznov Labs, Czech Republic EU
0 Kudos
Reply

1,407 Views
Gcoakley
Contributor I
Thanks Pavel,
 
I did overlook the clock programming.  I will also look at the S08 core reference manual to check and possibly rewrite the code.  Thanks again.
 
-gcoakley
0 Kudos
Reply

1,407 Views
Gcoakley
Contributor I

Ok, I have done a rewrite of the flash functions and the erase call seems to work perfectly.  I am having trouble with the flash writing function however.  If I step through or use breakpoints to step through one byte write at a time, the memory locations are written to correctly.  But if I execute the code without breakpoints, the memory locations are filled with the character (0x04).  This seems like a timing issue, but I am not sure.  I am using Pavel's clock setting below to set the clock correctly.  Any advice would be appreciated.  I have attached the code below.

-gcoakley

 

prog64:
         LDA   #$40
         STA   $715
   prog64loop:
         LDA   $1825       ;check flash status register
         AND   #$80        ;mask for empty flag
         BEQ   prog64loop
         
         LDHX  $700        ;load source address
         LDA   0,x
         AIX   #1
         STHX  $700        ;store new source address+1
         
         LDHX  $705        ;load destination address
         STA   0,x
         AIX   #1
         STHX  $705        ;store new destination address+1
         
         LDA   #$25        ;burst program command
         STA   $1826       ;write command
         LDA   #$80        ;mask to initiate command
         STA   $1825       ;register command
         
         LDA   $715        ;load loop counter
         DECA              ;decrement counter
         STA   $715        ;save counter
         CMP   #$0
         BNE   prog64loop
   prog64chk:
         LDA   $1825       ;check flash status register
         LSLA
         BPL   prog64chk   ;loop if FCCF = 0
         RTS

0 Kudos
Reply