Getting started with MMT(Memory Management Tool)

This article explains what the Memory Management Tool (MMT) is. It shows also, through different examples, how to use it.

1 What is MMT ?

MMT stands for memory management tool. This STM32CubeMX feature handles the memory mapping in a more visual way. The main goal of the tool is to facilitate the creation, the partition, and the deletion of memory blocks in RAM, flash, or external memory thanks to IP connectivity : OCTOSPI, FMC, etc. In fact, the MMT abstracts the difficulty from the linker file by providing a simple use. Also, the MMT configures the memory management peripherals such as MPU, SAU, GTZC, and flash memory.

2 Visual presentation

The MMT is under Tools > Memory Management.

2.1 TrustZone® aware project (with secure and nonsecure applications)

Let us introduce the memory management tool with the picture below. This feature is available since the 6.9.0 STM32CubeMX version, and this article is based upon the 6.9.1 STM32CubeMX version.

Screen S&NS MMT.png

The MMT panel is split in six parts:
1. The main panel is composed of a memory view by the CPU (left hand) and the Application Regions (right hand) with addresses on the left.
2. The second point is the legend indicating the corresponding color from memory.
3. The third block is composed of two toggle buttons, which indicate the code generation configuration :

  • If the Application Regions settings are applied to peripherals.
  • If the Application Regions settings are applied in linker files during code generation.

4. This Search bar allows to find easily an application region.
5. This fifth section shows the selected application region in the main panel. It lists much useful information such as code name, start address, size, security, access permission, code execution, shareability, and cacheability.
6. The Display Settings and Reset View allows configuring the memory view.

2.2 Non Trustzone aware project (with only nonsecure application)

The nonsecure aware project has exactly the same interface, except there is only one type of Application Region. However, the principle remains strictly the same.

3 Configure MMT to handle the memory

3.1 Objectives

  • Create a simple project with a secure (S) and a nonsecure (NS) application.​
  • Learn how to configure the memory through the tool.
  • Verify the correct functionality by checking the linker file.

3.2 Create the project in STM32CubeMX

  • File > New Project > Board Selector in the main panel, or click on the shortcut Access to Board Selector.

Start Board.png

The example uses the NUCLEO-U575ZI-Q board.

  • Enter NUCLEO-U575ZI-Q in the commercial part number as shown in the figure below.
  • Select the NUCLEO-U575ZI-Q in the board List.
  • Then, click on Start Project.

Start Project.png

  • Select Yes in the pop-up window to initialize all the peripherals with their default mode.

Initialize.png

  • Answer with TrustZone activated to the question "Do you want to create a new project ?", then click OK.

TZ activated.png

Info white.png Information
The project is by default, neither saved, nor named. You might save it from File > Save Project , or by doing Ctrl + S with your keyboard. Select a name (for example: Test_MMT), and the location folder before clicking on Save.

3.3 Memory zone definition

Go to the memory management tool located under Tools > Memory Management. By default, the RAM & flash memory regions are created because they are necessary to link applications. At this level, we create two more Application Regions:

  • App_Secure
  • App_NonSecure

3.3.1 Secure Application Region

  • Select the RAM (S) region at the address 0x30000000.
  • Change the size to 192kB.

Add Zone RAM S.png

  • A new memory region available for an Application Region appears in white. When you mouse over it, a green cross appears.

Add Zone Sec.png

  • Click on this green cross section and the popup below opens up.
  • In the Name field, enter App_Secure.
  • The Address is the base address from the selected memory block. You can keep it by default.
  • Keep the Size of 64 kB.
  • Click on Add.

Popup Secure.png

Info white.png Information
Note that a Reserved Alias Region (S) have been created in addition to the App_Secure.

3.3.2 Nonsecure Application Region

  • Select the RAM (NS) region at the address 0x20040000.
  • Change the size to 256 kB.

Add Zone RAM NS.png

  • A new memory region available for an Application Region appears in white. When you mouse over it, a green cross appears.

Add Zone MMT.png

  • Click on this green cross section and the popup below opens up.
  • In the Name field, enter App_NonSecure.
  • The Address is the base address from the selected memory block, you can keep it by default.
  • Change the Size to 128 kB.
  • Click on Add.

Popup NonSecure.png

Info white.png Information
Note that a Reserved Alias Region (NS) has been created in addition to the App_NonSecure.


Warning white.png Warning
Check that both memory areas have been created.


Info white.png Information
To create or delete an Application Region, you can also click right and add or remove it.

3.3.3 Apply MMT

