All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
MPU: Memory Protection Unit

## Overview

The MPU driver provides hardware access control for all memory references generated in the device. Use the MPU driver to program the region descriptors that define memory spaces and their access rights. After initialization, the MPU concurrently monitors the system bus transactions and evaluates the appropriateness.

# Initialization and Deinitialize

To initialize the MPU module, call the MPU_Init() function and provide the user configuration data structure. This function sets the configuration of the MPU module automatically and enables the MPU module.
Note that the configuration start address, end address, the region valid value, and the debugger's access permission for the MPU region 0 cannot be changed.

This is example code to configure the MPU driver:

// Defines the MPU memory access permission configuration structure . //
mpu_rwxrights_master_access_control_t mpuRwxAccessRightsMasters =
{
kMPU_SupervisorReadWriteExecute,
kMPU_UserNoAccessRights,
kMPU_IdentifierDisable,
kMPU_SupervisorEqualToUsermode,
kMPU_UserNoAccessRights,
kMPU_IdentifierDisable,
kMPU_SupervisorEqualToUsermode,
kMPU_UserNoAccessRights,
kMPU_IdentifierDisable,
kMPU_SupervisorEqualToUsermode,
kMPU_UserNoAccessRights,
kMPU_IdentifierDisable
}
mpu_rwrights_master_access_control_t mpuRwAccessRightsMasters =
{
false,
false,
false,
false,
false,
false,
false,
false
};
// Defines the MPU region configuration structure. //
mpu_region_config_t mpuRegionConfig =
{
0,
0x0,
0xffffffff,
mpuRwxAccessRightsMasters,
mpuRwAccessRightsMasters,
0,
0
};
// Defines the MPU user configuration structure. //
mpu_config_t mpuUserConfig =
{
mpuRegionConfig,
NULL
};
// Initializes the MPU region 0. //
MPU_Init(MPU, &mpuUserConfig);

# Basic Control Operations

MPU can be enabled/disabled for the entire memory protection region by calling the MPU_Enable(). To save the power for any unused special regions when the entire memory protection region is disabled, call the MPU_RegionEnable().

After MPU initialization, the MPU_SetRegionLowMasterAccessRights() and MPU_SetRegionHighMasterAccessRights() can be used to change the access rights for special master ports and for special region numbers. The MPU_SetRegionConfig can be used to set the whole region with the start/end address with access rights.

The MPU_GetHardwareInfo() API is provided to get the hardware information for the device. The MPU_GetSlavePortErrorStatus() API is provided to get the error status of a special slave port. When an error happens in this port, the MPU_GetDetailErrorAccessInfo() API is provided to get the detailed error information.

## Data Structures

struct | mpu_hardware_info_t
| MPU hardware basic information. More...
struct | mpu_access_err_info_t
| MPU detail error access information. More...
struct | mpu_rwxrights_master_access_control_t
| MPU read/write/execute rights control for bus master 0 ~ 3. More...
struct | mpu_rwrights_master_access_control_t
| MPU read/write access control for bus master 4 ~ 7. More...
struct | mpu_region_config_t
| MPU region configuration structure. More...
struct | mpu_config_t
| The configuration structure for the MPU initialization. More...

## Macros

#define | MPU_REGION_RWXRIGHTS_MASTER_SHIFT(n) (n * 6)
| MPU the bit shift for masters with privilege rights: read write and execute. More...
#define | MPU_REGION_RWXRIGHTS_MASTER_MASK(n) (0x1Fu << MPU_REGION_RWXRIGHTS_MASTER_SHIFT(n))
| MPU masters with read, write and execute rights bit mask. More...
#define | MPU_REGION_RWXRIGHTS_MASTER_WIDTH 5
| MPU masters with read, write and execute rights bit width. More...
#define | MPU_REGION_RWXRIGHTS_MASTER(n, x) (((uint32_t)(((uint32_t)(x)) << MPU_REGION_RWXRIGHTS_MASTER_SHIFT(n))) & MPU_REGION_RWXRIGHTS_MASTER_MASK(n))
| MPU masters with read, write and execute rights priority setting. More...
#define | MPU_REGION_RWXRIGHTS_MASTER_PE_SHIFT(n) (n * 6 + MPU_REGION_RWXRIGHTS_MASTER_WIDTH)
| MPU masters with read, write and execute rights process enable bit shift. More...
#define | MPU_REGION_RWXRIGHTS_MASTER_PE_MASK(n) (0x1u << MPU_REGION_RWXRIGHTS_MASTER_PE_SHIFT(n))
| MPU masters with read, write and execute rights process enable bit mask. More...
#define | MPU_REGION_RWXRIGHTS_MASTER_PE(n, x) (((uint32_t)(((uint32_t)(x)) << MPU_REGION_RWXRIGHTS_MASTER_PE_SHIFT(n))) & MPU_REGION_RWXRIGHTS_MASTER_PE_MASK(n))
| MPU masters with read, write and execute rights process enable setting. More...
#define | MPU_REGION_RWRIGHTS_MASTER_SHIFT(n) ((n - FSL_FEATURE_MPU_PRIVILEGED_RIGHTS_MASTER_COUNT) * 2 + 24)
| MPU masters with normal read write permission bit shift. More...
#define | MPU_REGION_RWRIGHTS_MASTER_MASK(n) (0x3u << MPU_REGION_RWRIGHTS_MASTER_SHIFT(n))
| MPU masters with normal read write rights bit mask. More...
#define | MPU_REGION_RWRIGHTS_MASTER(n, x) (((uint32_t)(((uint32_t)(x)) << MPU_REGION_RWRIGHTS_MASTER_SHIFT(n))) & MPU_REGION_RWRIGHTS_MASTER_MASK(n))
| MPU masters with normal read write rights priority setting. More...

## Enumerations

enum | mpu_region_total_num_t { kMPU_8Regions = 0x0U, kMPU_12Regions = 0x1U, kMPU_16Regions = 0x2U }
| Describes the number of MPU regions. More...
enum | mpu_slave_t { kMPU_Slave0 = 4U, kMPU_Slave1 = 3U, kMPU_Slave2 = 2U, kMPU_Slave3 = 1U, kMPU_Slave4 = 0U }
| MPU slave port number. More...
enum | mpu_err_access_control_t { kMPU_NoRegionHit = 0U, kMPU_NoneOverlappRegion = 1U, kMPU_OverlappRegion = 2U }
| MPU error access control detail. More...
enum | mpu_err_access_type_t { kMPU_ErrTypeRead = 0U, kMPU_ErrTypeWrite = 1U }
| MPU error access type. More...
enum | mpu_err_attributes_t { kMPU_InstructionAccessInUserMode = 0U, kMPU_DataAccessInUserMode = 1U, kMPU_InstructionAccessInSupervisorMode = 2U, kMPU_DataAccessInSupervisorMode = 3U }
| MPU access error attributes. More...
enum | mpu_supervisor_access_rights_t { kMPU_SupervisorReadWriteExecute = 0U, kMPU_SupervisorReadExecute = 1U, kMPU_SupervisorReadWrite = 2U, kMPU_SupervisorEqualToUsermode = 3U }
| MPU access rights in supervisor mode for bus master 0 ~ 3. More...
enum | mpu_user_access_rights_t { kMPU_UserNoAccessRights = 0U, kMPU_UserExecute = 1U, kMPU_UserWrite = 2U, kMPU_UserWriteExecute = 3U, kMPU_UserRead = 4U, kMPU_UserReadExecute = 5U, kMPU_UserReadWrite = 6U, kMPU_UserReadWriteExecute = 7U }
| MPU access rights in user mode for bus master 0 ~ 3. More...

## Driver version

#define | FSL_MPU_DRIVER_VERSION (MAKE_VERSION(2, 1, 0))
| MPU driver version 2.1.0. More...

## Initialization and deinitialization

void | MPU_Init (MPU_Type *base, const mpu_config_t *config)
| Initializes the MPU with the user configuration structure. More...
void | MPU_Deinit (MPU_Type *base)
| Deinitializes the MPU regions. More...

