extend "spminterface.h" for support of ATmega128
[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.8
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 ;spmcr (spmcrval determines SPM action) will be register: r18
41 ;MCU dependend RA(MPZ should be transfered within register: r11
42 ;lo8(Z) should be transfered within register: r12
43 ;hi8(Z) should be transfered within register: r13
44 ;( as definition of SPM low8bit of dataword are stored within r0 )
45 ;( as definition of SPM hi8bit of dataword are stored within r1 )
46
47 ;<-->USED/CHANGED:
48 ;temp0 will be register: r11
49 ;temp1 will be register: r12
50 ;temp2 will be register: r13
51 ;spmcrval (r18) may also be changed due to rww reenable-phase r18
52 ;Z (r31:r30) wil be changed during operation
53
54 ;<--OUT:
55 ;==================================================================
56 ; TODO: waitA and waitB could be merged to subroutine saving 2 opc
57 ;==================================================================
58
59 ;load pageaddress (Z) from (r11:)r13:12 since it may was used for icall
60 mov rampZ, r11
61 mov r30, r12
62 mov r31, r13
63
64 waitA: ;check for pending SPM complete
65 in temp0, SPMCR
66 sbrc temp0, SPMEN
67 rjmp waitA
68
69 out SPMCR, spmcrval ;SPM timed sequence
70 spm
71
72 waitB: ;check for previous SPM complete
73 in temp0, SPMCR
74 sbrc temp0, SPMEN
75 rjmp waitB
76
77 ;avoid crash of userapplication
78 ldi spmcrval, ((1<<RWWSRE) | (1<<SPMEN))
79 in temp0, SPMCR
80 sbrc temp0, RWWSB
81 rjmp waitA
82
83 ret
84
85 *
86 */
87
88 #include <avr/io.h>
89
90
91
92 /*
93 * This MACRO commands the linker to place correspondig
94 * data on the top of the firmware. (Right after the
95 * interrupt-vector-table)
96 * This is necessary to always locate the
97 * "bootloader__do_spm" for example at 0x1826, even if
98 * there are existing PROGMEM within the firmware...
99 */
100 #define BOOTLIBLINK __attribute__ ((section (".vectors") ))
101
102
103 #ifndef funcaddr___bootloader__do_spm
104 #if (defined(BOOTLOADER_ADDRESS)) && (!(defined(NEW_BOOTLOADER_ADDRESS)))
105 #if HAVE_SPMINTEREFACE
106 #define funcaddr___bootloader__do_spm (&bootloader__do_spm)
107 #endif
108 #else
109 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
110 #define funcaddr___bootloader__do_spm 0x1826
111 #elif defined (__AVR_ATmega32__)
112 #define funcaddr___bootloader__do_spm 0x7054
113 #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88PA__)
114 #define funcaddr___bootloader__do_spm 0x1834
115 #elif defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
116 #define funcaddr___bootloader__do_spm 0x387c
117 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168PA__)
118 #define funcaddr___bootloader__do_spm 0x3868
119 #elif defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || defined (__AVR_ATmega324PA__)
120 #define funcaddr___bootloader__do_spm 0x707c
121 #elif defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
122 #define funcaddr___bootloader__do_spm 0x7068
123 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
124 #define funcaddr___bootloader__do_spm 0xe07c
125 #elif defined (__AVR_ATmega128__)
126 #define funcaddr___bootloader__do_spm 0x1e08c
127 #elif defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
128 #define funcaddr___bootloader__do_spm 0x1e08c
129 #else
130 #error "unknown MCU - where is bootloader__do_spm located?"
131 #endif
132 #endif
133 #endif
134
135
136 /*
137 * Call the "bootloader__do_spm"-function, located within the BLS via comfortable C-interface
138 * During operation code will block - disable or reset watchdog before call.
139 *
140 * ATTANTION: Since the underlying "bootloader__do_spm" will automatically reenable the
141 * rww-section, only one way to program the flash will work.
142 * (First erase the page, then fill temp. buffer, finally program...)
143 * Since unblocking rww-section erases the temp. pagebuffer (which happens
144 * after a page-erase), first programming this buffer does not help !!
145 *
146 * REMEMBER: interrupts have to be disabled! (otherwise code may crash non-deterministic)
147 *
148 */
149 #define __do_spm_Ex(flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
150 ({ \
151 asm volatile ( \
152 "push r0\n\t" \
153 "push r1\n\t" \
154 \
155 "mov r13, %B[flashaddress]\n\t" \
156 "mov r12, %A[flashaddress]\n\t" \
157 "mov r11, %C[flashaddress]\n\t" \
158 \
159 /* also load the spmcrval */ \
160 "mov r18, %[spmcrval]\n\t" \
161 \
162 \
163 "mov r1, %B[data]\n\t" \
164 "mov r0, %A[data]\n\t" \
165 \
166 /* finally call the bootloader-function */ \
167 "icall\n\r" \
168 \
169 "pop r1\n\t" \
170 "pop r0\n\t" \
171 \
172 : \
173 : [flashaddress] "r" (flash_wordaddress), \
174 [spmfunctionaddress] "z" (___bootloader__do_spm__ptr), \
175 [spmcrval] "r" (spmcrval), \
176 [data] "r" (dataword) \
177 : "r0","r1","r11","r12","r13","r18" \
178 ); \
179 })
180
181
182 #if (!(defined(BOOTLOADER_ADDRESS))) || (defined(NEW_BOOTLOADER_ADDRESS))
183 void do_spm(const uint32_t flash_byteaddress, const uint8_t spmcrval, const uint16_t dataword) {
184 __do_spm_Ex(flash_byteaddress, spmcrval, dataword, funcaddr___bootloader__do_spm >> 1);
185 }
186 #endif
187
188
189 #include "bootloaderconfig.h"
190
191 #if (HAVE_SPMINTEREFACE) && (defined(BOOTLOADER_ADDRESS)) && (!(defined(NEW_BOOTLOADER_ADDRESS)))
192
193 /*
194 * insert architecture dependend "bootloader_do_spm"-code
195 *
196 * try to make this array as big as possible
197 * (so bootloader always uses 2kbytes flash)
198 */
199 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__) || defined (__AVR_ATmega32__)
200
201 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
202 #if (BOOTLOADER_ADDRESS != 0x1800)
203 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
204 #endif
205 #elif defined (__AVR_ATmega32__)
206 #if (BOOTLOADER_ADDRESS != 0x7000)
207 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
208 #endif
209 #else
210 #error undefined device selection - this should not happen!
211 #endif
212
213 //assume SPMCR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6
214 const uint16_t bootloader__do_spm[20] BOOTLIBLINK = {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
215 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF, 0xFFFF,
216 0xFFFF, 0xFFFF};
217 /*
218 00001826 <bootloader__do_spm>:
219 1826: 00 00 nop
220 1828: ec 2d mov r30, r12
221 182a: fd 2d mov r31, r13
222
223 0000182c <waitA>:
224 182c: b7 b6 in r11, 0x37 ; 55
225 182e: b0 fc sbrc r11, 0
226 1830: fd cf rjmp .-6 ; 0x182c <waitA>
227 1832: 27 bf out 0x37, r18 ; 55
228 1834: e8 95 spm
229
230 00001836 <waitB>:
231 1836: b7 b6 in r11, 0x37 ; 55
232 1838: b0 fc sbrc r11, 0
233 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
234 183c: 21 e1 ldi r18, 0x11 ; 17
235 183e: b7 b6 in r11, 0x37 ; 55
236 1840: b6 fc sbrc r11, 6
237 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
238 1844: 08 95 ret
239 */
240
241
242
243
244
245 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48P__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
246 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
247 const uint16_t bootloader__do_spm[20] BOOTLIBLINK = {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
248 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF, 0xFFFF,
249 0xFFFF, 0xFFFF};
250
251 #if defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__)
252 #if (BOOTLOADER_ADDRESS != 0x1800)
253 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
254 #endif
255 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
256 #if (BOOTLOADER_ADDRESS != 0x3800)
257 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
258 #endif
259 #else
260 #error undefined device selection - this should not happen!
261 #endif
262
263 /*
264 00001826 <bootloader__do_spm>:
265 1826: 00 00 nop
266 1828: ec 2d mov r30, r12
267 182a: fd 2d mov r31, r13
268
269 0000182c <waitA>:
270 182c: b7 b6 in r11, 0x37 ; 55
271 182e: b0 fc sbrc r11, 0
272 1830: fd cf rjmp .-6 ; 0x182c <waitA>
273 1832: 27 bf out 0x37, r18 ; 55
274 1834: e8 95 spm
275
276 00001836 <waitB>:
277 1836: b7 b6 in r11, 0x37 ; 55
278 1838: b0 fc sbrc r11, 0
279 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
280 183c: 21 e1 ldi r18, 0x11 ; 17
281 183e: b7 b6 in r11, 0x37 ; 55
282 1840: b6 fc sbrc r11, 6
283 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
284 1844: 08 95 ret
285 */
286
287
288
289
290
291 #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__)
292
293 #if defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88PA__)
294 #if (BOOTLOADER_ADDRESS != 0x1800)
295 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
296 #endif
297 #elif defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168PA__)
298 #if (BOOTLOADER_ADDRESS != 0x3800)
299 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
300 #endif
301 #elif defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
302 #if (BOOTLOADER_ADDRESS != 0x7000)
303 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
304 #endif
305 #else
306 #error undefined device selection - this should not happen!
307 #endif
308
309 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
310 const uint16_t bootloader__do_spm[20] BOOTLIBLINK = {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
311 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF, 0xFFFF,
312 0xFFFF, 0xFFFF};
313 /*
314 00001826 <bootloader__do_spm>:
315 1826: 00 00 nop
316 1828: ec 2d mov r30, r12
317 182a: fd 2d mov r31, r13
318
319 0000182c <waitA>:
320 182c: b7 b6 in r11, 0x37 ; 55
321 182e: b0 fc sbrc r11, 0
322 1830: fd cf rjmp .-6 ; 0x182c <waitA>
323 1832: 27 bf out 0x37, r18 ; 55
324 1834: e8 95 spm
325
326 00001836 <waitB>:
327 1836: b7 b6 in r11, 0x37 ; 55
328 1838: b0 fc sbrc r11, 0
329 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
330 183c: 21 e1 ldi r18, 0x11 ; 17
331 183e: b7 b6 in r11, 0x37 ; 55
332 1840: b6 fc sbrc r11, 6
333 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
334 1844: 08 95 ret
335 */
336
337
338
339
340
341 #elif defined (__AVR_ATmega128__)
342 //assume SPMCR:=SPMCSR==0x68, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6 and rampZ=0x3b
343 const uint16_t bootloader__do_spm[20] BOOTLIBLINK = {0xbebb, 0x2dec, 0x2dfd, 0x90b0, 0x0068, 0xfcb0, 0xcffc, 0x9320, 0x0068,
344 0x95e8, 0x90b0, 0x0068, 0xfcb0, 0xcffc, 0xe121, 0x90b0, 0x0068, 0xfcb6,
345 0xcff0, 0x9508};
346
347 #if defined (__AVR_ATmega128__)
348 #if (BOOTLOADER_ADDRESS != 0x1E000)
349 #error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
350 #endif
351 #else
352 #error undefined device selection - this should not happen!
353 #endif
354
355 /*
356 0001e08c <bootloader__do_spm>:
357 1e08c: bb be out 0x3b, r11 ; 59
358 1e08e: ec 2d mov r30, r12
359 1e090: fd 2d mov r31, r13
360
361 0001e092 <waitA>:
362 1e092: b0 90 68 00 lds r11, 0x0068
363 1e096: b0 fc sbrc r11, 0
364 1e098: fc cf rjmp .-8 ; 0x1e092 <waitA>
365 1e09a: 20 93 68 00 sts 0x0068, r18
366 1e09e: e8 95 spm
367
368 0001e0a0 <waitB>:
369 1e0a0: b0 90 68 00 lds r11, 0x0068
370 1e0a4: b0 fc sbrc r11, 0
371 1e0a6: fc cf rjmp .-8 ; 0x1e0a0 <waitB>
372 1e0a8: 21 e1 ldi r18, 0x11 ; 17
373 1e0aa: b0 90 68 00 lds r11, 0x0068
374 1e0ae: b6 fc sbrc r11, 6
375 1e0b0: f0 cf rjmp .-32 ; 0x1e092 <waitA>
376 1e0b2: 08 95 ret
377 */
378
379
380
381
382
383 #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__)
384 //assume SPMCR:=SPCSR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6 and rampZ=0x3b
385 const uint16_t bootloader__do_spm[20] BOOTLIBLINK = {0xbebb, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
386 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF, 0xFFFF
387 0xFFFF, 0xFFFF};
388
389 #if defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
390 #if (BOOTLOADER_ADDRESS != 0x3800)
391 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
392 #endif
393 #elif defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || defined (__AVR_ATmega324PA__)
394 #if (BOOTLOADER_ADDRESS != 0x7000)
395 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
396 #endif
397 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
398 #if (BOOTLOADER_ADDRESS != 0xE000)
399 #error BOOTLOADER_ADDRESS!=0xE000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
400 #endif
401 #elif defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
402 #if (BOOTLOADER_ADDRESS != 0x1E000)
403 #error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
404 #endif
405 #else
406 #error undefined device selection - this should not happen!
407 #endif
408
409 /*
410 00001826 <bootloader__do_spm>:
411 1826: bb be out 0x3b,r11 ; rampZ=r11; (rampZ is at IO 0x3b)
412 1828: ec 2d mov r30, r12
413 182a: fd 2d mov r31, r13
414
415 0000182c <waitA>:
416 182c: b7 b6 in r11, 0x37 ; 55
417 182e: b0 fc sbrc r11, 0
418 1830: fd cf rjmp .-6 ; 0x182c <waitA>
419 1832: 27 bf out 0x37, r18 ; 55
420 1834: e8 95 spm
421
422 00001836 <waitB>:
423 1836: b7 b6 in r11, 0x37 ; 55
424 1838: b0 fc sbrc r11, 0
425 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
426 183c: 21 e1 ldi r18, 0x11 ; 17
427 183e: b7 b6 in r11, 0x37 ; 55
428 1840: b6 fc sbrc r11, 6
429 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
430 1844: 08 95 ret
431 */
432
433
434
435
436
437 #else
438 #error "bootloader__do_spm has to be adapted, since there is no architecture code, yet"
439 #endif
440
441
442 #endif
443
444 #endif
445