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