-
Notifications
You must be signed in to change notification settings - Fork 13
/
tinyBoot.S
63 lines (54 loc) · 1.42 KB
/
tinyBoot.S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* tinyBoot - reset vector replacement code for ATtiny series
* @author: Ralph Doncaster
* @version: $Id: picoboot.S 30 2013-09-01 15:08:10Z [email protected] $
*/
#define DATAPAGE (FLASHEND - 127 - SPM_PAGESIZE)
#define spmArg r17
#define dataPageLo r18
#define dataPageHi r19
#define chipErased r22
#define appJumpLo r24
#define appJumpHi r25
; save application boot vector by writing to DATAPAGE
SaveAppStart:
movw ZL, dataPageLo
movw r0, appJumpLo
rcall DoSPM
movw appJumpLo, r2 ; zero appJump
rjmp WritePage
; write page buffer to flash - jumps to CommandLoop when done
WritePage:
rcall ErasePage
ldi spmArg, ((1<<PGWRT)|(1<<SPMEN))
rcall DoSPM
; check if AppJump needs to be written
sbiw appJumpLo, 0
brne CommandLoop
rjmp SaveAppStart
ChipErase:
ldi chipErased, (1<<PGERS)
movw ZL, dataPageLo
; fall into ErasePage
; check Z pointer to see if it points to bootloader section
ErasePage:
mov spmArg, chipErased
;movw tmpWordLo, ZL
; subtract bootloader start address
;subi tmpWordLo, lo8(DATAPAGE)
;sbci tmpWordHi, hi8(DATAPAGE)
cpi ZL, hi8(DATAPAGE) ; 256 byte protection area
brcs DoSPM
ret ; block write to bootloader section
DoSafeSPM:
sbiw ZL, 0
brne DoSPM
movw appJumpLo, r0 ; save application starting opcode
; now replace r0, r1 with current bootload vector
lpm r0, Z+
lpm r1, Z
; fall through to DoSPM
DoSPM:
ori spmArg, (1<<SPMEN)
out SPMCSR, spmArg
spm
ret