Debian Codegraph @

bootloaders/optiboot/boot.h

raw
1 /* Modified to use out for SPM access
2 ** Peter Knight, Optiboot project http://optiboot.googlecode.com
3 **
4 ** Todo: Tidy up
5 **
6 ** "_short" routines execute 1 cycle faster and use 1 less word of flash
7 ** by using "out" instruction instead of "sts".
8 **
9 ** Additional elpm variants that trust the value of RAMPZ
10 */
11
12 /* Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007 Eric B. Weddington
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are met:
17
18 * Redistributions of source code must retain the above copyright
19 notice, this list of conditions and the following disclaimer.
20 * Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in
22 the documentation and/or other materials provided with the
23 distribution.
24 * Neither the name of the copyright holders nor the names of
25 contributors may be used to endorse or promote products derived
26 from this software without specific prior written permission.
27
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 POSSIBILITY OF SUCH DAMAGE. */
39
40 #ifndef _AVR_BOOT_H_
41 #define _AVR_BOOT_H_ 1
42
43 /** \file */
44 /** \defgroup avr_boot <avr/boot.h>: Bootloader Support Utilities
45 \code
46 #include <avr/io.h>
47 #include <avr/boot.h>
48 \endcode
49
50 The macros in this module provide a C language interface to the
51 bootloader support functionality of certain AVR processors. These
52 macros are designed to work with all sizes of flash memory.
53
54 Global interrupts are not automatically disabled for these macros. It
55 is left up to the programmer to do this. See the code example below.
56 Also see the processor datasheet for caveats on having global interrupts
57 enabled during writing of the Flash.
58
59 \note Not all AVR processors provide bootloader support. See your
60 processor datasheet to see if it provides bootloader support.
61
62 \todo From email with Marek: On smaller devices (all except ATmega64/128),
63 __SPM_REG is in the I/O space, accessible with the shorter "in" and "out"
64 instructions - since the boot loader has a limited size, this could be an
65 important optimization.
66
67 \par API Usage Example
68 The following code shows typical usage of the boot API.
69
70 \code
71 #include <inttypes.h>
72 #include <avr/interrupt.h>
73 #include <avr/pgmspace.h>
74
75 void boot_program_page (uint32_t page, uint8_t *buf)
76 {
77 uint16_t i;
78 uint8_t sreg;
79
80 // Disable interrupts.
81
82 sreg = SREG;
83 cli();
84
85 eeprom_busy_wait ();
86
87 boot_page_erase (page);
88 boot_spm_busy_wait (); // Wait until the memory is erased.
89
90 for (i=0; i<SPM_PAGESIZE; i+=2)
91 {
92 // Set up little-endian word.
93
94 uint16_t w = *buf++;
95 w += (*buf++) << 8;
96
97 boot_page_fill (page + i, w);
98 }
99
100 boot_page_write (page); // Store buffer in flash page.
101 boot_spm_busy_wait(); // Wait until the memory is written.
102
103 // Reenable RWW-section again. We need this if we want to jump back
104 // to the application after bootloading.
105
106 boot_rww_enable ();
107
108 // Re-enable interrupts (if they were ever enabled).
109
110 SREG = sreg;
111 }\endcode */
112
113 #include <avr/eeprom.h>
114 #include <avr/io.h>
115 #include <inttypes.h>
116 #include <limits.h>
117
118 /* Check for SPM Control Register in processor. */
119 #if defined (SPMCSR)
120 # define __SPM_REG SPMCSR
121 #elif defined (SPMCR)
122 # define __SPM_REG SPMCR
123 #else
124 # error AVR processor does not provide bootloader support!
125 #endif
126
127
128 /* Check for SPM Enable bit. */
129 #if defined(SPMEN)
130 # define __SPM_ENABLE SPMEN
131 #elif defined(SELFPRGEN)
132 # define __SPM_ENABLE SELFPRGEN
133 #else
134 # error Cannot find SPM Enable bit definition!
135 #endif
136
137 /** \ingroup avr_boot
138 \def BOOTLOADER_SECTION
139
140 Used to declare a function or variable to be placed into a
141 new section called .bootloader. This section and its contents
142 can then be relocated to any address (such as the bootloader
143 NRWW area) at link-time. */
144
145 #define BOOTLOADER_SECTION __attribute__ ((section (".bootloader")))
146
147 /* Create common bit definitions. */
148 #ifdef ASB
149 #define __COMMON_ASB ASB
150 #else
151 #define __COMMON_ASB RWWSB
152 #endif
153
154 #ifdef ASRE
155 #define __COMMON_ASRE ASRE
156 #else
157 #define __COMMON_ASRE RWWSRE
158 #endif
159
160 /* Define the bit positions of the Boot Lock Bits. */
161
162 #define BLB12 5
163 #define BLB11 4
164 #define BLB02 3
165 #define BLB01 2
166
167 /** \ingroup avr_boot
168 \def boot_spm_interrupt_enable()
169 Enable the SPM interrupt. */
170
171 #define boot_spm_interrupt_enable() (__SPM_REG |= (uint8_t)_BV(SPMIE))
172
173 /** \ingroup avr_boot
174 \def boot_spm_interrupt_disable()
175 Disable the SPM interrupt. */
176
177 #define boot_spm_interrupt_disable() (__SPM_REG &= (uint8_t)~_BV(SPMIE))
178
179 /** \ingroup avr_boot
180 \def boot_is_spm_interrupt()
181 Check if the SPM interrupt is enabled. */
182
183 #define boot_is_spm_interrupt() (__SPM_REG & (uint8_t)_BV(SPMIE))
184
185 /** \ingroup avr_boot
186 \def boot_rww_busy()
187 Check if the RWW section is busy. */
188
189 #define boot_rww_busy() (__SPM_REG & (uint8_t)_BV(__COMMON_ASB))
190
191 /** \ingroup avr_boot
192 \def boot_spm_busy()
193 Check if the SPM instruction is busy. */
194
195 #define boot_spm_busy() (__SPM_REG & (uint8_t)_BV(__SPM_ENABLE))
196
197 /** \ingroup avr_boot
198 \def boot_spm_busy_wait()
199 Wait while the SPM instruction is busy. */
200
201 #define boot_spm_busy_wait() do{}while(boot_spm_busy())
202
203 #define __BOOT_PAGE_ERASE (_BV(__SPM_ENABLE) | _BV(PGERS))
204 #define __BOOT_PAGE_WRITE (_BV(__SPM_ENABLE) | _BV(PGWRT))
205 #define __BOOT_PAGE_FILL _BV(__SPM_ENABLE)
206 #define __BOOT_RWW_ENABLE (_BV(__SPM_ENABLE) | _BV(__COMMON_ASRE))
207 #define __BOOT_LOCK_BITS_SET (_BV(__SPM_ENABLE) | _BV(BLBSET))
208
209 #define __boot_page_fill_short(address, data) \
210 (__extension__({ \
211 __asm__ __volatile__ \
212 ( \
213 "movw r0, %3\n\t" \
214 "out %0, %1\n\t" \
215 "spm\n\t" \
216 "clr r1\n\t" \
217 : \
218 : "i" (_SFR_IO_ADDR(__SPM_REG)), \
219 "r" ((uint8_t)__BOOT_PAGE_FILL), \
220 "z" ((uint16_t)address), \
221 "r" ((uint16_t)data) \
222 : "r0" \
223 ); \
224 }))
225
226 #define __boot_page_fill_normal(address, data) \
227 (__extension__({ \
228 __asm__ __volatile__ \
229 ( \
230 "movw r0, %3\n\t" \
231 "sts %0, %1\n\t" \
232 "spm\n\t" \
233 "clr r1\n\t" \
234 : \
235 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
236 "r" ((uint8_t)__BOOT_PAGE_FILL), \
237 "z" ((uint16_t)address), \
238 "r" ((uint16_t)data) \
239 : "r0" \
240 ); \
241 }))
242
243 #define __boot_page_fill_alternate(address, data)\
244 (__extension__({ \
245 __asm__ __volatile__ \
246 ( \
247 "movw r0, %3\n\t" \
248 "sts %0, %1\n\t" \
249 "spm\n\t" \
250 ".word 0xffff\n\t" \
251 "nop\n\t" \
252 "clr r1\n\t" \
253 : \
254 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
255 "r" ((uint8_t)__BOOT_PAGE_FILL), \
256 "z" ((uint16_t)address), \
257 "r" ((uint16_t)data) \
258 : "r0" \
259 ); \
260 }))
261
262 #define __boot_page_fill_extended(address, data) \
263 (__extension__({ \
264 __asm__ __volatile__ \
265 ( \
266 "movw r0, %4\n\t" \
267 "movw r30, %A3\n\t" \
268 "sts %1, %C3\n\t" \
269 "sts %0, %2\n\t" \
270 "spm\n\t" \
271 "clr r1\n\t" \
272 : \
273 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
274 "i" (_SFR_MEM_ADDR(RAMPZ)), \
275 "r" ((uint8_t)__BOOT_PAGE_FILL), \
276 "r" ((uint32_t)address), \
277 "r" ((uint16_t)data) \
278 : "r0", "r30", "r31" \
279 ); \
280 }))
281
282 #define __boot_page_fill_extended_short(address, data) \
283 (__extension__({ \
284 __asm__ __volatile__ \
285 ( \
286 "movw r0, %4\n\t" \
287 "movw r30, %A3\n\t" \
288 "out %1, %C3\n\t" \
289 "out %0, %2\n\t" \
290 "spm\n\t" \
291 "clr r1\n\t" \
292 : \
293 : "i" (_SFR_IO_ADDR(__SPM_REG)), \
294 "i" (_SFR_IO_ADDR(RAMPZ)), \
295 "r" ((uint8_t)__BOOT_PAGE_FILL), \
296 "r" ((uint32_t)address), \
297 "r" ((uint16_t)data) \
298 : "r0", "r30", "r31" \
299 ); \
300 }))
301
302 #define __boot_page_erase_short(address) \
303 (__extension__({ \
304 __asm__ __volatile__ \
305 ( \
306 "out %0, %1\n\t" \
307 "spm\n\t" \
308 : \
309 : "i" (_SFR_IO_ADDR(__SPM_REG)), \
310 "r" ((uint8_t)__BOOT_PAGE_ERASE), \
311 "z" ((uint16_t)address) \
312 ); \
313 }))
314
315
316 #define __boot_page_erase_normal(address) \
317 (__extension__({ \
318 __asm__ __volatile__ \
319 ( \
320 "sts %0, %1\n\t" \
321 "spm\n\t" \
322 : \
323 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
324 "r" ((uint8_t)__BOOT_PAGE_ERASE), \
325 "z" ((uint16_t)address) \
326 ); \
327 }))
328
329 #define __boot_page_erase_alternate(address) \
330 (__extension__({ \
331 __asm__ __volatile__ \
332 ( \
333 "sts %0, %1\n\t" \
334 "spm\n\t" \
335 ".word 0xffff\n\t" \
336 "nop\n\t" \
337 : \
338 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
339 "r" ((uint8_t)__BOOT_PAGE_ERASE), \
340 "z" ((uint16_t)address) \
341 ); \
342 }))
343
344 #define __boot_page_erase_extended(address) \
345 (__extension__({ \
346 __asm__ __volatile__ \
347 ( \
348 "movw r30, %A3\n\t" \
349 "sts %1, %C3\n\t" \
350 "sts %0, %2\n\t" \
351 "spm\n\t" \
352 : \
353 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
354 "i" (_SFR_MEM_ADDR(RAMPZ)), \
355 "r" ((uint8_t)__BOOT_PAGE_ERASE), \
356 "r" ((uint32_t)address) \
357 : "r30", "r31" \
358 ); \
359 }))
360 #define __boot_page_erase_extended_short(address) \
361 (__extension__({ \
362 __asm__ __volatile__ \
363 ( \
364 "movw r30, %A3\n\t" \
365 "out %1, %C3\n\t" \
366 "out %0, %2\n\t" \
367 "spm\n\t" \
368 : \
369 : "i" (_SFR_IO_ADDR(__SPM_REG)), \
370 "i" (_SFR_IO_ADDR(RAMPZ)), \
371 "r" ((uint8_t)__BOOT_PAGE_ERASE), \
372 "r" ((uint32_t)address) \
373 : "r30", "r31" \
374 ); \
375 }))
376
377 #define __boot_page_write_short(address) \
378 (__extension__({ \
379 __asm__ __volatile__ \
380 ( \
381 "out %0, %1\n\t" \
382 "spm\n\t" \
383 : \
384 : "i" (_SFR_IO_ADDR(__SPM_REG)), \
385 "r" ((uint8_t)__BOOT_PAGE_WRITE), \
386 "z" ((uint16_t)address) \
387 ); \
388 }))
389
390 #define __boot_page_write_normal(address) \
391 (__extension__({ \
392 __asm__ __volatile__ \
393 ( \
394 "sts %0, %1\n\t" \
395 "spm\n\t" \
396 : \
397 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
398 "r" ((uint8_t)__BOOT_PAGE_WRITE), \
399 "z" ((uint16_t)address) \
400 ); \
401 }))
402
403 #define __boot_page_write_alternate(address) \
404 (__extension__({ \
405 __asm__ __volatile__ \
406 ( \
407 "sts %0, %1\n\t" \
408 "spm\n\t" \
409 ".word 0xffff\n\t" \
410 "nop\n\t" \
411 : \
412 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
413 "r" ((uint8_t)__BOOT_PAGE_WRITE), \
414 "z" ((uint16_t)address) \
415 ); \
416 }))
417
418 #define __boot_page_write_extended(address) \
419 (__extension__({ \
420 __asm__ __volatile__ \
421 ( \
422 "movw r30, %A3\n\t" \
423 "sts %1, %C3\n\t" \
424 "sts %0, %2\n\t" \
425 "spm\n\t" \
426 : \
427 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
428 "i" (_SFR_MEM_ADDR(RAMPZ)), \
429 "r" ((uint8_t)__BOOT_PAGE_WRITE), \
430 "r" ((uint32_t)address) \
431 : "r30", "r31" \
432 ); \
433 }))
434 #define __boot_page_write_extended_short(address) \
435 (__extension__({ \
436 __asm__ __volatile__ \
437 ( \
438 "movw r30, %A3\n\t" \
439 "out %1, %C3\n\t" \
440 "out %0, %2\n\t" \
441 "spm\n\t" \
442 : \
443 : "i" (_SFR_IO_ADDR(__SPM_REG)), \
444 "i" (_SFR_IO_ADDR(RAMPZ)), \
445 "r" ((uint8_t)__BOOT_PAGE_WRITE), \
446 "r" ((uint32_t)address) \
447 : "r30", "r31" \
448 ); \
449 }))
450
451 #define __boot_rww_enable_short() \
452 (__extension__({ \
453 __asm__ __volatile__ \
454 ( \
455 "out %0, %1\n\t" \
456 "spm\n\t" \
457 : \
458 : "i" (_SFR_IO_ADDR(__SPM_REG)), \
459 "r" ((uint8_t)__BOOT_RWW_ENABLE) \
460 ); \
461 }))
462
463 #define __boot_rww_enable() \
464 (__extension__({ \
465 __asm__ __volatile__ \
466 ( \
467 "sts %0, %1\n\t" \
468 "spm\n\t" \
469 : \
470 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
471 "r" ((uint8_t)__BOOT_RWW_ENABLE) \
472 ); \
473 }))
474
475 #define __boot_rww_enable_alternate() \
476 (__extension__({ \
477 __asm__ __volatile__ \
478 ( \
479 "sts %0, %1\n\t" \
480 "spm\n\t" \
481 ".word 0xffff\n\t" \
482 "nop\n\t" \
483 : \
484 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
485 "r" ((uint8_t)__BOOT_RWW_ENABLE) \
486 ); \
487 }))
488
489 /* From the mega16/mega128 data sheets (maybe others):
490
491 Bits by SPM To set the Boot Loader Lock bits, write the desired data to
492 R0, write "X0001001" to SPMCR and execute SPM within four clock cycles
493 after writing SPMCR. The only accessible Lock bits are the Boot Lock bits
494 that may prevent the Application and Boot Loader section from any
495 software update by the MCU.
496
497 If bits 5..2 in R0 are cleared (zero), the corresponding Boot Lock bit
498 will be programmed if an SPM instruction is executed within four cycles
499 after BLBSET and SPMEN (or SELFPRGEN) are set in SPMCR. The Z-pointer is
500 don't care during this operation, but for future compatibility it is
501 recommended to load the Z-pointer with $0001 (same as used for reading the
502 Lock bits). For future compatibility It is also recommended to set bits 7,
503 6, 1, and 0 in R0 to 1 when writing the Lock bits. When programming the
504 Lock bits the entire Flash can be read during the operation. */
505
506 #define __boot_lock_bits_set_short(lock_bits) \
507 (__extension__({ \
508 uint8_t value = (uint8_t)(~(lock_bits)); \
509 __asm__ __volatile__ \
510 ( \
511 "ldi r30, 1\n\t" \
512 "ldi r31, 0\n\t" \
513 "mov r0, %2\n\t" \
514 "out %0, %1\n\t" \
515 "spm\n\t" \
516 : \
517 : "i" (_SFR_IO_ADDR(__SPM_REG)), \
518 "r" ((uint8_t)__BOOT_LOCK_BITS_SET), \
519 "r" (value) \
520 : "r0", "r30", "r31" \
521 ); \
522 }))
523
524 #define __boot_lock_bits_set(lock_bits) \
525 (__extension__({ \
526 uint8_t value = (uint8_t)(~(lock_bits)); \
527 __asm__ __volatile__ \
528 ( \
529 "ldi r30, 1\n\t" \
530 "ldi r31, 0\n\t" \
531 "mov r0, %2\n\t" \
532 "sts %0, %1\n\t" \
533 "spm\n\t" \
534 : \
535 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
536 "r" ((uint8_t)__BOOT_LOCK_BITS_SET), \
537 "r" (value) \
538 : "r0", "r30", "r31" \
539 ); \
540 }))
541
542 #define __boot_lock_bits_set_alternate(lock_bits) \
543 (__extension__({ \
544 uint8_t value = (uint8_t)(~(lock_bits)); \
545 __asm__ __volatile__ \
546 ( \
547 "ldi r30, 1\n\t" \
548 "ldi r31, 0\n\t" \
549 "mov r0, %2\n\t" \
550 "sts %0, %1\n\t" \
551 "spm\n\t" \
552 ".word 0xffff\n\t" \
553 "nop\n\t" \
554 : \
555 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
556 "r" ((uint8_t)__BOOT_LOCK_BITS_SET), \
557 "r" (value) \
558 : "r0", "r30", "r31" \
559 ); \
560 }))
561
562 /*
563 Reading lock and fuse bits:
564
565 Similarly to writing the lock bits above, set BLBSET and SPMEN (or
566 SELFPRGEN) bits in __SPMREG, and then (within four clock cycles) issue an
567 LPM instruction.
568
569 Z address: contents:
570 0x0000 low fuse bits
571 0x0001 lock bits
572 0x0002 extended fuse bits
573 0x0003 high fuse bits
574
575 Sounds confusing, doesn't it?
576
577 Unlike the macros in pgmspace.h, no need to care for non-enhanced
578 cores here as these old cores do not provide SPM support anyway.
579 */
580
581 /** \ingroup avr_boot
582 \def GET_LOW_FUSE_BITS
583 address to read the low fuse bits, using boot_lock_fuse_bits_get
584 */
585 #define GET_LOW_FUSE_BITS (0x0000)
586 /** \ingroup avr_boot
587 \def GET_LOCK_BITS
588 address to read the lock bits, using boot_lock_fuse_bits_get
589 */
590 #define GET_LOCK_BITS (0x0001)
591 /** \ingroup avr_boot
592 \def GET_EXTENDED_FUSE_BITS
593 address to read the extended fuse bits, using boot_lock_fuse_bits_get
594 */
595 #define GET_EXTENDED_FUSE_BITS (0x0002)
596 /** \ingroup avr_boot
597 \def GET_HIGH_FUSE_BITS
598 address to read the high fuse bits, using boot_lock_fuse_bits_get
599 */
600 #define GET_HIGH_FUSE_BITS (0x0003)
601
602 /** \ingroup avr_boot
603 \def boot_lock_fuse_bits_get(address)
604
605 Read the lock or fuse bits at \c address.
606
607 Parameter \c address can be any of GET_LOW_FUSE_BITS,
608 GET_LOCK_BITS, GET_EXTENDED_FUSE_BITS, or GET_HIGH_FUSE_BITS.
609
610 \note The lock and fuse bits returned are the physical values,
611 i.e. a bit returned as 0 means the corresponding fuse or lock bit
612 is programmed.
613 */
614 #define boot_lock_fuse_bits_get_short(address) \
615 (__extension__({ \
616 uint8_t __result; \
617 __asm__ __volatile__ \
618 ( \
619 "ldi r30, %3\n\t" \
620 "ldi r31, 0\n\t" \
621 "out %1, %2\n\t" \
622 "lpm %0, Z\n\t" \
623 : "=r" (__result) \
624 : "i" (_SFR_IO_ADDR(__SPM_REG)), \
625 "r" ((uint8_t)__BOOT_LOCK_BITS_SET), \
626 "M" (address) \
627 : "r0", "r30", "r31" \
628 ); \
629 __result; \
630 }))
631
632 #define boot_lock_fuse_bits_get(address) \
633 (__extension__({ \
634 uint8_t __result; \
635 __asm__ __volatile__ \
636 ( \
637 "ldi r30, %3\n\t" \
638 "ldi r31, 0\n\t" \
639 "sts %1, %2\n\t" \
640 "lpm %0, Z\n\t" \
641 : "=r" (__result) \
642 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
643 "r" ((uint8_t)__BOOT_LOCK_BITS_SET), \
644 "M" (address) \
645 : "r0", "r30", "r31" \
646 ); \
647 __result; \
648 }))
649
650 /** \ingroup avr_boot
651 \def boot_signature_byte_get(address)
652
653 Read the Signature Row byte at \c address. For some MCU types,
654 this function can also retrieve the factory-stored oscillator
655 calibration bytes.
656
657 Parameter \c address can be 0-0x1f as documented by the datasheet.
658 \note The values are MCU type dependent.
659 */
660
661 #define __BOOT_SIGROW_READ (_BV(__SPM_ENABLE) | _BV(SIGRD))
662
663 #define boot_signature_byte_get_short(addr) \
664 (__extension__({ \
665 uint16_t __addr16 = (uint16_t)(addr); \
666 uint8_t __result; \
667 __asm__ __volatile__ \
668 ( \
669 "out %1, %2\n\t" \
670 "lpm %0, Z" "\n\t" \
671 : "=r" (__result) \
672 : "i" (_SFR_IO_ADDR(__SPM_REG)), \
673 "r" ((uint8_t) __BOOT_SIGROW_READ), \
674 "z" (__addr16) \
675 ); \
676 __result; \
677 }))
678
679 #define boot_signature_byte_get(addr) \
680 (__extension__({ \
681 uint16_t __addr16 = (uint16_t)(addr); \
682 uint8_t __result; \
683 __asm__ __volatile__ \
684 ( \
685 "sts %1, %2\n\t" \
686 "lpm %0, Z" "\n\t" \
687 : "=r" (__result) \
688 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \
689 "r" ((uint8_t) __BOOT_SIGROW_READ), \
690 "z" (__addr16) \
691 ); \
692 __result; \
693 }))
694
695 /** \ingroup avr_boot
696 \def boot_page_fill(address, data)
697
698 Fill the bootloader temporary page buffer for flash
699 address with data word.
700
701 \note The address is a byte address. The data is a word. The AVR
702 writes data to the buffer a word at a time, but addresses the buffer
703 per byte! So, increment your address by 2 between calls, and send 2
704 data bytes in a word format! The LSB of the data is written to the lower
705 address; the MSB of the data is written to the higher address.*/
706
707 /** \ingroup avr_boot
708 \def boot_page_erase(address)
709
710 Erase the flash page that contains address.
711
712 \note address is a byte address in flash, not a word address. */
713
714 /** \ingroup avr_boot
715 \def boot_page_write(address)
716
717 Write the bootloader temporary page buffer
718 to flash page that contains address.
719
720 \note address is a byte address in flash, not a word address. */
721
722 /** \ingroup avr_boot
723 \def boot_rww_enable()
724
725 Enable the Read-While-Write memory section. */
726
727 /** \ingroup avr_boot
728 \def boot_lock_bits_set(lock_bits)
729
730 Set the bootloader lock bits.
731
732 \param lock_bits A mask of which Boot Loader Lock Bits to set.
733
734 \note In this context, a 'set bit' will be written to a zero value.
735 Note also that only BLBxx bits can be programmed by this command.
736
737 For example, to disallow the SPM instruction from writing to the Boot
738 Loader memory section of flash, you would use this macro as such:
739
740 \code
741 boot_lock_bits_set (_BV (BLB11));
742 \endcode
743
744 \note Like any lock bits, the Boot Loader Lock Bits, once set,
745 cannot be cleared again except by a chip erase which will in turn
746 also erase the boot loader itself. */
747
748 /* Normal versions of the macros use 16-bit addresses.
749 Extended versions of the macros use 32-bit addresses.
750 Alternate versions of the macros use 16-bit addresses and require special
751 instruction sequences after LPM.
752
753 FLASHEND is defined in the ioXXXX.h file.
754 USHRT_MAX is defined in <limits.h>. */
755
756 #if defined(__AVR_ATmega161__) || defined(__AVR_ATmega163__) \
757 || defined(__AVR_ATmega323__)
758
759 /* Alternate: ATmega161/163/323 and 16 bit address */
760 #define boot_page_fill(address, data) __boot_page_fill_alternate(address, data)
761 #define boot_page_erase(address) __boot_page_erase_alternate(address)
762 #define boot_page_write(address) __boot_page_write_alternate(address)
763 #define boot_rww_enable() __boot_rww_enable_alternate()
764 #define boot_lock_bits_set(lock_bits) __boot_lock_bits_set_alternate(lock_bits)
765
766 #elif (FLASHEND > USHRT_MAX)
767
768 /* Extended: >16 bit address */
769 #define boot_page_fill(address, data) __boot_page_fill_extended_short(address, data)
770 #define boot_page_erase(address) __boot_page_erase_extended_short(address)
771 #define boot_page_write(address) __boot_page_write_extended_short(address)
772 #define boot_rww_enable() __boot_rww_enable_short()
773 #define boot_lock_bits_set(lock_bits) __boot_lock_bits_set_short(lock_bits)
774
775 #else
776
777 /* Normal: 16 bit address */
778 #define boot_page_fill(address, data) __boot_page_fill_short(address, data)
779 #define boot_page_erase(address) __boot_page_erase_short(address)
780 #define boot_page_write(address) __boot_page_write_short(address)
781 #define boot_rww_enable() __boot_rww_enable_short()
782 #define boot_lock_bits_set(lock_bits) __boot_lock_bits_set_short(lock_bits)
783
784 #endif
785
786 /** \ingroup avr_boot
787
788 Same as boot_page_fill() except it waits for eeprom and spm operations to
789 complete before filling the page. */
790
791 #define boot_page_fill_safe(address, data) \
792 do { \
793 boot_spm_busy_wait(); \
794 eeprom_busy_wait(); \
795 boot_page_fill(address, data); \
796 } while (0)
797
798 /** \ingroup avr_boot
799
800 Same as boot_page_erase() except it waits for eeprom and spm operations to
801 complete before erasing the page. */
802
803 #define boot_page_erase_safe(address) \
804 do { \
805 boot_spm_busy_wait(); \
806 eeprom_busy_wait(); \
807 boot_page_erase (address); \
808 } while (0)
809
810 /** \ingroup avr_boot
811
812 Same as boot_page_write() except it waits for eeprom and spm operations to
813 complete before writing the page. */
814
815 #define boot_page_write_safe(address) \
816 do { \
817 boot_spm_busy_wait(); \
818 eeprom_busy_wait(); \
819 boot_page_write (address); \
820 } while (0)
821
822 /** \ingroup avr_boot
823
824 Same as boot_rww_enable() except waits for eeprom and spm operations to
825 complete before enabling the RWW mameory. */
826
827 #define boot_rww_enable_safe() \
828 do { \
829 boot_spm_busy_wait(); \
830 eeprom_busy_wait(); \
831 boot_rww_enable(); \
832 } while (0)
833
834 /** \ingroup avr_boot
835
836 Same as boot_lock_bits_set() except waits for eeprom and spm operations to
837 complete before setting the lock bits. */
838
839 #define boot_lock_bits_set_safe(lock_bits) \
840 do { \
841 boot_spm_busy_wait(); \
842 eeprom_busy_wait(); \
843 boot_lock_bits_set (lock_bits); \
844 } while (0)
845
846 #endif /* _AVR_BOOT_H_ */