BUGfix: Abort timeout when bootLoaderConditionSimple()
[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) 2013 by Stephan Baerwolf
6 * License: GNU GPL v2 (see License.txt)
7 * Version: 0.96.5-testing
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_ATmega8535__)
114 #define funcaddr___bootloader__do_spm 0x182a
115 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
116 #define funcaddr___bootloader__do_spm 0x1826
117 #elif defined (__AVR_ATmega16__)
118 #define funcaddr___bootloader__do_spm 0x3854
119 #elif defined (__AVR_ATmega32__)
120 #define funcaddr___bootloader__do_spm 0x7054
121 #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88PA__)
122 #define funcaddr___bootloader__do_spm 0x1834
123 #elif defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
124 #define funcaddr___bootloader__do_spm 0x387c
125 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168PA__)
126 #define funcaddr___bootloader__do_spm 0x3868
127 #elif defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || defined (__AVR_ATmega324PA__)
128 #define funcaddr___bootloader__do_spm 0x707c
129 #elif defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
130 #define funcaddr___bootloader__do_spm 0x7068
131 #elif defined (__AVR_ATmega640__)
132 #define funcaddr___bootloader__do_spm 0xe0e4
133 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
134 #define funcaddr___bootloader__do_spm 0xe07c
135 #elif defined (__AVR_ATmega128__)
136 #define funcaddr___bootloader__do_spm 0x1e08c
137 #elif defined (__AVR_ATmega1280__)
138 #define funcaddr___bootloader__do_spm 0x1e0e4
139 #elif defined (__AVR_ATmega1281__)
140 #define funcaddr___bootloader__do_spm 0x1e0cc
141 #elif defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
142 #define funcaddr___bootloader__do_spm 0x1e08c
143 #elif defined (__AVR_ATmega2560__)
144 #define funcaddr___bootloader__do_spm 0x3e0e4
145 #elif defined (__AVR_ATmega2561__)
146 #define funcaddr___bootloader__do_spm 0x3e0cc
147 #else
148 #error "unknown MCU - where is bootloader__do_spm located?"
149 #endif
150
151 #if ((defined(_VECTORS_SIZE)) && (defined(BOOTLOADER_ADDRESS)))
152 #if (funcaddr___bootloader__do_spm != (BOOTLOADER_ADDRESS+_VECTORS_SIZE))
153 #error "bootloader__do_spm is not located after interrupts - sth. is very wrong here!"
154 #endif
155 #endif
156
157 #endif
158 #endif
159
160
161 #ifndef SPMEN
162 #define SPMEN SELFPRGEN
163 #endif
164
165 /*
166 * Call the "bootloader__do_spm"-function, located within the BLS via comfortable C-interface
167 * During operation code will block - disable or reset watchdog before call.
168 *
169 * ATTANTION: Since the underlying "bootloader__do_spm" will automatically reenable the
170 * rww-section, only one way to program the flash will work.
171 * (First erase the page, then fill temp. buffer, finally program...)
172 * Since unblocking rww-section erases the temp. pagebuffer (which happens
173 * after a page-erase), first programming this buffer does not help !!
174 *
175 * REMEMBER: interrupts have to be disabled! (otherwise code may crash non-deterministic)
176 *
177 */
178
179 #define __do_spm_Ex(arguments...) __do_spm_GeneralEx(HAVE_SPMINTEREFACE_MAGICVALUE, ##arguments)
180
181
182 #if HAVE_SPMINTEREFACE_MAGICVALUE
183 #define __do_spm_GeneralEx __do_spm_ExASMEx_magic
184 #else
185 #define __do_spm_GeneralEx __do_spm_ExASMEx_
186 #endif
187
188
189 #if (defined(EIND) && ((FLASHEND)>131071))
190 /*
191 * Huge flash version using eicall (and EIND)
192 * MV defines the magic value to be send to bootloader_do_spm
193 */
194 #define __do_spm_ExASMEx_(MV, flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
195 ({ \
196 asm volatile ( \
197 "push r0\n\t" \
198 "push r1\n\t" \
199 \
200 "mov r13, %B[flashaddress]\n\t" \
201 "mov r12, %A[flashaddress]\n\t" \
202 "mov r11, %C[flashaddress]\n\t" \
203 \
204 /* prepare the EIND for following eicall */ \
205 "in r18, %[eind]\n\t" \
206 "push r18\n\t" \
207 "ldi r18, %[spmfuncaddrEIND]\n\t" \
208 "out %[eind], r18\n\t" \
209 \
210 /* also load the spmcrval */ \
211 "mov r18, %[spmcrval]\n\t" \
212 \
213 \
214 "mov r1, %B[data]\n\t" \
215 "mov r0, %A[data]\n\t" \
216 \
217 /* finally call the bootloader-function */ \
218 "eicall\n\t" \
219 "pop r1\n\t" \
220 "out %[eind], r1\n\t" \
221 \
222 /* \
223 * bootloader__do_spm should change spmcrval (r18) to \
224 * "((1<<RWWSRE) | (1<<SPMEN))" in case of success \
225 */ \
226 "cpi r18, %[spmret]\n\t" \
227 /* loop infitinte if not so, most likely we called an bootloader__do_spm \
228 * with wrong magic! To avoid calls to wrong initialized pages, better crash here... \
229 */ \
230 "loop%=: \n\t" \
231 "brne loop%= \n\t" \
232 \
233 "pop r1\n\t" \
234 "pop r0\n\t" \
235 \
236 : \
237 : [flashaddress] "r" (flash_wordaddress), \
238 [spmfunctionaddress] "z" ((uint16_t)(___bootloader__do_spm__ptr)), \
239 [spmfuncaddrEIND] "M" ((uint8_t)(___bootloader__do_spm__ptr>>16)), \
240 [eind] "I" (_SFR_IO_ADDR(EIND)), \
241 [spmcrval] "r" (spmcrval), \
242 [data] "r" (dataword), \
243 [spmret] "M" ((1<<RWWSRE) | (1<<SPMEN)) \
244 : "r0","r1","r11","r12","r13","r18" \
245 ); \
246 })
247
248 #define __do_spm_ExASMEx_magic(MV, flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
249 ({ \
250 asm volatile ( \
251 "push r0\n\t" \
252 "push r1\n\t" \
253 \
254 "ldi r23, %[magicD] \n\t" \
255 "ldi r22, %[magicC] \n\t" \
256 "ldi r21, %[magicB] \n\t" \
257 "ldi r20, %[magicA] \n\t" \
258 \
259 "mov r13, %B[flashaddress]\n\t" \
260 "mov r12, %A[flashaddress]\n\t" \
261 "mov r11, %C[flashaddress]\n\t" \
262 \
263 /* prepare the EIND for following eicall */ \
264 "in r18, %[eind]\n\t" \
265 "push r18\n\t" \
266 "ldi r18, %[spmfuncaddrEIND]\n\t" \
267 "out %[eind], r18\n\t" \
268 \
269 /* also load the spmcrval */ \
270 "mov r18, %[spmcrval]\n\t" \
271 \
272 \
273 "mov r1, %B[data]\n\t" \
274 "mov r0, %A[data]\n\t" \
275 \
276 /* finally call the bootloader-function */ \
277 "eicall\n\t" \
278 "pop r1\n\t" \
279 "out %[eind], r1\n\t" \
280 \
281 /* \
282 * bootloader__do_spm should change spmcrval (r18) to \
283 * "((1<<RWWSRE) | (1<<SPMEN))" in case of success \
284 */ \
285 "cpi r18, %[spmret]\n\t" \
286 /* loop infitinte if not so, most likely we called an bootloader__do_spm \
287 * with wrong magic! To avoid calls to wrong initialized pages, better crash here... \
288 */ \
289 "loop%=: \n\t" \
290 "brne loop%= \n\t" \
291 \
292 "pop r1\n\t" \
293 "pop r0\n\t" \
294 \
295 : \
296 : [flashaddress] "r" (flash_wordaddress), \
297 [spmfunctionaddress] "z" ((uint16_t)(___bootloader__do_spm__ptr)), \
298 [spmfuncaddrEIND] "M" ((uint8_t)(___bootloader__do_spm__ptr>>16)), \
299 [eind] "I" (_SFR_IO_ADDR(EIND)), \
300 [spmcrval] "r" (spmcrval), \
301 [data] "r" (dataword), \
302 [spmret] "M" ((1<<RWWSRE) | (1<<SPMEN)), \
303 [magicD] "M" (((MV)>>24)&0xff), \
304 [magicC] "M" (((MV)>>16)&0xff), \
305 [magicB] "M" (((MV)>> 8)&0xff), \
306 [magicA] "M" (((MV)>> 0)&0xff) \
307 : "r0","r1","r11","r12","r13","r18","r20","r21","r22","r23" \
308 ); \
309 })
310
311
312 #else
313 /*
314 * Normal version for devices with <=128KiB flash (using icall)
315 */
316 #if ((FLASHEND)>131071)
317 #error "Using inappropriate code for device with more than 128kib flash"
318 #endif
319
320 #define __do_spm_ExASMEx_(MV, flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
321 ({ \
322 asm volatile ( \
323 "push r0\n\t" \
324 "push r1\n\t" \
325 \
326 "mov r13, %B[flashaddress]\n\t" \
327 "mov r12, %A[flashaddress]\n\t" \
328 "mov r11, %C[flashaddress]\n\t" \
329 \
330 /* also load the spmcrval */ \
331 "mov r18, %[spmcrval]\n\t" \
332 \
333 \
334 "mov r1, %B[data]\n\t" \
335 "mov r0, %A[data]\n\t" \
336 \
337 /* finally call the bootloader-function */ \
338 "icall\n\t" \
339 \
340 /* \
341 * bootloader__do_spm should change spmcrval (r18) to \
342 * "((1<<RWWSRE) | (1<<SPMEN))" in case of success \
343 */ \
344 "cpi r18, %[spmret]\n\t" \
345 /* loop infitinte if not so, most likely we called an bootloader__do_spm \
346 * with wrong magic! To avoid calls to wrong initialized pages, better crash here... \
347 */ \
348 "loop%=: \n\t" \
349 "brne loop%= \n\t" \
350 \
351 "pop r1\n\t" \
352 "pop r0\n\t" \
353 \
354 : \
355 : [flashaddress] "r" (flash_wordaddress), \
356 [spmfunctionaddress] "z" ((uint16_t)(___bootloader__do_spm__ptr)), \
357 [spmcrval] "r" (spmcrval), \
358 [data] "r" (dataword), \
359 [spmret] "M" ((1<<RWWSRE) | (1<<SPMEN)) \
360 : "r0","r1","r11","r12","r13","r18" \
361 ); \
362 })
363
364 #define __do_spm_ExASMEx_magic(MV, flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
365 ({ \
366 asm volatile ( \
367 "push r0\n\t" \
368 "push r1\n\t" \
369 \
370 "ldi r23, %[magicD] \n\t" \
371 "ldi r22, %[magicC] \n\t" \
372 "ldi r21, %[magicB] \n\t" \
373 "ldi r20, %[magicA] \n\t" \
374 \
375 "mov r13, %B[flashaddress]\n\t" \
376 "mov r12, %A[flashaddress]\n\t" \
377 "mov r11, %C[flashaddress]\n\t" \
378 \
379 /* also load the spmcrval */ \
380 "mov r18, %[spmcrval]\n\t" \
381 \
382 \
383 "mov r1, %B[data]\n\t" \
384 "mov r0, %A[data]\n\t" \
385 \
386 /* finally call the bootloader-function */ \
387 "icall\n\t" \
388 \
389 /* \
390 * bootloader__do_spm should change spmcrval (r18) to \
391 * "((1<<RWWSRE) | (1<<SPMEN))" in case of success \
392 */ \
393 "cpi r18, %[spmret]\n\t" \
394 /* loop infitinte if not so, most likely we called an bootloader__do_spm \
395 * with wrong magic! To avoid calls to wrong initialized pages, better crash here... \
396 */ \
397 "loop%=: \n\t" \
398 "brne loop%= \n\t" \
399 \
400 "pop r1\n\t" \
401 "pop r0\n\t" \
402 \
403 : \
404 : [flashaddress] "r" (flash_wordaddress), \
405 [spmfunctionaddress] "z" ((uint16_t)(___bootloader__do_spm__ptr)), \
406 [spmcrval] "r" (spmcrval), \
407 [data] "r" (dataword), \
408 [spmret] "M" ((1<<RWWSRE) | (1<<SPMEN)), \
409 [magicD] "M" (((MV)>>24)&0xff), \
410 [magicC] "M" (((MV)>>16)&0xff), \
411 [magicB] "M" (((MV)>> 8)&0xff), \
412 [magicA] "M" (((MV)>> 0)&0xff) \
413 : "r0","r1","r11","r12","r13","r18","r20","r21","r22","r23" \
414 ); \
415 })
416 #endif
417
418 #if (!(defined(BOOTLOADER_ADDRESS))) || (defined(NEW_BOOTLOADER_ADDRESS))
419 void do_spm(const uint32_t flash_byteaddress, const uint8_t spmcrval, const uint16_t dataword) {
420 __do_spm_Ex(flash_byteaddress, spmcrval, dataword, funcaddr___bootloader__do_spm >> 1);
421 }
422 #endif
423
424 #if HAVE_SPMINTEREFACE_NORETMAGIC
425 #define bootloader__do_spm_magic_exitstrategy(a) (0xf7f9)
426 #else
427 #define bootloader__do_spm_magic_exitstrategy(a) (a)
428 #endif
429
430 #if (HAVE_SPMINTEREFACE) && (defined(BOOTLOADER_ADDRESS)) && (!(defined(NEW_BOOTLOADER_ADDRESS)))
431
432 /*
433 * insert architecture dependend "bootloader_do_spm"-code
434 */
435 #if defined (__AVR_ATmega8535__) || defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__)
436
437 #if defined (__AVR_ATmega8535__) || defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
438 #if (BOOTLOADER_ADDRESS != 0x1800)
439 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
440 #endif
441 #elif defined (__AVR_ATmega16__)
442 #if (BOOTLOADER_ADDRESS != 0x3800)
443 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
444 #endif
445 #elif defined (__AVR_ATmega32__)
446 #if (BOOTLOADER_ADDRESS != 0x7000)
447 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
448 #endif
449 #else
450 #error undefined device selection - this should not happen!
451 #endif
452
453 //assume SPMCR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6
454 #if HAVE_SPMINTEREFACE_MAGICVALUE
455 const uint16_t bootloader__do_spm[23] BOOTLIBLINK = {
456 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
457 bootloader__do_spm_magic_exitstrategy(0xf4a1), // brne +20
458 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
459 bootloader__do_spm_magic_exitstrategy(0xf491), // brne +18
460 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
461 bootloader__do_spm_magic_exitstrategy(0xf481), // brne +16
462 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
463 bootloader__do_spm_magic_exitstrategy(0xf471), // brne +14
464 #else
465 const uint16_t bootloader__do_spm[15] BOOTLIBLINK = {
466 #endif
467 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
468 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
469 };
470
471 /*
472 00001826 <bootloader__do_spm>:
473 1828: ec 2d mov r30, r12
474 182a: fd 2d mov r31, r13
475
476 0000182c <waitA>:
477 182c: b7 b6 in r11, 0x37 ; 55
478 182e: b0 fc sbrc r11, 0
479 1830: fd cf rjmp .-6 ; 0x182c <waitA>
480 1832: 27 bf out 0x37, r18 ; 55
481 1834: e8 95 spm
482
483 00001836 <waitB>:
484 1836: b7 b6 in r11, 0x37 ; 55
485 1838: b0 fc sbrc r11, 0
486 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
487 183c: 21 e1 ldi r18, 0x11 ; 17
488 183e: b7 b6 in r11, 0x37 ; 55
489 1840: b6 fc sbrc r11, 6
490 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
491 1844: 08 95 ret
492 */
493
494
495
496
497
498 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48P__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
499
500 #if defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__)
501 #if (BOOTLOADER_ADDRESS != 0x1800)
502 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
503 #endif
504 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
505 #if (BOOTLOADER_ADDRESS != 0x3800)
506 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
507 #endif
508 #else
509 #error undefined device selection - this should not happen!
510 #endif
511
512 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
513 #if HAVE_SPMINTEREFACE_MAGICVALUE
514 const uint16_t bootloader__do_spm[23] BOOTLIBLINK = {
515 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
516 bootloader__do_spm_magic_exitstrategy(0xf4a1), // brne +20
517 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
518 bootloader__do_spm_magic_exitstrategy(0xf491), // brne +18
519 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
520 bootloader__do_spm_magic_exitstrategy(0xf481), // brne +16
521 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
522 bootloader__do_spm_magic_exitstrategy(0xf471), // brne +14
523 #else
524 const uint16_t bootloader__do_spm[15] BOOTLIBLINK = {
525 #endif
526 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
527 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
528 };
529 /*
530 00001826 <bootloader__do_spm>:
531 1828: ec 2d mov r30, r12
532 182a: fd 2d mov r31, r13
533
534 0000182c <waitA>:
535 182c: b7 b6 in r11, 0x37 ; 55
536 182e: b0 fc sbrc r11, 0
537 1830: fd cf rjmp .-6 ; 0x182c <waitA>
538 1832: 27 bf out 0x37, r18 ; 55
539 1834: e8 95 spm
540
541 00001836 <waitB>:
542 1836: b7 b6 in r11, 0x37 ; 55
543 1838: b0 fc sbrc r11, 0
544 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
545 183c: 21 e1 ldi r18, 0x11 ; 17
546 183e: b7 b6 in r11, 0x37 ; 55
547 1840: b6 fc sbrc r11, 6
548 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
549 1844: 08 95 ret
550 */
551
552
553
554
555
556 #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__)
557
558 #if defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88PA__)
559 #if (BOOTLOADER_ADDRESS != 0x1800)
560 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
561 #endif
562 #elif defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168PA__)
563 #if (BOOTLOADER_ADDRESS != 0x3800)
564 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
565 #endif
566 #elif defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
567 #if (BOOTLOADER_ADDRESS != 0x7000)
568 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
569 #endif
570 #else
571 #error undefined device selection - this should not happen!
572 #endif
573
574 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
575 #if HAVE_SPMINTEREFACE_MAGICVALUE
576 const uint16_t bootloader__do_spm[23] BOOTLIBLINK = {
577 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
578 bootloader__do_spm_magic_exitstrategy(0xf4a1), // brne +20
579 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
580 bootloader__do_spm_magic_exitstrategy(0xf491), // brne +18
581 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
582 bootloader__do_spm_magic_exitstrategy(0xf481), // brne +16
583 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
584 bootloader__do_spm_magic_exitstrategy(0xf471), // brne +14
585 #else
586 const uint16_t bootloader__do_spm[15] BOOTLIBLINK = {
587 #endif
588 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
589 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
590 };
591 /*
592 00001826 <bootloader__do_spm>:
593 1828: ec 2d mov r30, r12
594 182a: fd 2d mov r31, r13
595
596 0000182c <waitA>:
597 182c: b7 b6 in r11, 0x37 ; 55
598 182e: b0 fc sbrc r11, 0
599 1830: fd cf rjmp .-6 ; 0x182c <waitA>
600 1832: 27 bf out 0x37, r18 ; 55
601 1834: e8 95 spm
602
603 00001836 <waitB>:
604 1836: b7 b6 in r11, 0x37 ; 55
605 1838: b0 fc sbrc r11, 0
606 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
607 183c: 21 e1 ldi r18, 0x11 ; 17
608 183e: b7 b6 in r11, 0x37 ; 55
609 1840: b6 fc sbrc r11, 6
610 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
611 1844: 08 95 ret
612 */
613
614
615
616
617
618 #elif defined (__AVR_ATmega128__)
619
620 #if defined (__AVR_ATmega128__)
621 #if (BOOTLOADER_ADDRESS != 0x1E000)
622 #error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
623 #endif
624 #else
625 #error undefined device selection - this should not happen!
626 #endif
627
628 //assume SPMCR:=SPMCSR==0x68, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6 and rampZ=0x3b
629 #if HAVE_SPMINTEREFACE_MAGICVALUE
630 const uint16_t bootloader__do_spm[28] BOOTLIBLINK = {
631 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
632 bootloader__do_spm_magic_exitstrategy(0xf4c9), // brne +21+4
633 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
634 bootloader__do_spm_magic_exitstrategy(0xf4b9), // brne +19+4
635 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
636 bootloader__do_spm_magic_exitstrategy(0xf4a9), // brne +17+4
637 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
638 bootloader__do_spm_magic_exitstrategy(0xf499), // brne +15+4
639 #else
640 const uint16_t bootloader__do_spm[20] BOOTLIBLINK = {
641 #endif
642 0xbebb, 0x2dec, 0x2dfd, 0x90b0, 0x0068, 0xfcb0, 0xcffc, 0x9320, 0x0068,
643 0x95e8, 0x90b0, 0x0068, 0xfcb0, 0xcffc, 0xe121, 0x90b0, 0x0068, 0xfcb6,
644 0xcff0, 0x9508
645 };
646 /*
647 0001e08c <bootloader__do_spm>:
648 1e08c: bb be out 0x3b, r11 ; 59
649 1e08e: ec 2d mov r30, r12
650 1e090: fd 2d mov r31, r13
651
652 0001e092 <waitA>:
653 1e092: b0 90 68 00 lds r11, 0x0068
654 1e096: b0 fc sbrc r11, 0
655 1e098: fc cf rjmp .-8 ; 0x1e092 <waitA>
656 1e09a: 20 93 68 00 sts 0x0068, r18
657 1e09e: e8 95 spm
658
659 0001e0a0 <waitB>:
660 1e0a0: b0 90 68 00 lds r11, 0x0068
661 1e0a4: b0 fc sbrc r11, 0
662 1e0a6: fc cf rjmp .-8 ; 0x1e0a0 <waitB>
663 1e0a8: 21 e1 ldi r18, 0x11 ; 17
664 1e0aa: b0 90 68 00 lds r11, 0x0068
665 1e0ae: b6 fc sbrc r11, 6
666 1e0b0: f0 cf rjmp .-32 ; 0x1e092 <waitA>
667 1e0b2: 08 95 ret
668 */
669
670
671
672
673
674 #elif defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__) || defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || defined (__AVR_ATmega324PA__) || defined (__AVR_ATmega640__) || defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__) || defined (__AVR_ATmega1280__) || defined (__AVR_ATmega1281__) || defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__) || defined (__AVR_ATmega2560__) || defined (__AVR_ATmega2561__)
675
676 #if defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
677 #if (BOOTLOADER_ADDRESS != 0x3800)
678 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
679 #endif
680 #elif defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || defined (__AVR_ATmega324PA__)
681 #if (BOOTLOADER_ADDRESS != 0x7000)
682 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
683 #endif
684 #elif defined (__AVR_ATmega640__)
685 #if (BOOTLOADER_ADDRESS != 0xE000)
686 #error BOOTLOADER_ADDRESS!=0xE000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
687 #endif
688 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
689 #if (BOOTLOADER_ADDRESS != 0xE000)
690 #error BOOTLOADER_ADDRESS!=0xE000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
691 #endif
692 #elif defined (__AVR_ATmega1280__)
693 #if (BOOTLOADER_ADDRESS != 0x1E000)
694 #error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
695 #endif
696 #elif defined (__AVR_ATmega1281__)
697 #if (BOOTLOADER_ADDRESS != 0x1E000)
698 #error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
699 #endif
700 #elif defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
701 #if (BOOTLOADER_ADDRESS != 0x1E000)
702 #error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
703 #endif
704 #elif defined (__AVR_ATmega2560__)
705 #if (BOOTLOADER_ADDRESS != 0x3E000)
706 #error BOOTLOADER_ADDRESS!=0x3E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
707 #endif
708 #elif defined (__AVR_ATmega2561__)
709 #if (BOOTLOADER_ADDRESS != 0x3E000)
710 #error BOOTLOADER_ADDRESS!=0x3E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
711 #endif
712 #else
713 #error undefined device selection - this should not happen!
714 #endif
715
716 //assume SPMCR:=SPCSR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6 and rampZ=0x3b
717 #if HAVE_SPMINTEREFACE_MAGICVALUE
718 const uint16_t bootloader__do_spm[24] BOOTLIBLINK = {
719 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
720 bootloader__do_spm_magic_exitstrategy(0xf4a9), // brne +21
721 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
722 bootloader__do_spm_magic_exitstrategy(0xf499), // brne +19
723 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
724 bootloader__do_spm_magic_exitstrategy(0xf489), // brne +17
725 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
726 bootloader__do_spm_magic_exitstrategy(0xf479), // brne +15
727 #else
728 const uint16_t bootloader__do_spm[16] BOOTLIBLINK = {
729 #endif
730 0xbebb,
731 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
732 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
733 };
734 /*
735 00001826 <bootloader__do_spm>:
736 1826: bb be out 0x3b,r11 ; rampZ=r11; (rampZ is at IO 0x3b)
737 1828: ec 2d mov r30, r12
738 182a: fd 2d mov r31, r13
739
740 0000182c <waitA>:
741 182c: b7 b6 in r11, 0x37 ; 55
742 182e: b0 fc sbrc r11, 0
743 1830: fd cf rjmp .-6 ; 0x182c <waitA>
744 1832: 27 bf out 0x37, r18 ; 55
745 1834: e8 95 spm
746
747 00001836 <waitB>:
748 1836: b7 b6 in r11, 0x37 ; 55
749 1838: b0 fc sbrc r11, 0
750 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
751 183c: 21 e1 ldi r18, 0x11 ; 17
752 183e: b7 b6 in r11, 0x37 ; 55
753 1840: b6 fc sbrc r11, 6
754 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
755 1844: 08 95 ret
756 */
757
758
759
760
761
762 #else
763 #error "bootloader__do_spm has to be adapted, since there is no architecture code, yet"
764 #endif
765
766
767 #endif
768
769 #endif
770