da465018b9b7778d405db278c15500c840a2635e
[pub/USBaspLoader.git] / firmware / spminterface.h
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)
7 * Version: 0.4
8 */
9
10 #ifndef SPMINTERFACE_H_f70ba6adf7624275947e859bdbff0599
11 #define SPMINTERFACE_H_f70ba6adf7624275947e859bdbff0599
12
13 /*
14 * spminterface.h offers a lightweight interface by inserting
15 * an small machine-subroutine into the bootsection. (right
16 * after the interrupt-vector-table)
17 * This subroutine can be called by normal code in order to
18 * enable it to program the flash (and depending on BLB11-lockbit
19 * also the bootsection itself). Since SPM-calls from RWW-sections
20 * will fail to work. The routine will be called "bootloader__do_spm".
21 * Its principle assembler-code is depicted below (real code is a
22 * little machinedependend).
23 * Interfaces will be the 8-bit registers r10..r13, for details
24 * also see below. As also the pageaddress-registes (Z and rampZ)
25 * are interfaced via different registers, it is possible to call
26 * this routine via indirect call (icall).
27 * Traditionally it is also possible to rcall, therefore you can
28 * define "bootloader__do_spm" for your normal code via defsym at
29 * linking time.
30 * Example for an atmega8: "-Wl,--defsym=transfer_point=0x1826"
31 * (since BOOTLOADER_ADDRESS is 0x1800 and there are
32 * 2x19 = 38 = 0x26 byte for interrupts)
33 *
34
35 bootloader__do_spm:
36 ;disable interrupts (if enabled) before calling!
37 ;you may also want to disable wdt, since this routine may busy-loop
38 ;==================================================================
39 ;-->INPUT:
40 ;spmcr (spmcrval determines SPM action) will be register: r18
41 ;MCU dependend RA(MPZ should be transfered within register: r11
42 ;lo8(Z) should be transfered within register: r12
43 ;hi8(Z) should be transfered within register: r13
44 ;( as definition of SPM low8bit of dataword are stored within r0 )
45 ;( as definition of SPM hi8bit of dataword are stored within r1 )
46
47 ;<-->USED/CHANGED:
48 ;temp0 will be register: r11
49 ;temp1 will be register: r12
50 ;temp2 will be register: r13
51 ;spmcrval (r18) may also be changed due to rww reenable-phase r18
52 ;Z (r31:r30) wil be changed during operation
53
54 ;<--OUT:
55 ;==================================================================
56 ; TODO: waitA and waitB could be merged to subroutine saving 2 opc
57 ;==================================================================
58
59 ;load pageaddress (Z) from (r11:)r13:12 since it may was used for icall
60 mov rampZ, r11
61 mov r30, r12
62 mov r31, r13
63
64 waitA: ;check for pending SPM complete
65 in temp0, SPMCR
66 sbrc temp0, SPMEN
67 rjmp waitA
68
69 out SPMCR, spmcrval ;SPM timed sequence
70 spm
71
72 waitB: ;check for previous SPM complete
73 in temp0, SPMCR
74 sbrc temp0, SPMEN
75 rjmp waitB
76
77 ;avoid crash of userapplication
78 ldi spmcrval, ((1<<RWWSRE) | (1<<SPMEN))
79 in temp0, SPMCR
80 sbrc temp0, RWWSB
81 rjmp waitA
82
83 ret
84
85 *
86 */
87
88 #include <avr/io.h>
89 #include <avr/pgmspace.h>
90
91 #ifndef BOOTLOADER_ADDRESS
92 // this header is the interface for user-code
93
94 #ifndef funcaddr___bootloader__do_spm
95 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8HVA__)
96 #define funcaddr___bootloader__do_spm 0x1826
97
98 #else
99 #error "unknown MCU - where is bootloader__do_spm located?"
100 #endif
101 #endif
102
103
104 /*
105 * Call the "bootloader__do_spm"-function, located within the BLS via comfortable C-interface
106 * During operation code will block - disable or reset watchdog before call.
107 *
108 * ATTANTION: Since the underlying "bootloader__do_spm" will automatically reenable the
109 * rww-section, only one way to program the flash will work.
110 * (First erase the page, then fill temp. buffer, finally program...)
111 * Since unblocking rww-section erases the temp. pagebuffer (which happens
112 * after a page-erase), first programming this buffer does not help !!
113 *
114 * REMEMBER: interrupts have to be disabled! (otherwise code may crash non-deterministic)
115 *
116 */
117 void do_spm(const uint32_t flash_byteaddress, const uint8_t spmcrval, const uint8_t r1_highdatabyte, const uint8_t r0_lowdatabyte) {
118 uint8_t loaddr = (flash_byteaddress >> 0) & 0xff;
119 uint8_t hiaddr = (flash_byteaddress >> 8) & 0xff;
120 uint8_t adaddr = (flash_byteaddress >> 16) & 0xff;
121
122 asm volatile (
123 "push r0\n\t"
124 "push r1\n\t"
125
126 "mov r13, %[hiaddr]\n\t"
127 "mov r12, %[loaddr]\n\t"
128 "mov r11, %[adaddr]\n\t"
129
130
131 /* high8 of address of bootloader__do_spm */
132 "ldi r18, %[hi8spmfunc]\n\t"
133 "mov r31, r18\n\t"
134
135 /* low8 of address of bootloader__do_spm */
136 "ldi r18, %[lo8spmfunc]\n\t"
137 "mov r30, r18\n\t"
138
139 /* also load the spmcrval */
140 "mov r18, %[spmcrval]\n\t"
141
142 /* correkt address of bootloader__do_spm from bytes into words */
143 "lsr r31\n\t"
144 "ror r30\n\t"
145
146 "mov r1, %[hidata]\n\t"
147 "mov r0, %[lodata]\n\t"
148
149 /* finally call the bootloader-function */
150 "icall\n\r"
151
152 "pop r1\n\t"
153 "pop r0\n\t"
154
155 :
156 : [loaddr] "r" (loaddr), [hiaddr] "r" (hiaddr), [adaddr] "r" (adaddr), [spmcrval] "r" (spmcrval),
157 [lodata] "r" (r0_lowdatabyte), [hidata] "r" (r1_highdatabyte),
158 [hi8spmfunc] "M" (funcaddr___bootloader__do_spm >> 8), [lo8spmfunc] "M" (funcaddr___bootloader__do_spm & 0xff)
159
160 : "r0","r1","r11","r12","r13","r18"
161 );
162 }
163
164
165
166 #else /*ifndef BOOTLOADER_ADDRESS*/
167 // this header is used directly within bootloader_do_spm
168 #include "bootloaderconfig.h"
169
170 #if HAVE_SPMINTEREFACE
171
172
173 /*
174 * insert architecture dependend "bootloader_do_spm"-code
175 *
176 * try to make this array as big as possible
177 * (so bootloader always uses 2kbytes flash)
178 */
179 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8HVA__)
180 //assume SPMCR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6
181 const uint16_t bootloader__do_spm[17] PROGMEM = {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
182 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF};
183 /*
184 00001826 <bootloader__do_spm>:
185 1826: 00 00 nop
186 1828: ec 2d mov r30, r12
187 182a: fd 2d mov r31, r13
188
189 0000182c <waitA>:
190 182c: b7 b6 in r11, 0x37 ; 55
191 182e: b0 fc sbrc r11, 0
192 1830: fd cf rjmp .-6 ; 0x182c <waitA>
193 1832: 27 bf out 0x37, r18 ; 55
194 1834: e8 95 spm
195
196 00001836 <waitB>:
197 1836: b7 b6 in r11, 0x37 ; 55
198 1838: b0 fc sbrc r11, 0
199 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
200 183c: 21 e1 ldi r18, 0x11 ; 17
201 183e: b7 b6 in r11, 0x37 ; 55
202 1840: b6 fc sbrc r11, 6
203 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
204 1844: 08 95 ret
205 */
206
207
208
209
210
211 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48P__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
212 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
213 const uint16_t bootloader__do_spm[17] PROGMEM = {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
214 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF};
215 /*
216 00001826 <bootloader__do_spm>:
217 1826: 00 00 nop
218 1828: ec 2d mov r30, r12
219 182a: fd 2d mov r31, r13
220
221 0000182c <waitA>:
222 182c: b7 b6 in r11, 0x37 ; 55
223 182e: b0 fc sbrc r11, 0
224 1830: fd cf rjmp .-6 ; 0x182c <waitA>
225 1832: 27 bf out 0x37, r18 ; 55
226 1834: e8 95 spm
227
228 00001836 <waitB>:
229 1836: b7 b6 in r11, 0x37 ; 55
230 1838: b0 fc sbrc r11, 0
231 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
232 183c: 21 e1 ldi r18, 0x11 ; 17
233 183e: b7 b6 in r11, 0x37 ; 55
234 1840: b6 fc sbrc r11, 6
235 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
236 1844: 08 95 ret
237 */
238
239
240
241
242
243 #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__)
244 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
245 const uint16_t bootloader__do_spm[17] PROGMEM = {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
246 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF};
247 /*
248 00001826 <bootloader__do_spm>:
249 1826: 00 00 nop
250 1828: ec 2d mov r30, r12
251 182a: fd 2d mov r31, r13
252
253 0000182c <waitA>:
254 182c: b7 b6 in r11, 0x37 ; 55
255 182e: b0 fc sbrc r11, 0
256 1830: fd cf rjmp .-6 ; 0x182c <waitA>
257 1832: 27 bf out 0x37, r18 ; 55
258 1834: e8 95 spm
259
260 00001836 <waitB>:
261 1836: b7 b6 in r11, 0x37 ; 55
262 1838: b0 fc sbrc r11, 0
263 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
264 183c: 21 e1 ldi r18, 0x11 ; 17
265 183e: b7 b6 in r11, 0x37 ; 55
266 1840: b6 fc sbrc r11, 6
267 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
268 1844: 08 95 ret
269 */
270
271
272
273
274
275 #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__)
276 //assume SPMCR:=SPCSR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6
277 const uint16_t bootloader__do_spm[17] PROGMEM = {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
278 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF};
279 /*
280 00001826 <bootloader__do_spm>:
281 1826: 00 00 nop
282 1828: ec 2d mov r30, r12
283 182a: fd 2d mov r31, r13
284
285 0000182c <waitA>:
286 182c: b7 b6 in r11, 0x37 ; 55
287 182e: b0 fc sbrc r11, 0
288 1830: fd cf rjmp .-6 ; 0x182c <waitA>
289 1832: 27 bf out 0x37, r18 ; 55
290 1834: e8 95 spm
291
292 00001836 <waitB>:
293 1836: b7 b6 in r11, 0x37 ; 55
294 1838: b0 fc sbrc r11, 0
295 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
296 183c: 21 e1 ldi r18, 0x11 ; 17
297 183e: b7 b6 in r11, 0x37 ; 55
298 1840: b6 fc sbrc r11, 6
299 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
300 1844: 08 95 ret
301 */
302
303
304
305
306
307 #else
308 #error "bootloader__do_spm has to be adapted, since there is no architecture code, yet"
309 #endif
310
311
312 #endif
313 #endif /*ifdef BOOTLOADER_ADDRESS*/
314
315 #endif
316