Remove precompiled hexfiles - use demoboard for testing
[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.4
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 * try to make this array as big as possible
436 * (so bootloader always uses 2kbytes flash)
437 */
438 #if defined (__AVR_ATmega8535__) || defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__)
439
440 #if defined (__AVR_ATmega8535__) || defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
441 #if (BOOTLOADER_ADDRESS != 0x1800)
442 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
443 #endif
444 #elif defined (__AVR_ATmega16__)
445 #if (BOOTLOADER_ADDRESS != 0x3800)
446 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
447 #endif
448 #elif defined (__AVR_ATmega32__)
449 #if (BOOTLOADER_ADDRESS != 0x7000)
450 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
451 #endif
452 #else
453 #error undefined device selection - this should not happen!
454 #endif
455
456 //assume SPMCR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6
457 #if HAVE_SPMINTEREFACE_MAGICVALUE
458 const uint16_t bootloader__do_spm[23] BOOTLIBLINK = {
459 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
460 bootloader__do_spm_magic_exitstrategy(0xf4a1), // brne +20
461 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
462 bootloader__do_spm_magic_exitstrategy(0xf491), // brne +18
463 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
464 bootloader__do_spm_magic_exitstrategy(0xf481), // brne +16
465 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
466 bootloader__do_spm_magic_exitstrategy(0xf471), // brne +14
467 #else
468 const uint16_t bootloader__do_spm[15] BOOTLIBLINK = {
469 #endif
470 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
471 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
472 };
473
474 /*
475 00001826 <bootloader__do_spm>:
476 1828: ec 2d mov r30, r12
477 182a: fd 2d mov r31, r13
478
479 0000182c <waitA>:
480 182c: b7 b6 in r11, 0x37 ; 55
481 182e: b0 fc sbrc r11, 0
482 1830: fd cf rjmp .-6 ; 0x182c <waitA>
483 1832: 27 bf out 0x37, r18 ; 55
484 1834: e8 95 spm
485
486 00001836 <waitB>:
487 1836: b7 b6 in r11, 0x37 ; 55
488 1838: b0 fc sbrc r11, 0
489 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
490 183c: 21 e1 ldi r18, 0x11 ; 17
491 183e: b7 b6 in r11, 0x37 ; 55
492 1840: b6 fc sbrc r11, 6
493 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
494 1844: 08 95 ret
495 */
496
497
498
499
500
501 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48P__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
502
503 #if defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__)
504 #if (BOOTLOADER_ADDRESS != 0x1800)
505 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
506 #endif
507 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
508 #if (BOOTLOADER_ADDRESS != 0x3800)
509 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
510 #endif
511 #else
512 #error undefined device selection - this should not happen!
513 #endif
514
515 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
516 #if HAVE_SPMINTEREFACE_MAGICVALUE
517 const uint16_t bootloader__do_spm[23] BOOTLIBLINK = {
518 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
519 bootloader__do_spm_magic_exitstrategy(0xf4a1), // brne +20
520 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
521 bootloader__do_spm_magic_exitstrategy(0xf491), // brne +18
522 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
523 bootloader__do_spm_magic_exitstrategy(0xf481), // brne +16
524 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
525 bootloader__do_spm_magic_exitstrategy(0xf471), // brne +14
526 #else
527 const uint16_t bootloader__do_spm[15] BOOTLIBLINK = {
528 #endif
529 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
530 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
531 };
532 /*
533 00001826 <bootloader__do_spm>:
534 1828: ec 2d mov r30, r12
535 182a: fd 2d mov r31, r13
536
537 0000182c <waitA>:
538 182c: b7 b6 in r11, 0x37 ; 55
539 182e: b0 fc sbrc r11, 0
540 1830: fd cf rjmp .-6 ; 0x182c <waitA>
541 1832: 27 bf out 0x37, r18 ; 55
542 1834: e8 95 spm
543
544 00001836 <waitB>:
545 1836: b7 b6 in r11, 0x37 ; 55
546 1838: b0 fc sbrc r11, 0
547 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
548 183c: 21 e1 ldi r18, 0x11 ; 17
549 183e: b7 b6 in r11, 0x37 ; 55
550 1840: b6 fc sbrc r11, 6
551 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
552 1844: 08 95 ret
553 */
554
555
556
557
558
559 #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__)
560
561 #if defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88PA__)
562 #if (BOOTLOADER_ADDRESS != 0x1800)
563 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
564 #endif
565 #elif defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168PA__)
566 #if (BOOTLOADER_ADDRESS != 0x3800)
567 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
568 #endif
569 #elif defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
570 #if (BOOTLOADER_ADDRESS != 0x7000)
571 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
572 #endif
573 #else
574 #error undefined device selection - this should not happen!
575 #endif
576
577 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
578 #if HAVE_SPMINTEREFACE_MAGICVALUE
579 const uint16_t bootloader__do_spm[23] BOOTLIBLINK = {
580 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
581 bootloader__do_spm_magic_exitstrategy(0xf4a1), // brne +20
582 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
583 bootloader__do_spm_magic_exitstrategy(0xf491), // brne +18
584 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
585 bootloader__do_spm_magic_exitstrategy(0xf481), // brne +16
586 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
587 bootloader__do_spm_magic_exitstrategy(0xf471), // brne +14
588 #else
589 const uint16_t bootloader__do_spm[15] BOOTLIBLINK = {
590 #endif
591 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
592 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
593 };
594 /*
595 00001826 <bootloader__do_spm>:
596 1828: ec 2d mov r30, r12
597 182a: fd 2d mov r31, r13
598
599 0000182c <waitA>:
600 182c: b7 b6 in r11, 0x37 ; 55
601 182e: b0 fc sbrc r11, 0
602 1830: fd cf rjmp .-6 ; 0x182c <waitA>
603 1832: 27 bf out 0x37, r18 ; 55
604 1834: e8 95 spm
605
606 00001836 <waitB>:
607 1836: b7 b6 in r11, 0x37 ; 55
608 1838: b0 fc sbrc r11, 0
609 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
610 183c: 21 e1 ldi r18, 0x11 ; 17
611 183e: b7 b6 in r11, 0x37 ; 55
612 1840: b6 fc sbrc r11, 6
613 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
614 1844: 08 95 ret
615 */
616
617
618
619
620
621 #elif defined (__AVR_ATmega128__)
622
623 #if defined (__AVR_ATmega128__)
624 #if (BOOTLOADER_ADDRESS != 0x1E000)
625 #error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
626 #endif
627 #else
628 #error undefined device selection - this should not happen!
629 #endif
630
631 //assume SPMCR:=SPMCSR==0x68, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6 and rampZ=0x3b
632 #if HAVE_SPMINTEREFACE_MAGICVALUE
633 const uint16_t bootloader__do_spm[28] BOOTLIBLINK = {
634 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
635 bootloader__do_spm_magic_exitstrategy(0xf4c9), // brne +21+4
636 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
637 bootloader__do_spm_magic_exitstrategy(0xf4b9), // brne +19+4
638 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
639 bootloader__do_spm_magic_exitstrategy(0xf4a9), // brne +17+4
640 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
641 bootloader__do_spm_magic_exitstrategy(0xf499), // brne +15+4
642 #else
643 const uint16_t bootloader__do_spm[20] BOOTLIBLINK = {
644 #endif
645 0xbebb, 0x2dec, 0x2dfd, 0x90b0, 0x0068, 0xfcb0, 0xcffc, 0x9320, 0x0068,
646 0x95e8, 0x90b0, 0x0068, 0xfcb0, 0xcffc, 0xe121, 0x90b0, 0x0068, 0xfcb6,
647 0xcff0, 0x9508
648 };
649 /*
650 0001e08c <bootloader__do_spm>:
651 1e08c: bb be out 0x3b, r11 ; 59
652 1e08e: ec 2d mov r30, r12
653 1e090: fd 2d mov r31, r13
654
655 0001e092 <waitA>:
656 1e092: b0 90 68 00 lds r11, 0x0068
657 1e096: b0 fc sbrc r11, 0
658 1e098: fc cf rjmp .-8 ; 0x1e092 <waitA>
659 1e09a: 20 93 68 00 sts 0x0068, r18
660 1e09e: e8 95 spm
661
662 0001e0a0 <waitB>:
663 1e0a0: b0 90 68 00 lds r11, 0x0068
664 1e0a4: b0 fc sbrc r11, 0
665 1e0a6: fc cf rjmp .-8 ; 0x1e0a0 <waitB>
666 1e0a8: 21 e1 ldi r18, 0x11 ; 17
667 1e0aa: b0 90 68 00 lds r11, 0x0068
668 1e0ae: b6 fc sbrc r11, 6
669 1e0b0: f0 cf rjmp .-32 ; 0x1e092 <waitA>
670 1e0b2: 08 95 ret
671 */
672
673
674
675
676
677 #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__)
678
679 #if defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
680 #if (BOOTLOADER_ADDRESS != 0x3800)
681 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
682 #endif
683 #elif defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || defined (__AVR_ATmega324PA__)
684 #if (BOOTLOADER_ADDRESS != 0x7000)
685 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
686 #endif
687 #elif defined (__AVR_ATmega640__)
688 #if (BOOTLOADER_ADDRESS != 0xE000)
689 #error BOOTLOADER_ADDRESS!=0xE000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
690 #endif
691 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
692 #if (BOOTLOADER_ADDRESS != 0xE000)
693 #error BOOTLOADER_ADDRESS!=0xE000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
694 #endif
695 #elif defined (__AVR_ATmega1280__)
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_ATmega1281__)
700 #if (BOOTLOADER_ADDRESS != 0x1E000)
701 #error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
702 #endif
703 #elif defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
704 #if (BOOTLOADER_ADDRESS != 0x1E000)
705 #error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
706 #endif
707 #elif defined (__AVR_ATmega2560__)
708 #if (BOOTLOADER_ADDRESS != 0x3E000)
709 #error BOOTLOADER_ADDRESS!=0x3E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
710 #endif
711 #elif defined (__AVR_ATmega2561__)
712 #if (BOOTLOADER_ADDRESS != 0x3E000)
713 #error BOOTLOADER_ADDRESS!=0x3E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
714 #endif
715 #else
716 #error undefined device selection - this should not happen!
717 #endif
718
719 //assume SPMCR:=SPCSR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6 and rampZ=0x3b
720 #if HAVE_SPMINTEREFACE_MAGICVALUE
721 const uint16_t bootloader__do_spm[24] BOOTLIBLINK = {
722 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
723 bootloader__do_spm_magic_exitstrategy(0xf4a9), // brne +21
724 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
725 bootloader__do_spm_magic_exitstrategy(0xf499), // brne +19
726 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
727 bootloader__do_spm_magic_exitstrategy(0xf489), // brne +17
728 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
729 bootloader__do_spm_magic_exitstrategy(0xf479), // brne +15
730 #else
731 const uint16_t bootloader__do_spm[16] BOOTLIBLINK = {
732 #endif
733 0xbebb,
734 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
735 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
736 };
737 /*
738 00001826 <bootloader__do_spm>:
739 1826: bb be out 0x3b,r11 ; rampZ=r11; (rampZ is at IO 0x3b)
740 1828: ec 2d mov r30, r12
741 182a: fd 2d mov r31, r13
742
743 0000182c <waitA>:
744 182c: b7 b6 in r11, 0x37 ; 55
745 182e: b0 fc sbrc r11, 0
746 1830: fd cf rjmp .-6 ; 0x182c <waitA>
747 1832: 27 bf out 0x37, r18 ; 55
748 1834: e8 95 spm
749
750 00001836 <waitB>:
751 1836: b7 b6 in r11, 0x37 ; 55
752 1838: b0 fc sbrc r11, 0
753 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
754 183c: 21 e1 ldi r18, 0x11 ; 17
755 183e: b7 b6 in r11, 0x37 ; 55
756 1840: b6 fc sbrc r11, 6
757 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
758 1844: 08 95 ret
759 */
760
761
762
763
764
765 #else
766 #error "bootloader__do_spm has to be adapted, since there is no architecture code, yet"
767 #endif
768
769
770 #endif
771
772 #endif
773