📄 iap.inc
字号:
areset:
rjmp breset
breset:
ldi r16,high(ramend) ;sp setup
out sph,r16
ldi r16,low(ramend)
out spl,r16
cli
rcall IO_INIT_IAP
rcall UART1_INIT_IAP
nop
rcall CLOSED_IAP
nop
rcall lcd_rst_iap
rcall lcdbl_on_iap
ldi r16,80
rcall bdelay
rcall lcdinit_iap
ldi r16,100
rcall bdelay
rcall lcd_rst_iap
ldi r16,80
rcall bdelay
rcall lcdinit_iap
ldi r16,100
rcall bdelay
rcall clrdi_IAP
rcall keyc_IAP
lds r16,KEYF
andi r16, 0b11111111
cpi r16, 0
brne bootm11
rjmp bootm99
bootm11: ldi r16,120
rcall bdelay
rcall keyc_IAP
lds r16,KEYF
andi r16, 0b11111111
cpi r16, 0b00000100 ;计数
breq bootm12
rjmp bootm99
bootm12:
rcall show_crlf
rcall show_vid
rcall show_crlf
rcall show_did
rcall show_crlf
bootm13:
;显示:upgrade
ldi zl,low(UPGRADE*2)
ldi zh,high(UPGRADE*2)
ldi r16,1
out rampz,r16
ldi r21,3 ;行
ldi r22,12 ;列
ldi r23,7 ;;个数
rcall dispes_IAP
bootm21:
rcall getc_IAP
cpi r16,0x56 ;'V'
brne bootm22
rcall show_vid
rcall show_crlf
rjmp bootm21
bootm22:
cpi r16,0x44 ;'D'
brne bootm23
rcall show_did
rcall show_crlf
rjmp bootm21
bootm23:
cpi r16,0x46 ;'F'
brne bootm24
rcall show_fver
rcall show_crlf
rjmp bootm21
bootm24:
cpi r16,0x50 ;'P'
brne bootm25
rcall show_pver
rcall show_crlf
rjmp bootm21
bootm25:
cpi r16,0x49 ;'I' iap
brne bootm26
ldi r16,0x49
rcall putc_IAP
bootm255:
;;显示upgrading
ldi zl,low(UPGRADING*2)
ldi zh,high(UPGRADING*2)
ldi r16,1
out rampz,r16
ldi r21,3 ;行
ldi r22,11 ;列
ldi r23,9 ;个数
rcall dispes_IAP
rcall iap
;- - - - - - - - - - - - - - -
lds r16,ERR_IAP
cpi r16,0
breq bootm259
cpi r16, 1
brne bootm25_6
ldi zl,low(ERR_IAP1*2)
ldi zh,high(ERR_IAP1*2)
jmp bootm258
bootm25_6: cpi r16, 2
brne bootm25_7
ldi zl,low(ERR_IAP2*2)
ldi zh,high(ERR_IAP2*2)
jmp bootm258
bootm25_7: cpi r16, 3
brne bootm25_8
ldi zl,low(ERR_IAP3*2)
ldi zh,high(ERR_IAP3*2)
jmp bootm258
bootm25_8:
ldi zl,low(ERR_IAP1*2)
ldi zh,high(ERR_IAP1*2)
jmp bootm258
bootm258:
ldi r16,1
out rampz,r16
ldi r21,3 ;行
ldi r22,11 ;列
ldi r23,9 ;个数
rcall dispes_IAP
rjmp bootm21
;- - - - - - - - - - - - - - -
bootm259:
rjmp bootm99
bootm26:
ldi r16,0x3F ;
rcall putc_IAP
rcall show_crlf
rjmp bootm21
bootm99:
nop
nop
ldi r16,0x45 ;
rcall putc_IAP
nop
nop
nop
nop
ldi r16,0x45 ;
rcall putc_IAP
jmp reset
;- - - - - - - - - - - - - - - - - - - - - - - - -
iap:
cli
clr r16
out rampz,r16
sts ERR_IAP,r16;清 错误种类
ldi yl,0
ldi yh,0
ldi r16,0x0
mov r14,r16
;- - - - - - - - -
iap2: ldi xh,high(0x1400)
ldi xl,low(0x1400)
ldi r17,0xff
iap3:
ldi r16,0xff
st x+,r16
dec r17
breq iap3
;- - - - - - - - -
ldi r16,16
mov r15,r16
ldi zh,high(0x1400)
ldi zl,low(0x1400)
iap5:
push zh
push zl
rcall rdhex;;读到一行数据到0x300
lds r16,ERR_IAP
cpi r16,0
brne iap99
ldi xh,high(0x1300)
ldi xl,low(0x1300);;读到的一行数据(bin)用x索引
pop zl
pop zh
ld r16,x+
ld r8,x+;一行的字节个数
ld r9,x+;要写入的绝对地址
ld r10,x+
ld r11,x+;
mov r16,r11
cpi r16,1
brne iap6
rjmp iap99;;到了文件末尾
iap6:
ld r16,x+
st z+,r16
dec r8
brne iap6 ;;0x300-->0x400
dec r15 ;16次 --
breq iap9 ;;接受到256个字节
rjmp iap5
iap9:
;- - - - - - - - - - - - - - - -
lds r16,PORTH
sbr r16,1<<BIT7
sts PORTH,r16 ;????????????关翻板电机
;- - - - - - - - - - - - - - - -
clr zl
mov zh,yl ;;r14: xl页的计数器
out rampz,yh
;- - - - - - - - - - - - - - - -
;;判断写入的页地址是否超过0xf000*2
mov r16,yh
cpi r16,0
breq iap9_0
mov r16,yl
cpi r16,0xDF
brlo iap9_0
ldi r16,2
sts ERR_IAP,r16
rjmp iap99
;- - - - - - - - - - - - - - - -
iap9_0:
ldi xh,high(0x1400)
ldi xl,low(0x1400)
rcall Write_page
lds r16,ERR_IAP
cpi r16,0
brne iap99
adiw yh:yl,1
rjmp iap2
iap99:
ret
;- - - - - - - - - - - - - - - - - - - - - - - - -
clrbm:
ldi xh,high(0x1200)
ldi xl,low(0x1200)
ldi r17,0xff
clrbm1:
ldi r16,0xff
st x+,r16
dec r17
breq clrbm1
ldi xh,high(0x1300)
ldi xl,low(0x1300)
ldi r17,0xff
clrbm2:
ldi r16,0xff
st x+,r16
dec r17
breq clrbm2
ret
;- - - - - - - - - - - - - - - - - - - - - - - - -
rdhex:;;收到的一行数据字节存到0x300
rcall clrbm
rdhex1: ldi r16,0x43 ;'C'
rcall putc_IAP
rdhex2: clr r21 ;recheve
ldi xh,high(0x1200)
ldi xl,low(0x1200)
rdhex3: rcall getc_IAP
st x+,r16
inc r21 ;接受ASCII字符的个数统计
cpi r16,0xa ;换行
breq rdhex4
rjmp rdhex3
rdhex4: ;2asi to 1 bin
ldi xh,high(0x1200)
ldi xl,low(0x1200)
ldi zh,high(0x1300)
ldi zl,low(0x1300)
ld r16,z+
ld r16,x+
cpi r16,0x3a ;;:
breq rdhex5
rjmp rdhex21 ;;重发
rdhex5:
ld r16,x+
cpi r16,0xd ;;回车
breq rdhex8
rcall hextobin
brtc rdhex6
rjmp rdhex21 ;;重发
rdhex6:
mov r17,r16
ld r16,x+
cpi r16,0xd
breq rdhex8
rcall hextobin
brtc rdhex7
rjmp rdhex21 ;;重发
rdhex7:
swap r17
add r16,r17
st z+,r16 ;;2asi to 1 bin暂存到 z(0x1300)
rjmp rdhex5
rdhex8:
dec r21
dec r21
dec r21
lsr r21;;字节个数
ldi zh,high(0x1301) ;check sum
ldi zl,low(0x1301)
clr r17
rdhex9:
ld r16,z+
add r17,r16
dec r21
brne rdhex9
cpi r17,0
breq rdhex10
rjmp rdhex21
rdhex10:
ret
rdhex21:
ldi r16,1
sts ERR_IAP,r16
rjmp rdhex10
rcall clrbm
ldi R16,0x52 ; 'R' repeat
rcall putc_IAP
rjmp rdhex2
;- - - - - - - - - - - - - - - - - - - - - - - - -
hextobin:
clt
cpi r16,0x30
brsh hextobin1
set
rjmp hextobin9
hextobin1:
cpi r16,0x3a
brsh hextobin2
clt
subi r16,0x30
rjmp hextobin9
hextobin2:
cpi r16,0x41
brsh hextobin4
set
rjmp hextobin9
hextobin4:
cpi r16,0x47
brlo hextobin5
set
rjmp hextobin9
hextobin5:
clt
subi r16,0x37
hextobin9:
ret
;- - - - - - - - - - - - - - - - - - - - - - - - -
;- - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - -
;-the routine writes one page of data from RAM to Flash
; the first data location in RAM is pointed to by the Y pointer
; the first data location in Flash is pointed to by the Z-pointer
;-error handling is not included
;-the routine must be placed inside the boot space
; (at least the Do_spm sub routine). Only code inside NRWW section can
; be read during self-programming (page erase and page write).
;-registers used: r0, r1, temp1 (r16), temp2 (r17), looplo (r24),
; loophi (r25), spmcsrval (r20)
; storing and restoring of registers is not included in the routine
; register usage can be optimized at the expense of code size
;-It is assumed that either the interrupt table is moved to the Boot
; loader section or that the interrupts are disabled.
.equ PAGESIZEB = PAGESIZE*2 ;PAGESIZEB is page size in BYTES, not words
Write_page:
; page erase
ldi r20, (1<<PGERS) | (1<<SPMEN)
rcall Do_spm
; re-enable the RWW section
ldi r20, (1<<RWWSRE) | (1<<SPMEN)
rcall Do_spm
;transfer data from RAM to Flash page buffer
ldi r24, low(PAGESIZEB) ;init loop variable
ldi r25, high(PAGESIZEB) ;not required for PAGESIZEB<=256
Wrloop:
ld r0, x+
ld r1, x+
ldi r20, (1<<SPMEN)
rcall Do_spm
adiw ZH:ZL, 2
sbiw r25:r24, 2 ;use subi for PAGESIZEB<=256
brne Wrloop
;execute page write
subi ZL, low(PAGESIZEB) ;restore pointer
sbci ZH, high(PAGESIZEB) ;not required for PAGESIZEB<=256
ldi r20, (1<<PGWRT) | (1<<SPMEN)
rcall Do_spm
;re-enable the RWW section
ldi r20, (1<<RWWSRE) | (1<<SPMEN)
rcall Do_spm
;read back and check, optional
ldi r24, low(PAGESIZEB) ;init loop variable
ldi r25, high(PAGESIZEB);not required for PAGESIZEB<=256
subi xl, low(PAGESIZEB) ;restore pointer
sbci xh, high(PAGESIZEB)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -