www.pudn.com > flashhuibian.rar > flash.asm
*********************************************************************************
* seeddsk.asm v1.00 *
* copy (c) 2002- SEED Lt.d *
* function£º flash writing and the way of bootloader from flash *
* design£º lifeng duan *
*********************************************************************************
.copy "flash.h"
.copy "VC5402.h"
.global _main
.mmregs
***********************************************************************************
* *
* when running this code ,please load the blink.out under flashboot\ *
* blink\. *
* *
***********************************************************************************
stack .usect ".stack",64
.text
__STACK_SIZE .set 40H
SWWSR_VALI .set 0FFFFH ;I/O:10w/s;Data U:10w/s,L:4w/s;PROG U:10w/s,L:4w/s
SWCR_VALI .set 0000H ;Wait states x2
***********************************************************************************
* *
* we boot the blink.out in this example *
* init the DSP vc5402 *
***********************************************************************************
_main: STM #(stack+__STACK_SIZE-1),SP ; set to beginning of stack memory
; add size to get to top
ANDM #0fffeh,*(SP) ; make sure it is an even address
RSBX SXM ; make the led is off
SSBX XF ; turn on LED
ANDM #0000H,*(IMR) ; forbiden all interruption
ANDM #OVLY_0,*(PMST) ; OVLY = 0
RSBX CPL ; make the DP is ok
STM #SWWSR_VALI,SWWSR ; set the SWWSR
STM #SWCR_VALI,SWCR
LD #04H,DP ; set the dp register
STM #0,AH
STM #FLASHENB,AL
STL A,1H
NOP
NOP
NOP
NOP
PORTW 1H,CNTL2 ; make flash is at high 512k of the program memory
NOP
NOP
NOP
NOP
NOP
****************************************************************************************
* erase flash ¡¡ *
****************************************************************************************
CALL flash_erase
****************************************************************************************
* the firt step of the bootload: *
* write the address of the boottable to 0xFFFF,which is the address of the data *
* space *
* the address of the Seeddsk's flash is 0x4000~0xFFFF,so ,the first address of our *
* boottable at 0x4000. *
* the first address of our flash in program is 0x80000,so, we write 0x4000 to *
* 0x84000. *
****************************************************************************************
STM #Flash_base,AH ;the entrance for write flash is 0x80000
STM #0FFFFH,AL ;the offsize is 0x4000
ST #4000H,4H ; the data which write to flash is 0x203H
CALL flash_write
BC next1,ANEQ ;if A!=0 ,write next data
B $
****************************************************************************************
* the second step of the bootload: *
* write the first word of the boot table ,which is tell the DSP how many bit flash *
* you selected. we select 16-bit flash ,so we write 0x10aa to this word. *
****************************************************************************************
next1: STM #Flash_base,AH
STM #4000H,AL
ST #10AAH,4H
CALL flash_write
BC next2,ANEQ ;if A!=0 ,write next data
B $
******************************************************************************************
* the third step of the bootload: *
* write the second word of the boot table ,which will initialize the value of SWWSR *
* we write 0xffff to this register ,this value is reset value *
******************************************************************************************
next2: STM #Flash_base,AH
STM #4001H,AL
ST #0FFFFH,4H
CALL flash_write
BC next3,ANEQ ;if A!=0 ,write next data
B $
******************************************************************************************
* the four step of the bootload: *
* write the third word of the boot table ,which will initialize the value of BSCR *
* we write 0x800 to this register, *
******************************************************************************************
next3: STM #Flash_base,AH
STM #4002H,AL
ST #0800H,4H
CALL flash_write
BC next4,ANEQ ;if A!=0 ,write next data
B $
******************************************************************************************
* the five step of the bootload: *
* write the Entry point (Xpc) and write the Entry point PC *
* you can see the blink.map ,find the address of _cinit00 *
* in the example XPC is 0 PC is 0x1036
******************************************************************************************
next4: STM #Flash_base,AH ;write the XPC to 4003
STM #4003H,AL
ST #0000H,4H
CALL flash_write
BC next5,ANEQ ;if A!=0 ,write next data
B $
next5: STM #Flash_base,AH ;write the PC to 4004
STM #4004H,AL
ST #1036H,4H
CALL flash_write
BC next6,ANEQ ;if A!=0 ,write next data
B $
******************************************************************************************
* the sixth step of the bootload: *
* write the code to flash ,we only write the code and the INITIALIZED section. *
* First ,write the size of the section *
* Second ,write the destination XPC of the section *
* Third ,write the destination PC of the section *
* fourth, write the data of the code *
* so ,writing the first section *
******************************************************************************************
next6: STM #Flash_base,AH ;write the size of the first section(.cinit)to 4005
STM #4005H,AL
ST #036H,4H
CALL flash_write
BC next7,ANEQ ;if A!=0 ,write next data
B $
next7: STM #Flash_base,AH ;write the xPC of the first section(.cinit)to 4006
STM #4006H,AL
ST #0,4H
CALL flash_write
BC next8,ANEQ ;if A!=0 ,write next data
B $
next8: STM #Flash_base,AH ;write the PC of the first section(.cinit)to 4007
STM #4007H,AL
ST #01000H,4H
CALL flash_write
BC next9,ANEQ ;if A!=0 ,write next data
B $
******************************************************************************************
* save the address of flash to 6H *
******************************************************************************************
next9: STM #Flash_base,AH
STM #4008H,AL
DST A,6H ;save the address of the flash
STM #0,AH
STM #01000H,AL
STLM A,AR1 ;write the address of fisrt section(.cinit) to AR1
NOP
NOP
NOP
STM #0,AH
STM #036H,AL
STLM A,AR2 ;AR2 is the longth of we recieved fisrt section(.cinit)
DLD 6H,A ;get the address of flash
CALL bwrite ;write to flash
BC bnext1,ANEQ ;if(a=0),write to flash is error
B $
bnext1: NOP
NOP
DLD 2H,A
NOP
NOP
ADD #1,A ;get the end address to A
NOP
NOP
DST A,6H ;save next block address of flash
*****************************************************************************************
* write the sencod section (.text) *
*****************************************************************************************
DLD 6H,A ;get the address of flash
ST #035aH,4H
CALL flash_write ;write the size of the sencod section(.text)
BC nexta,ANEQ ;if A!=0 ,write next data
B $
nexta: DLD 6H,A ;get the address of flash
NOP
NOP
ADD #1,A ;get the end address to A
NOP
NOP
DST A,6H ;save next block address of flash
DLD 6H,A ;get the address of flash
ST #0,4H
CALL flash_write ;write the XPC of the sencod section(.text)to 4006
BC nextb,ANEQ ;if A!=0 ,write next data
B $
nextb: DLD 6H,A ;get the address of flash
NOP
NOP
ADD #1,A ;get the end address to A
NOP
NOP
DST A,6H ;save next block address of flash
DLD 6H,A ;get the address of flash
ST #1036H,4H
CALL flash_write ;write the PC of the sencod section(.text)
BC nextc,ANEQ ;if A!=0 ,write next data
B $
nextc: DLD 6H,A ;get the address of flash
NOP
NOP
ADD #1,A ;get the end address to A
NOP
NOP
DST A,6H ;save next block address of flash
STM #0,AH
STM #01036H,AL
STLM A,AR1 ;write the address of sencod section(.text) to AR1
NOP
NOP
NOP
STM #0,AH
STM #035aH,AL
STLM A,AR2 ;AR2 is the longth of the sencod section(.text)
DLD 6H,A ;get the address of flash
CALL bwrite ;write to flash
BC bnext2,ANEQ ;if(a=0),write to flash is error
B $
bnext2: NOP
NOP
DLD 2H,A
NOP
NOP
ADD #1,A ;get the end address to A
NOP
NOP
DST A,6H ;save next block address of flash
******************************************************************************************
* write the third section(.vector) to flash *
******************************************************************************************
DLD 6H,A ;get the address of flash
ST #078H,4H
CALL flash_write ;write the size of the third section(.vectors)
BC nextd,ANEQ ;if A!=0 ,write next data
B $
nextd: DLD 6H,A ;get the address of flash
NOP
NOP
ADD #1,A ;get the end address to A
NOP
NOP
DST A,6H ;save next block address of flash
DLD 6H,A ;get the address of flash
ST #0,4H
CALL flash_write ;write the XPC of the third section(.vectors)
BC nexte,ANEQ ;if A!=0 ,write next data
B $
nexte: DLD 6H,A ;get the address of flash
NOP
NOP
ADD #1,A ;get the end address to A
NOP
NOP
DST A,6H ;save next block address of flash
DLD 6H,A ;get the address of flash
ST #300H,4H
CALL flash_write ;write the PC of the third section(.vectors)
BC nextf,ANEQ ;if A!=0 ,write next data
B $
nextf: DLD 6H,A ;get the address of flash
NOP
NOP
ADD #1,A ;get the end address to A
NOP
NOP
DST A,6H ;save next block address of flash
STM #0,AH
STM #0300H,AL
STLM A,AR1 ;write the address of third section(.vectors) to AR1
NOP
NOP
NOP
STM #0,AH
STM #078H,AL
STLM A,AR2 ;AR2 is the longth of the third section(.vectors)
DLD 6H,A ;get the address of flash
CALL bwrite ;write to flash
BC bnext3,ANEQ ;if(a=0),write to flash is error
B $
bnext3: NOP
NOP
DLD 2H,A
NOP
NOP
ADD #1,A ;get the end address to A
NOP
NOP
DST A,6H ;save next block address of flash
******************************************************************************************
* write the fourth section(.vector) to flash *
******************************************************************************************
DLD 6H,A ;get the address of flash
ST #01H,4H
CALL flash_write ;write the size of the fourth section(.trap)
BC next10,ANEQ ;if A!=0 ,write next data
B $
next10: DLD 6H,A ;get the address of flash
NOP
NOP
ADD #1,A ;get the end address to A
NOP
NOP
DST A,6H ;save next block address of flash
DLD 6H,A ;get the address of flash
ST #0,4H
CALL flash_write ;write the XPC of the fourth section(.trap)
BC next11,ANEQ ;if A!=0 ,write next data
B $
next11: DLD 6H,A ;get the address of flash
NOP
NOP
ADD #1,A ;get the end address to A
NOP
NOP
DST A,6H ;save next block address of flash
DLD 6H,A ;get the address of flash
ST #60H,4H
CALL flash_write ;write the PC of the fourth section(.trap)
BC next12,ANEQ ;if A!=0 ,write next data
B $
next12: DLD 6H,A ;get the address of flash
NOP
NOP
ADD #1,A ;get the end address to A
NOP
NOP
DST A,6H ;save next block address of flash
STM #0,AH
STM #060H,AL
STLM A,AR1 ;write the address of fourth section(.trap) to AR1
NOP
NOP
NOP
STM #0,AH
STM #01H,AL
STLM A,AR2 ;AR2 is the longth of the fourth section(.trap)
DLD 6H,A ;get the address of flash
CALL bwrite ;write to flash
BC bnext4,ANEQ ;if(a=0),write to flash is error
B $
bnext4: NOP
NOP
DLD 2H,A
NOP
NOP
ADD #1,A ;get the end address to A
NOP
NOP
DST A,6H ;save next block address of flash
*****************************************************************************************
* the seventh step of the bootload: *
* write the indicates the end of the boot table (0000) *
*****************************************************************************************
DLD 6H,A ;get the address of flash
ST #00H,4H
CALL flash_write ;write the size of the fourth section(.trap)
BC next1f,ANEQ ;if A!=0 ,write next data
next1f: B $
******************************************************************************************
B $ ;boot table is writing over
******************************************************************************************
* FUCTION *
******************************************************************************************
******************************************************************************************
* *
* the block for flash write *
* register A is the distination address of you want to write *
* register AR2 is the longth for you write to flash * *
* register AR1 is the source address of the code to be writen to *
* *
******************************************************************************************
bwrite: DST A,2H ; save the address
LD *AR1+,A
STL A,4H ;store the data to 4H
DLD 2H,A ; get the flash address
CALL flash_write;write a data to flash
BC fover,AEQ ;if A!=0 ,write next data
LDM AR2,A
STM #0,AH
SUB #1,A ; the long size sub one
BC fover1,AEQ ;if A=0 ,write flash is over
STLM A,AR2 ; save the changed long size
DLD 2H,A ; get the flash address
ADD #1,A ; address of flash +1
B bwrite ;write next data
fover1: LD #1,A ; write is over
NOP
NOP
NOP
RET ;exit with OK
fover: NOP ;exit with error
NOP
NOP
RET
******************************************************************************************
* write a 16-bit data to the flash address *
* register A is the address of the flash that we want write to *
* relative address 4H is the data you want to write to the flash *
******************************************************************************************
flash_write: DST A,0H ; save the address of the flash
STM #Flash_base,AH ; The address of the flash is 0x80555H
STM #Flash_5555,AL
STM #Flash_UL1,BL ; write operation code to 1H
WRITA *(BL) ; AAH --> (80555H)
STM #Flash_2AAA,AL ; The address of the flash is 0x802aaH
STM #Flash_UL2,BL ; write operation code to 1H
WRITA *(BL) ; 55H --> (802AAH)
STM #Flash_5555,AL ; The address of the flash is 0x80555H
STM #Flash_PRG,BL ; write operation code to 1H
WRITA *(BL) ; A0H --> (80555H)
DLD 0H,A ; get the flash address
WRITA 4H ; 3H is your data of wanting to write to the flash
write_poll: READA *(BL)
XOR 4H,B ; data ^ B
BITF *(BL),#Polling_Bit
BC write_poll,TC ;Is the action for write "ok"?
READA *(BL)
XOR 4H,B
STM #0,BH ; Is the data is ok?
LD #1,A ; if ok,write 1 to A
XC 1,BNEQ
LD #0,A ; else ,wriet 0 to A
RET
******************************************************************************************
* erase the all the 39VF400b *
******************************************************************************************
flash_erase: STM #Flash_base,AH ; High address of the flash is 0x80000H
STM #Flash_5555,AL
ST #Flash_UL1,1H
WRITA 1H ;AAH --> (80555H)
STM #Flash_2AAA,AL
ST #Flash_UL2,1H
WRITA 1H ;55H --> (802AAH)
STM #Flash_5555,AL
ST #Flash_ERASE,1H
WRITA 1H ;80H --> (80555H)
STM #Flash_5555,AL
ST #Flash_UL1,1H
WRITA 1H ;AAH --> (80555H)
STM #Flash_2AAA,AL
ST #Flash_UL2,1H
WRITA 1H ;55H --> (802AAH)
STM #Flash_5555,AL
ST #Flash_CE,1H
WRITA 1H
nop
nop
nop
nop
nop ;AAH --> (80555H)
erase_poll: STM #0H,AL
READA 1H
BITF 1H,#Polling_Bit
BC erase_poll,NTC ; Is the action for erase "ok"?
STM #0H,AL
STM #03H,BH
STM #0FFFFH,BL
erase_check: READA 1H
CMPM 1H,#Flash_BLANK
BCD erase_err,NTC
ADD #1,A
SUB #1,B
BCD erase_check,BNEQ
NOP
NOP
NOP
NOP
LD #1,A
NOP
NOP
BD erase_end
erase_err: LD #0H,A
erase_end: NOP
RET
.end