## Basic Control Operations

static void | MPU_Enable (MPU_Type *base, bool enable)
| Enables/disables the MPU globally. More...
static void | MPU_RegionEnable (MPU_Type *base, uint32_t number, bool enable)
| Enables/disables the MPU for a special region. More...
void | MPU_GetHardwareInfo (MPU_Type *base, mpu_hardware_info_t *hardwareInform)
| Gets the MPU basic hardware information. More...
void | MPU_SetRegionConfig (MPU_Type *base, const mpu_region_config_t *regionConfig)
| Sets the MPU region. More...
void | MPU_SetRegionAddr (MPU_Type *base, uint32_t regionNum, uint32_t startAddr, uint32_t endAddr)
| Sets the region start and end address. More...
void | MPU_SetRegionRwxMasterAccessRights (MPU_Type *base, uint32_t regionNum, uint32_t masterNum, const mpu_rwxrights_master_access_control_t *accessRights)
| Sets the MPU region access rights for masters with read, write and execute rights. More...
void | MPU_SetRegionRwMasterAccessRights (MPU_Type *base, uint32_t regionNum, uint32_t masterNum, const mpu_rwrights_master_access_control_t *accessRights)
| Sets the MPU region access rights for masters with read and write rights. More...
bool | MPU_GetSlavePortErrorStatus (MPU_Type *base, mpu_slave_t slaveNum)
| Gets the numbers of slave ports where errors occur. More...
void | MPU_GetDetailErrorAccessInfo (MPU_Type *base, mpu_slave_t slaveNum, mpu_access_err_info_t *errInform)
| Gets the MPU detailed error access information. More...

## Data Structure Documentation

struct mpu_hardware_info_t

### Data Fields

uint8_t | hardwareRevisionLevel
| Specifies the MPU's hardware and definition reversion level. More...
uint8_t | slavePortsNumbers
| Specifies the number of slave ports connected to MPU. More...
mpu_region_total_num_t | regionsNumbers
| Indicates the number of region descriptors implemented. More...

#### Field Documentation

uint8_t mpu_hardware_info_t::hardwareRevisionLevel

uint8_t mpu_hardware_info_t::slavePortsNumbers

mpu_region_total_num_t mpu_hardware_info_t::regionsNumbers

struct mpu_access_err_info_t

### Data Fields

uint32_t | master
| Access error master. More...
mpu_err_attributes_t | attributes
| Access error attributes. More...
mpu_err_access_type_t | accessType
| Access error type. More...
mpu_err_access_control_t | accessControl
| Access error control. More...
uint32_t | address
| Access error address. More...

#### Field Documentation

uint32_t mpu_access_err_info_t::master

mpu_err_attributes_t mpu_access_err_info_t::attributes

mpu_err_access_type_t mpu_access_err_info_t::accessType

mpu_err_access_control_t mpu_access_err_info_t::accessControl

uint32_t mpu_access_err_info_t::address

struct mpu_rwxrights_master_access_control_t

### Data Fields

mpu_supervisor_access_rights_t | superAccessRights
| Master access rights in supervisor mode. More...
mpu_user_access_rights_t | userAccessRights
| Master access rights in user mode. More...

#### Field Documentation

mpu_supervisor_access_rights_t mpu_rwxrights_master_access_control_t::superAccessRights

mpu_user_access_rights_t mpu_rwxrights_master_access_control_t::userAccessRights

struct mpu_rwrights_master_access_control_t

### Data Fields

bool | writeEnable
| Enables or disables write permission. More...
bool | readEnable
| Enables or disables read permission. More...

#### Field Documentation

bool mpu_rwrights_master_access_control_t::writeEnable

bool mpu_rwrights_master_access_control_t::readEnable

struct mpu_region_config_t

