22.4.3.3 Erase Commands

Erase commands are allowed only on unlocked regions. Depending on the Flash memory, several commands can be used to erase the Flash:

  • Erase All Memory (EA): All memory is erased. The processor must not fetch code from the Flash memory.
  • Erase Pages (EPA): 4, 8, 16, or 32 pages are erased in the Flash sector selected. The first page to be erased is specified in the FARG[15:2] field of the EEFC_FCR. The first page number must be a multiple of 8, 16, or 32 depending on the number of pages to erase simultaneously.
  • Erase Sector (ES): A full memory sector is erased. Sector size depends on the Flash memory. EEFC_FCR.FARG must be set with a page number that is in the sector to be erased.

Note: If one sub-sector is locked within the first sector, the Erase Sector (ES) command cannot be processed on non-locked sub-sectors of the first sector. All the lock bits of the first sector must be cleared prior to issuing an ES command on the first sector. After the ES command has been issued, the first sector lock bits must be reverted to the state before clearing them.

If the processor is fetching code from the Flash memory while the EPA or ES command is being executed, the processor accesses are stalled until the EPA command is completed. To avoid stalling the processor, the code can be run out of internal SRAM.

The following are the erase sequence:

  1. Erase starts immediately one of the erase commands and the FARG field are written in EEFC_FCR.

    For the EPA command, the two lowest bits of the FARG field define the number of pages to be erased (FARG[1:0]), see table below.

    Table 22-3. EEFC_FCR.FARG Field for EPA Command
    FARG[1:0] Number of pages to be erased with EPA command
    0 4 pages (only valid for small 8-KB sectors)
    1 8 pages (only valid for small 8-KB sectors)
    2 16 pages
    3 32 pages (not valid for small 8-KB sectors)
  2. When erasing is completed, the EEFC_FSR.FRDY bit rises. If an interrupt has been enabled by setting the EEFC_FMR.FRDY bit, the interrupt line of the interrupt controller is activated.

Three errors can be detected in EEFC_FSR after an erasing sequence:

  • Command Error: A bad keyword has been written in EEFC_FCR.
  • Lock Error: At least one page to be erased belongs to a locked region. The erase command has been refused, no page has been erased. A command must be run previously to unlock the corresponding region.
  • Flash Error: At the end of the erase period, the EraseVerify test of the Flash memory has failed. After a first programming pulse, a Verify is applied. The memory is read, to compare both programmed and expected values. If that comparison fails, a second programming pulse is applied, and so on, until a maximum pulse number is reached. At this time, if the memory is still not containing what is expected, the FLERR flag is set high.