MPU Module

The Memory Protection Unit (MPU) API provides functions to configure the MPU. The MPU is tightly coupled to the Cortex-M processor core and provides a means to establish access permissions on regions of memory.

Up to eight memory regions can be defined. Each region has a base address and a size. The size is specified as a power of 2 between 32 bytes and 4 GB, inclusive. The region’s base address must be aligned to the size of the region. Each region also has access permissions. Code execution can be allowed or disallowed for a region. A region can be configured for read-only access, read/write access, or no access for both privileged and user modes. Access permissions can be used to create an environment where only kernel or system code can access certain hardware registers or sections of code.

The MPU creates 8 sub-regions within each region. Any sub-region or combination of sub-regions can be disabled, allowing creation of “holes” or complex overlaying regions with different permissions. The sub-regions can also be used to create an unaligned beginning or ending of a region by disabling one or more of the leading or trailing sub-regions. Once the regions are defined and the MPU is enabled, any access violation of a region causes a memory management fault, and the fault handler is activated.

group mpu_api

Defines

MPU_CONFIG_PRIV_DEFAULT 0x4U

region for priv sw access.

Enable default memory map as bg

MPU_CONFIG_HARDFLT_NMI 0x2U

NMI and FAULTMASK handlers.

MPU is enabled during hard fault,

MPU_CONFIG_NONE 0x0U

None of the above configurations.

MPU_RGN_0 0x0U
MPU_RGN_1 0x1U
MPU_RGN_2 0x2U
MPU_RGN_3 0x3U
MPU_RGN_4 0x4U
MPU_RGN_5 0x5U
MPU_RGN_6 0x6U
MPU_RGN_7 0x7U
MPU_RGN_SIZE_32B (4U << 1U)

Region Size is 32-bytes.

MPU_RGN_SIZE_64B (5U << 1U)

Region Size is 64-bytes.

MPU_RGN_SIZE_128B (6U << 1U)

Region Size is 128-bytes.

MPU_RGN_SIZE_256B (7U << 1U)

Region Size is 256-bytes.

MPU_RGN_SIZE_512B (8U << 1U)

Region Size is 512-bytes.

MPU_RGN_SIZE_1K (9U << 1U)

Region Size is 1K-bytes.

MPU_RGN_SIZE_2K (10U << 1U)

Region Size is 2K-bytes.

MPU_RGN_SIZE_4K (11U << 1U)

Region Size is 4K-bytes.

MPU_RGN_SIZE_8K (12U << 1U)

Region Size is 8K-bytes.

MPU_RGN_SIZE_16K (13U << 1U)

Region Size is 16K-bytes.

MPU_RGN_SIZE_32K (14U << 1U)

Region Size is 32K-bytes.

MPU_RGN_SIZE_64K (15U << 1U)

Region Size is 64K-bytes.

MPU_RGN_SIZE_128K (16U << 1U)

Region Size is 128K-bytes.

MPU_RGN_SIZE_256K (17U << 1U)

Region Size is 256K-bytes.

MPU_RGN_SIZE_512K (18U << 1U)

Region Size is 512K-bytes.

MPU_RGN_SIZE_1M (19U << 1U)

Region Size is 1M-bytes.

MPU_RGN_SIZE_2M (20U << 1U)

Region Size is 2M-bytes.

MPU_RGN_SIZE_4M (21U << 1U)

Region Size is 4M-bytes.

MPU_RGN_SIZE_8M (22U << 1U)

Region Size is 8M-bytes.

MPU_RGN_SIZE_16M (23U << 1U)

Region Size is 16M-bytes.

MPU_RGN_SIZE_32M (24U << 1U)

Region Size is 32M-bytes.

MPU_RGN_SIZE_64M (25U << 1U)

Region Size is 64M-bytes.

MPU_RGN_SIZE_128M (26U << 1U)

Region Size is 128M-bytes.

MPU_RGN_SIZE_256M (27U << 1U)

Region Size is 256M-bytes.

MPU_RGN_SIZE_512M (28U << 1U)

Region Size is 512M-bytes.

MPU_RGN_SIZE_1G (29U << 1U)

Region Size is 1G-bytes.

MPU_RGN_SIZE_2G (30U << 1U)

Region Size is 2G-bytes.

MPU_RGN_SIZE_4G (31U << 1U)

Region Size is 4G-bytes.

MPU_RGN_PERM_EXEC 0x00000000U

Inst. fetches are enabled.

MPU_RGN_PERM_NOEXEC 0x10000000U

Inst. fetches are disabled.

MPU_RGN_PERM_PRV_NO_USR_NO 0x00000000U

unprivileged access

No access to privileged &

MPU_RGN_PERM_PRV_RW_USR_NO 0x01000000U

access to unprivileged sw

R/W to privileged sw, no

MPU_RGN_PERM_PRV_RW_USR_RO 0x02000000U

access to unpriv sw

R/W access to priv sw, no

MPU_RGN_PERM_PRV_RW_USR_RW 0x03000000U

access to unpriv sw

R/W access to priv sw, R/W

MPU_RGN_PERM_PRV_RO_USR_NO 0x05000000U

access to unpriv sw

RO access to priv sw, no

MPU_RGN_PERM_PRV_RO_USR_RO 0x06000000U

access to unpriv sw

RO access to priv sw, RO

MPU_SUB_RGN_DISABLE_0 0x00000100U

Disable sub-region 0.

MPU_SUB_RGN_DISABLE_1 0x00000200U

Disable sub-region 1.

MPU_SUB_RGN_DISABLE_2 0x00000400U

Disable sub-region 2.

MPU_SUB_RGN_DISABLE_3 0x00000800U

Disable sub-region 3.

MPU_SUB_RGN_DISABLE_4 0x00001000U

Disable sub-region 4.

MPU_SUB_RGN_DISABLE_5 0x00002000U

Disable sub-region 5.

MPU_SUB_RGN_DISABLE_6 0x00004000U

Disable sub-region 6.

MPU_SUB_RGN_DISABLE_7 0x00008000U

Disable sub-region 7.

MPU_RGN_ENABLE 1U

Enable MPU Region.

MPU_RGN_DISABLE 0U

Disable MPU Region.

MPU_RGN_CONFIG_M ((uint32_t)NVIC_MPU_RASR_ENABLE

| \

(uint32_t)

NVIC_MPU_RASR_SIZE_M

| \

(uint32_t)

NVIC_MPU_RASR_SRD_M

| \

(uint32_t)

NVIC_MPU_RASR_B

| \

(uint32_t)

NVIC_MPU_RASR_C

| \

(uint32_t)

NVIC_MPU_RASR_S

| \

(uint32_t)

NVIC_MPU_RASR_TEX_M

| \

(uint32_t)

NVIC_MPU_RASR_AP_M

| \

(uint32_t)

NVIC_MPU_RASR_XN)

Functions

void MPU_enable(uint32_t config)

Enables and configures the MPU for use.

This function enables the Cortex-M memory protection unit. It also configures the default behavior when in privileged mode and while handling a hard fault or NMI. Prior to enabling the MPU, at least one region must be set by calling

MPU_setRegionAttributes() or else by enabling the default region for privileged mode by passing the MPU_CONFIG_PRIV_DEFAULT flag to MPU_enable(). Once the MPU is enabled, a memory management fault is generated for memory access violations.
Parameters
  • config: is the logical OR of the possible configurations.

The config parameter should be the logical OR of any of the following:

  • MPU_CONFIG_PRIV_DEFAULT enables the default memory map when in privileged mode and when no other regions are defined. If this option is not enabled, then there must be at least one valid region already defined when the MPU is enabled.

  • MPU_CONFIG_HARDFLT_NMI enables the MPU while in a hard fault or NMI exception handler. If this option is not enabled, then the MPU is disabled while in one of these exception handlers and the default memory map is applied.

  • MPU_CONFIG_NONE chooses none of the above options. In this case, no default memory map is provided in privileged mode, and the MPU is not enabled in the fault handlers.