This structure is used to configure the regionNum region. The accessRights1[0] ~ accessRights1[3] are used to configure the bus master 0 ~ 3 with the privilege rights setting. The accessRights2[0] ~ accessRights2[3] are used to configure the high master 4 ~ 7 with the normal read write permission. The master port assignment is the chip configuration. Normally, the core is the master 0, debugger is the master 1. Note: MPU assigns a priority scheme where the debugger is treated as the highest priority master followed by the core and then all the remaining masters. MPU protection does not allow writes from the core to affect the "regionNum 0" start and end address nor the permissions associated with the debugger. It can only write the permission fields associated with the other masters. This protection guarantee the debugger always has access to the entire address space and those rights can't be changed by the core or any other bus master. Prepare the region configuration when regionNum is 0.

### Data Fields

uint32_t | regionNum
| MPU region number, range form 0 ~ FSL_FEATURE_MPU_DESCRIPTOR_COUNT - 1. More...
uint32_t | startAddress
| Memory region start address. More...
uint32_t | endAddress
| Memory region end address. More...
mpu_rwxrights_master_access_control_t | accessRights1 [4]
| Masters with read, write and execute rights setting. More...
mpu_rwrights_master_access_control_t | accessRights2 [4]
| Masters with normal read write rights setting. More...

#### Field Documentation

uint32_t mpu_region_config_t::regionNum

uint32_t mpu_region_config_t::startAddress

Note: bit0 ~ bit4 always be marked as 0 by MPU. The actual start address is 0-modulo-32 byte address.

uint32_t mpu_region_config_t::endAddress

Note: bit0 ~ bit4 always be marked as 1 by MPU. The actual end address is 31-modulo-32 byte address.

mpu_rwxrights_master_access_control_t mpu_region_config_t::accessRights1[4]

mpu_rwrights_master_access_control_t mpu_region_config_t::accessRights2[4]

struct mpu_config_t

This structure is used when calling the MPU_Init function.

### Data Fields

mpu_region_config_t | regionConfig
| region access permission. More...
struct _mpu_config * | next
| pointer to the next structure. More...

#### Field Documentation

mpu_region_config_t mpu_config_t::regionConfig

struct _mpu_config* mpu_config_t::next

## Macro Definition Documentation

#define FSL_MPU_DRIVER_VERSION (MAKE_VERSION(2, 1, 0))

#define MPU_REGION_RWXRIGHTS_MASTER_SHIFT | ( |  | n | ) | (n * 6)

#define MPU_REGION_RWXRIGHTS_MASTER_MASK | ( |  | n | ) | (0x1Fu << MPU_REGION_RWXRIGHTS_MASTER_SHIFT(n))

#define MPU_REGION_RWXRIGHTS_MASTER_WIDTH 5

#define MPU_REGION_RWXRIGHTS_MASTER | ( |  | n,
|  |  | x
| ) |  | (((uint32_t)(((uint32_t)(x)) << MPU_REGION_RWXRIGHTS_MASTER_SHIFT(n))) & MPU_REGION_RWXRIGHTS_MASTER_MASK(n))

#define MPU_REGION_RWXRIGHTS_MASTER_PE_SHIFT | ( |  | n | ) | (n * 6 + MPU_REGION_RWXRIGHTS_MASTER_WIDTH)

#define MPU_REGION_RWXRIGHTS_MASTER_PE_MASK | ( |  | n | ) | (0x1u << MPU_REGION_RWXRIGHTS_MASTER_PE_SHIFT(n))

#define MPU_REGION_RWXRIGHTS_MASTER_PE | ( |  | n,
|  |  | x
| ) |  | (((uint32_t)(((uint32_t)(x)) << MPU_REGION_RWXRIGHTS_MASTER_PE_SHIFT(n))) & MPU_REGION_RWXRIGHTS_MASTER_PE_MASK(n))

#define MPU_REGION_RWRIGHTS_MASTER_SHIFT | ( |  | n | ) | ((n - FSL_FEATURE_MPU_PRIVILEGED_RIGHTS_MASTER_COUNT) * 2 + 24)

#define MPU_REGION_RWRIGHTS_MASTER_MASK | ( |  | n | ) | (0x3u << MPU_REGION_RWRIGHTS_MASTER_SHIFT(n))

