Understanding STM32 Flash: Everything You Need to Know

Flash memory in STM32 microcontrollers is the reason your code runs after every reset. But for many developers, the process of programming STM32 flash is still confusing. You’ve probably seen terms like HAL_FLASH_Program() or bootloader, but what do they really mean? And how do you flash STM32 the right way?
If you’ve ever been stuck on firmware updates, flash errors, or permanent data storage, this guide is for you. It breaks down STM32 flash step-by-step — in simple language — so you can write, erase, and control flash like a pro. Whether you're using STM32CubeIDE or coding bare-metal, you'll find everything you need to get it working safely and cleanly.
What Is STM32 Flash?
STM32 flash is the non-volatile memory built into STM32 chips. It keeps your firmware and saved data even when the power is turned off. It’s similar to a tiny SSD baked into your microcontroller.
Flash memory is different from RAM:
| Feature | Flash | RAM |
| --------------------------------- | ----- | --- |
| Keeps data after power off | ✅ | ❌ |
| Used for program code | ✅ | ❌ |
| Used for variables during runtime | ❌ | ✅ |
Where is STM32 flash used?
  Stores your compiled firmware
  Holds configuration data
  Used in firmware over-the-air (FOTA) updates
  Bootloader lives in flash
The main flash memory starts at address 0x08000000 and varies in size depending on your chip. Most STM32 boards use this as the default firmware start point.
STM32 Flash Memory Layout
STM32 flash is divided into pages or sectors. Each sector must be erased before it can be written again. The size and structure depend on the chip family.
Examples:
  STM32F1: 1 KB or 2 KB pages
  STM32F4: 16 KB, 64 KB, or 128 KB sectors
  STM32G4: Uniform 2 KB pages
Every time you write to flash, it goes from a 1 to 0. If you want to go back to 1, you need to erase the sector.
Important addresses:
  Flash base: 0x08000000
  End of flash: Varies (check datasheet)
  Option bytes (for protection settings): Typically after main flash
How to Flash an STM32 Microcontroller
You can flash STM32 microcontrollers using several tools. The most common method is via ST-Link using STM32CubeProgrammer.
Method 1: Using STM32CubeProgrammer
  Connect ST-Link to your STM32 board
  Open STM32CubeProgrammer
  Select ST-Link, then click "Connect"
  Choose your .hex or .bin firmware file
  Set address to 0x08000000
  Click “Start Programming”
After this, press the reset button and your program should run.
Method 2: Flash via USB (DFU Mode)
Some boards support USB Device Firmware Update (DFU):
  Hold BOOT0 while resetting
  Connect USB to PC
  Use STM32CubeProgrammer in USB mode
This is great for flashing without ST-Link.
Method 3: Flash via USART
  Set BOOT0 pin high
  Use serial tools like Flash Loader Demo or STM32CubeProgrammer
  Flash via UART (usually PA9/PA10)
This method works well for low-cost boards with no USB or debugger.
STM32 Flash Programming in Code
If you want to store settings or log data in flash during runtime, you’ll need to write to flash memory from code.
Here’s how:
Step-by-Step Flash Write (Using HAL)
HAL_FLASH_Unlock(); // Step 1: Unlock flash
uint32_t address = 0x0801F000; // Example location
uint32_t data = 0x12345678; // Data to write
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data); // Step 2: Write
HAL_FLASH_Lock(); // Step 3: Lock flash again
When to Use STM32 Flash for Data Storage
STM32 flash can store things like:
  Last known settings
  Calibration data
  User preferences
  Error logs
You must manage wear manually. Flash has a limited number of erase/write cycles (typically ~10,000 per sector). Write too often, and that part of memory can wear out.
How to Erase Flash on STM32
Before you can write new data, the flash page must be erased.
Here’s how to erase one page:
FLASH_Erase_Struct eraseConfig;
eraseConfig.TypeErase = FLASH_TYPEERASE_PAGES;
eraseConfig.PageAddress = 0x0801F000;
eraseConfig.NbPages = 1;
HAL_FLASH_Unlock();
HAL_FLASHEx_Erase(&eraseConfig, &pageError);
HAL_FLASH_Lock();
Make sure not to erase the sectors where your firmware lives. You could brick the board.
HAL_FLASH_Program() Explained Simply
HAL_FLASH_Program() is the function used to write values to flash memory.
HAL_FLASH_Program(Type, Address, Data);
Common Type Options:
  FLASH_TYPEPROGRAM_WORD: 32-bit word
  FLASH_TYPEPROGRAM_HALFWORD: 16-bit
  FLASH_TYPEPROGRAM_DOUBLEWORD: 64-bit (some chips only)
Rules:
  Address must be aligned
  Flash must be unlocked
  You can’t write without erase
  Only go from 1s to 0s — not the other way around
