104 #define _AVR_BOOT_H_ 1 115 #include <inttypes.h> 120 # define __SPM_REG SPMCSR 121 #elif defined (SPMCR) 122 # define __SPM_REG SPMCR 124 # error AVR processor does not provide bootloader support! 130 # define __SPM_ENABLE SPMEN 131 #elif defined(SELFPRGEN) 132 # define __SPM_ENABLE SELFPRGEN 134 # error Cannot find SPM Enable bit definition! 145 #define BOOTLOADER_SECTION __attribute__ ((section (".bootloader"))) 149 #define __COMMON_ASB ASB 151 #define __COMMON_ASB RWWSB 155 #define __COMMON_ASRE ASRE 157 #define __COMMON_ASRE RWWSRE 171 #define boot_spm_interrupt_enable() (__SPM_REG |= (uint8_t)_BV(SPMIE)) 177 #define boot_spm_interrupt_disable() (__SPM_REG &= (uint8_t)~_BV(SPMIE)) 183 #define boot_is_spm_interrupt() (__SPM_REG & (uint8_t)_BV(SPMIE)) 189 #define boot_rww_busy() (__SPM_REG & (uint8_t)_BV(__COMMON_ASB)) 195 #define boot_spm_busy() (__SPM_REG & (uint8_t)_BV(__SPM_ENABLE)) 201 #define boot_spm_busy_wait() do{}while(boot_spm_busy()) 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)) 208 #define __BOOT_LOCK_BITS_SET (_BV(__SPM_ENABLE) | _BV(BLBSET)) 210 #define __BOOT_LOCK_BITS_SET (_BV(__SPM_ENABLE) | _BV(RFLB)) 213 #define __boot_page_fill_normal(address, data) \ 215 __asm__ __volatile__ \ 222 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \ 223 "r" ((uint8_t)(__BOOT_PAGE_FILL)), \ 224 "z" ((uint16_t)(address)), \ 225 "r" ((uint16_t)(data)) \ 230 #define __boot_page_fill_alternate(address, data)\ 232 __asm__ __volatile__ \ 241 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \ 242 "r" ((uint8_t)(__BOOT_PAGE_FILL)), \ 243 "z" ((uint16_t)(address)), \ 244 "r" ((uint16_t)(data)) \ 249 #define __boot_page_fill_extended(address, data) \ 251 __asm__ __volatile__ \ 254 "movw r30, %A3\n\t" \ 260 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \ 261 "i" (_SFR_MEM_ADDR(RAMPZ)), \ 262 "r" ((uint8_t)(__BOOT_PAGE_FILL)), \ 263 "r" ((uint32_t)(address)), \ 264 "r" ((uint16_t)(data)) \ 265 : "r0", "r30", "r31" \ 269 #define __boot_page_erase_normal(address) \ 271 __asm__ __volatile__ \ 276 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \ 277 "r" ((uint8_t)(__BOOT_PAGE_ERASE)), \ 278 "z" ((uint16_t)(address)) \ 282 #define __boot_page_erase_alternate(address) \ 284 __asm__ __volatile__ \ 291 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \ 292 "r" ((uint8_t)(__BOOT_PAGE_ERASE)), \ 293 "z" ((uint16_t)(address)) \ 297 #define __boot_page_erase_extended(address) \ 299 __asm__ __volatile__ \ 301 "movw r30, %A3\n\t" \ 306 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \ 307 "i" (_SFR_MEM_ADDR(RAMPZ)), \ 308 "r" ((uint8_t)(__BOOT_PAGE_ERASE)), \ 309 "r" ((uint32_t)(address)) \ 314 #define __boot_page_write_normal(address) \ 316 __asm__ __volatile__ \ 321 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \ 322 "r" ((uint8_t)(__BOOT_PAGE_WRITE)), \ 323 "z" ((uint16_t)(address)) \ 327 #define __boot_page_write_alternate(address) \ 329 __asm__ __volatile__ \ 336 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \ 337 "r" ((uint8_t)(__BOOT_PAGE_WRITE)), \ 338 "z" ((uint16_t)(address)) \ 342 #define __boot_page_write_extended(address) \ 344 __asm__ __volatile__ \ 346 "movw r30, %A3\n\t" \ 351 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \ 352 "i" (_SFR_MEM_ADDR(RAMPZ)), \ 353 "r" ((uint8_t)(__BOOT_PAGE_WRITE)), \ 354 "r" ((uint32_t)(address)) \ 359 #define __boot_rww_enable() \ 361 __asm__ __volatile__ \ 366 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \ 367 "r" ((uint8_t)(__BOOT_RWW_ENABLE)) \ 371 #define __boot_rww_enable_alternate() \ 373 __asm__ __volatile__ \ 380 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \ 381 "r" ((uint8_t)(__BOOT_RWW_ENABLE)) \ 402 #define __boot_lock_bits_set(lock_bits) \ 404 uint8_t value = (uint8_t)(~(lock_bits)); \ 405 __asm__ __volatile__ \ 413 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \ 414 "r" ((uint8_t)(__BOOT_LOCK_BITS_SET)), \ 416 : "r0", "r30", "r31" \ 420 #define __boot_lock_bits_set_alternate(lock_bits) \ 422 uint8_t value = (uint8_t)(~(lock_bits)); \ 423 __asm__ __volatile__ \ 433 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \ 434 "r" ((uint8_t)(__BOOT_LOCK_BITS_SET)), \ 436 : "r0", "r30", "r31" \ 463 #define GET_LOW_FUSE_BITS (0x0000) 468 #define GET_LOCK_BITS (0x0001) 473 #define GET_EXTENDED_FUSE_BITS (0x0002) 478 #define GET_HIGH_FUSE_BITS (0x0003) 492 #define boot_lock_fuse_bits_get(address) \ 495 __asm__ __volatile__ \ 500 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \ 501 "r" ((uint8_t)(__BOOT_LOCK_BITS_SET)), \ 502 "z" ((uint16_t)(address)) \ 518 #define __BOOT_SIGROW_READ (_BV(__SPM_ENABLE) | _BV(SIGRD)) 520 #define boot_signature_byte_get(addr) \ 523 __asm__ __volatile__ \ 528 : "i" (_SFR_MEM_ADDR(__SPM_REG)), \ 529 "r" ((uint8_t)(__BOOT_SIGROW_READ)), \ 530 "z" ((uint16_t)(addr)) \ 596 #if defined(__AVR_ATmega161__) || defined(__AVR_ATmega163__) \ 597 || defined(__AVR_ATmega323__) 600 #define boot_page_fill(address, data) __boot_page_fill_alternate(address, data) 601 #define boot_page_erase(address) __boot_page_erase_alternate(address) 602 #define boot_page_write(address) __boot_page_write_alternate(address) 603 #define boot_rww_enable() __boot_rww_enable_alternate() 604 #define boot_lock_bits_set(lock_bits) __boot_lock_bits_set_alternate(lock_bits) 606 #elif (FLASHEND > USHRT_MAX) 609 #define boot_page_fill(address, data) __boot_page_fill_extended(address, data) 610 #define boot_page_erase(address) __boot_page_erase_extended(address) 611 #define boot_page_write(address) __boot_page_write_extended(address) 612 #define boot_rww_enable() __boot_rww_enable() 613 #define boot_lock_bits_set(lock_bits) __boot_lock_bits_set(lock_bits) 618 #define boot_page_fill(address, data) __boot_page_fill_normal(address, data) 619 #define boot_page_erase(address) __boot_page_erase_normal(address) 620 #define boot_page_write(address) __boot_page_write_normal(address) 621 #define boot_rww_enable() __boot_rww_enable() 622 #define boot_lock_bits_set(lock_bits) __boot_lock_bits_set(lock_bits) 631 #define boot_page_fill_safe(address, data) \ 633 boot_spm_busy_wait(); \ 634 eeprom_busy_wait(); \ 635 boot_page_fill(address, data); \ 643 #define boot_page_erase_safe(address) \ 645 boot_spm_busy_wait(); \ 646 eeprom_busy_wait(); \ 647 boot_page_erase (address); \ 655 #define boot_page_write_safe(address) \ 657 boot_spm_busy_wait(); \ 658 eeprom_busy_wait(); \ 659 boot_page_write (address); \ 667 #define boot_rww_enable_safe() \ 669 boot_spm_busy_wait(); \ 670 eeprom_busy_wait(); \ 679 #define boot_lock_bits_set_safe(lock_bits) \ 681 boot_spm_busy_wait(); \ 682 eeprom_busy_wait(); \ 683 boot_lock_bits_set (lock_bits); \ Data EEPROM Contained in the AVR Microcontrollers.
AVR device-specific IO Definitions.