cancel
Showing results for 
Search instead for 
Did you mean: 

How to configure the MPU of an STM32 using STM32CubeMX

ST AME Support NF
ST Employee

How to configure the MPU of an STM32 using STM32CubeMX

1. Introduction

The MPU (Memory Protection Unit) is included in the Cortex-M4 and M7 based STM32 Microcontrollers (refer to specific product datasheets to confirm availability). The MPU can be used to make an embedded system more robust and more secure by:
  • prohibiting the user applications from corrupting data used by critical tasks (such as the operating system kernel)
  • defining the SRAM memory region as non-executable (execute never XN) to prevent code injection attacks
  • changing the memory access attributes
 This article shows how to enable and configure the MPU using the STM32CubeMX software.
 

2. Pre-requisite

  • Software
    • STM32CubeMX
 

3. Theory

The purpose of this article is to explain how to use STM32CubeMX to configure the MPU. For details about the MPU please refer to the Application Note (AN), Managing memory protection unit in STM32 MCUs - Application note
As an example configuration, we will define three MPU regions following the example provided in the application note as shown below:
1760.png
Note: All the terms in this table are explained in the application note as well as all the definitions and explanations.

The STM32H743AI microcontroller will be used as the target device. It uses an Arm Cortex-M7 core that embeds the MPU.

4.  Steps

 
  1. Open STM32CubeMX  
  2. Create a new project and select the STM32H743AI for this example

1761.png
 
  1. MPU settings
  • To gain access to the MPU settings, go to the Pinout & Configuration Tab and then click on Cortex-M7 as shown below:
1762.png
  1. Enable the MPU by selecting one of the four possible modes
1763.png
  1. Enable and configure the different MPU regions as show below:
1764.png
 
  1. Configure the three regions as defined in the table from the application note:
1765.png
 
  1. Region 0 settings:

1766.png
  • In STM32CubeMX, make the following selections:

1767.png
 
  1. Region 1 settings:
1768.png
  • In STM32CubeMX, the configuration for this region should be set to:
1769.png
 
  1. Region 2 settings:
1770.png
  • In STM32CubeMX, the configuration for this region should be set to:
1771.png
The MPU configuration is now complete. While this article shows how to configure the MPU for the STM32H743AI following the example table in the application note, the MPU is highly configurable and can be tailored to variety of application needs.
 

5. Links

STM32H753xI - Datasheet
STM32H742, STM32H743/753 and STM32H750 Value line advanced Arm®-based 32-bit MCUs - Reference manual
STM32F7 Series and STM32H7 Series Cortex®-M7 processor programming manual

Managing memory protection unit in STM32 MCUs - Application note
STM32CubeMX - STM32Cube initialization code generator - STMicroelectronics
 
Comments
OAgui.1
Associate II

Hi community. I am new using ST MCUs and I have to say that I am very pleased.

I am planning to use memory protection unit (MPU) in my project. My MCU is STM32G031K8 but I cannot find how to activate this function with CubeMX. I would want to do the same as the example above:

In addition in the reference manual there is no much information about this function or the registers involved:

https://www.st.com/resource/en/reference_manual/rm0444-stm32g0x1-advanced-armbased-32bit-mcus-stmicroelectronics.pdf

or in the datasheet

https://www.st.com/resource/en/datasheet/stm32g031k8.pdf

An I wrong? or maybe this function is not available for this MCU. According to this is link I can use this function but I am not sure.

https://www.st.com/content/ccc/resource/technical/document/application_note/group0/bc/2d/f7/bd/fb/3f/48/47/DM00272912/files/DM00272912.pdf/jcr:content/translations/en.DM00272912.pdf

Thanks for your help!

LauraCx
ST Employee

Hello @OAgui.1​ ,

@ST AME Support NF​ should be able to help you.

Thank you,

Laura

ST AME Support NF
ST Employee

Hello,

You are correct.

For STM32G0 you cannot configure the MPU from CubeMX, you can configure the MPU by code and here is an example from the STM32CubeG0.

C:\Users\xxxxx\STM32Cube\Repository\STM32Cube_FW_G0_V1.6.1\Projects\NUCLEO-G071RB\Examples\CORTEX\CORTEXM_MPU

BR

Nico

OAgui.1
Associate II

Thank you so much for your explanation.

A.Rade.96
Associate III

Hello, how to config Static Ram, Flash Memory and LCD ? I'm trying config, but displaying message conflict NE2 and NE3....

Version history
Last update:
‎2021-11-16 02:19 AM
Updated by: