unsigned char StorageDataToFlashAsm(unsigned short a,unsigned char d){
asm{
psha ;temporarily save entry data FlashProg1:
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 ;Z = 0 means there was an error
DoOnStack: pshx
pshh ;save pointer to flash
psha ;save command on stack
ldhx #SpSubEnd ;point at last byte to move to stack
SpMoveLoop: lda ,x ;read from flash
psha ;move onto stack
aix #-1 ;next byte to move
cphx #SpSub-1 ;past end?
bne SpMoveLoop ;loop till whole sub on stack
tsx ;point to sub on stack
tpa ;move CCR to A for testing
and #$08 ;check the I mask
bne I_set ;skip if I already set
sei ;block interrupts while FLASH busy
lda SpSubSize+6,sp ;preload data for command
jsr ,x ;execute the sub on the stack
cli ;ok to clear I mask now
bra I_cont ;continue to stack de-allocation
I_set: lda SpSubSize+6,sp ;preload data for command
jsr ,x ;execute the sub on the stack
I_cont: ais #SpSubSize+3 ;deallocate sub body + H:X + command
;H:X flash pointer OK from SpSub
lsla ;A=00 & Z=1 unless PVIOL or ACCERR
rts ;to flash where DoOnStack was called
SpSub: ldhx SpSubSize+4,sp ;get flash address from stack
sta 0,x ;write to flash; latch addr and data
lda SpSubSize+3,sp ;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
ChkDone: lda FSTAT ;[prpp] so FCCF is valid
lsla ;FCCF now in MSB
bpl ChkDone ;loop if FCCF = 0
SpSubEnd: rts ;back into DoOnStack in flash
SpSubSize: equ (*-SpSub)
}
}
谢谢楼上!作者: seuafu2005 时间: 2005-5-19 16:22
SpSub就是一个标号,直接定义。
全部用汇编的话,我建议你把汇编代码单独放在一个.asm后缀的文件里面,不要和C文件混在一起。汇编文件给出入口地址,C文件调用入口地址就可以了。如果汇编和C混在一个文件里面的话,因为汇编语句比较多,定义也比较多,所以容易出错。比如说要进行擦除操作,可以这样写:
Erase_Entry:
ldhx #$17ff ;end address
pshx
pshh
ldhx #$1600 ; start address,erase $1600-17FF
jsr ErasePages
bne erasErr
erasNoErr:
lda #FlashNoErr
sta Err_Flag
bra doneEras
erasErr:
lda #FlashErr
sta Err_Flag
doneEras:
ais #2 ;deallocate end address
rts