diff --git a/stk500v2/main.cpp b/stk500v2/main.cpp index 73cf410..1a2e870 100644 --- a/stk500v2/main.cpp +++ b/stk500v2/main.cpp @@ -332,6 +332,27 @@ void writeEepromByte(uint8_t *addr, uint8_t value) } // namespace +static inline uint16_t getBootloaderSize() +{ + const auto highFuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS); + constexpr auto BOOTSZ0 = 1; + constexpr auto BOOTSZ1 = 2; + + if (highFuse & (1 << BOOTSZ1) && highFuse & (1 << BOOTSZ0)) + return 256 * 2; + else if (highFuse & (1 << BOOTSZ1)) + return 512 * 2; + else if (highFuse & (1 << BOOTSZ0)) + return 1024 * 2; + return 2048 * 2; +} + +static inline uint32_t getFlashSize() +{ + const auto bootloaderSize = getBootloaderSize(); + return (FLASHEND - bootloaderSize + 1); +} + static inline void performChipErase() { constexpr auto getEepromEraseFuseBit = []() -> bool { @@ -339,23 +360,8 @@ static inline void performChipErase() return boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS) & (1 << EESAVE); }; - constexpr auto getBootloaderSize = []() -> uint16_t { - const auto highFuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS); - constexpr auto BOOTSZ0 = 1; - constexpr auto BOOTSZ1 = 2; - - if (highFuse & (1 << BOOTSZ1) && highFuse & (1 << BOOTSZ0)) - return 256 * 2; - else if (highFuse & (1 << BOOTSZ1)) - return 512 * 2; - else if (highFuse & (1 << BOOTSZ0)) - return 1024 * 2; - return 2048 * 2; - }; - - constexpr auto eraseFlash = [getBootloaderSize]() { - const auto bootloaderSize = getBootloaderSize(); - const auto flashSize = (FLASHEND - bootloaderSize + 1); + constexpr auto eraseFlash = []() { + const auto flashSize = getFlashSize(); for (uint16_t i = 0; i < flashSize; i += SPM_PAGESIZE) { boot_page_erase(i);