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