AN4838 · Application noteIntroduction to memory protection unit management on STM32 MCUs
Download .pdfOpen in the full document

MPU features comparison between Cortex cores

MPU features comparison between Cortex cores

There are few MPU differences between Cortex-M0+, Cortex-M3/M4, Cortex-M7, Cortex‑M33 and Cortex‑M55.The user must be aware of them if the MPU configuration software has to be used. The table below illustrates these differences.
Table 7. Comparison of MPU features between Cortex cores
FeaturesCortex-M0+Cortex-M3/M4Cortex-M7Cortex-M33Cortex-M55
Number of regions888/16 (1)(2)8 MPU_S / 8 MPU_NS(3)16 MPU_S / 16 MPU_NS
Region addressYesYesYesYesYes
Region size256 bytes to 4 Gbytes32 bytes to 4 Gbytes32 bytes to 4 Gbytes32 bytes to 4 Gbytes32 bytes to 4 Gbytes
Region memory attributesS, C, B, XN(4)TEX, S, C, B, XNTEX, S, C, B, XNS,C, E (5),G (6), R (7), XNS,C, E(5) ,G (6), R (7), XN, PXN(8)
Region access permission (AP)YesYesYesYes (privileged or not)Yes (privileged or not)
Subregion disable8 bits8 bits8 bitsNANA
MPU bypass for NMI/HardFaultYesYesYesYesYes
Alias of MPU registersNoYesYesYesYes
Fault exceptionHardFault onlyHardFault/MemManageHardFault/MemManageHardFault/MemManageHardFault/MemManage
1 For STM32H7 series devices.
2 For STM32F7 series devices.
3 For STM32H5/STM32U3: 12 MPU_S / 8 MPU_NS .
4 Cortex-M0+ supports one level of cache policy. That is why the TEX field is not available in Cortex-M0+ processor.
5 Early write acknowledge (similar to bufferable)
6 Gathering
7 Reordering
8 Privileged eXecute Never attribute in Armv8.1-M architecture.

MPU setting example with STM32Cube HAL on Armv6 and Armv7 architectures

The table below describes an example of setting up the MPU with the following memory regions: Internal SRAM, flash memory and peripherals. The default memory map is used for privileged accesses as a background region, the MPU is not enabled for the HardFault handler and NMI.

Internal SRAM: 8 Kbytes of internal SRAM is configured as Region0.

Memory attributes: shareable and cacheable memory, write through with no write allocate, full access permission and code execution enabled.

Flash memory: the whole Flash memory is configured as Region.

Memory attributes: shareable and cacheable memory, write through with no write allocate, full access permission and code execution enabled.

Peripheral region: is configured as Region2.

Memory attributes: cached and shared device, full access permission and execute never.

Table 8. Example of setting up the MPU
ConfigurationMemory typeBase addressRegion numberMemory sizeMemory attributes
Internal SRAMNormal memory0x2000 0000Region08 KbytesShareable, write through, no write allocate

C = 1, B = 0, TEX = 0, S = 1

SRD = 0, XN = 0, AP = full access

Flash memoryNormal memory0x0800 0000Region11 MbyteNon-shareable write through, no write allocate

C = 1, B = 0, TEX = 0, S = 1

SRD = 0, XN = 0, AP = full access

FMCDevice memory0x4000 0000Region2512 MbytesShareable, write through, no write allocate

C = 1, B = 0, TEX = 0, S = 1

SRD = 0, XN = 1, AP = full access

Setting the MPU with STM32Cube HAL

void MPU_RegionConfig(void)
{
MPU_Region_InitTypeDef MPU_InitStruct;
/* Disable MPU */
HAL_MPU_Disable();
/* Configure RAM region as Region N°0, 8kB of size and R/W region */
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
MPU_InitStruct.BaseAddress = 0x24000000;
MPU_InitStruct.Size = MPU_REGION_SIZE_8KB;
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
MPU_InitStruct.IsCacheable = MPU_ACCESS_CACHEABLE;
MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
MPU_InitStruct.SubRegionDisable = 0x00;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/* Configure FLASH region as REGION N°1, 1MB of size and R/W region */
MPU_InitStruct.BaseAddress = 0x08000000;
MPU_InitStruct.Size = MPU_REGION_SIZE_1MB;
MPU_InitStruct.Number = MPU_REGION_NUMBER1;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/* Configure FMC region as REGION N°2, 0.5GB of size, R/W region */
MPU_InitStruct.BaseAddress = 0x40000000;
MPU_InitStruct.Size = MPU_REGION_SIZE_512MB;
MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER2;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/* Enable MPU */
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
}