STM32 Flash Programming

Pasindu_Sandima
Arimac
3 min readJan 7, 2021

--

What is flash memory?

Flash memory is a non-volatile storage medium that can be electrically erased and reprogrammed. There are two types of flash memory technologies; NAND flash and NOR flash. In most of the STM32 devices, NOR flash is used because of its suitability in embedded applications.

Why do we need flash memory?

In microcontrollers, the flash memory is primarily used to store the program which is to be executed. Also, the application can use the flash memory as an easy replacement for an EEPROM ( Electrically Erasable Programmable Read Only Memory). Thereby, the flash memory can be used to store constant data the application needs when the device reboots.

Flash Memory Organization of STM32 devices

In this article, the STM32F103CB microcontroller ( which is commonly named as STM32 blue pill ) is used for explanations. The device consists of 128KB of flash storage and 20KB of SRAM storage. The flash memory is divided into 128 pages of 1KB each. The memory organization of your device can be found by referring to the reference manual .

Flash Module Organization of STM32F103CB microcontroller

How to emulate flash memory as EEPROM

The flash memory is write-protected by default. Therefore, prior to any write operation, a sequence of operations needs to be followed to enable the write mode. This task is implemented in the following CubeHAL library function.

Before any changes are done to the flash memory, it needs to be erased. This task can be achieved either by erasing the flash memory page by page or completely. In this case, the first method is used. The following code snippet can be used to achieve this.

When selecting pages to be erased, make sure you choose from the bottom to top because the program is stored from top to bottom. In this case I’m using the page 127 address 0x0801FC00 which can be found by referring to the above table.

Code Snippet for Erasing a Flash Page

Finally, the data can be written into the flash memory. The flash memory can be programmed only 16 bits at a time. Through software implementations HAL library provides functions to write 32 bit and 64 bit variables as well. Given below is a code snippet to write an unsigned integer of 64 bits to the memory address 0x0801FC00.

Code Snippet for Writing to a Flash Address

The write protection should be enabled after programming the flash memory to protect the flash memory from accidental writings. This can be achieved by using the function:

Data can be read from the flash memory by using the following code snippet. In the first line, a pointer variable of type uint64_t is created RDAddr which points to the address which is to be read. Then a uint64_t variable is created and is assigned the value stored at RDAddr.

Troubleshooting

The erase operation and the write operation can be verified by viewing the memory monitor in the debug mode. (Using CubeIDE )

Memory after erasing
Memory after Writing

The read operation can be verified by viewing the variables pane in the debug mode.

Variables pane of STM32 Cube IDE in debug perspective

Conclusion

Using the above basic functions, the flash memory can be used as an alternative for the EEPROM. Building on these functions, any data structure can be stored to the flash memory and retrieved from it even after a reset. The complete main.c file, which includes all the above functions can be found here.

--

--

Pasindu_Sandima
Arimac
Writer for

Graduate | Electronics and Telecommunication Engineering | University of Moratuwa