Getting started with MMT

Revision as of 16:48, 21 August 2023 by Registered User (→‎Pinout & Configuration)

This article explains what the Memory Mapping Tool (MMT) is and how to use it through examples.

1. What is MMT ?

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

2. Visual presentation

The MMT is under Tools > Memory Management.

2.1. Trustzone aware Project (with secure and non secure Application)

Let's introduce the Memory Management Tool with the picture below. This feature is available since the 6.9.0 STM32CubeMX version and this Getting Started has been realized with the 6.9.1 STM32CubeMX version.

Screen S&NS MMT.png

The MMT pannel is split in 6 parts:
1. The main pannel is 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 indicates the Code Generation Configuration :

  • if the application regions settings are applied to peripherals
  • if the application regions settings will be applied into linker files during code generation.

4. This Search bar allows to easily find an application region
5. This fifth section shows the selected Application Region in the main pannel. It lists many useful information. (Code Name, Start address, Size, Security, Access Permission, Code execution, Shareability and Cacheability)
6. The Display Settings and Reset View configure the memory view.

2.2. Non Trustzone aware Project (with only non secure Application)

The Non Secure aware project have 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 memory

3.1. Objectives

  • Create a simple project with a Secure (S) and a Non-Secure (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
  • And then click on Start Project.

Start Project.png

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

Initialize.png

  • Answer with TrustZone activated to the question "Do you want to create a new project ?" and OK

TZ activated.png

Info white.png Information
The project is by default neither saved nor named. You might save it in File > Save Project or Ctrl + S. Then, choose the name (eg: Test_MMT) and the location 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 regions are created because they are necessary to link applications. In this part, we are gonna 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 pops 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. Non Secure 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 pops 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) have 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

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

Apply linker.png

Warning white.png Warning
If you don't apply applications regions 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 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

  • Then 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 Non-Secure application.

Look at the memory definition :

  • Check the "App_Secure" 's declaration in the Secure world and the "App_NonSecure" in the Non Secure 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
Optionaly, check in debug mode or the .map to verify the Array location.

4. External memory

To go further, it's possible to manage 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 on 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, please 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 (eg : 2 secure regions cannot have the same name).
  • Overlap : 2 regions cannot overlap AND a region cannot span on a reserved area.
No categories assignedEdit