EraseFLASHPage

Purpose: Erase the FLASH (or program) memory "page" which contains the address specified. "Erase" here means to reset all BITs withing a limited range to logic "1". Any one of 128 addresses selects the same FLASH page in the 328P, so the low 7 bits are ignored, in effect. In the 1284P and 2560, FLASH pages have 256 BYTEs instead of 128, so the low 8 bits have no influence on the FLASH page selection.
Assumes: The caller is in either Manager mode or Administrator mode and desires to erase the entire 128-BYTE (in the 328P) or 256-BYTE (in the 1284P and 2560 MPUs) block of memory. See the table in the notes below for more detailed range information.
The Address 0 (or 0x0000, or 0x00000) is not accepted since it is often a numeric input issue and there are other ways to intentionally erase that same 128-BYTE or 256-BYTE block.
Passed: R25:R24  WORD   wAddress address of the FLASH memory page to erase; 0x0001 to 0x6FFF for 328P.
Use R26:R25:R24 only in the 1284 (0x00001 to 0x1DFFF) or 2560 (0x00001 to 0x3DFFF), even for addresses below 0x10000.
Returns:   R25:R24 WORD wAddrWORD   wAddress/2 of the page if it was erased OR
0xFFFF if it was not erased
Alters: R18, R19, R23, R30, R31, and the 128-byte FLASH memory page selected
Example: 
EraseFlashRange:
   MOVW    R26, R24            ; Transfer the starting address to R27:R26
   MOVW    R20, R22            ; Transfer the ending address to R20:R21
   ANDI    R26, 0x7F           ; Point to the first byte in the address range
   ANDI    R20, 0x7F           ;  of the FLASH memory page to be reset

EFRange_Loop:
   CP      R20, R26            ; Verify that the ending address is not above
   CPC     R21, R27            ;  the starting address
   BRLO    AEFRange_Exit       ; If so, this exits even before the first loop
   MOVW    R24, R26            ; Set the address of the range to be reset
   CALL    ADumpRegs_Begin     ; Display each starting addres before erasing
   CALL    AEraseFLASHPage     ; Call the system routine to do the erasure
   SUBI    R26, -128           ; Add 128 to the value in R27:R26 (but since
   SBCI    R27, -1             ;  there is no ADDI, subtract -128 instead)
   RJMP    AEFRange_Loop       ; Unconditional loop back; check at the top

EFRange_Exit:
   RET
Notes: The ATmega 328P FLASH pages have ranges of 0x##00 to 0x##7F or 0x##80 to 0x##FF. Those in the 1284P and 2560 have ranges of 0x###00 to 0x###FF.
This table summarizes the memory ranges in MIRTOS Version 3.01.
MCUFLASH
Pages
BYTES
per Page
Available for
User Code
Erase in
Manager Mode
Erase in
Admin Mode
Prevented from
being displayed
Protected
from Erasure
328P 256128 = 0x80 0x0100 to 0x38FF 0x0000 to 0x38FF 0x0000 to 0x6FFF 0x4000 to 0x7FFF 0x7000 to 0x7FFF
1284P 512256 = 0x100 0x00100 to 0x1A2FF 0x00000 to 0x1A2FF 0x00000 to 0x1DFFF 0x1A400 to 0x1FFFF 0x1E000 to 0x1FFFF
2560 1024 256 = 0x100   0x00100 to 0x3A0FF   0x00000 to 0x3A0FF   0x00000 to 0x3DFFF   0x3A200 to 0x3FFFF   0x3E000 to 0x3FFFF 
Be VERY careful if erasing memory in the range only available to be erased in Administrative mode!
Source code is available for all factory logic and data provided in the range listed "Available for User Code" column above. In the protected but visible Admin section, there is a Jump Table for user ISRs (Interrupt Service Routines) for unused hardware interrupts as well as a pointer to a second Jump Table (for indices 0x80 to 0xFF), if it is desired to implement one. There is also a Jump Table for all COM2, COM3, or COM4 service routines when two or four USARTs are supported by the MCU.
It is not necessary to erase a FLASH page before changing an area of FLASH using the iHEX command. The iHEX processing logic handles the erase-before-write logic. In fact, by default, it reads and saves the entire block to SRAM then inserts the BYTEs in the iHEX command into the block then erases and rewrites the entire block upon exit.
Dropin: 
(setup
code)
;   Setup for the EraseFLASHPage() call:
; Passed:  R25:R24 WORD  wAddress (legal values: 0x0000 to 0x6FFF for 328P)
;          R26:R25:R24 BYTE * bPtrAddress (only for the 1284 or 2560)
; Returns: R25:R24 WORD  wAddress/2 of the page erased of 0xFFFF if none
; Alters:  R18, R19, R23, R30, R31, and the single FLASH memory page selected
Also see:  -