extend "spminterface.h" for support of multiple ATmegas
[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_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
122 #define funcaddr___bootloader__do_spm 0xe07c
123 #elif defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
124 #define funcaddr___bootloader__do_spm 0x1e08c
125 #else
126 #error "unknown MCU - where is bootloader__do_spm located?"
127 #endif
128 #endif
129 #endif
130
131
132 /*
133 * Call the "bootloader__do_spm"-function, located within the BLS via comfortable C-interface
134 * During operation code will block - disable or reset watchdog before call.
135 *
136 * ATTANTION: Since the underlying "bootloader__do_spm" will automatically reenable the
137 * rww-section, only one way to program the flash will work.
138 * (First erase the page, then fill temp. buffer, finally program...)
139 * Since unblocking rww-section erases the temp. pagebuffer (which happens
140 * after a page-erase), first programming this buffer does not help !!
141 *
142 * REMEMBER: interrupts have to be disabled! (otherwise code may crash non-deterministic)
143 *
144 */
145 #define __do_spm_Ex(flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
146 ({ \
147 asm volatile ( \
148 "push r0\n\t" \
149 "push r1\n\t" \
150 \
151 "mov r13, %B[flashaddress]\n\t" \
152 "mov r12, %A[flashaddress]\n\t" \
153 "mov r11, %C[flashaddress]\n\t" \
154 \
155 /* also load the spmcrval */ \
156 "mov r18, %[spmcrval]\n\t" \
157 \
158 \
159 "mov r1, %B[data]\n\t" \
160 "mov r0, %A[data]\n\t" \
161 \
162 /* finally call the bootloader-function */ \
163 "icall\n\r" \
164 \
165 "pop r1\n\t" \
166 "pop r0\n\t" \
167 \
168 : \
169 : [flashaddress] "r" (flash_wordaddress), \
170 [spmfunctionaddress] "z" (___bootloader__do_spm__ptr), \
171 [spmcrval] "r" (spmcrval), \
172 [data] "r" (dataword) \
173 : "r0","r1","r11","r12","r13","r18" \
174 ); \
175 })
176
177
178 #if (!(defined(BOOTLOADER_ADDRESS))) || (defined(NEW_BOOTLOADER_ADDRESS))
179 void do_spm(const uint32_t flash_byteaddress, const uint8_t spmcrval, const uint16_t dataword) {
180 __do_spm_Ex(flash_byteaddress, spmcrval, dataword, funcaddr___bootloader__do_spm >> 1);
181 }
182 #endif
183
184
185 #include "bootloaderconfig.h"
186
187 #if (HAVE_SPMINTEREFACE) && (defined(BOOTLOADER_ADDRESS)) && (!(defined(NEW_BOOTLOADER_ADDRESS)))
188
189 /*
190 * insert architecture dependend "bootloader_do_spm"-code
191 *
192 * try to make this array as big as possible
193 * (so bootloader always uses 2kbytes flash)
194 */
195 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__) || defined (__AVR_ATmega32__)
196
197 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
198 #if (BOOTLOADER_ADDRESS != 0x1800)
199 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
200 #endif
201 #elif defined (__AVR_ATmega32__)
202 #if (BOOTLOADER_ADDRESS != 0x7000)
203 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
204 #endif
205 #else
206 #error undefined device selection - this should not happen!
207 #endif
208
209 //assume SPMCR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6
210 const uint16_t bootloader__do_spm[17] BOOTLIBLINK = {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
211 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF};
212 /*
213 00001826 <bootloader__do_spm>:
214 1826: 00 00 nop
215 1828: ec 2d mov r30, r12
216 182a: fd 2d mov r31, r13
217
218 0000182c <waitA>:
219 182c: b7 b6 in r11, 0x37 ; 55
220 182e: b0 fc sbrc r11, 0
221 1830: fd cf rjmp .-6 ; 0x182c <waitA>
222 1832: 27 bf out 0x37, r18 ; 55
223 1834: e8 95 spm
224
225 00001836 <waitB>:
226 1836: b7 b6 in r11, 0x37 ; 55
227 1838: b0 fc sbrc r11, 0
228 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
229 183c: 21 e1 ldi r18, 0x11 ; 17
230 183e: b7 b6 in r11, 0x37 ; 55
231 1840: b6 fc sbrc r11, 6
232 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
233 1844: 08 95 ret
234 */
235
236
237
238
239
240 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48P__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
241 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
242 const uint16_t bootloader__do_spm[17] BOOTLIBLINK = {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
243 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF};
244
245 #if defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__)
246 #if (BOOTLOADER_ADDRESS != 0x1800)
247 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
248 #endif
249 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
250 #if (BOOTLOADER_ADDRESS != 0x3800)
251 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
252 #endif
253 #else
254 #error undefined device selection - this should not happen!
255 #endif
256
257 /*
258 00001826 <bootloader__do_spm>:
259 1826: 00 00 nop
260 1828: ec 2d mov r30, r12
261 182a: fd 2d mov r31, r13
262
263 0000182c <waitA>:
264 182c: b7 b6 in r11, 0x37 ; 55
265 182e: b0 fc sbrc r11, 0
266 1830: fd cf rjmp .-6 ; 0x182c <waitA>
267 1832: 27 bf out 0x37, r18 ; 55
268 1834: e8 95 spm
269
270 00001836 <waitB>:
271 1836: b7 b6 in r11, 0x37 ; 55
272 1838: b0 fc sbrc r11, 0
273 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
274 183c: 21 e1 ldi r18, 0x11 ; 17
275 183e: b7 b6 in r11, 0x37 ; 55
276 1840: b6 fc sbrc r11, 6
277 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
278 1844: 08 95 ret
279 */
280
281
282
283
284
285 #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__)
286
287 #if defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88PA__)
288 #if (BOOTLOADER_ADDRESS != 0x1800)
289 #error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
290 #endif
291 #elif defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168PA__)
292 #if (BOOTLOADER_ADDRESS != 0x3800)
293 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
294 #endif
295 #else
296 #error undefined device selection - this should not happen!
297 #endif
298
299 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
300 const uint16_t bootloader__do_spm[17] BOOTLIBLINK = {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
301 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF};
302 /*
303 00001826 <bootloader__do_spm>:
304 1826: 00 00 nop
305 1828: ec 2d mov r30, r12
306 182a: fd 2d mov r31, r13
307
308 0000182c <waitA>:
309 182c: b7 b6 in r11, 0x37 ; 55
310 182e: b0 fc sbrc r11, 0
311 1830: fd cf rjmp .-6 ; 0x182c <waitA>
312 1832: 27 bf out 0x37, r18 ; 55
313 1834: e8 95 spm
314
315 00001836 <waitB>:
316 1836: b7 b6 in r11, 0x37 ; 55
317 1838: b0 fc sbrc r11, 0
318 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
319 183c: 21 e1 ldi r18, 0x11 ; 17
320 183e: b7 b6 in r11, 0x37 ; 55
321 1840: b6 fc sbrc r11, 6
322 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
323 1844: 08 95 ret
324 */
325
326
327
328
329
330 #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__)
331 //assume SPMCR:=SPCSR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6 and rampZ=0x3b
332 const uint16_t bootloader__do_spm[17] BOOTLIBLINK = {0xbebb, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
333 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF};
334
335 #if defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
336 #if (BOOTLOADER_ADDRESS != 0x3800)
337 #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
338 #endif
339 #elif defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || defined (__AVR_ATmega324PA__)
340 #if (BOOTLOADER_ADDRESS != 0x7000)
341 #error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
342 #endif
343 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
344 #if (BOOTLOADER_ADDRESS != 0xE000)
345 #error BOOTLOADER_ADDRESS!=0xE000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
346 #endif
347 #elif defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
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 00001826 <bootloader__do_spm>:
357 1826: bb be out 0x3b,r11 ; rampZ=r11; (rampZ is at IO 0x3b)
358 1828: ec 2d mov r30, r12
359 182a: fd 2d mov r31, r13
360
361 0000182c <waitA>:
362 182c: b7 b6 in r11, 0x37 ; 55
363 182e: b0 fc sbrc r11, 0
364 1830: fd cf rjmp .-6 ; 0x182c <waitA>
365 1832: 27 bf out 0x37, r18 ; 55
366 1834: e8 95 spm
367
368 00001836 <waitB>:
369 1836: b7 b6 in r11, 0x37 ; 55
370 1838: b0 fc sbrc r11, 0
371 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
372 183c: 21 e1 ldi r18, 0x11 ; 17
373 183e: b7 b6 in r11, 0x37 ; 55
374 1840: b6 fc sbrc r11, 6
375 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
376 1844: 08 95 ret
377 */
378
379
380
381
382
383 #else
384 #error "bootloader__do_spm has to be adapted, since there is no architecture code, yet"
385 #endif
386
387
388 #endif
389
390 #endif
391