+ if ((BlockNumber >= 4) && (BlockNumber < (4 + (FIRMWARE_FILE_SIZE / SECTOR_SIZE_BYTES))))
+ {
+ uint32_t WriteFlashAddress = (uint32_t)(BlockNumber - 4) * SECTOR_SIZE_BYTES;
+
+ for (uint16_t i = 0; i < SECTOR_SIZE_BYTES; i += 2)
+ {
+ /* Disallow writing to the bootloader section */
+ if (WriteFlashAddress > BOOT_START_ADDR)
+ continue;
+
+ if ((WriteFlashAddress % SPM_PAGESIZE) == 0)
+ {
+ /* Erase the given FLASH page, ready to be programmed */
+ boot_page_erase(WriteFlashAddress);
+ boot_spm_busy_wait();
+ }
+
+ /* Write the next data word to the FLASH page */
+ boot_page_fill(WriteFlashAddress, (BlockBuffer[i + 1] << 8) | BlockBuffer[i]);
+ WriteFlashAddress += 2;
+
+ if ((WriteFlashAddress % SPM_PAGESIZE) == 0)
+ {
+ /* Write the filled FLASH page to memory */
+ boot_page_write(WriteFlashAddress - SPM_PAGESIZE);
+ boot_spm_busy_wait();
+ }
+ }
+ }