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