If you’re working with STM32 microcontrollers and don’t understand STM32 flash memory yet, you’re likely stuck on firmware updates, debugging, or even just saving settings across resets. You’re not alone—many developers hit a wall trying to figure out how to program flash or use functions like HAL_FLASH_Program()
safely. It can feel like you’re missing a basic piece of the puzzle.
Here’s the fix: this guide breaks down STM32 flash memory in simple terms, shows you how to use it properly, and walks you through common flashing methods step by step. No complex words. No fluff. Just clear, accurate information that works. Whether you’re flashing your first STM32 board or trying to understand how internal flash works, you’re in the right place.
What Is STM32 Flash Memory?
STM32 flash memory is the non-volatile memory built into STM32 microcontrollers. It stores your firmware so the code runs every time the board powers on. Unlike RAM, it doesn’t reset when power is lost.
You can think of it as permanent storage on your microcontroller. When you build a project using STM32CubeIDE or Keil, your compiled binary gets stored in flash. That’s what runs when you press the reset button.
Key points about STM32 flash memory:
- It holds the main program code (firmware).
- It is internal to the STM32 chip.
- It is electrically erasable and writable.
- You use it to store data that needs to survive power cycles.
STM32 flash memory comes in different sizes depending on the chip. For example, the STM32F103C8 has 64KB, while the STM32F429ZI can have 2MB or more.
Why STM32 Flash Matters in Your Projects
STM32 flash is more than just a place to store firmware. It also lets you:
- Update firmware without needing a debugger.
- Store configuration data permanently.
- Add bootloaders to allow USB or UART flashing.
- Save sensor calibration values that persist after resets.
If you’re building anything beyond simple blink codes, learning how STM32 flash works is a must. It gives you control over how your device behaves over time and across reboots.
Also, internal flash programming is needed if you’re updating firmware remotely (FOTA), building mass production firmware pipelines, or even just making your development workflow easier.
Flash Memory Basics: How It Works on STM32
STM32 flash is divided into sectors or pages. You can erase and write to these sectors using special functions from the STM32 HAL (Hardware Abstraction Layer) library.
Here’s what happens under the hood:
- Flash memory starts in a locked state.
- You must unlock it before writing using
HAL_FLASH_Unlock()
. - You erase a page or sector using
HAL_FLASH_Erase()
. - You write data using
HAL_FLASH_Program()
. - After writing, you lock it again using
HAL_FLASH_Lock()
.
Flash writes can only go from 1 to 0. To go back to 1s, you must erase the whole sector. This is why flash memory wears out slowly over many write cycles.
Common HAL functions:
HAL_FLASH_Program()
: Write to flashHAL_FLASH_Unlock()
: Enable writingHAL_FLASH_Lock()
: Disable writingHAL_FLASH_Erase()
: Erase memory
How to Flash STM32 Microcontrollers (Step-by-Step)
Flashing your STM32 means writing your compiled firmware (usually .hex
or .bin
) into the flash memory.
Here’s a simple step-by-step method using STM32CubeProgrammer and ST-Link:
- Connect your board using a USB ST-Link or ST-Link V2.
- Open STM32CubeProgrammer on your PC.
- Select ST-Link as your connection method and click “Connect.”
- Load your firmware file (.hex or .bin).
- Choose the target flash memory starting at address
0x08000000
. - Click “Start Programming.”
That’s it. After flashing, press reset, and your STM32 will boot from the new firmware.
Other flashing options:
- DFU Mode (USB Bootloader) – for boards with USB.
- USART Bootloader – using serial and boot pins.
- JTAG/SWD Flashing – for full debugger control.
All methods work with internal flash. They just change how the binary file gets into the chip.
STM32 Flash Programming Using HAL Libraries
To write data into STM32 flash manually in code (like saving settings or logs), you’ll use STM32 HAL functions. Here’s a sample code block:
HAL_FLASH_Unlock();
uint32_t address = 0x0801F000; // Example address
uint32_t data = 0x12345678;
HAL_FLASH_Program(TYPEPROGRAM_WORD, address, data);
HAL_FLASH_Lock();
Best practices:
- Only write to unused areas of flash.
- Avoid frequent writes to reduce wear.
- Always erase before writing.
- Protect sensitive sectors using option bytes.
This is handy when you need to store non-volatile settings without external EEPROM.
Great! Continuing with the next sections of the blog as promised.
STM32 Flash Memory Sizes and Types
Different STM32 microcontrollers have different amounts and layouts of flash memory. This depends on the series, model, and package size.
Here’s a quick comparison of common STM32 chips:
STM32 Model | Flash Size | Flash Start Address |
---|---|---|
STM32F030F4P6 | 16 KB | 0x08000000 |
STM32F103C8T6 | 64 KB | 0x08000000 |
STM32F407VG | 1 MB | 0x08000000 |
STM32F767ZI | 2 MB | 0x08000000 |
STM32G431KB | 128 KB | 0x08000000 |
Some chips also include dual-bank flash, which lets you read and write at the same time. This is useful for firmware updates without interrupting execution.
Flash page size also varies:
- STM32F1: 1 KB or 2 KB per page
- STM32F4/F7: 16 KB to 128 KB sectors
- STM32G0/G4: 2 KB per page
Check the reference manual or datasheet for your exact model before writing or erasing flash.
STM32 Flash vs External Flash
STM32 MCUs have built-in flash, but you might wonder if external flash chips are better.
Here’s a quick comparison:
Internal Flash (STM32)
- Faster access
- No extra hardware needed
- Ideal for firmware and small data storage
- Limited size (max 2MB in most cases)
External Flash (e.g. SPI NOR Flash)
- Larger size (up to 128MB or more)
- Requires SPI or QSPI interface
- Better for storing logs, audio files, or firmware backups
- Slower than internal flash
Use internal flash for firmware and small settings. Use external flash for large files, especially when memory needs exceed your STM32’s internal flash.
Common Errors When Flashing STM32
Flashing doesn’t always go smoothly. Here are common mistakes and how to fix them:
- Wrong Flash Address
- Always start at
0x08000000
for firmware unless using a bootloader.
- Always start at
- Flash Not Unlocked
- Use
HAL_FLASH_Unlock()
before writing. Forgetting this will cause the flash write to fail.
- Use
- Forgot to Erase
- Flash writes only turn bits from 1 to 0. You must erase before writing new data.
- Using Wrong Sector/Page
- STM32 chips divide flash into pages or sectors. Writing outside the valid range causes faults.
- Debugger Interference
- Sometimes the debugger holds flash or SRAM. Try disconnecting and reflashing.
- Write Protection Enabled
- Check option bytes in STM32CubeProgrammer to make sure sectors are not protected.
- Power Issues
- Low voltage during programming can corrupt flash. Always ensure stable power.
If you’re stuck, checking your device’s reference manual often solves the issue.
Real Project Example: Saving Data in STM32 Flash
Here’s a practical example: Suppose you’re building a temperature logger and want to save the last 100 readings even after the device restarts.
Here’s how you could do it:
- Choose a flash address: Pick an unused address like
0x0801F000
. - Store values using
HAL_FLASH_Program()
. - Read them back using normal pointer access:
uint32_t lastTemp = *(uint32_t*)0x0801F000;
Tips:
- Use a defined sector only for this data.
- Erase the sector before each session or implement wear leveling.
- Keep track of how many writes you do to avoid memory wear.
This method avoids the need for external EEPROM and works well for small datasets.
Frequently Asked Questions (FAQ)
What is STM32 flash memory used for?
It stores your firmware and any settings you want to keep after power off.
How do I flash an STM32 microcontroller?
You can use STM32CubeProgrammer, ST-Link, or a USB bootloader to write firmware into flash memory.
Where is flash memory located on STM32?
It starts at address 0x08000000
in the memory map.
How to use HAL_FLASH_Program()
properly?
First unlock flash, then erase the sector, write using HAL_FLASH_Program()
, and lock it afterward.
Can I save variables to STM32 flash?
Yes, but you need to manage erase/write operations manually and avoid writing too often.
Why does flash write fail on STM32?
Possible reasons include locked flash, wrong address, or write protection.
Can STM32 flash be updated without debugger?
Yes, using USB DFU bootloader or UART bootloader methods.
Does STM32 flash memory wear out?
Yes, like all flash, it wears out after about 10,000 write/erase cycles per sector.
Is external flash better than internal flash?
Use internal for firmware and external for large data like logs or graphics.
What’s the difference between flash and EEPROM in STM32?
Flash is larger and used for firmware. EEPROM (if present) is smaller and used for tiny settings.
Conclusion: Why STM32 Flash Matters and What To Do Next
STM32 flash memory is the core of every STM32 project. Whether you’re flashing firmware, saving data, or setting up bootloaders, knowing how flash works makes your development easier and safer.
If you’ve never written to flash manually before, try using HAL_FLASH_Program()
in a test project. Make sure to unlock, erase, and lock properly. Once you’re comfortable with the process, it becomes part of your toolkit like any other feature.
Want to learn more? Bookmark this STM32 flash memory tutorial, try it in your own project, and don’t forget to explore other STM32 guides here on ControllersTech.