#define MPU_REGION_RWRIGHTS_MASTER | ( |  | n,
|  |  | x
| ) |  | (((uint32_t)(((uint32_t)(x)) << MPU_REGION_RWRIGHTS_MASTER_SHIFT(n))) & MPU_REGION_RWRIGHTS_MASTER_MASK(n))

## Enumeration Type Documentation

enum mpu_region_total_num_t

Enumerator
kMPU_8Regions | MPU supports 8 regions.
kMPU_12Regions | MPU supports 12 regions.
kMPU_16Regions | MPU supports 16 regions.

enum mpu_slave_t

Enumerator
kMPU_Slave0 | MPU slave port 0.
kMPU_Slave1 | MPU slave port 1.
kMPU_Slave2 | MPU slave port 2.
kMPU_Slave3 | MPU slave port 3.
kMPU_Slave4 | MPU slave port 4.

enum mpu_err_access_control_t

Enumerator
kMPU_NoRegionHit | No region hit error.
kMPU_NoneOverlappRegion | Access single region error.
kMPU_OverlappRegion | Access overlapping region error.

enum mpu_err_access_type_t

Enumerator
kMPU_ErrTypeRead | MPU error access type — read.
kMPU_ErrTypeWrite | MPU error access type — write.

enum mpu_err_attributes_t

Enumerator
kMPU_InstructionAccessInUserMode | Access instruction error in user mode.
kMPU_DataAccessInUserMode | Access data error in user mode.
kMPU_InstructionAccessInSupervisorMode | Access instruction error in supervisor mode.
kMPU_DataAccessInSupervisorMode | Access data error in supervisor mode.

enum mpu_supervisor_access_rights_t

Enumerator
kMPU_SupervisorReadWriteExecute | Read write and execute operations are allowed in supervisor mode.
kMPU_SupervisorReadExecute | Read and execute operations are allowed in supervisor mode.
kMPU_SupervisorReadWrite | Read write operations are allowed in supervisor mode.
kMPU_SupervisorEqualToUsermode | Access permission equal to user mode.

enum mpu_user_access_rights_t

Enumerator
kMPU_UserNoAccessRights | No access allowed in user mode.
kMPU_UserExecute | Execute operation is allowed in user mode.
kMPU_UserWrite | Write operation is allowed in user mode.
kMPU_UserWriteExecute | Write and execute operations are allowed in user mode.
kMPU_UserRead | Read is allowed in user mode.
kMPU_UserReadExecute | Read and execute operations are allowed in user mode.
kMPU_UserReadWrite | Read and write operations are allowed in user mode.
kMPU_UserReadWriteExecute | Read write and execute operations are allowed in user mode.

## Function Documentation

void MPU_Init | ( | MPU_Type * | base,
|  | const mpu_config_t * | config
| ) |  |

This function configures the MPU module with the user-defined configuration.

Parameters

  base | MPU peripheral base address.
  config | The pointer to the configuration structure.

void MPU_Deinit | ( | MPU_Type * | base | ) |

Parameters

  base | MPU peripheral base address.

static void MPU_Enable | ( | MPU_Type * | base,
|  | bool | enable
| ) |  |

inlinestatic

Call this API to enable or disable the MPU module.

Parameters

  base | MPU peripheral base address.
  enable | True enable MPU, false disable MPU.

static void MPU_RegionEnable | ( | MPU_Type * | base,
|  | uint32_t | number,
|  | bool | enable
| ) |  |

inlinestatic

When MPU is enabled, call this API to disable an unused region of an enabled MPU. Call this API to minimize the power dissipation.

Parameters

  base | MPU peripheral base address.
  number | MPU region number.
  enable | True enable the special region MPU, false disable the special region MPU.

void MPU_GetHardwareInfo | ( | MPU_Type * | base,
|  | mpu_hardware_info_t * | hardwareInform
| ) |  |

Parameters

  base | MPU peripheral base address.
  hardwareInform | The pointer to the MPU hardware information structure. See "mpu_hardware_info_t".

void MPU_SetRegionConfig | ( | MPU_Type * | base,
|  | const mpu_region_config_t * | regionConfig
| ) |  |

