add and extend fuse-bit settings
[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) 2012 by Stephan Baerwolf
6 * License: GNU GPL v2 (see License.txt)
7 * Version: 0.85
8 */
9
10 #ifndef SPMINTERFACE_H_f70ba6adf7624275947e859bdbff0599
11 #define SPMINTERFACE_H_f70ba6adf7624275947e859bdbff0599
12
13 /*
14 * spminterface.h offers a lightweight interface by inserting
15 * an small machine-subroutine into the bootsection. (right
16 * after the interrupt-vector-table)
17 * This subroutine can be called by normal code in order to
18 * enable it to program the flash (and depending on BLB11-lockbit
19 * also the bootsection itself). Since SPM-calls from RWW-sections
20 * will fail to work. The routine will be called "bootloader__do_spm".
21 * Its principle assembler-code is depicted below (real code is a
22 * little machinedependend).
23 * Interfaces will be the 8-bit registers r10..r13, for details
24 * also see below. As also the pageaddress-registes (Z and rampZ)
25 * are interfaced via different registers, it is possible to call
26 * this routine via indirect call (icall).
27 * Traditionally it is also possible to rcall, therefore you can
28 * define "bootloader__do_spm" for your normal code via defsym at
29 * linking time.
30 * Example for an atmega8: "-Wl,--defsym=transfer_point=0x1826"
31 * (since BOOTLOADER_ADDRESS is 0x1800 and there are
32 * 2x19 = 38 = 0x26 byte for interrupts)
33 *
34
35 bootloader__do_spm:
36 ;disable interrupts (if enabled) before calling!
37 ;you may also want to disable wdt, since this routine may busy-loop
38 ;==================================================================
39 ;-->INPUT:
40 ;#if HAVE_SPMINTEREFACE_MAGICVALUE
41 ;magicvalue in r23:r22:r21:r20
42 ;#endif
43 ;spmcr (spmcrval determines SPM action) will be register: r18
44 ;MCU dependend RA(MPZ should be transfered within register: r11
45 ;lo8(Z) should be transfered within register: r12
46 ;hi8(Z) should be transfered within register: r13
47 ;( as definition of SPM low8bit of dataword are stored within r0 )
48 ;( as definition of SPM hi8bit of dataword are stored within r1 )
49
50 ;<-->USED/CHANGED:
51 ;temp0 will be register: r11
52 ;temp1 will be register: r12
53 ;temp2 will be register: r13
54 ;spmcrval (r18) may also be changed due to rww reenable-phase r18
55 ;Z (r31:r30) wil be changed during operation
56
57 ;<--OUT:
58 ;==================================================================
59 ; TODO: waitA and waitB could be merged to subroutine saving 2 opc
60 ;==================================================================
61 ;<magicvalue specific code (not depicted here)>
62 ;load pageaddress (Z) from (r11:)r13:12 since it may was used for icall
63 mov rampZ, r11
64 mov r30, r12
65 mov r31, r13
66
67 waitA: ;check for pending SPM complete
68 in temp0, SPMCR
69 sbrc temp0, SPMEN
70 rjmp waitA
71
72 out SPMCR, spmcrval ;SPM timed sequence
73 spm
74
75 waitB: ;check for previous SPM complete
76 in temp0, SPMCR
77 sbrc temp0, SPMEN
78 rjmp waitB
79
80 ;avoid crash of userapplication
81 ldi spmcrval, ((1<<RWWSRE) | (1<<SPMEN))
82 in temp0, SPMCR
83 sbrc temp0, RWWSB
84 rjmp waitA
85
86 ret
87
88 *
89 */
90
91 #include <avr/io.h>
92 #include "bootloaderconfig.h"
93
94
95
96 /*
97 * This MACRO commands the linker to place correspondig
98 * data on the top of the firmware. (Right after the
99 * interrupt-vector-table)
100 * This is necessary to always locate the
101 * "bootloader__do_spm" for example at 0x1826, even if
102 * there are existing PROGMEM within the firmware...
103 */
104 #define BOOTLIBLINK __attribute__ ((section (".vectors") ))
105
106
107 #ifndef funcaddr___bootloader__do_spm
108 #if (defined(BOOTLOADER_ADDRESS)) && (!(defined(NEW_BOOTLOADER_ADDRESS)))
109 #if HAVE_SPMINTEREFACE
110 #define funcaddr___bootloader__do_spm (&bootloader__do_spm)
111 #endif
112 #else
113 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
114 #define funcaddr___bootloader__do_spm 0x1826
115 #elif defined (__AVR_ATmega32__)
116 #define funcaddr___bootloader__do_spm 0x7054
117 #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88PA__)
118 #define funcaddr___bootloader__do_spm 0x1834
119 #elif defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
120 #define funcaddr___bootloader__do_spm 0x387c
121 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168PA__)
122 #define funcaddr___bootloader__do_spm 0x3868
123 #elif defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || defined (__AVR_ATmega324PA__)
124 #define funcaddr___bootloader__do_spm 0x707c
125 #elif defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
126 #define funcaddr___bootloader__do_spm 0x7068
127 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
128 #define funcaddr___bootloader__do_spm 0xe07c
129 #elif defined (__AVR_ATmega128__)
130 #define funcaddr___bootloader__do_spm 0x1e08c
131 #elif defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
132 #define funcaddr___bootloader__do_spm 0x1e08c
133 #else
134 #error "unknown MCU - where is bootloader__do_spm located?"
135 #endif
136 #endif
137 #endif
138
139
140 /*
141 * Call the "bootloader__do_spm"-function, located within the BLS via comfortable C-interface
142 * During operation code will block - disable or reset watchdog before call.
143 *
144 * ATTANTION: Since the underlying "bootloader__do_spm" will automatically reenable the
145 * rww-section, only one way to program the flash will work.
146 * (First erase the page, then fill temp. buffer, finally program...)
147 * Since unblocking rww-section erases the temp. pagebuffer (which happens
148 * after a page-erase), first programming this buffer does not help !!
149 *
150 * REMEMBER: interrupts have to be disabled! (otherwise code may crash non-deterministic)
151 *
152 */
153 #if HAVE_SPMINTEREFACE_MAGICVALUE
154 #define __do_spm_Ex __do_spm_Ex_magic
155 #else
156 #define __do_spm_Ex __do_spm_Ex_
157 #endif
158
159 #define __do_spm_Ex_(flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
160 ({ \
161 asm volatile ( \
162 "push r0\n\t" \
163 "push r1\n\t" \
164 \
165 "mov r13, %B[flashaddress]\n\t" \
166 "mov r12, %A[flashaddress]\n\t" \
167 "mov r11, %C[flashaddress]\n\t" \
168 \
169 /* also load the spmcrval */ \
170 "mov r18, %[spmcrval]\n\t" \
171 \
172 \
173 "mov r1, %B[data]\n\t" \
174 "mov r0, %A[data]\n\t" \
175 \
176 /* finally call the bootloader-function */ \
177 "icall\n\t" \
178 \
179 /* \
180 * bootloader__do_spm should change spmcrval (r18) to \
181 * "((1<<RWWSRE) | (1<<SPMEN))" in case of success \
182 */ \
183 "cpi r18, %[spmret]\n\t" \
184 /* loop infitinte if not so, most likely we called an bootloader__do_spm \
185 * with wrong magic! To avoid calls to wrong initialized pages, better crash here... \
186 */ \
187 "loop%=: \n\t" \
188 "brne loop%= \n\t" \
189 \
190 "pop r1\n\t" \
191 "pop r0\n\t" \
192 \
193 : \
194 : [flashaddress] "r" (flash_wordaddress), \
195 [spmfunctionaddress] "z" ((uint16_t)(___bootloader__do_spm__ptr)), \
196 [spmcrval] "r" (spmcrval), \
197 [data] "r" (dataword), \
198 [spmret] "M" ((1<<RWWSRE) | (1<<SPMEN)) \
199 : "r0","r1","r11","r12","r13","r18" \
200 ); \
201 })
202
203 #define __do_spm_Ex_magic(flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
204 ({ \
205 asm volatile ( \
206 "push r0\n\t" \
207 "push r1\n\t" \
208 \
209 "ldi r23, %[magicD] \n\t" \
210 "ldi r22, %[magicC] \n\t" \
211 "ldi r21, %[magicB] \n\t" \
212 "ldi r20, %[magicA] \n\t" \
213 \
214 "mov r13, %B[flashaddress]\n\t" \
215 "mov r12, %A[flashaddress]\n\t" \
216 "mov r11, %C[flashaddress]\n\t" \
217 \
218 /* also load the spmcrval */ \
219 "mov r18, %[spmcrval]\n\t" \
220 \
221 \
222 "mov r1, %B[data]\n\t" \
223 "mov r0, %A[data]\n\t" \
224 \
225 /* finally call the bootloader-function */ \
226 "icall\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 [spmcrval] "r" (spmcrval), \
246 [data] "r" (dataword), \
247 [spmret] "M" ((1<<RWWSRE) | (1<<SPMEN)), \
248 [magicD] "M" ((HAVE_SPMINTEREFACE_MAGICVALUE>>24)&0xff), \
249 [magicC] "M" ((HAVE_SPMINTEREFACE_MAGICVALUE>>16)&0xff), \
250 [magicB] "M" ((HAVE_SPMINTEREFACE_MAGICVALUE>> 8)&0xff), \
251 [magicA] "M" ((HAVE_SPMINTEREFACE_MAGICVALUE>> 0)&0xff) \
252 : "r0","r1","r11","r12","r13","r18","r20","r21","r22","r23" \
253 ); \
254 })
255
256
257 #if (!(defined(BOOTLOADER_ADDRESS))) || (defined(NEW_BOOTLOADER_ADDRESS))
258 void do_spm(const uint32_t flash_byteaddress, const uint8_t spmcrval, const uint16_t dataword) {
259 __do_spm_Ex(flash_byteaddress, spmcrval, dataword, funcaddr___bootloader__do_spm >> 1);
260 }
261 #endif
262
263
264 #if (HAVE_SPMINTEREFACE) && (defined(BOOTLOADER_ADDRESS)) && (!(defined(NEW_BOOTLOADER_ADDRESS)))
265
266 /*
267 * insert architecture dependend "bootloader_do_spm"-code
268 *
269 * try to make this array as big as possible
270 * (so bootloader always uses 2kbytes flash)
271 */
272 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__) || defined (__AVR_ATmega32__)
273
274 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
275 #if (BOOTLOADER_ADDRESS != 0x1800)
276 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
277 #endif
278 #elif defined (__AVR_ATmega32__)
279 #if (BOOTLOADER_ADDRESS != 0x7000)
280 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
281 #endif
282 #else
283 #error undefined device selection - this should not happen!
284 #endif
285
286 //assume SPMCR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6
287 #if HAVE_SPMINTEREFACE_MAGICVALUE
288 const uint16_t bootloader__do_spm[23] BOOTLIBLINK = {
289 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
290 0xf4a1, // brne +20
291 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
292 0xf491, // brne +18
293 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
294 0xf481, // brne +16
295 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
296 0xf471, // brne +14
297 #else
298 const uint16_t bootloader__do_spm[15] BOOTLIBLINK = {
299 #endif
300 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
301 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
302 };
303
304 /*
305 00001826 <bootloader__do_spm>:
306 1828: ec 2d mov r30, r12
307 182a: fd 2d mov r31, r13
308
309 0000182c <waitA>:
310 182c: b7 b6 in r11, 0x37 ; 55
311 182e: b0 fc sbrc r11, 0
312 1830: fd cf rjmp .-6 ; 0x182c <waitA>
313 1832: 27 bf out 0x37, r18 ; 55
314 1834: e8 95 spm
315
316 00001836 <waitB>:
317 1836: b7 b6 in r11, 0x37 ; 55
318 1838: b0 fc sbrc r11, 0
319 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
320 183c: 21 e1 ldi r18, 0x11 ; 17
321 183e: b7 b6 in r11, 0x37 ; 55
322 1840: b6 fc sbrc r11, 6
323 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
324 1844: 08 95 ret
325 */
326
327
328
329
330
331 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48P__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
332
333 #if defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__)
334 #if (BOOTLOADER_ADDRESS != 0x1800)
335 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
336 #endif
337 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
338 #if (BOOTLOADER_ADDRESS != 0x3800)
339 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
340 #endif
341 #else
342 #error undefined device selection - this should not happen!
343 #endif
344
345 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
346 #if HAVE_SPMINTEREFACE_MAGICVALUE
347 const uint16_t bootloader__do_spm[23] BOOTLIBLINK = {
348 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
349 0xf4a1, // brne +20
350 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
351 0xf491, // brne +18
352 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
353 0xf481, // brne +16
354 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
355 0xf471, // brne +14
356 #else
357 const uint16_t bootloader__do_spm[15] BOOTLIBLINK = {
358 #endif
359 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
360 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
361 };
362 /*
363 00001826 <bootloader__do_spm>:
364 1828: ec 2d mov r30, r12
365 182a: fd 2d mov r31, r13
366
367 0000182c <waitA>:
368 182c: b7 b6 in r11, 0x37 ; 55
369 182e: b0 fc sbrc r11, 0
370 1830: fd cf rjmp .-6 ; 0x182c <waitA>
371 1832: 27 bf out 0x37, r18 ; 55
372 1834: e8 95 spm
373
374 00001836 <waitB>:
375 1836: b7 b6 in r11, 0x37 ; 55
376 1838: b0 fc sbrc r11, 0
377 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
378 183c: 21 e1 ldi r18, 0x11 ; 17
379 183e: b7 b6 in r11, 0x37 ; 55
380 1840: b6 fc sbrc r11, 6
381 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
382 1844: 08 95 ret
383 */
384
385
386
387
388
389 #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__)
390
391 #if defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88PA__)
392 #if (BOOTLOADER_ADDRESS != 0x1800)
393 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
394 #endif
395 #elif defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168PA__)
396 #if (BOOTLOADER_ADDRESS != 0x3800)
397 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
398 #endif
399 #elif defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
400 #if (BOOTLOADER_ADDRESS != 0x7000)
401 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
402 #endif
403 #else
404 #error undefined device selection - this should not happen!
405 #endif
406
407 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
408 #if HAVE_SPMINTEREFACE_MAGICVALUE
409 const uint16_t bootloader__do_spm[23] BOOTLIBLINK = {
410 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
411 0xf4a1, // brne +20
412 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
413 0xf491, // brne +18
414 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
415 0xf481, // brne +16
416 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
417 0xf471, // brne +14
418 #else
419 const uint16_t bootloader__do_spm[15] BOOTLIBLINK = {
420 #endif
421 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
422 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
423 };
424 /*
425 00001826 <bootloader__do_spm>:
426 1828: ec 2d mov r30, r12
427 182a: fd 2d mov r31, r13
428
429 0000182c <waitA>:
430 182c: b7 b6 in r11, 0x37 ; 55
431 182e: b0 fc sbrc r11, 0
432 1830: fd cf rjmp .-6 ; 0x182c <waitA>
433 1832: 27 bf out 0x37, r18 ; 55
434 1834: e8 95 spm
435
436 00001836 <waitB>:
437 1836: b7 b6 in r11, 0x37 ; 55
438 1838: b0 fc sbrc r11, 0
439 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
440 183c: 21 e1 ldi r18, 0x11 ; 17
441 183e: b7 b6 in r11, 0x37 ; 55
442 1840: b6 fc sbrc r11, 6
443 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
444 1844: 08 95 ret
445 */
446
447
448
449
450
451 #elif defined (__AVR_ATmega128__)
452
453 #if defined (__AVR_ATmega128__)
454 #if (BOOTLOADER_ADDRESS != 0x1E000)
455 #error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
456 #endif
457 #else
458 #error undefined device selection - this should not happen!
459 #endif
460
461 //assume SPMCR:=SPMCSR==0x68, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6 and rampZ=0x3b
462 #if HAVE_SPMINTEREFACE_MAGICVALUE
463 const uint16_t bootloader__do_spm[28] BOOTLIBLINK = {
464 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
465 0xf4c9, // brne +21+4
466 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
467 0xf4b9, // brne +19+4
468 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
469 0xf4a9, // brne +17+4
470 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
471 0xf499, // brne +15+4
472 #else
473 const uint16_t bootloader__do_spm[20] BOOTLIBLINK = {
474 #endif
475 0xbebb, 0x2dec, 0x2dfd, 0x90b0, 0x0068, 0xfcb0, 0xcffc, 0x9320, 0x0068,
476 0x95e8, 0x90b0, 0x0068, 0xfcb0, 0xcffc, 0xe121, 0x90b0, 0x0068, 0xfcb6,
477 0xcff0, 0x9508
478 };
479 /*
480 0001e08c <bootloader__do_spm>:
481 1e08c: bb be out 0x3b, r11 ; 59
482 1e08e: ec 2d mov r30, r12
483 1e090: fd 2d mov r31, r13
484
485 0001e092 <waitA>:
486 1e092: b0 90 68 00 lds r11, 0x0068
487 1e096: b0 fc sbrc r11, 0
488 1e098: fc cf rjmp .-8 ; 0x1e092 <waitA>
489 1e09a: 20 93 68 00 sts 0x0068, r18
490 1e09e: e8 95 spm
491
492 0001e0a0 <waitB>:
493 1e0a0: b0 90 68 00 lds r11, 0x0068
494 1e0a4: b0 fc sbrc r11, 0
495 1e0a6: fc cf rjmp .-8 ; 0x1e0a0 <waitB>
496 1e0a8: 21 e1 ldi r18, 0x11 ; 17
497 1e0aa: b0 90 68 00 lds r11, 0x0068
498 1e0ae: b6 fc sbrc r11, 6
499 1e0b0: f0 cf rjmp .-32 ; 0x1e092 <waitA>
500 1e0b2: 08 95 ret
501 */
502
503
504
505
506
507 #elif defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__) || defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || defined (__AVR_ATmega324PA__) || defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__) || defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
508
509 #if defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
510 #if (BOOTLOADER_ADDRESS != 0x3800)
511 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
512 #endif
513 #elif defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || defined (__AVR_ATmega324PA__)
514 #if (BOOTLOADER_ADDRESS != 0x7000)
515 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
516 #endif
517 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
518 #if (BOOTLOADER_ADDRESS != 0xE000)
519 #error BOOTLOADER_ADDRESS!=0xE000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
520 #endif
521 #elif defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
522 #if (BOOTLOADER_ADDRESS != 0x1E000)
523 #error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
524 #endif
525 #else
526 #error undefined device selection - this should not happen!
527 #endif
528
529 //assume SPMCR:=SPCSR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6 and rampZ=0x3b
530 #if HAVE_SPMINTEREFACE_MAGICVALUE
531 const uint16_t bootloader__do_spm[24] BOOTLIBLINK = {
532 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
533 0xf4a9, // brne +21
534 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
535 0xf499, // brne +19
536 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
537 0xf489, // brne +17
538 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
539 0xf479, // brne +15
540 #else
541 const uint16_t bootloader__do_spm[16] BOOTLIBLINK = {
542 #endif
543 0xbebb,
544 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
545 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
546 };
547 /*
548 00001826 <bootloader__do_spm>:
549 1826: bb be out 0x3b,r11 ; rampZ=r11; (rampZ is at IO 0x3b)
550 1828: ec 2d mov r30, r12
551 182a: fd 2d mov r31, r13
552
553 0000182c <waitA>:
554 182c: b7 b6 in r11, 0x37 ; 55
555 182e: b0 fc sbrc r11, 0
556 1830: fd cf rjmp .-6 ; 0x182c <waitA>
557 1832: 27 bf out 0x37, r18 ; 55
558 1834: e8 95 spm
559
560 00001836 <waitB>:
561 1836: b7 b6 in r11, 0x37 ; 55
562 1838: b0 fc sbrc r11, 0
563 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
564 183c: 21 e1 ldi r18, 0x11 ; 17
565 183e: b7 b6 in r11, 0x37 ; 55
566 1840: b6 fc sbrc r11, 6
567 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
568 1844: 08 95 ret
569 */
570
571
572
573
574
575 #else
576 #error "bootloader__do_spm has to be adapted, since there is no architecture code, yet"
577 #endif
578
579
580 #endif
581
582 #endif
583