24.7.13 Simple Assembly Code Example for a Boot Loader
;-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), spmcrval (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
.org
PAGESIZEB = PAGESIZE*2
SMALLBOOTSTART
PAGESIZEB is page size in BYTES, not words
Write_page:
;
Page Erase
ldi
call
spmcrval, (1<<PGERS) | (1<<SPMEN)
Do_spm
;
ldi
call
re-enable the RWW section
spmcrval, (1<<RWWSRE) | (1<<SPMEN)
Do_spm
;
ldi
ldi
transfer data from RAM to Flash page buffer
looplo, low(PAGESIZEB)
loophi, high(PAGESIZEB)
;init loop variable
;not required for PAGESIZEB<=256
Wrloop:
ld
r0, Y+
ld
r1, Y+
ldi
spmcrval, (1<<SPMEN)
Do_spm
ZH:ZL, 2
loophi:looplo, 2
Wrloop
call
adiw
sbiw
brne
;use subi for PAGESIZEB<=256
;
execute Page Write
ZL, low(PAGESIZEB)
ZH, high(PAGESIZEB)
spmcrval, (1<<PGWRT) | (1<<SPMEN)
Do_spm
subi
sbci
ldi
call
;restore pointer
;not required for PAGESIZEB<=256
;
ldi
call
re-enable the RWW section
spmcrval, (1<<RWWSRE) | (1<<SPMEN)
Do_spm
;
ldi
ldi
read back and check, optional
looplo, low(PAGESIZEB)
loophi, high(PAGESIZEB)
YL, low(PAGESIZEB)
;init loop variable
;not required for PAGESIZEB<=256
;restore pointer
subi
sbci
Rdloop:
lpm
YH, high(PAGESIZEB)
r0, Z+
ld
r1, Y+
cpse
jmp
r0, r1
Error
sbiw
brne
loophi:looplo, 1
Rdloop
;use subi for PAGESIZEB<=256
250
ATmega16/32/64/M1/C1 [DATASHEET]
7647O–AVR–01/15