Note: Due to the MPU protection, the Region number 0 does not allow writes from core to affect the start and end address nor the permissions associated with the debugger. It can only write the permission fields associated with the other masters.

Parameters

  base | MPU peripheral base address.
  regionConfig | The pointer to the MPU user configuration structure. See "mpu_region_config_t".

void MPU_SetRegionAddr | ( | MPU_Type * | base,
|  | uint32_t | regionNum,
|  | uint32_t | startAddr,
|  | uint32_t | endAddr
| ) |  |

Memory region start address. Note: bit0 ~ bit4 is always marked as 0 by MPU. The actual start address by MPU is 0-modulo-32 byte address. Memory region end address. Note: bit0 ~ bit4 always be marked as 1 by MPU. The actual end address used by MPU is 31-modulo-32 byte address. Note: Due to the MPU protection, the startAddr and endAddr can't be changed by the core when regionNum is 0.

Parameters

  base | MPU peripheral base address.
  regionNum | MPU region number. The range is from 0 to FSL_FEATURE_MPU_DESCRIPTOR_COUNT - 1.
  startAddr | Region start address.
  endAddr | Region end address.

void MPU_SetRegionRwxMasterAccessRights | ( | MPU_Type * | base,
|  | uint32_t | regionNum,
|  | uint32_t | masterNum,
|  | const mpu_rwxrights_master_access_control_t * | accessRights
| ) |  |

The MPU access rights depend on two board classifications of bus masters. The privilege rights masters and the normal rights masters. The privilege rights masters have the read, write and execute access rights. So except the normal read and write rights, the execute rights is also allowed for these masters. The privilege rights masters are normally range from bus masters 0 - 3. However, the maximum master number is device-specific. See the "FSL_FEATURE_MPU_PRIVILEGED_RIGHTS_MASTER_MAX_INDEX". The normal rights masters access rights control see "MPU_SetRegionRwMasterAccessRights()".

Parameters

  base | MPU peripheral base address.
  regionNum | MPU region number. Should range from 0 to FSL_FEATURE_MPU_DESCRIPTOR_COUNT - 1.
  masterNum | MPU bus master number. Should range from 0 to FSL_FEATURE_MPU_PRIVILEGED_RIGHTS_MASTER_MAX_INDEX.
  accessRights | The pointer to the MPU access rights configuration. See "mpu_rwxrights_master_access_control_t".

void MPU_SetRegionRwMasterAccessRights | ( | MPU_Type * | base,
|  | uint32_t | regionNum,
|  | uint32_t | masterNum,
|  | const mpu_rwrights_master_access_control_t * | accessRights
| ) |  |

The MPU access rights depend on two board classifications of bus masters. The privilege rights masters and the normal rights masters. The normal rights masters only have the read and write access permissions. The privilege rights access control see "MPU_SetRegionRwxMasterAccessRights".

Parameters

  base | MPU peripheral base address.
  regionNum | MPU region number. The range is from 0 to FSL_FEATURE_MPU_DESCRIPTOR_COUNT - 1.
  masterNum | MPU bus master number. Should range from FSL_FEATURE_MPU_PRIVILEGED_RIGHTS_MASTER_COUNT to ~ FSL_FEATURE_MPU_MASTER_MAX_INDEX.
  accessRights | The pointer to the MPU access rights configuration. See "mpu_rwrights_master_access_control_t".

bool MPU_GetSlavePortErrorStatus | ( | MPU_Type * | base,
|  | mpu_slave_t | slaveNum
| ) |  |

Parameters

  base | MPU peripheral base address.
  slaveNum | MPU slave port number.

Returns
  The slave ports error status. true - error happens in this slave port. false - error didn't happen in this slave port.

void MPU_GetDetailErrorAccessInfo | ( | MPU_Type * | base,
|  | mpu_slave_t | slaveNum,
|  | mpu_access_err_info_t * | errInform
| ) |  |

Parameters

  base | MPU peripheral base address.
  slaveNum | MPU slave port number.
  errInform | The pointer to the MPU access error information. See "mpu_access_err_info_t".