How to Use Internal Flash Memory

download all demo files: Flash.7z

1 Internal Flash Memory

1.1 Introduction

In embedded systems, it is very important to have a memory which can store the information. Programs (set of instructions) are used to make an embedded system behave in such a manner according to the user requirements. Our programs are written in C or Waijung Blockset which compiles the set of instructions. Microcontroller uses flash memory which can be erased and re-programmed. Some space of the memory can be re-programmed and some space of the memory is fixed (i.e. cannot be re-programmed) and it depends on the manufacturer of the microcontroller how they manages the memory allocations.

STM32F4DISCOVERY board have a STM32F407VGT6 microcontroller which has a capacity of 1 MByte flash memory. The flash memory of the microcontroller has been divided into the sectors and each sector have its respective starting and ending addresses. Some sectors are of small size and some are of large size. Allocation of flash memory space is shown in Figure 1‑1.

Figure 1-1: Allocation of flash memory space within the STM32F407 [1, p. 54]

The allocation of internal flash memory of STM32F407 is as follows.

The Waijung library has a blockset for using Internal Flash Memory Block having an option of erasing read and write data. The use is limited from sector 5 to 11.

1.2 How to use Flash Memory Block

Simulink library >> Waijung Blockset >> STM32F4 target >> On Peripheral Chip >> FLASH contains a blockset for managing the data in the flash memory. Flash Erase block parameters are shown in Figure 1‑2 which is used for deleting the data from the sectors. It takes about 1 to 2 seconds to erase the data from 128 KB memory [2, p. 106].

Figure 1-2: Features of Flash Erase Block

Block parameters of Flash Read are shown in Figure 1‑3. It is used to read the data from specified memory. For example, if used wants to read the address: 0x080E0500, following are the steps.

Thus Offset = 0x80E0500 – 0x80E0000

= 0x0000500 = 20480 (in decimal)

Figure 1-3: Characteristics of Flash Read Block

Flash Write block is shown in Figure 1-4. It is used for write data to the memory offset of specific sector id. The procedure for assigning the address and specifying the offset address is same as that of Flash Read Block as described above.

Figure 1-4: Characteristics of Flash Back Block

2 Testing the Flash Memory

2.1 Equipment used in the Experiment

2.2 Experiment for Saving and Deleting Data


Figure 2-1: Simulink model for writing and erasing the data from flash memory

This experiment is conducted to write or erase flash memory address at 0x080E0500 using Simulink model as shown in Figure 1‑1 which consists of a three major blocks.

Figure 2-2: Within Flash Erase Subsystem model

Figure 2-3: Within FlashWritingSeq Subsystem model

While writing in the flash memory, it is good to erase the already existing information on that sector which can be done by using Flash Erase Block which will delete the information of the entire sector.

After the Simulink model is downloaded into STM32F4DISCOVERY board, LED1 which is connected with PD12 will blink. To observe the changes in the flash memory, this experiment uses STM32 ST-Utility program and have the following main steps.

2.2.1 Procedure for Connecting STM32 ST-Utility

Figure 2-4: STM32 ST-Link Utility window after connecting with STM32F4DISCOVERY

Figure 2-5: Core panel window

2.2.2 How to use Memory Display

Data in memory will be shown in hexadecimal format in a table form. Rows and columns show the position of address. User can adjust the memory display as follows.

Figure 2-6: Window displaying data in the memory (Data Width 32 bits or 4 Bytes)

When user set the Data Width = 8 bits on the memory display then the display will change as shown in Figure 2‑7. Now the size of address is 8 bits value (00 to FF).

Figure 2-7: Window displaying data in memory (Data Width 8 bits or 1 Byte)

2.2.3 Experiment with Writing and Erasing

Figure 2-8: Windows displaying the data in memory after writing

Figure 2-9: Window displaying the data in memory after deletion

2.3 Experimental data from Flash Memory


Figure 2-10: Simulink Model for reading data from Flash Memory and displayed through the LCD

From the example, in this experiment, users can observe data by STM32 ST-Utility connected to a computer with USB cable. Furthermore, user can observe data that has been record via LCD.

This experiment uses the model as shown in Figure 2‑10 for reading data from the flash memory and display it on the LCD which is an additional part when compared with the model shown in Figure 2‑1. Switch SW3 is used for updating the value read from flash memory and then display it on LCD. Figure 2‑11 shows internal subsystem model for reading data at address: 0x080E0500.

Figure 2-11: FlashRead Subsystem Model for reading data from Flash Memory

If user wants to write multiple data sets, user can edit Block in FlashWriteSeq Subsystem as in Figure 2‑13. In this example, 3 sets of data are written which have different type of variables (uint8, uint32 and single). User can add Flash Write Block and set any address within range of 4 Bytes

Figure 2-12: Values read from Flash Memory through the LCD display

If user wants to write multiple data sets, user can edit Block in FlashWriteSeq Subsystem as in Figure 2‑13. In this example, 3 sets of data are written which have different type of variables (uint8, uint32 and single). User can add Flash Write Block and set any address within range of 4 Bytes.

Figure 2-13: Subsystem model for writing or recording in multiple Flash Memory

So in the example, if the value read from the DIP switch is set to 12. Information address: 0x080E504 is equal to 12000 and address: 0x080E508 is equal to 1481.40 so users can check in flash memory window which displays the data in memory as shown in Figure 2‑14.

Figure 2-14: Window displaying data in the memory

Users can learn how to convert a decimal number to binary floating-point arithmetic from the following link This is known as IEEE-754 standard.

Figure 2‑15 is an example of subsystem for reading 3 data sets of different type of variables (uint8, uint32 and single). User can add the Flash Read Block and set address that want to read.

Figure 2-15: Subsystem for reading data of multiple types

Download the model and information stored in DIP switch, SW1 switch and SW2 switch will be displayed on the LCD display which is shown in Figure 2‑16.

Figure 2-16: Reading value from multiple data sets of flash memory and display it on the LCD

Although user can turn ON-OFF the electric power or download model into STM32F4DISCOVERY again, data in sector 5 – sector 11 will still remain unchanged. So using internal flash memory is suitable for working with data which is not changed so often, for example security or setup section.

3 References

  1. STMicroelectronics, “RM0090: STM32F40xxx Reference Manual [Online],” 2013.
  2. STMicroelectronics, “STM32 F407xx Datasheet-production data [Online],” 2013.
  3. STMicroelectronics, “User manual: STM32 ST-LINK Utility software description [Online],” 2014.
  4. Olaf Pfeiffer and Andrew Ayre, “Using Flash Memory in Embedded Applications,” [Online]. Available:
  5. IEEE, “IEEE Standard for Floating-Point Arithmetic”, 2008