66fcd3931213d196da80c22379a11eebd34bafa6
[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.95
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
137 #if defined(_VECTORS_SIZE)
138 #if (funcaddr___bootloader__do_spm != (BOOTLOADER_ADDRESS+_VECTORS_SIZE))
139 #error "bootloader__do_spm is not located after interrupts - sth. is very wrong here!"
140 #endif
141 #endif
142
143 #endif
144 #endif
145
146
147 #ifndef SPMEN
148 #define SPMEN SELFPRGEN
149 #endif
150
151 /*
152 * Call the "bootloader__do_spm"-function, located within the BLS via comfortable C-interface
153 * During operation code will block - disable or reset watchdog before call.
154 *
155 * ATTANTION: Since the underlying "bootloader__do_spm" will automatically reenable the
156 * rww-section, only one way to program the flash will work.
157 * (First erase the page, then fill temp. buffer, finally program...)
158 * Since unblocking rww-section erases the temp. pagebuffer (which happens
159 * after a page-erase), first programming this buffer does not help !!
160 *
161 * REMEMBER: interrupts have to be disabled! (otherwise code may crash non-deterministic)
162 *
163 */
164 #if HAVE_SPMINTEREFACE_MAGICVALUE
165 #define __do_spm_Ex __do_spm_Ex_magic
166 #else
167 #define __do_spm_Ex __do_spm_Ex_
168 #endif
169
170 #define __do_spm_Ex_(flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
171 ({ \
172 asm volatile ( \
173 "push r0\n\t" \
174 "push r1\n\t" \
175 \
176 "mov r13, %B[flashaddress]\n\t" \
177 "mov r12, %A[flashaddress]\n\t" \
178 "mov r11, %C[flashaddress]\n\t" \
179 \
180 /* also load the spmcrval */ \
181 "mov r18, %[spmcrval]\n\t" \
182 \
183 \
184 "mov r1, %B[data]\n\t" \
185 "mov r0, %A[data]\n\t" \
186 \
187 /* finally call the bootloader-function */ \
188 "icall\n\t" \
189 \
190 /* \
191 * bootloader__do_spm should change spmcrval (r18) to \
192 * "((1<<RWWSRE) | (1<<SPMEN))" in case of success \
193 */ \
194 "cpi r18, %[spmret]\n\t" \
195 /* loop infitinte if not so, most likely we called an bootloader__do_spm \
196 * with wrong magic! To avoid calls to wrong initialized pages, better crash here... \
197 */ \
198 "loop%=: \n\t" \
199 "brne loop%= \n\t" \
200 \
201 "pop r1\n\t" \
202 "pop r0\n\t" \
203 \
204 : \
205 : [flashaddress] "r" (flash_wordaddress), \
206 [spmfunctionaddress] "z" ((uint16_t)(___bootloader__do_spm__ptr)), \
207 [spmcrval] "r" (spmcrval), \
208 [data] "r" (dataword), \
209 [spmret] "M" ((1<<RWWSRE) | (1<<SPMEN)) \
210 : "r0","r1","r11","r12","r13","r18" \
211 ); \
212 })
213
214 #define __do_spm_Ex_magic(flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
215 ({ \
216 asm volatile ( \
217 "push r0\n\t" \
218 "push r1\n\t" \
219 \
220 "ldi r23, %[magicD] \n\t" \
221 "ldi r22, %[magicC] \n\t" \
222 "ldi r21, %[magicB] \n\t" \
223 "ldi r20, %[magicA] \n\t" \
224 \
225 "mov r13, %B[flashaddress]\n\t" \
226 "mov r12, %A[flashaddress]\n\t" \
227 "mov r11, %C[flashaddress]\n\t" \
228 \
229 /* also load the spmcrval */ \
230 "mov r18, %[spmcrval]\n\t" \
231 \
232 \
233 "mov r1, %B[data]\n\t" \
234 "mov r0, %A[data]\n\t" \
235 \
236 /* finally call the bootloader-function */ \
237 "icall\n\t" \
238 \
239 /* \
240 * bootloader__do_spm should change spmcrval (r18) to \
241 * "((1<<RWWSRE) | (1<<SPMEN))" in case of success \
242 */ \
243 "cpi r18, %[spmret]\n\t" \
244 /* loop infitinte if not so, most likely we called an bootloader__do_spm \
245 * with wrong magic! To avoid calls to wrong initialized pages, better crash here... \
246 */ \
247 "loop%=: \n\t" \
248 "brne loop%= \n\t" \
249 \
250 "pop r1\n\t" \
251 "pop r0\n\t" \
252 \
253 : \
254 : [flashaddress] "r" (flash_wordaddress), \
255 [spmfunctionaddress] "z" ((uint16_t)(___bootloader__do_spm__ptr)), \
256 [spmcrval] "r" (spmcrval), \
257 [data] "r" (dataword), \
258 [spmret] "M" ((1<<RWWSRE) | (1<<SPMEN)), \
259 [magicD] "M" ((HAVE_SPMINTEREFACE_MAGICVALUE>>24)&0xff), \
260 [magicC] "M" ((HAVE_SPMINTEREFACE_MAGICVALUE>>16)&0xff), \
261 [magicB] "M" ((HAVE_SPMINTEREFACE_MAGICVALUE>> 8)&0xff), \
262 [magicA] "M" ((HAVE_SPMINTEREFACE_MAGICVALUE>> 0)&0xff) \
263 : "r0","r1","r11","r12","r13","r18","r20","r21","r22","r23" \
264 ); \
265 })
266
267
268 #if (!(defined(BOOTLOADER_ADDRESS))) || (defined(NEW_BOOTLOADER_ADDRESS))
269 void do_spm(const uint32_t flash_byteaddress, const uint8_t spmcrval, const uint16_t dataword) {
270 __do_spm_Ex(flash_byteaddress, spmcrval, dataword, funcaddr___bootloader__do_spm >> 1);
271 }
272 #endif
273
274 #if HAVE_SPMINTEREFACE_NORETMAGIC
275 #define bootloader__do_spm_magic_exitstrategy(a) (0xf7f9)
276 #else
277 #define bootloader__do_spm_magic_exitstrategy(a) (a)
278 #endif
279
280 #if (HAVE_SPMINTEREFACE) && (defined(BOOTLOADER_ADDRESS)) && (!(defined(NEW_BOOTLOADER_ADDRESS)))
281
282 /*
283 * insert architecture dependend "bootloader_do_spm"-code
284 *
285 * try to make this array as big as possible
286 * (so bootloader always uses 2kbytes flash)
287 */
288 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__) || defined (__AVR_ATmega32__)
289
290 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
291 #if (BOOTLOADER_ADDRESS != 0x1800)
292 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
293 #endif
294 #elif defined (__AVR_ATmega32__)
295 #if (BOOTLOADER_ADDRESS != 0x7000)
296 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
297 #endif
298 #else
299 #error undefined device selection - this should not happen!
300 #endif
301
302 //assume SPMCR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6
303 #if HAVE_SPMINTEREFACE_MAGICVALUE
304 const uint16_t bootloader__do_spm[23] BOOTLIBLINK = {
305 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
306 bootloader__do_spm_magic_exitstrategy(0xf4a1), // brne +20
307 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
308 bootloader__do_spm_magic_exitstrategy(0xf491), // brne +18
309 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
310 bootloader__do_spm_magic_exitstrategy(0xf481), // brne +16
311 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
312 bootloader__do_spm_magic_exitstrategy(0xf471), // brne +14
313 #else
314 const uint16_t bootloader__do_spm[15] BOOTLIBLINK = {
315 #endif
316 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
317 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
318 };
319
320 /*
321 00001826 <bootloader__do_spm>:
322 1828: ec 2d mov r30, r12
323 182a: fd 2d mov r31, r13
324
325 0000182c <waitA>:
326 182c: b7 b6 in r11, 0x37 ; 55
327 182e: b0 fc sbrc r11, 0
328 1830: fd cf rjmp .-6 ; 0x182c <waitA>
329 1832: 27 bf out 0x37, r18 ; 55
330 1834: e8 95 spm
331
332 00001836 <waitB>:
333 1836: b7 b6 in r11, 0x37 ; 55
334 1838: b0 fc sbrc r11, 0
335 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
336 183c: 21 e1 ldi r18, 0x11 ; 17
337 183e: b7 b6 in r11, 0x37 ; 55
338 1840: b6 fc sbrc r11, 6
339 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
340 1844: 08 95 ret
341 */
342
343
344
345
346
347 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48P__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
348
349 #if defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__)
350 #if (BOOTLOADER_ADDRESS != 0x1800)
351 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
352 #endif
353 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
354 #if (BOOTLOADER_ADDRESS != 0x3800)
355 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
356 #endif
357 #else
358 #error undefined device selection - this should not happen!
359 #endif
360
361 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
362 #if HAVE_SPMINTEREFACE_MAGICVALUE
363 const uint16_t bootloader__do_spm[23] BOOTLIBLINK = {
364 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
365 bootloader__do_spm_magic_exitstrategy(0xf4a1), // brne +20
366 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
367 bootloader__do_spm_magic_exitstrategy(0xf491), // brne +18
368 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
369 bootloader__do_spm_magic_exitstrategy(0xf481), // brne +16
370 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
371 bootloader__do_spm_magic_exitstrategy(0xf471), // brne +14
372 #else
373 const uint16_t bootloader__do_spm[15] BOOTLIBLINK = {
374 #endif
375 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
376 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
377 };
378 /*
379 00001826 <bootloader__do_spm>:
380 1828: ec 2d mov r30, r12
381 182a: fd 2d mov r31, r13
382
383 0000182c <waitA>:
384 182c: b7 b6 in r11, 0x37 ; 55
385 182e: b0 fc sbrc r11, 0
386 1830: fd cf rjmp .-6 ; 0x182c <waitA>
387 1832: 27 bf out 0x37, r18 ; 55
388 1834: e8 95 spm
389
390 00001836 <waitB>:
391 1836: b7 b6 in r11, 0x37 ; 55
392 1838: b0 fc sbrc r11, 0
393 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
394 183c: 21 e1 ldi r18, 0x11 ; 17
395 183e: b7 b6 in r11, 0x37 ; 55
396 1840: b6 fc sbrc r11, 6
397 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
398 1844: 08 95 ret
399 */
400
401
402
403
404
405 #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__)
406
407 #if defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88PA__)
408 #if (BOOTLOADER_ADDRESS != 0x1800)
409 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
410 #endif
411 #elif defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168PA__)
412 #if (BOOTLOADER_ADDRESS != 0x3800)
413 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
414 #endif
415 #elif defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
416 #if (BOOTLOADER_ADDRESS != 0x7000)
417 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
418 #endif
419 #else
420 #error undefined device selection - this should not happen!
421 #endif
422
423 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
424 #if HAVE_SPMINTEREFACE_MAGICVALUE
425 const uint16_t bootloader__do_spm[23] BOOTLIBLINK = {
426 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
427 bootloader__do_spm_magic_exitstrategy(0xf4a1), // brne +20
428 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
429 bootloader__do_spm_magic_exitstrategy(0xf491), // brne +18
430 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
431 bootloader__do_spm_magic_exitstrategy(0xf481), // brne +16
432 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
433 bootloader__do_spm_magic_exitstrategy(0xf471), // brne +14
434 #else
435 const uint16_t bootloader__do_spm[15] BOOTLIBLINK = {
436 #endif
437 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
438 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
439 };
440 /*
441 00001826 <bootloader__do_spm>:
442 1828: ec 2d mov r30, r12
443 182a: fd 2d mov r31, r13
444
445 0000182c <waitA>:
446 182c: b7 b6 in r11, 0x37 ; 55
447 182e: b0 fc sbrc r11, 0
448 1830: fd cf rjmp .-6 ; 0x182c <waitA>
449 1832: 27 bf out 0x37, r18 ; 55
450 1834: e8 95 spm
451
452 00001836 <waitB>:
453 1836: b7 b6 in r11, 0x37 ; 55
454 1838: b0 fc sbrc r11, 0
455 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
456 183c: 21 e1 ldi r18, 0x11 ; 17
457 183e: b7 b6 in r11, 0x37 ; 55
458 1840: b6 fc sbrc r11, 6
459 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
460 1844: 08 95 ret
461 */
462
463
464
465
466
467 #elif defined (__AVR_ATmega128__)
468
469 #if defined (__AVR_ATmega128__)
470 #if (BOOTLOADER_ADDRESS != 0x1E000)
471 #error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
472 #endif
473 #else
474 #error undefined device selection - this should not happen!
475 #endif
476
477 //assume SPMCR:=SPMCSR==0x68, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6 and rampZ=0x3b
478 #if HAVE_SPMINTEREFACE_MAGICVALUE
479 const uint16_t bootloader__do_spm[28] BOOTLIBLINK = {
480 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
481 bootloader__do_spm_magic_exitstrategy(0xf4c9), // brne +21+4
482 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
483 bootloader__do_spm_magic_exitstrategy(0xf4b9), // brne +19+4
484 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
485 bootloader__do_spm_magic_exitstrategy(0xf4a9), // brne +17+4
486 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
487 bootloader__do_spm_magic_exitstrategy(0xf499), // brne +15+4
488 #else
489 const uint16_t bootloader__do_spm[20] BOOTLIBLINK = {
490 #endif
491 0xbebb, 0x2dec, 0x2dfd, 0x90b0, 0x0068, 0xfcb0, 0xcffc, 0x9320, 0x0068,
492 0x95e8, 0x90b0, 0x0068, 0xfcb0, 0xcffc, 0xe121, 0x90b0, 0x0068, 0xfcb6,
493 0xcff0, 0x9508
494 };
495 /*
496 0001e08c <bootloader__do_spm>:
497 1e08c: bb be out 0x3b, r11 ; 59
498 1e08e: ec 2d mov r30, r12
499 1e090: fd 2d mov r31, r13
500
501 0001e092 <waitA>:
502 1e092: b0 90 68 00 lds r11, 0x0068
503 1e096: b0 fc sbrc r11, 0
504 1e098: fc cf rjmp .-8 ; 0x1e092 <waitA>
505 1e09a: 20 93 68 00 sts 0x0068, r18
506 1e09e: e8 95 spm
507
508 0001e0a0 <waitB>:
509 1e0a0: b0 90 68 00 lds r11, 0x0068
510 1e0a4: b0 fc sbrc r11, 0
511 1e0a6: fc cf rjmp .-8 ; 0x1e0a0 <waitB>
512 1e0a8: 21 e1 ldi r18, 0x11 ; 17
513 1e0aa: b0 90 68 00 lds r11, 0x0068
514 1e0ae: b6 fc sbrc r11, 6
515 1e0b0: f0 cf rjmp .-32 ; 0x1e092 <waitA>
516 1e0b2: 08 95 ret
517 */
518
519
520
521
522
523 #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__)
524
525 #if defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
526 #if (BOOTLOADER_ADDRESS != 0x3800)
527 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
528 #endif
529 #elif defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || defined (__AVR_ATmega324PA__)
530 #if (BOOTLOADER_ADDRESS != 0x7000)
531 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
532 #endif
533 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
534 #if (BOOTLOADER_ADDRESS != 0xE000)
535 #error BOOTLOADER_ADDRESS!=0xE000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
536 #endif
537 #elif defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
538 #if (BOOTLOADER_ADDRESS != 0x1E000)
539 #error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
540 #endif
541 #else
542 #error undefined device selection - this should not happen!
543 #endif
544
545 //assume SPMCR:=SPCSR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6 and rampZ=0x3b
546 #if HAVE_SPMINTEREFACE_MAGICVALUE
547 const uint16_t bootloader__do_spm[24] BOOTLIBLINK = {
548 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
549 bootloader__do_spm_magic_exitstrategy(0xf4a9), // brne +21
550 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
551 bootloader__do_spm_magic_exitstrategy(0xf499), // brne +19
552 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 12) & 0xf))<<8) | (0x50 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 8) & 0xf))), // r21
553 bootloader__do_spm_magic_exitstrategy(0xf489), // brne +17
554 (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 4) & 0xf))<<8) | (0x40 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 0) & 0xf))), // r20
555 bootloader__do_spm_magic_exitstrategy(0xf479), // brne +15
556 #else
557 const uint16_t bootloader__do_spm[16] BOOTLIBLINK = {
558 #endif
559 0xbebb,
560 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
561 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
562 };
563 /*
564 00001826 <bootloader__do_spm>:
565 1826: bb be out 0x3b,r11 ; rampZ=r11; (rampZ is at IO 0x3b)
566 1828: ec 2d mov r30, r12
567 182a: fd 2d mov r31, r13
568
569 0000182c <waitA>:
570 182c: b7 b6 in r11, 0x37 ; 55
571 182e: b0 fc sbrc r11, 0
572 1830: fd cf rjmp .-6 ; 0x182c <waitA>
573 1832: 27 bf out 0x37, r18 ; 55
574 1834: e8 95 spm
575
576 00001836 <waitB>:
577 1836: b7 b6 in r11, 0x37 ; 55
578 1838: b0 fc sbrc r11, 0
579 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
580 183c: 21 e1 ldi r18, 0x11 ; 17
581 183e: b7 b6 in r11, 0x37 ; 55
582 1840: b6 fc sbrc r11, 6
583 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
584 1844: 08 95 ret
585 */
586
587
588
589
590
591 #else
592 #error "bootloader__do_spm has to be adapted, since there is no architecture code, yet"
593 #endif
594
595
596 #endif
597
598 #endif
599