Internal vs External Flash for STM32 Projects
STM32 microcontrollers have internal flash built into the chip. But in some cases, developers use external flash chips for extra storage. Here’s how they compare:
Internal Flash
  Already built into the MCU
  Fast and directly accessible
  Used for firmware and small data
  Simple setup with HAL or direct registers
  Size limited by chip (often 64 KB to 2 MB)
External Flash (like SPI Flash or QSPI NOR Flash)
  Much larger storage (up to 128 MB or more)
  Requires SPI, QSPI, or parallel interface
  Slightly slower read/write speeds
  Best for graphics, logs, files, and backups
  Needs a flash driver (external libraries or middleware)
When to use internal flash: For firmware and small configuration data.
When to use external flash: For storing large files, audio, images, or firmware backups.
Common STM32 Flash Errors and How to Fix Them
Flashing doesn’t always go as planned. Here are some common problems developers face — and how to fix them:
1. Flash Write Has No Effect
Cause: Flash is still locked.
Fix: Use HAL_FLASH_Unlock() before writing.
2. Flash Write Fails or Crashes
Cause: Writing to wrong address or unaligned data.
Fix: Double-check the address and use 32-bit aligned values.
3. Erase Fails or Corrupts
Cause: Sector is write-protected.
Fix: Check and clear write protection using STM32CubeProgrammer.
4. Flash Not Retaining Data
Cause: Writing to RAM instead of flash.
Fix: Verify memory map — flash starts at 0x08000000.
5. Flashing Tool Doesn’t Detect Device
Cause: Incorrect BOOT pin configuration.
Fix: Make sure BOOT0 and BOOT1 are set correctly.
Always refer to your chip’s datasheet to confirm memory sizes and sector layouts before programming.
Flash Wear and How to Avoid It
STM32 flash memory can only be erased and re-written a limited number of times per sector — usually about 10,000 cycles. After that, the sector may become unreliable.
To make your flash last longer:
Tips to Reduce Flash Wear
  Avoid writing too often
  Only write when values change. Use flags or buffers in RAM to reduce unnecessary writes.
  Use wear leveling
  Spread out writes across several pages or sectors.
  Log less frequently
  Instead of saving every second, log once per minute or when data changes significantly.
  Use EEPROM emulation
  STM32 provides software libraries that simulate EEPROM using flash, with wear handling included.
If you’re storing logs, calibration data, or user settings, these methods can greatly increase reliability over time.
STM32 Flash Security and Write Protection
STM32 flash has security features to protect your firmware and prevent unwanted writes or reads.
Flash Write Protection
  You can lock specific pages to prevent overwriting
  Controlled through option bytes
  Set using STM32CubeProgrammer
Read Protection (RDP)
  Level 0: No protection
  Level 1: Debug disabled, no flash read allowed
  Level 2: Full lock, can’t be reversed (even by you)
How to Enable RDP Level 1:
  Open STM32CubeProgrammer
  Connect to your device
  Go to "Option Bytes"
  Set RDP to Level 1
  Click "Apply"
Use this if you’re shipping a product and want to prevent firmware theft.
Frequently Asked Questions (FAQ)
What address does STM32 flash start at?
Most STM32 flash starts at 0x08000000.
Can I store variables in STM32 flash memory?
Yes, as long as you manage erase and write operations manually.
What happens if I write to flash without erasing?
Bits can only go from 1 to 0. To reset to 1, you must erase the page.
Is it safe to write to flash during runtime?
Yes, but don’t do it from interrupt context or time-sensitive tasks.
How do I check flash size in STM32?
Look in the datasheet or use HAL_GetFlashSize() if supported.
Can I flash without ST-Link?
Yes, using DFU (USB) or USART bootloader if your board supports it.
How to protect firmware from being copied?
Enable RDP Level 1 or 2 in the option bytes.
What is the use of HAL_FLASH_Program()?
It’s used to write data to flash memory in STM32.
Why is my flash memory not saving data?
You may be writing to RAM or skipping unlock/erase steps.
Does STM32 flash wear out over time?
Yes, but it lasts up to 10,000 erase cycles per sector if used properly.
Final Thoughts: Start Using STM32 Flash with Confidence
Now you understand how STM32 flash memory works — and how to use it safely in your projects. From flashing firmware to storing custom data with HAL_FLASH_Program(), it’s easier than you think once you follow the right steps.
Don’t worry if you make mistakes early on. Just work on test boards, keep your firmware backed up, and double-check your memory addresses. As you get more comfortable, writing to STM32 flash becomes second nature.
Want more STM32 tutorials?
📌 Visit ControllersTech and explore hands-on guides to master embedded systems step by step.
Note: IndiBlogHub features both user-submitted and editorial content. We do not verify third-party contributions. Read our Disclaimer and Privacy Policyfor details.