Go to code generation configuration as below:

  • Click on the first toggle button to apply Application Regions to Peripherals:

Apply periph.png

  • Click on the second button to apply Application Regions in linker files during the code generation:

Apply linker.png

Warning white.png Warning
If you do not apply Application Region settings to peripherals or linker files, all changes will remain local to STM32CubeMx.
Info white.png Information
Linker files (.ld) are accessible (Flash & RAM) once the project is generate.

3.4 Pinout & Configuration

  • Now, go to Pinout & Configuration > System Core > SAU. The SAU is under MMT control and is in Read Only.

Configure aplication.png

Info white.png Information
CORTEX_M33, FLASH and GTZC are also in Read Only. That means the Application Regions settings performed in MMT have been applied correctly.

3.5 Generate project and edit linker file

The easiest way to generate the code is to:

  • Click on Project Manager > Project > Project Settings.
  • Enter a Project Name, and select STM32CubeIDE in the ToolChain / IDE.

Generate Project.png

  • Select the Code Generator menu.
  • Check the box to Generate peripheral initialization as a pair of '.c/.h' files per peripheral (optional).
  • Click on Generate Code.

Generate CodeGenerator.png

Info white.png Information
Keep all the other settings in their default mode !
  • Click on Open Project.

Open Project.png

  • On the left side of the screen in the project explorer, expand the tree structure (NS & S project).

Tree project.png

  • Open the linker files (.ld).
Info white.png Information
Linker files are the mapping source file for secure and nonsecure applications.

Look at the memory definition:

  • Check the "App_Secure" declaration in the secure world and the "App_NonSecure" in the nonsecure world.

Memory definition.png

  • Check also the section definition below.

Mem Section.png

3.6 Compile

  • Click on Build button Built.png.
  • Check the Memory Regions in the Build Analyzer.

Build Analyzer.png

Info white.png Information
The memory region App_Secure is empy, 0% is used.

3.7 Edit main.c

Create an array to place in the App_Secure region:

  • Open the main.c file, which is the main source file for this secure project.
Info white.png Information
Insert your code between /* USER CODE BEGIN PD */ and /* USER CODE END PD */ tags.
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define LOCATE_FUNC __attribute__((section(".App_Secure_Section")))
#define SIZE 100
/* USER CODE END PD */
Info white.png Information
Insert your code between /* USER CODE BEGIN 0 */ and /* USER CODE END 0 */ tags.
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint32_t LOCATE_FUNC Array[SIZE] = {0};
/* USER CODE END 0 */
  • Click on Build button Built.png.
  • Verify the Memory Regions a second time in the Build Analyzer.

Build Analyzer Secure.png

  • Check other values for SIZE (200, 5000, 30000).
  • Conclude about the change by watching the section in Build Analyzer.
Info white.png Information
Optionally, check in the debug mode, or the .map to verify the Array location.

4 External memory

To go further, it is possible to manage the external memory through MMT.
Go to Pinout & Configuration > Connectivity to configure interfaces.

Info white.png Information
For both OctoSPI & FMC, the setup is arbitrary.

4.1 OCTOSPI

Active the OCTOSPI in Non Secure as shown on the picture below.

  • Set the mode in Single SPI.
  • Select Port1 CLK for the CLK, Port1 NCS for the chip select, and Port1 IO[1:0] for Data [3:0].
  • Choose the Device Type : RAM.

Ext Mem OctoSPI.png

4.2 FMC Bank

Active the FMC in Non Secure as shown in the picture below.

  • Choose the NE1 for the chip select.
  • Configure the Memory type to NOR Flash.
  • Select 16bits for the Address (mandatory to be 128 KBytes aligned), 16 bits for Data and Burst Read regarding the clock.
  • Tick the Address valid.

Ext Mem FMCBank.png

Warning white.png Warning
If there are conflicts with PB7 and PG2, click on both pins and select "Reset State".

4.3 MMT

Once the configuration above is done,

  • Go to the MMT under Tools > Memory Management.
  • Observe the activation of the External RAM (OctoSPI1) and the External Flash (FMC).

Ext Mem MMT.png

  • Create your application regions as needed (128 KBytes aligned).

5 Frequent error cases

Here is a list of the most common errors:

  • Alignment : regions must be aligned. (eg : 32 Bytes for Backup SRAM, 512 Bytes for SRAM, 8192 Bytes for FLASH and 128 KBytes for external memories).
  • Naming : 2 regions with the same name cannot coexist in the same context (for example: 2 secure regions cannot have the same name).
  • Overlap : 2 regions cannot overlap, and a region cannot span on a reserved area.