1 /* Name: spminterface.h
2 * Project: USBaspLoader
3 * Author: Stephan Baerwolf
4 * Creation Date: 2012-08-01
5 * Copyright: (c) 2012 by Stephan Baerwolf
6 * License: GNU GPL v2 (see License.txt)
9 #ifndef SPMINTERFACE_H_f70ba6adf7624275947e859bdbff0599
10 #define SPMINTERFACE_H_f70ba6adf7624275947e859bdbff0599
13 * spminterface.h offers a lightweight interface by inserting
14 * an small machine-subroutine into the bootsection. (right
15 * after the interrupt-vector-table)
16 * This subroutine can be called by normal code in order to
17 * enable it to program the flash (and depending on BLB11-lockbit
18 * also the bootsection itself). Since SPM-calls from RWW-sections
19 * will fail to work. The Routine will be called "bootloader__do_spm".
20 * Its principle assembler-code is depicted below (real code is a
21 * little machinedependen).
22 * Interfaces will be the 8-bit registers r10..r13, for details
23 * also see below. As also the pageaddress-registes (Z and rampZ)
24 * are interfaced via different registers, it is possible to call
25 * this routine via indirect call (icall).
26 * Traditionally it is also possible to rcall, therefore you can
27 * define "bootloader__do_spm" for your normal code via defsym at
29 * Example for an atmega8: "-Wl,--defsym=transfer_point=0x1826"
30 * (since BOOTLOADER_ADDRESS is 0x1800 and there are
31 * 2x19 = 38 = 0x26 byte for interrupts)
35 ;disable interrupts (if enabled) before calling!
36 ;you may also want to disable wdt, since this routine may busy-loop
37 ;==================================================================
39 ;spmcr (spmcrval determines SPM action) will be register: r18
40 ;MCU dependend RA(MPZ should be transfered within register: r11
41 ;lo8(Z) should be transfered within register: r12
42 ;hi8(Z) should be transfered within register: r13
43 ;( as definition of SPM low8bit of dataword are stored within r0 )
44 ;( as definition of SPM hi8bit of dataword are stored within r1 )
47 ;temp0 will be register: r11
48 ;temp1 will be register: r12
49 ;temp2 will be register: r13
50 ;spmcrval (r18) may also be changed due to rww reenable-phase r18
51 ;Z (r31:r30) wil be changed during operation
54 ;==================================================================
55 ; TODO: waitA and waitB could be merged to subroutine saving 2 opc
56 ;==================================================================
58 ;load pageaddress (Z) from (r11:)r13:12 since it may was used for icall
63 waitA: ;check for pending SPM complete
68 out SPMCR, spmcrval ;SPM timed sequence
71 waitB: ;check for previous SPM complete
76 ;avoid crash of userapplication
77 ldi spmcrval, ((1<<RWWSRE) | (1<<SPMEN))
88 #include <avr/pgmspace.h>
90 #ifndef BOOTLOADER_ADDRESS
91 // this header is the interface for user-code
96 #else /*ifndef BOOTLOADER_ADDRESS*/
97 // this header is used directly within bootloader_do_spm
98 #include "bootloaderconfig.h"
100 #if HAVE_SPMINTEREFACE
104 * insert architecture dependend "bootloader_do_spm"-code
106 * try to make this array as big as possible
107 * (so bootloader always uses 2kbytes flash)
109 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8HVA__)
110 //assume SPMCR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6
111 const uint16_t bootloader__do_spm
[19] PROGMEM
= {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
112 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF, 0xFFFF,
115 00001826 <bootloader__do_spm>:
117 1828: ec 2d mov r30, r12
118 182a: fd 2d mov r31, r13
121 182c: b7 b6 in r11, 0x37 ; 55
122 182e: b0 fc sbrc r11, 0
123 1830: fd cf rjmp .-6 ; 0x182c <waitA>
124 1832: 27 bf out 0x37, r18 ; 55
128 1836: b7 b6 in r11, 0x37 ; 55
129 1838: b0 fc sbrc r11, 0
130 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
131 183c: 21 e1 ldi r18, 0x11 ; 17
132 183e: b7 b6 in r11, 0x37 ; 55
133 1840: b6 fc sbrc r11, 6
134 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
142 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48P__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
143 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
144 const uint16_t bootloader__do_spm
[19] PROGMEM
= {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
145 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF, 0xFFFF,
148 00001826 <bootloader__do_spm>:
150 1828: ec 2d mov r30, r12
151 182a: fd 2d mov r31, r13
154 182c: b7 b6 in r11, 0x37 ; 55
155 182e: b0 fc sbrc r11, 0
156 1830: fd cf rjmp .-6 ; 0x182c <waitA>
157 1832: 27 bf out 0x37, r18 ; 55
161 1836: b7 b6 in r11, 0x37 ; 55
162 1838: b0 fc sbrc r11, 0
163 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
164 183c: 21 e1 ldi r18, 0x11 ; 17
165 183e: b7 b6 in r11, 0x37 ; 55
166 1840: b6 fc sbrc r11, 6
167 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
175 #elif defined (__AVR_ATmega48A__) || defined (__AVR_ATmega48PA__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88PA__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168PA__) || defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
176 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
177 const uint16_t bootloader__do_spm
[19] PROGMEM
= {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
178 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF, 0xFFFF,
181 00001826 <bootloader__do_spm>:
183 1828: ec 2d mov r30, r12
184 182a: fd 2d mov r31, r13
187 182c: b7 b6 in r11, 0x37 ; 55
188 182e: b0 fc sbrc r11, 0
189 1830: fd cf rjmp .-6 ; 0x182c <waitA>
190 1832: 27 bf out 0x37, r18 ; 55
194 1836: b7 b6 in r11, 0x37 ; 55
195 1838: b0 fc sbrc r11, 0
196 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
197 183c: 21 e1 ldi r18, 0x11 ; 17
198 183e: b7 b6 in r11, 0x37 ; 55
199 1840: b6 fc sbrc r11, 6
200 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
208 #elif defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164PA__) || defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324PA__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644PA__) || defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
209 //assume SPMCR:=SPCSR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6
210 const uint16_t bootloader__do_spm
[19] PROGMEM
= {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
211 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF, 0xFFFF,
214 00001826 <bootloader__do_spm>:
216 1828: ec 2d mov r30, r12
217 182a: fd 2d mov r31, r13
220 182c: b7 b6 in r11, 0x37 ; 55
221 182e: b0 fc sbrc r11, 0
222 1830: fd cf rjmp .-6 ; 0x182c <waitA>
223 1832: 27 bf out 0x37, r18 ; 55
227 1836: b7 b6 in r11, 0x37 ; 55
228 1838: b0 fc sbrc r11, 0
229 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
230 183c: 21 e1 ldi r18, 0x11 ; 17
231 183e: b7 b6 in r11, 0x37 ; 55
232 1840: b6 fc sbrc r11, 6
233 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
242 #error "bootloader__do_spm has to be adapted, since there is no architecture code, yet"
247 #endif /*ifdef BOOTLOADER_ADDRESS*/