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