Return

None.

void MPU_disable(void)

Disables the MPU for use.

Return

None.

Parameters
  • None: This function disables the Cortex-M memory protection unit. When the MPU is disabled, the default memory map is used and memory management faults are not generated.

uint32_t MPU_getRegionCount(void)

Gets the count of regions supported by the MPU.

Return

The number of memory protection regions that are available for programming using MPU_setRegionAttributes().

Parameters
  • None: This function is used to get the total number of regions that are supported by the MPU, including regions that are already programmed.

void MPU_enableRegion(uint32_t regionNum)

Enables a specific region.

This function is used to enable a memory protection region. The region should already be configured with the

MPU_setRegionAttributes() function. Once enabled, the memory protection rules of the region are applied and access violations cause a memory management fault.
Parameters
  • regionNum: is the region number to enable.

Return

None.

void MPU_disableRegion(uint32_t regionNum)

Disables a specific region.

This function is used to disable a previously enabled memory protection region. The region remains configured if it is not overwritten with another call to

MPU_setRegionAttributes(), and can be enabled again by calling MPU_enableRegion().
Parameters
  • regionNum: is the region number to disable.

Return

None.

void MPU_setRegionAttributes(uint32_t regionNum, uint32_t baseAddr, uint32_t attrFlags)

Sets up the access rules for a specific region.

This function sets up the protection rules for a region. The region has a base address and a set of attributes including the size.

Parameters
  • regionNum: is the region number to set up.

  • baseAddr: is the base address of the region. It must be aligned according to the size of the region specified in attrFlags.

  • attrFlags: is a set of flags to define the attributes of the region.

The region number parameter, regionNum specifies the region number to be setup. Valid values for regionNum parameter are:

  • MPU_RGN_0

  • MPU_RGN_1

  • MPU_RGN_2

  • MPU_RGN_3

  • MPU_RGN_4

  • MPU_RGN_5

  • MPU_RGN_6

  • MPU_RGN_7

The base address parameter, baseAddr, must be aligned according to the size, and the size must be a power of 2.

The attrFlags parameter is the logical OR of all of the attributes of the region. It is a combination of choices for region size, execute permission, read/write permissions, disabled sub-regions, and a flag to determine if the region is enabled.

The size flag determines the size of a region and must be one of the following:

  • MPU_RGN_SIZE_32B

  • MPU_RGN_SIZE_64B

  • MPU_RGN_SIZE_128B

  • MPU_RGN_SIZE_256B

  • MPU_RGN_SIZE_512B

  • MPU_RGN_SIZE_1K

  • MPU_RGN_SIZE_2K

  • MPU_RGN_SIZE_4K

  • MPU_RGN_SIZE_8K

  • MPU_RGN_SIZE_16K

  • MPU_RGN_SIZE_32K

  • MPU_RGN_SIZE_64K

  • MPU_RGN_SIZE_128K

  • MPU_RGN_SIZE_256K

  • MPU_RGN_SIZE_512K

  • MPU_RGN_SIZE_1M

  • MPU_RGN_SIZE_2M

  • MPU_RGN_SIZE_4M

  • MPU_RGN_SIZE_8M

  • MPU_RGN_SIZE_16M

  • MPU_RGN_SIZE_32M

  • MPU_RGN_SIZE_64M

  • MPU_RGN_SIZE_128M

  • MPU_RGN_SIZE_256M

  • MPU_RGN_SIZE_512M

  • MPU_RGN_SIZE_1G

  • MPU_RGN_SIZE_2G

  • MPU_RGN_SIZE_4G

The execute permission flag must be one of the following:

  • MPU_RGN_PERM_EXEC enables the region for execution of code

  • MPU_RGN_PERM_NOEXEC disables the region for execution of code

