6338f035d863eb3aba6217152aebb757b2a32caa
[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.95
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 ;#if HAVE_SPMINTEREFACE_MAGICVALUE
41 ;magicvalue in r23:r22:r21:r20
42 ;#endif
43 ;spmcr (spmcrval determines SPM action) will be register: r18
44 ;MCU dependend RA(MPZ should be transfered within register: r11
45 ;lo8(Z) should be transfered within register: r12
46 ;hi8(Z) should be transfered within register: r13
47 ;( as definition of SPM low8bit of dataword are stored within r0 )
48 ;( as definition of SPM hi8bit of dataword are stored within r1 )
49
50 ;<-->USED/CHANGED:
51 ;temp0 will be register: r11
52 ;temp1 will be register: r12
53 ;temp2 will be register: r13
54 ;spmcrval (r18) may also be changed due to rww reenable-phase r18
55 ;Z (r31:r30) wil be changed during operation
56
57 ;<--OUT:
58 ;==================================================================
59 ; TODO: waitA and waitB could be merged to subroutine saving 2 opc
60 ;==================================================================
61 ;<magicvalue specific code (not depicted here)>
62 ;load pageaddress (Z) from (r11:)r13:12 since it may was used for icall
63 mov rampZ, r11
64 mov r30, r12
65 mov r31, r13
66
67 waitA: ;check for pending SPM complete
68 in temp0, SPMCR
69 sbrc temp0, SPMEN
70 rjmp waitA
71
72 out SPMCR, spmcrval ;SPM timed sequence
73 spm
74
75 waitB: ;check for previous SPM complete
76 in temp0, SPMCR
77 sbrc temp0, SPMEN
78 rjmp waitB
79
80 ;avoid crash of userapplication
81 ldi spmcrval, ((1<<RWWSRE) | (1<<SPMEN))
82 in temp0, SPMCR
83 sbrc temp0, RWWSB
84 rjmp waitA
85
86 ret
87
88 *
89 */
90
91 #include <avr/io.h>
92 #include "bootloaderconfig.h"
93
94
95
96 /*
97 * This MACRO commands the linker to place correspondig
98 * data on the top of the firmware. (Right after the
99 * interrupt-vector-table)
100 * This is necessary to always locate the
101 * "bootloader__do_spm" for example at 0x1826, even if
102 * there are existing PROGMEM within the firmware...
103 */
104 #define BOOTLIBLINK __attribute__ ((section (".vectors") ))
105
106
107 #ifndef funcaddr___bootloader__do_spm
108 #if (defined(BOOTLOADER_ADDRESS)) && (!(defined(NEW_BOOTLOADER_ADDRESS)))
109 #if HAVE_SPMINTEREFACE
110 #define funcaddr___bootloader__do_spm (&bootloader__do_spm)
111 #endif
112 #else
113 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
114 #define funcaddr___bootloader__do_spm 0x1826
115 #elif defined (__AVR_ATmega16__)
116 #define funcaddr___bootloader__do_spm 0x3854
117 #elif defined (__AVR_ATmega32__)
118 #define funcaddr___bootloader__do_spm 0x7054
119 #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88PA__)
120 #define funcaddr___bootloader__do_spm 0x1834
121 #elif defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
122 #define funcaddr___bootloader__do_spm 0x387c
123 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168PA__)
124 #define funcaddr___bootloader__do_spm 0x3868
125 #elif defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || defined (__AVR_ATmega324PA__)
126 #define funcaddr___bootloader__do_spm 0x707c
127 #elif defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
128 #define funcaddr___bootloader__do_spm 0x7068
129 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
130 #define funcaddr___bootloader__do_spm 0xe07c
131 #elif defined (__AVR_ATmega128__)
132 #define funcaddr___bootloader__do_spm 0x1e08c
133 #elif defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
134 #define funcaddr___bootloader__do_spm 0x1e08c
135 #else
136 #error "unknown MCU - where is bootloader__do_spm located?"
137 #endif
138
139 #if defined(_VECTORS_SIZE)
140 #if (funcaddr___bootloader__do_spm != (BOOTLOADER_ADDRESS+_VECTORS_SIZE))
141 #error "bootloader__do_spm is not located after interrupts - sth. is very wrong here!"
142 #endif
143 #endif
144
145 #endif
146 #endif
147
148
149 #ifndef SPMEN
150 #define SPMEN SELFPRGEN
151 #endif
152
153 /*
154 * Call the "bootloader__do_spm"-function, located within the BLS via comfortable C-interface
155 * During operation code will block - disable or reset watchdog before call.
156 *
157 * ATTANTION: Since the underlying "bootloader__do_spm" will automatically reenable the
158 * rww-section, only one way to program the flash will work.
159 * (First erase the page, then fill temp. buffer, finally program...)
160 * Since unblocking rww-section erases the temp. pagebuffer (which happens
161 * after a page-erase), first programming this buffer does not help !!
162 *
163 * REMEMBER: interrupts have to be disabled! (otherwise code may crash non-deterministic)
164 *
165 */
166 #if HAVE_SPMINTEREFACE_MAGICVALUE
167 #define __do_spm_Ex __do_spm_Ex_magic
168 #else
169 #define __do_spm_Ex __do_spm_Ex_
170 #endif
171
172 #define __do_spm_Ex_(flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
173 ({ \
174 asm volatile ( \
175 "push r0\n\t" \
176 "push r1\n\t" \
177 \
178 "mov r13, %B[flashaddress]\n\t" \
179 "mov r12, %A[flashaddress]\n\t" \
180 "mov r11, %C[flashaddress]\n\t" \
181 \
182 /* also load the spmcrval */ \
183 "mov r18, %[spmcrval]\n\t" \
184 \
185 \
186 "mov r1, %B[data]\n\t" \
187 "mov r0, %A[data]\n\t" \
188 \
189 /* finally call the bootloader-function */ \
190 "icall\n\t" \
191 \
192 /* \
193 * bootloader__do_spm should change spmcrval (r18) to \
194 * "((1<<RWWSRE) | (1<<SPMEN))" in case of success \
195 */ \
196 "cpi r18, %[spmret]\n\t" \
197 /* loop infitinte if not so, most likely we called an bootloader__do_spm \
198 * with wrong magic! To avoid calls to wrong initialized pages, better crash here... \
199 */ \
200 "loop%=: \n\t" \
201 "brne loop%= \n\t" \
202 \
203 "pop r1\n\t" \
204 "pop r0\n\t" \
205 \
206 : \
207 : [flashaddress] "r" (flash_wordaddress), \
208 [spmfunctionaddress] "z" ((uint16_t)(___bootloader__do_spm__ptr)), \
209 [spmcrval] "r" (spmcrval), \
210 [data] "r" (dataword), \
211 [spmret] "M" ((1<<RWWSRE) | (1<<SPMEN)) \
212 : "r0","r1","r11","r12","r13","r18" \
213 ); \
214 })
215
216 #define __do_spm_Ex_magic(flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
217 ({ \
218 asm volatile ( \
219 "push r0\n\t" \
220 "push r1\n\t" \
221 \
222 "ldi r23, %[magicD] \n\t" \
223 "ldi r22, %[magicC] \n\t" \
224 "ldi r21, %[magicB] \n\t" \
225 "ldi r20, %[magicA] \n\t" \
226 \
227 "mov r13, %B[flashaddress]\n\t" \
228 "mov r12, %A[flashaddress]\n\t" \
229 "mov r11, %C[flashaddress]\n\t" \
230 \
231 /* also load the spmcrval */ \
232 "mov r18, %[spmcrval]\n\t" \
233 \
234 \
235 "mov r1, %B[data]\n\t" \
236 "mov r0, %A[data]\n\t" \
237 \
238 /* finally call the bootloader-function */ \
239 "icall\n\t" \
240 \
241 /* \
242 * bootloader__do_spm should change spmcrval (r18) to \
243 * "((1<<RWWSRE) | (1<<SPMEN))" in case of success \
244 */ \
245 "cpi r18, %[spmret]\n\t" \
246 /* loop infitinte if not so, most likely we called an bootloader__do_spm \
247 * with wrong magic! To avoid calls to wrong initialized pages, better crash here... \
248 */ \
249 "loop%=: \n\t" \
250 "brne loop%= \n\t" \
251 \
252 "pop r1\n\t" \
253 "pop r0\n\t" \
254 \
255 : \
256 : [flashaddress] "r" (flash_wordaddress), \
257 [spmfunctionaddress] "z" ((uint16_t)(___bootloader__do_spm__ptr)), \
258 [spmcrval] "r" (spmcrval), \
259 [data] "r" (dataword), \
260 [spmret] "M" ((1<<RWWSRE) | (1<<SPMEN)), \
261 [magicD] "M" ((HAVE_SPMINTEREFACE_MAGICVALUE>>24)&0xff), \
262 [magicC] "M" ((HAVE_SPMINTEREFACE_MAGICVALUE>>16)&0xff), \
263 [magicB] "M" ((HAVE_SPMINTEREFACE_MAGICVALUE>> 8)&0xff), \
264 [magicA] "M" ((HAVE_SPMINTEREFACE_MAGICVALUE>> 0)&0xff) \
265 : "r0","r1","r11","r12","r13","r18","r20","r21","r22","r23" \
266 ); \
267 })
268
269
270 #if (!(defined(BOOTLOADER_ADDRESS))) || (defined(NEW_BOOTLOADER_ADDRESS))
271 void do_spm(const uint32_t flash_byteaddress, const uint8_t spmcrval, const uint16_t dataword) {
272 __do_spm_Ex(flash_byteaddress, spmcrval, dataword, funcaddr___bootloader__do_spm >> 1);
273 }
274 #endif
275
276 #if HAVE_SPMINTEREFACE_NORETMAGIC
277 #define bootloader__do_spm_magic_exitstrategy(a) (0xf7f9)
278 #else
279 #define bootloader__do_spm_magic_exitstrategy(a) (a)
280 #endif
281
282 #if (HAVE_SPMINTEREFACE) && (defined(BOOTLOADER_ADDRESS)) && (!(defined(NEW_BOOTLOADER_ADDRESS)))
283
284 /*
285 * insert architecture dependend "bootloader_do_spm"-code
286 *
287 * try to make this array as big as possible
288 * (so bootloader always uses 2kbytes flash)
289 */
290 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__)
291
292 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
293 #if (BOOTLOADER_ADDRESS != 0x1800)
294 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
295 #endif
296 #elif defined (__AVR_ATmega16__)
297 #if (BOOTLOADER_ADDRESS != 0x3800)
298 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
299 #endif
300 #elif defined (__AVR_ATmega32__)
301 #if (BOOTLOADER_ADDRESS != 0x7000)
302 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
303 #endif
304 #else
305 #error undefined device selection - this should not happen!
306 #endif
307
308 //assume SPMCR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6
309 #if HAVE_SPMINTEREFACE_MAGICVALUE
310 const uint16_t bootloader__do_spm[23] BOOTLIBLINK = {
311 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
312 bootloader__do_spm_magic_exitstrategy(0xf4a1), // brne +20
313 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
314 bootloader__do_spm_magic_exitstrategy(0xf491), // brne +18
315 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
316 bootloader__do_spm_magic_exitstrategy(0xf481), // brne +16
317 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
318 bootloader__do_spm_magic_exitstrategy(0xf471), // brne +14
319 #else
320 const uint16_t bootloader__do_spm[15] BOOTLIBLINK = {
321 #endif
322 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
323 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
324 };
325
326 /*
327 00001826 <bootloader__do_spm>:
328 1828: ec 2d mov r30, r12
329 182a: fd 2d mov r31, r13
330
331 0000182c <waitA>:
332 182c: b7 b6 in r11, 0x37 ; 55
333 182e: b0 fc sbrc r11, 0
334 1830: fd cf rjmp .-6 ; 0x182c <waitA>
335 1832: 27 bf out 0x37, r18 ; 55
336 1834: e8 95 spm
337
338 00001836 <waitB>:
339 1836: b7 b6 in r11, 0x37 ; 55
340 1838: b0 fc sbrc r11, 0
341 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
342 183c: 21 e1 ldi r18, 0x11 ; 17
343 183e: b7 b6 in r11, 0x37 ; 55
344 1840: b6 fc sbrc r11, 6
345 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
346 1844: 08 95 ret
347 */
348
349
350
351
352
353 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48P__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
354
355 #if defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__)
356 #if (BOOTLOADER_ADDRESS != 0x1800)
357 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
358 #endif
359 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
360 #if (BOOTLOADER_ADDRESS != 0x3800)
361 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
362 #endif
363 #else
364 #error undefined device selection - this should not happen!
365 #endif
366
367 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
368 #if HAVE_SPMINTEREFACE_MAGICVALUE
369 const uint16_t bootloader__do_spm[23] BOOTLIBLINK = {
370 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
371 bootloader__do_spm_magic_exitstrategy(0xf4a1), // brne +20
372 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
373 bootloader__do_spm_magic_exitstrategy(0xf491), // brne +18
374 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
375 bootloader__do_spm_magic_exitstrategy(0xf481), // brne +16
376 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
377 bootloader__do_spm_magic_exitstrategy(0xf471), // brne +14
378 #else
379 const uint16_t bootloader__do_spm[15] BOOTLIBLINK = {
380 #endif
381 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
382 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
383 };
384 /*
385 00001826 <bootloader__do_spm>:
386 1828: ec 2d mov r30, r12
387 182a: fd 2d mov r31, r13
388
389 0000182c <waitA>:
390 182c: b7 b6 in r11, 0x37 ; 55
391 182e: b0 fc sbrc r11, 0
392 1830: fd cf rjmp .-6 ; 0x182c <waitA>
393 1832: 27 bf out 0x37, r18 ; 55
394 1834: e8 95 spm
395
396 00001836 <waitB>:
397 1836: b7 b6 in r11, 0x37 ; 55
398 1838: b0 fc sbrc r11, 0
399 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
400 183c: 21 e1 ldi r18, 0x11 ; 17
401 183e: b7 b6 in r11, 0x37 ; 55
402 1840: b6 fc sbrc r11, 6
403 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
404 1844: 08 95 ret
405 */
406
407
408
409
410
411 #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__)
412
413 #if defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88PA__)
414 #if (BOOTLOADER_ADDRESS != 0x1800)
415 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
416 #endif
417 #elif defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168PA__)
418 #if (BOOTLOADER_ADDRESS != 0x3800)
419 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
420 #endif
421 #elif defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
422 #if (BOOTLOADER_ADDRESS != 0x7000)
423 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
424 #endif
425 #else
426 #error undefined device selection - this should not happen!
427 #endif
428
429 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
430 #if HAVE_SPMINTEREFACE_MAGICVALUE
431 const uint16_t bootloader__do_spm[23] BOOTLIBLINK = {
432 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
433 bootloader__do_spm_magic_exitstrategy(0xf4a1), // brne +20
434 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
435 bootloader__do_spm_magic_exitstrategy(0xf491), // brne +18
436 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
437 bootloader__do_spm_magic_exitstrategy(0xf481), // brne +16
438 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
439 bootloader__do_spm_magic_exitstrategy(0xf471), // brne +14
440 #else
441 const uint16_t bootloader__do_spm[15] BOOTLIBLINK = {
442 #endif
443 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
444 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
445 };
446 /*
447 00001826 <bootloader__do_spm>:
448 1828: ec 2d mov r30, r12
449 182a: fd 2d mov r31, r13
450
451 0000182c <waitA>:
452 182c: b7 b6 in r11, 0x37 ; 55
453 182e: b0 fc sbrc r11, 0
454 1830: fd cf rjmp .-6 ; 0x182c <waitA>
455 1832: 27 bf out 0x37, r18 ; 55
456 1834: e8 95 spm
457
458 00001836 <waitB>:
459 1836: b7 b6 in r11, 0x37 ; 55
460 1838: b0 fc sbrc r11, 0
461 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
462 183c: 21 e1 ldi r18, 0x11 ; 17
463 183e: b7 b6 in r11, 0x37 ; 55
464 1840: b6 fc sbrc r11, 6
465 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
466 1844: 08 95 ret
467 */
468
469
470
471
472
473 #elif defined (__AVR_ATmega128__)
474
475 #if defined (__AVR_ATmega128__)
476 #if (BOOTLOADER_ADDRESS != 0x1E000)
477 #error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
478 #endif
479 #else
480 #error undefined device selection - this should not happen!
481 #endif
482
483 //assume SPMCR:=SPMCSR==0x68, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6 and rampZ=0x3b
484 #if HAVE_SPMINTEREFACE_MAGICVALUE
485 const uint16_t bootloader__do_spm[28] BOOTLIBLINK = {
486 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
487 bootloader__do_spm_magic_exitstrategy(0xf4c9), // brne +21+4
488 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
489 bootloader__do_spm_magic_exitstrategy(0xf4b9), // brne +19+4
490 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
491 bootloader__do_spm_magic_exitstrategy(0xf4a9), // brne +17+4
492 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
493 bootloader__do_spm_magic_exitstrategy(0xf499), // brne +15+4
494 #else
495 const uint16_t bootloader__do_spm[20] BOOTLIBLINK = {
496 #endif
497 0xbebb, 0x2dec, 0x2dfd, 0x90b0, 0x0068, 0xfcb0, 0xcffc, 0x9320, 0x0068,
498 0x95e8, 0x90b0, 0x0068, 0xfcb0, 0xcffc, 0xe121, 0x90b0, 0x0068, 0xfcb6,
499 0xcff0, 0x9508
500 };
501 /*
502 0001e08c <bootloader__do_spm>:
503 1e08c: bb be out 0x3b, r11 ; 59
504 1e08e: ec 2d mov r30, r12
505 1e090: fd 2d mov r31, r13
506
507 0001e092 <waitA>:
508 1e092: b0 90 68 00 lds r11, 0x0068
509 1e096: b0 fc sbrc r11, 0
510 1e098: fc cf rjmp .-8 ; 0x1e092 <waitA>
511 1e09a: 20 93 68 00 sts 0x0068, r18
512 1e09e: e8 95 spm
513
514 0001e0a0 <waitB>:
515 1e0a0: b0 90 68 00 lds r11, 0x0068
516 1e0a4: b0 fc sbrc r11, 0
517 1e0a6: fc cf rjmp .-8 ; 0x1e0a0 <waitB>
518 1e0a8: 21 e1 ldi r18, 0x11 ; 17
519 1e0aa: b0 90 68 00 lds r11, 0x0068
520 1e0ae: b6 fc sbrc r11, 6
521 1e0b0: f0 cf rjmp .-32 ; 0x1e092 <waitA>
522 1e0b2: 08 95 ret
523 */
524
525
526
527
528
529 #elif defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__) || defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || defined (__AVR_ATmega324PA__) || defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__) || defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
530
531 #if defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
532 #if (BOOTLOADER_ADDRESS != 0x3800)
533 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
534 #endif
535 #elif defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || defined (__AVR_ATmega324PA__)
536 #if (BOOTLOADER_ADDRESS != 0x7000)
537 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
538 #endif
539 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
540 #if (BOOTLOADER_ADDRESS != 0xE000)
541 #error BOOTLOADER_ADDRESS!=0xE000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
542 #endif
543 #elif defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
544 #if (BOOTLOADER_ADDRESS != 0x1E000)
545 #error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
546 #endif
547 #else
548 #error undefined device selection - this should not happen!
549 #endif
550
551 //assume SPMCR:=SPCSR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6 and rampZ=0x3b
552 #if HAVE_SPMINTEREFACE_MAGICVALUE
553 const uint16_t bootloader__do_spm[24] BOOTLIBLINK = {
554 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
555 bootloader__do_spm_magic_exitstrategy(0xf4a9), // brne +21
556 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
557 bootloader__do_spm_magic_exitstrategy(0xf499), // brne +19
558 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
559 bootloader__do_spm_magic_exitstrategy(0xf489), // brne +17
560 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
561 bootloader__do_spm_magic_exitstrategy(0xf479), // brne +15
562 #else
563 const uint16_t bootloader__do_spm[16] BOOTLIBLINK = {
564 #endif
565 0xbebb,
566 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
567 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
568 };
569 /*
570 00001826 <bootloader__do_spm>:
571 1826: bb be out 0x3b,r11 ; rampZ=r11; (rampZ is at IO 0x3b)
572 1828: ec 2d mov r30, r12
573 182a: fd 2d mov r31, r13
574
575 0000182c <waitA>:
576 182c: b7 b6 in r11, 0x37 ; 55
577 182e: b0 fc sbrc r11, 0
578 1830: fd cf rjmp .-6 ; 0x182c <waitA>
579 1832: 27 bf out 0x37, r18 ; 55
580 1834: e8 95 spm
581
582 00001836 <waitB>:
583 1836: b7 b6 in r11, 0x37 ; 55
584 1838: b0 fc sbrc r11, 0
585 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
586 183c: 21 e1 ldi r18, 0x11 ; 17
587 183e: b7 b6 in r11, 0x37 ; 55
588 1840: b6 fc sbrc r11, 6
589 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
590 1844: 08 95 ret
591 */
592
593
594
595
596
597 #else
598 #error "bootloader__do_spm has to be adapted, since there is no architecture code, yet"
599 #endif
600
601
602 #endif
603
604 #endif
605