USBaspLoader third stable release v0.3
[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 */
8
9 #ifndef SPMINTERFACE_H_f70ba6adf7624275947e859bdbff0599
10 #define SPMINTERFACE_H_f70ba6adf7624275947e859bdbff0599
11
12 /*
13 * spminterface.h offers a lightweight interface by inserting
14 * an small machine-subroutine into the bootsection. (right
15 * after the interrupt-vector-table)
16 * This subroutine can be called by normal code in order to
17 * enable it to program the flash (and depending on BLB11-lockbit
18 * also the bootsection itself). Since SPM-calls from RWW-sections
19 * will fail to work. The Routine will be called "bootloader__do_spm".
20 * Its principle assembler-code is depicted below (real code is a
21 * little machinedependen).
22 * Interfaces will be the 8-bit registers r10..r13, for details
23 * also see below. As also the pageaddress-registes (Z and rampZ)
24 * are interfaced via different registers, it is possible to call
25 * this routine via indirect call (icall).
26 * Traditionally it is also possible to rcall, therefore you can
27 * define "bootloader__do_spm" for your normal code via defsym at
28 * linking time.
29 * Example for an atmega8: "-Wl,--defsym=transfer_point=0x1826"
30 * (since BOOTLOADER_ADDRESS is 0x1800 and there are
31 * 2x19 = 38 = 0x26 byte for interrupts)
32 *
33
34 bootloader__do_spm:
35 ;disable interrupts (if enabled) before calling!
36 ;you may also want to disable wdt, since this routine may busy-loop
37 ;==================================================================
38 ;-->INPUT:
39 ;spmcr (spmcrval determines SPM action) will be register: r18
40 ;MCU dependend RA(MPZ should be transfered within register: r11
41 ;lo8(Z) should be transfered within register: r12
42 ;hi8(Z) should be transfered within register: r13
43 ;( as definition of SPM low8bit of dataword are stored within r0 )
44 ;( as definition of SPM hi8bit of dataword are stored within r1 )
45
46 ;<-->USED/CHANGED:
47 ;temp0 will be register: r11
48 ;temp1 will be register: r12
49 ;temp2 will be register: r13
50 ;spmcrval (r18) may also be changed due to rww reenable-phase r18
51 ;Z (r31:r30) wil be changed during operation
52
53 ;<--OUT:
54 ;==================================================================
55 ; TODO: waitA and waitB could be merged to subroutine saving 2 opc
56 ;==================================================================
57
58 ;load pageaddress (Z) from (r11:)r13:12 since it may was used for icall
59 mov rampZ, r11
60 mov r30, r12
61 mov r31, r13
62
63 waitA: ;check for pending SPM complete
64 in temp0, SPMCR
65 sbrc temp0, SPMEN
66 rjmp waitA
67
68 out SPMCR, spmcrval ;SPM timed sequence
69 spm
70
71 waitB: ;check for previous SPM complete
72 in temp0, SPMCR
73 sbrc temp0, SPMEN
74 rjmp waitB
75
76 ;avoid crash of userapplication
77 ldi spmcrval, ((1<<RWWSRE) | (1<<SPMEN))
78 in temp0, SPMCR
79 sbrc temp0, RWWSB
80 rjmp waitA
81
82 ret
83
84 *
85 */
86
87 #include <avr/io.h>
88 #include <avr/pgmspace.h>
89
90 #ifndef BOOTLOADER_ADDRESS
91 // this header is the interface for user-code
92
93 #ifndef funcaddr___bootloader__do_spm
94 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8HVA__)
95 #define funcaddr___bootloader__do_spm 0x1826
96
97 #else
98 #error "unknown MCU - where is bootloader__do_spm located?"
99 #endif
100 #endif
101
102
103 /*
104 * Call the "bootloader__do_spm"-function, located within the BLS via comfortable C-interface
105 * During operation code will block - disable or reset watchdog before call.
106 *
107 *
108 * REMEMBER: interrupts have to be disabled! (otherwise code may crash non-deterministic)
109 *
110 */
111 void do_spm(const uint32_t flash_byteaddress, const uint8_t spmcrval, const uint8_t r1_highdatabyte, const uint8_t r0_lowdatabyte) {
112 uint8_t loaddr = (flash_byteaddress >> 0) & 0xff;
113 uint8_t hiaddr = (flash_byteaddress >> 8) & 0xff;
114 uint8_t adaddr = (flash_byteaddress >> 16) & 0xff;
115
116 asm volatile (
117 "push r0\n\t"
118 "push r1\n\t"
119
120 "mov r13, %[hiaddr]\n\t"
121 "mov r12, %[loaddr]\n\t"
122 "mov r11, %[adaddr]\n\t"
123
124
125 /* high8 of address of bootloader__do_spm */
126 "ldi r18, %[hi8spmfunc]\n\t"
127 "mov r31, r18\n\t"
128
129 /* low8 of address of bootloader__do_spm */
130 "ldi r18, %[lo8spmfunc]\n\t"
131 "mov r30, r18\n\t"
132
133 /* also load the spmcrval */
134 "mov r18, %[spmcrval]\n\t"
135
136 /* correkt address of bootloader__do_spm from bytes into words */
137 "lsr r31\n\t"
138 "ror r30\n\t"
139
140 "mov r1, %[hidata]\n\t"
141 "mov r0, %[lodata]\n\t"
142
143 /* finally call the bootloader-function */
144 "icall\n\r"
145
146 "pop r1\n\t"
147 "pop r0\n\t"
148
149 :
150 : [loaddr] "r" (loaddr), [hiaddr] "r" (hiaddr), [adaddr] "r" (adaddr), [spmcrval] "r" (spmcrval),
151 [lodata] "r" (r0_lowdatabyte), [hidata] "r" (r1_highdatabyte),
152 [hi8spmfunc] "M" (funcaddr___bootloader__do_spm >> 8), [lo8spmfunc] "M" (funcaddr___bootloader__do_spm & 0xff)
153
154 : "r0","r1","r11","r12","r13","r18"
155 );
156 }
157
158
159
160 #else /*ifndef BOOTLOADER_ADDRESS*/
161 // this header is used directly within bootloader_do_spm
162 #include "bootloaderconfig.h"
163
164 #if HAVE_SPMINTEREFACE
165
166
167 /*
168 * insert architecture dependend "bootloader_do_spm"-code
169 *
170 * try to make this array as big as possible
171 * (so bootloader always uses 2kbytes flash)
172 */
173 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8HVA__)
174 //assume SPMCR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6
175 const uint16_t bootloader__do_spm[19] PROGMEM = {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
176 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF, 0xFFFF,
177 0xFFFF};
178 /*
179 00001826 <bootloader__do_spm>:
180 1826: 00 00 nop
181 1828: ec 2d mov r30, r12
182 182a: fd 2d mov r31, r13
183
184 0000182c <waitA>:
185 182c: b7 b6 in r11, 0x37 ; 55
186 182e: b0 fc sbrc r11, 0
187 1830: fd cf rjmp .-6 ; 0x182c <waitA>
188 1832: 27 bf out 0x37, r18 ; 55
189 1834: e8 95 spm
190
191 00001836 <waitB>:
192 1836: b7 b6 in r11, 0x37 ; 55
193 1838: b0 fc sbrc r11, 0
194 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
195 183c: 21 e1 ldi r18, 0x11 ; 17
196 183e: b7 b6 in r11, 0x37 ; 55
197 1840: b6 fc sbrc r11, 6
198 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
199 1844: 08 95 ret
200 */
201
202
203
204
205
206 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48P__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
207 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
208 const uint16_t bootloader__do_spm[19] PROGMEM = {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
209 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF, 0xFFFF,
210 0xFFFF};
211 /*
212 00001826 <bootloader__do_spm>:
213 1826: 00 00 nop
214 1828: ec 2d mov r30, r12
215 182a: fd 2d mov r31, r13
216
217 0000182c <waitA>:
218 182c: b7 b6 in r11, 0x37 ; 55
219 182e: b0 fc sbrc r11, 0
220 1830: fd cf rjmp .-6 ; 0x182c <waitA>
221 1832: 27 bf out 0x37, r18 ; 55
222 1834: e8 95 spm
223
224 00001836 <waitB>:
225 1836: b7 b6 in r11, 0x37 ; 55
226 1838: b0 fc sbrc r11, 0
227 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
228 183c: 21 e1 ldi r18, 0x11 ; 17
229 183e: b7 b6 in r11, 0x37 ; 55
230 1840: b6 fc sbrc r11, 6
231 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
232 1844: 08 95 ret
233 */
234
235
236
237
238
239 #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__)
240 //assume SPMCR:=SPMCSR==0x37, SPMEN:=SELFPRGEN==0x0, RWWSRE=0x4, RWWSB=0x6
241 const uint16_t bootloader__do_spm[19] PROGMEM = {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
242 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF, 0xFFFF,
243 0xFFFF};
244 /*
245 00001826 <bootloader__do_spm>:
246 1826: 00 00 nop
247 1828: ec 2d mov r30, r12
248 182a: fd 2d mov r31, r13
249
250 0000182c <waitA>:
251 182c: b7 b6 in r11, 0x37 ; 55
252 182e: b0 fc sbrc r11, 0
253 1830: fd cf rjmp .-6 ; 0x182c <waitA>
254 1832: 27 bf out 0x37, r18 ; 55
255 1834: e8 95 spm
256
257 00001836 <waitB>:
258 1836: b7 b6 in r11, 0x37 ; 55
259 1838: b0 fc sbrc r11, 0
260 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
261 183c: 21 e1 ldi r18, 0x11 ; 17
262 183e: b7 b6 in r11, 0x37 ; 55
263 1840: b6 fc sbrc r11, 6
264 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
265 1844: 08 95 ret
266 */
267
268
269
270
271
272 #elif defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164PA__) || defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324PA__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644PA__) || defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
273 //assume SPMCR:=SPCSR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6
274 const uint16_t bootloader__do_spm[19] PROGMEM = {0x0000, 0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
275 0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508, 0xFFFF, 0xFFFF,
276 0xFFFF};
277 /*
278 00001826 <bootloader__do_spm>:
279 1826: 00 00 nop
280 1828: ec 2d mov r30, r12
281 182a: fd 2d mov r31, r13
282
283 0000182c <waitA>:
284 182c: b7 b6 in r11, 0x37 ; 55
285 182e: b0 fc sbrc r11, 0
286 1830: fd cf rjmp .-6 ; 0x182c <waitA>
287 1832: 27 bf out 0x37, r18 ; 55
288 1834: e8 95 spm
289
290 00001836 <waitB>:
291 1836: b7 b6 in r11, 0x37 ; 55
292 1838: b0 fc sbrc r11, 0
293 183a: fd cf rjmp .-6 ; 0x1836 <waitB>
294 183c: 21 e1 ldi r18, 0x11 ; 17
295 183e: b7 b6 in r11, 0x37 ; 55
296 1840: b6 fc sbrc r11, 6
297 1842: f4 cf rjmp .-24 ; 0x182c <waitA>
298 1844: 08 95 ret
299 */
300
301
302
303
304
305 #else
306 #error "bootloader__do_spm has to be adapted, since there is no architecture code, yet"
307 #endif
308
309
310 #endif
311 #endif /*ifdef BOOTLOADER_ADDRESS*/
312
313 #endif
314