atmega323l ATMEL Corporation, atmega323l Datasheet - Page 185

no-image

atmega323l

Manufacturer Part Number
atmega323l
Description
Atmega323 8-bit Avr Microcontroller With 32k Bytes Of In-system Programmable Flash
Manufacturer
ATMEL Corporation
Datasheet
Assembly code example for a
Boot Loader
1457G–AVR–09/03
;- the routine writes one page of data from RAM to Flash the first data
: location in RAM is pointed to by the Y pointer (lowest address) the first
; data location in Flash is pointed to by the Z-pointer (lowest address)
; - error handling is not included – the routine must be placed inside the
; boot space. Only code inside boot loader section should be read during
; self-programming.
;- registers used: r0, r1, temp1, temp2, looplo, loophi, spmcrval storing and
; restoring of registers is not included in the routine
;
;- It is assumed that the interrupts are disabled
.equ PAGESIZEB = PAGESIZE*2
.org SMALLBOOTSTART
Write_page:
; page erase
Wrloop:
Rdloop:
Return:
; re-enable the Application Section
; transfer data from RAM to Flash page buffer
; execute page write
; re-enable the Application Section
; read back and check, optional
; return to Application Section
; verify that Application Section is safe to read
; re-enable the Applicaiton Section
register usage can be optimized at the expense of code size
ldi
call Do_spm
ldi
call Do_spm
ldi
ldi
ld
ld
ldi
call Do_spm
adiw ZH:ZL, 2
sbiw loophi:looplo, 2
brne Wrloop
subi ZL, low(PAGESIZEB)
sbci ZH, high(PAGESIZEB)
ldi
call Do_spm
ldi
call Do_spm
ldi
ldi
subi YL, low(PAGESIZEB)
sbci YH, high(PAGESIZEB)
lpm
ld
cpse r0, r1
jmp
sbiw loophi:looplo, 2
brne Rdloop
in
sbrs temp1, ASB
ret
ldi
spmcrval, (1<<PGERS) + (1<<SPMEN)
spmcrval, (1<<ASRE) + (1<<SPMEN)
looplo, low(PAGESIZEB) ;init loop variable
loophi, high(PAGESIZEB);not required for PAGESIZEB<=256
r0, Y+
r1, Y+
spmcrval, (1<<SPMEN)
spmcrval, (1<<PGWRT) + (1<<SPMEN)
spmcrval, (1<<ASRE) + (1<<SPMEN)
looplo, low(PAGESIZEB) ;init loop variable
loophi, high(PAGESIZEB);not required for PAGESIZEB<=256
r0, Z+
r1, Y+
Error
temp1, SPMCR
spmcrval, (1<<ASRE) + (1<<SPMEN)
;PAGESIZEB is page size in BYTES, not words
;use subi for PAGESIZEB<=256
;restore pointer
;not required for PAGESIZEB<=256
;restore pointer
;use subi for PAGESIZEB<=256
; If ASB is set, the AS is not ready yet
ATmega323(L)
185

Related parts for atmega323l