The read/write access permissions are applied separately for the privileged and user modes. The read/write access flags must be one of the following:

  • MPU_RGN_PERM_PRV_NO_USR_NO - no access in privileged or user mode

  • MPU_RGN_PERM_PRV_RW_USR_NO - privileged read/write, user no access

  • MPU_RGN_PERM_PRV_RW_USR_RO - privileged read/write, user read-only

  • MPU_RGN_PERM_PRV_RW_USR_RW - privileged read/write, user read/write

  • MPU_RGN_PERM_PRV_RO_USR_NO - privileged read-only, user no access

  • MPU_RGN_PERM_PRV_RO_USR_RO - privileged read-only, user read-only

The region is automatically divided into 8 equally-sized sub-regions by the MPU. Sub-regions can only be used in regions of size 256 bytes or larger. Any of these 8 sub-regions can be disabled, allowing for creation of `holes’ in a region which can be left open, or overlaid by another region with different attributes. Any of the 8 sub-regions can be disabled with a logical OR of any of the following flags:

  • MPU_SUB_RGN_DISABLE_0

  • MPU_SUB_RGN_DISABLE_1

  • MPU_SUB_RGN_DISABLE_2

  • MPU_SUB_RGN_DISABLE_3

  • MPU_SUB_RGN_DISABLE_4

  • MPU_SUB_RGN_DISABLE_5

  • MPU_SUB_RGN_DISABLE_6

  • MPU_SUB_RGN_DISABLE_7

Finally, the region can be initially enabled or disabled with one of the following flags:

  • MPU_RGN_ENABLE

  • MPU_RGN_DISABLE

As an example, to set a region with the following attributes: size of 32 KB, execution enabled, read-only for both privileged and user, one sub-region disabled, and initially enabled; the attrFlags parameter would have the following value:

(MPU_RGN_SIZE_32K | MPU_RGN_PERM_EXEC | MPU_RGN_PERM_PRV_RO_USR_RO | MPU_SUB_RGN_DISABLE_2 | MPU_RGN_ENABLE)

Note

This function writes to multiple registers and is not protected from interrupts. It is possible that an interrupt which accesses a region may occur while that region is in the process of being changed. The safest way to handle this is to disable a region before changing it. Refer to the discussion of this in the API Detailed Description section.

Return

None.

void MPU_getRegionAttributes(uint32_t regionNum, uint32_t *baseAddr, uint32_t *attrFlags)

Gets the current settings for a specific region.

This function retrieves the configuration of a specific region. The meanings and format of the parameters is the same as that of the

MPU_setRegionAttributes() function.
Parameters
  • regionNum: is the region number to get.

  • baseAddr: points to storage for the base address of the region.

  • attrFlags: points to the attribute flags for the region.

This function can be used to save the configuration of a region for later use with the MPU_setRegionAttributes() function. The region’s enable state is preserved in the attributes that are saved.

Return

None.

void MPU_registerHandler(uint32_t intNum, void (*fnHandler)(void))

Registers an interrupt handler for the memory management fault.

This function sets and enables the handler to be called when the MPU generates a memory management fault due to a protection region access violation. The param intNum can take value FAULT_MPU.

Parameters
  • intNum: is the interrupt number to be registered.

  • fnHandler: is a pointer to the function to be called when the memory management fault occurs.

See

Interrupt_registerHandler() for important information about registering interrupt handlers.

Return

None.

void MPU_unregisterHandler(uint32_t intNum)

Unregisters an interrupt handler for the memory management fault.

This function disables and clears the handler to be called when a memory management fault occurs. The param intNum can take value FAULT_MPU.

Parameters
  • intNum: is the interrupt number to be unregistered.

See

Interrupt_registerHandler() for important information about registering interrupt handlers.

Return

None.

APIs are available to set/get MPU region attributes, enable/disable MPU regions, enable/disable MPU and register/unregister interrupt handler for MPU faults.

The code for this module is contained in driverlib_cm/mpu.c and driverlib_cm/mpu.h.