NELSON RODRIGUEZ

trouble some qy4 mcu's eeprom AN2346 software

Discussion created by NELSON RODRIGUEZ on Aug 3, 2009
Latest reply on Aug 3, 2009 by Tony Papadimitriou

hello i used the firmware described in the aplication note AN2346, in some micros hc08 qy4 it works fine, but in anothers does'nt work and the micro not back to work. why in some qy4 mcus works¿? meanwhile in others crash

 

heres a code that i implemented:

 

EraRnge     equ $2806 ;FLASH erase routine in ROM
PgrRnge     equ $2809 ;FLASH programming routine in ROM
CtrlByt     equ $88   ;control byte for ROM subroutines
CPUSpd      equ $89   ;CPU speed in units of 0.25MHz
LstAddr     equ $8A   ;8a ;last FLASH address to be programmed

; code section
MyCode:     SECTION
; this assembly routine is called the C/C++ application

           

          
           
;*******************************************************************************
;*Rutinas de retardos
;*******************************************************************************
delay_100us:    lda #$6A   ;106
                dbnza $
                rts
               
delay_500us:    lda #$6A  
                dbnza $
                lda #$6A  
                dbnza $
                lda #$6A  
                dbnza $
                lda #$6A  
                dbnza $
                lda #$6A  
                dbnza $
                rts
               
delay_1ms:      jsr delay_500us
                jsr delay_500us
                rts;
;//******************************************************************************
;*********************************************************************
;* *
;* FindClear - Finds first erased block within page *
;* *
;* Inputs: H:X - pointing to start of page used for required data *
;* Stack - block size last thing on stack *
;* *
;* Returns if erased block found: *
;* H:X - pointing to start of first erased block in page *
;* A - $FF *
;* Returns if no erased block found (page full): *
;* H:X - pointing to start of last written block *
;* A - $00 *
;* *
;*********************************************************************

FindClear:  lda #$40      ;number of bytes in a page
            sub 3,sp      ;less number in first block
            psha          ;save bytes left

floop:      lda ,x        ;get first data byte in block
            cmp #$FF      ;erased byte ?
            beq finish1   ;if so then exit, otherwise try next

            pula          ;bytes left
            sub 3,sp      ;less number in next block
            psha          ;resave bytes left
            bmi finish2   ;enough for another block ?

            txa           ;yes, get LS byte of address
            add 4,sp      ;add block size
            tax           ;put it back (can’t be a carry)
            bra floop     ;and try again

finish2:    clra          ;no room (A shouldn’t be $FF)
finish1:    ais #1        ;fix stack pointer
            rts



 ;*********************************************************************
;* *
;* WrtBlock - Writes a block of data into FLASH from RAM buffer *
;* *
;* Calling convention: ldhx #Blk1page *
;* lda #Blk1Size *
;* jsr WrtBlock *
;* *
;* Inputs: H:X - pointing to start of FLASH page used for data *
;* A - block size *
;* *
;* Returns: nothing *
;* *
;* Uses: FindClear, EraRnge (ROM), PgrRnge (ROM) *
;* *
;*********************************************************************

WrtBlock: mov #13,CPUSpd  ;3.2MHz/0.25MHz = 13
          clr CtrlByt     ;page (not mass) erase
          psha            ;save block size
          bsr FindClear   ;find first available erased block
          cmp #$FF        ;erased block found ?
          beq blkfnd      ;if so write to it
          jsr EraRnge     ;if not then erase page
          txa             ;get LS byte of FLASH address
          and #$C0        ;and reset it to start of page
          tax             ;H:X now pointing to first block
blkfnd:   pula            ;get block size
          pshx            ;save start address LS byte
          add 1,sp        ;add block size to LS byte
          deca            ;back to last address in block
          tax             ;last address now in H:X
          sthx LstAddr    ;save in RAM for use by ROM routine
          pulx            ;restore X (H hasn’t changed)
          jmp PgrRnge     ;program block (includes RTS)

;*********************************************************************
;* *
;* RdBlock - Reads a block of data from FLASH and puts it in RAM *
;* *
;* Calling convention: ldhx #Blk1page *
;* lda #Blk1Size *
;* jsr RdBlock *
;* *
;* Inputs: H:X - pointing to start of FLASH page used for data *
;* A - block size *
;* *
;* Returns: H:X - pointing to start of FLASH block containing data *
;* A - data from first byte of block *
;* *
;* Uses: FindClear *
;* *
;*********************************************************************

RdBlock:  psha            ;save block size
          bsr FindClear   ;find first erased block
          cmp #$FF        ;was an erased block found ?
          bne skipdec     ;if not then don’t go back a block
          txa             ;get LS byte of address
          and #$3F        ;only look at address within page
          beq skipdec     ;if 0 then no data so don’t go back
          txa             ;if not get LS byte of address again
          sub 1,sp        ;and subtract block size to point
          tax             ;to start of valid data block
skipdec:  lda ,x          ;get first byte of data
          ais #1          ;de-allocate stack
          rts 
         
save_rom:   ldhx #$FB00       ;FLASH page address used for this data            
            jsr EraRnge            
            ldhx #$FB00       ;FLASH page address used for this data
            lda #14           ;data block size
            jsr WrtBlock      ;write the block of data from RAM to FLASH
            rts           
         
         
read_rom:   ldhx #$FB00       ;example FLASH page for data
            lda #14           ;example data block size
            psha              ;save initial byte count (block size)
            jsr RdBlock ;get pointer to latest data block
            txa ;get address LS byte
            add 1,sp ;add block size
            deca ;and decrement
            tax ;H:X now point to last byte in block
again:      pshx
            pshh ;save H:X
            lda ,x ;get a byte of data
            clrh
            ldx 3,sp ;byte count now in H:X
            sta datoee1-1,x ;put byte into RAM
            pulh
            pulx ;retrieve FLASH data pointer
            decx ;and point to next (previous) byte
            dec 1,sp ;decrement byte count
            bne again ;finished ?
            pula ;fix stack           
            rts                        

Outcomes