Ning Kailiang Electronics Blog 简体中文
STM32 flash erase page

STM32 flash erase page:What is a flash page in STM32 microcontrollers and why must erasing be done page by page?

Author:Ning Kailiang Electronics Blog · Date:20261017

This page answers the following questions about“STM32 flash erase page”:What is a flash page in STM32 microcontrollers and why must erasing be done page by page?How do you erase a flash page on STM32 using the HAL library?What are common causes of flash erase failures on STM32 and how can they be fixed?How does flash page erase affect STM32 performance and endurance, and what best practices should be followed?What is the difference between page erase and mass erase on STM32, and when should each be used?

Q: What is a flash page in STM32 microcontrollers and why must erasing be done page by page?

A: In STM32 microcontrollers, the internal flash memory is organized into sectors or pages, which are the smallest erasable units. Unlike RAM, flash memory cannot be overwritten bit by bit; erasing sets bits to 1, and programming can only change 1s to 0s. Therefore, to modify data you must first erase the entire page containing it. Page sizes vary by series (e.g., 1 KB, 2 KB, or larger sectors on F4/F7 devices). Erasing a page sets all its bytes to 0xFF, after which you can program new data. This erase-before-write constraint is fundamental to flash operation.

Q: How do you erase a flash page on STM32 using the HAL library?

A: With STM32 HAL, you typically call HAL_FLASH_Unlock(), configure a FLASH_EraseInitTypeDef structure specifying the page or sector number, the number of pages, and the erase type, then call HAL_FLASHEx_Erase(). For example, set EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES, Page = your_page, NbPages = 1 on F1/L4 series, or use FLASH_TYPEERASE_SECTORS with Sector and NbSectors on F4/F7. The function returns an error code and fills a PageError variable if a page fails. Finally, call HAL_FLASH_Lock() to protect the flash. Always ensure interrupts and code execution are not running from the page being erased.

Q: What are common causes of flash erase failures on STM32 and how can they be fixed?

A: Common erase failures include: attempting to erase a write-protected page (check option bytes for write protection), insufficient voltage (flash operations require adequate VDD), executing code from the page being erased (causes bus faults), forgetting to unlock the flash with FLASH_Unlock(), or not clearing status flags like EOP, PGERR, or WRPERR before starting. Also, some STM32 parts require clearing error flags between operations. Fixes involve verifying option byte protection, ensuring power supply stability, relocating code away from the target page, unlocking flash properly, and clearing pending flags before and after each erase.

Q: How does flash page erase affect STM32 performance and endurance, and what best practices should be followed?

A: Flash erase operations are relatively slow (tens of milliseconds) and stall the CPU when accessing flash during erase, so avoid frequent erases in time-critical code. Each page has a limited endurance (typically 10,000 to 100,000 erase cycles per datasheet). Best practices include: grouping writes to minimize erase frequency, using wear-leveling schemes that rotate data across multiple pages, caching frequently changed data in RAM and writing periodically, keeping a backup of critical data, and avoiding erasing on every power cycle. Also consider using EEPROM emulation libraries provided by ST for small, frequently updated data.

Q: What is the difference between page erase and mass erase on STM32, and when should each be used?

A: Page (or sector) erase clears only a specified region of flash, leaving other pages intact, which is useful for updating a configuration block, firmware patch area, or data log while preserving the rest of the program. Mass erase clears the entire flash memory, returning all pages to 0xFF. Mass erase is faster per byte and is commonly used when reprogramming the whole device, recovering from a corrupted application, or resetting to factory state via ST-Link or bootloader. Use page erase for partial updates to reduce risk and preserve unaffected code or data; use mass erase during full firmware programming or production flashing.

STM32 flash erase page

Words of experience

Common scenarios of "STM32 flash erase page"

【Student】 Hi Professor, I have a question about STM32 flash memory. When we talk about erasing a page, what exactly does that mean?

【Professor】 Great question. In STM32 microcontrollers, flash memory is organized into sectors or pages. Erasing a page means resetting all bits in that page to 1, effectively clearing it so it can be reprogrammed.

【Student】 So after erasing, the page is all 0xFF?

【Professor】 Exactly. Erased flash bits read as 1, so a fully erased page typically reads as 0xFF for every byte. Programming can only change bits from 1 to 0, not the other way around.

【Student】 That means to change a 0 back to 1, we must erase the whole page?

【Professor】 Correct. That's why flash is erase-before-write. You can't arbitrarily flip bits; you erase an entire page or sector, then write the new data.

【Student】 How large is a page on a typical STM32?

【Professor】 It varies by series. For example, STM32F1 has 1 KB pages, while STM32F4 has sectors of varying sizes, from 16 KB up to 128 KB. Always check the reference manual for your specific part.

【Student】 Why do some series use sectors instead of uniform pages?

【Professor】 Larger sectors reduce die size and cost, but they make small updates inefficient. If you only need to change a few bytes, erasing a 128 KB sector is expensive.

【Student】 So there's a trade-off between granularity and cost?

【Professor】 Exactly. Smaller pages give finer erase granularity but more overhead. Designers often use wear-leveling or a file system like LittleFS to manage this.

【Student】 What happens if I try to program a page without erasing it first?

【Professor】 Programming can only clear bits. If the page isn't erased, you may write incorrect data because existing 0 bits can't become 1. Some STM32 also set an error flag if you try to write to a non-erased location.

【Student】 Is there a way to erase just a few bytes?

【Professor】 No, erasure is always at the page or sector level. To update a small amount of data, you typically read the page into RAM, modify it, erase the page, then write it back.

【Student】 That sounds risky if power fails during the process.

【Professor】 Very true. That's why robust designs use a backup copy, a journaling scheme, or dual-bank flash so a power loss doesn't corrupt data.

【Student】 What about endurance? How many erase cycles can a page handle?

【Professor】 Typically around 10,000 erase cycles for STM32 flash, though it varies. Wear-leveling helps distribute erases across pages to extend lifetime.

【Student】 Can I erase a page while executing code from flash?

【Professor】 Not from the same bank you're executing from. On dual-bank devices, you can execute from one bank while erasing the other. Otherwise, you must run the erase routine from RAM.

【Student】 Are there HAL functions for page erase?

【Professor】 Yes. STM32 HAL provides functions like HAL_FLASHEx_Erase with a page or sector structure. You unlock flash, configure the erase, wait for completion, then lock it again.

【Student】 What about interrupts during erase? The CPU stalls, right?

【Professor】 Correct. Flash erase and program operations stall the bus, so interrupts may be delayed. Keep critical ISRs short or disable interrupts during the operation if timing matters.

【Student】 Thanks, that clears up a lot. Any final advice?

【Professor】 Always consult your specific reference manual for page sizes and erase sequences, and design for power-loss safety and wear-leveling from the start. Flash is convenient but has strict rules.

This article was published byNing Kailiang Electronics Blog, For more knowledge about“stm32” please followNing Kailiang Electronics Blog。