Problem in programming FLASH with Do_On_Stack routine

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

Problem in programming FLASH with Do_On_Stack routine

5,760 Views
Gizzzmo
Contributor I
Hi,

I'm using the MC9S12E128 part.

I'm using the Freescale LRAE (flash bootloader allowing to load a program in RAM) to load a bootloader in the RAM. My bootloader have then to erase FLASH and write a progream into it.

I've used the AN2720 and AN2153 to do this. I'm using the AN2720SW (flash.c and Do_On_Stack.asm) to erase/write to flash. But i've a problem because my program blocks in the Do_On_Stack routine. Moreover, because of my bootloader in RAM, i can't use the BDM to make step-by-step.

Do you know why my program can block in the Do_On_Stack routine ?

;*******************************************************************
;* MOTOROLA
;*
;* DESCRIPTION: S12 Flash Asm Routines
;* SOURCE: Do_On_Stach.asm
;* COPYRIGHT: © 04/2004 Made in the USA
;* AUTHOR: rat579
;* REV. HISTORY: 060304 - fixed CCR return value and optimized
;* in SpSub routine
;*
;*******************************************************************/
;*****************************************************************************
; Local defines
;*****************************************************************************
CBEIF EQU $80
FSTAT EQU $105
FCMD EQU $106
CCIF EQU $40
PAGE_ADDR EQU $30

xdef DoOnStack
;*********************************************************************
;* DoOnStack - copy SpSub onto stack and call it (see also SpSub)
;* De-allocates the stack space used by SpSub after returning from it.
;* Allows final steps in a flash prog/erase command to execute out
;* of RAM (on stack) while flash is out of the memory map
;* This routine can be used for flash word-program or erase commands
;*
;* Calling Convention:
;* jsr DoOnStack
;*
;* Uses 32 bytes on stack + 3 bytes if Call instruction used
;********************************************************************
DoOnStack:
pshb ;save B - PPAGE
ldx #SpSubEnd-2 ;point at last word to move to stack
SpmoveLoop: ldd 2,x- ;read from flash
pshd ;move onto stack
cpx #SpSub-2 ;past end?
bne SpmoveLoop ;loop till whole sub on stack
tfr sp,x ;point to sub on stack
ldaa #CBEIF ;preload mask to register command
call 0,x,00 ;execute the sub on the stack
leas SpSubEnd-SpSub,sp ;de-allocate space used by sub
pulb ;restore B
rtc ;to flash where DoOnStack was called
; assume banked calling function

;*********************************************************************
;* SpSub - register flash command and wait for Flash CCIF
;* this subroutine is copied onto the stack before executing
;* because you can't execute out of flash while a flash command is
;* in progress (see DoOnStack to see how this is used)
;*
;* Note: must be even # of bytes!
;*
;* Uses 24 bytes on stack + 3 bytes for CALL above
;*********************************************************************
EVEN ;Make code start word aliened
SpSub:
ldab SpSubEnd-SpSub+3,sp ;get PPAGE back from stack
stab PAGE_ADDR ;Store the PPAGE address
tfr ccr,b ;get copy of ccr
orcc #$10 ;disable interrupts
staa FSTAT ;[PwO] register command
nop ;[O] wait min 4~ from w cycle to r
nop ;[O]
nop ;[O]
brclr FSTAT,CCIF,* ;[rfPPP] wait for queued commands to finish
tfr b,ccr ;restore ccr and int condition
rtc ;back into DoOnStack in flash
SpSubEnd:

/**********************************************************************/

Thanks
Labels (1)
0 Kudos
Reply
5 Replies

989 Views
Gizzzmo
Contributor I
I have found where i'm stucked. The flash is well erased/write but i'm stucked in the loop "brclr FSTAT,CCIF,*;". Is there a problem with this line ?

Thanks.
0 Kudos
Reply

989 Views
rocco
Senior Contributor II
I think so. I believe that
        brclr   FSTAT,CCIF,*

should be
        brclr   6,FSTAT,*

Since CCIF is defined as a mask ($40), it's no good in a BRCLR instruction. I prefer to make definitions more like this:
CCIFb:  equ     6       ;command complete flag bit
CCIFm:  equ     $40     ;command complete flag mask

Message Edited by rocco on 2006-06-19 12:45 PM

0 Kudos
Reply

989 Views
imajeff
Contributor III
rocco,

You sound like your talking about HC08 (I guess, or I do it with HC05). The HC12 CPU uses the mask (i.e. $40) instead of the bit position. Also note the addr comes first, then the mask in the HC12 instructions.
brclr   FSTAT,CCIF,*
0 Kudos
Reply

989 Views
rocco
Senior Contributor II
Oops, sorry, that was an HC08ism. I forgot what forum I was in . . .
0 Kudos
Reply

989 Views
Sten
Contributor IV
I didn't look too much, but I do not see where you are writing the data and address to the Flash???
0 Kudos
Reply