How to configure the MPU of an STM32 using STM32CubeMX
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Email to a Friend
- Printer Friendly Page
- Report Inappropriate Content
on ‎2021-11-16 2:19 AM
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
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:
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
- Open STM32CubeMX
- Create a new project and select the STM32H743AI for this example
- MPU settings
- To gain access to the MPU settings, go to the Pinout & Configuration Tab and then click on Cortex-M7 as shown below:
- Enable the MPU by selecting one of the four possible modes
- In this example the default memory map mode is selected and used for privileged accesses as a background region. The MPU is not enabled for the HardFault handler and NMI as done in the Application Note. If you want to understand the different settings of the MPU please refer to the application note.
- Enable and configure the different MPU regions as show below:
- Configure the three regions as defined in the table from the application note:
- Region 0 settings:
- In STM32CubeMX, make the following selections:
- Region 1 settings:
- In STM32CubeMX, the configuration for this region should be set to:
- Region 2 settings:
- In STM32CubeMX, the configuration for this region should be set to:
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 - DatasheetSTM32H742, 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
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Email to a Friend
- Report Inappropriate Content
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:
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.
Thanks for your help!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Email to a Friend
- Report Inappropriate Content
Hello @OAgui.1​ ,
@ST AME Support NF​ should be able to help you.
Thank you,
Laura
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Email to a Friend
- Report Inappropriate Content
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
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Email to a Friend
- Report Inappropriate Content
Thank you so much for your explanation.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Email to a Friend
- Report Inappropriate Content
Hello, how to config Static Ram, Flash Memory and LCD ? I'm trying config, but displaying message conflict NE2 and NE3....
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Email to a Friend
- Report Inappropriate Content
As stated in the article itself that both CortexM4 and M7 based MCU has MPU. I am using STM32F439 which is M4 based MCU and I have verified from the datasheet that it has MPU. But still CubeMX doesn't show any option to configure the MPU. Please clarify. Thank you.