| Kinetis SDK v.2.0 API Reference Manual Rev. 0 Freescale Semiconductor, Inc.

- Main Page
- Related Pages
- API Reference

All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
Files | Data Structures | Macros | Enumerations
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_low_masters_access_rights_t mpuLowAccessRights =
{
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_high_masters_access_rights_t mpuHighAccessRights =
{
false,
false,
false,
false,
false,
false,
false,
false
};
// Defines the MPU region configuration structure. //
mpu_region_config_t mpuRegionConfig =
{
kMPU_RegionNum00,
0x0,
0xffffffff,
mpuLowAccessRights,
mpuHighAccessRights,
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.

## Files
file | fsl_mpu.h

## 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_low_masters_access_rights_t
| MPU access rights for low master master port 0 ~ port 3. More...
struct | mpu_high_masters_access_rights_t
| MPU access rights mode for high master port 4 ~ port 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_WORD_LOW_MASTER_SHIFT(n) (n * 6)
| MPU low master bit shift. More...
#define | MPU_WORD_LOW_MASTER_MASK(n) (0x1Fu << MPU_WORD_LOW_MASTER_SHIFT(n))
| MPU low master bit mask. More...
#define | MPU_WORD_LOW_MASTER_WIDTH 5
| MPU low master bit width. More...
#define | MPU_WORD_LOW_MASTER(n, x) (((uint32_t)(((uint32_t)(x)) << MPU_WORD_LOW_MASTER_SHIFT(n))) & MPU_WORD_LOW_MASTER_MASK(n))
| MPU low master priority setting. More...
#define | MPU_LOW_MASTER_PE_SHIFT(n) (n * 6 + 5)
| MPU low master process enable bit shift. More...
#define | MPU_LOW_MASTER_PE_MASK(n) (0x1u << MPU_LOW_MASTER_PE_SHIFT(n))
| MPU low master process enable bit mask. More...
#define | MPU_WORD_MASTER_PE_WIDTH 1
| MPU low master process enable width. More...
#define | MPU_WORD_MASTER_PE(n, x) (((uint32_t)(((uint32_t)(x)) << MPU_LOW_MASTER_PE_SHIFT(n))) & MPU_LOW_MASTER_PE_MASK(n))
| MPU low master process enable setting. More...
#define | MPU_WORD_HIGH_MASTER_SHIFT(n) (n * 2 + 24)
| MPU high master bit shift. More...
#define | MPU_WORD_HIGH_MASTER_MASK(n) (0x03u << MPU_WORD_HIGH_MASTER_SHIFT(n))
| MPU high master bit mask. More...
#define | MPU_WORD_HIGH_MASTER_WIDTH 2
| MPU high master bit width. More...
#define | MPU_WORD_HIGH_MASTER(n, x) (((uint32_t)(((uint32_t)(x)) << MPU_WORD_HIGH_MASTER_SHIFT(n))) & MPU_WORD_HIGH_MASTER_MASK(n))
| MPU high master priority setting. More...

## Enumerations
enum | mpu_region_num_t
| MPU region number. More...
enum | mpu_master_t
| MPU master number. More...
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 master port 0 ~ port 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 master port 0 ~ port 3. More...

## Driver version
#define | FSL_MPU_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
| MPU driver version 2.0.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, mpu_region_num_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, mpu_region_num_t regionNum, uint32_t startAddr, uint32_t endAddr)
| Sets the region start and end address. More...
void | MPU_SetRegionLowMasterAccessRights (MPU_Type *base, mpu_region_num_t regionNum, mpu_master_t masterNum, const mpu_low_masters_access_rights_t *accessRights)
| Sets the MPU region access rights for low master port 0 ~ port 3. More...
void | MPU_SetRegionHighMasterAccessRights (MPU_Type *base, mpu_region_num_t regionNum, mpu_master_t masterNum, const mpu_high_masters_access_rights_t *accessRights)
| Sets the MPU region access rights for high master port 4 ~ port 7. 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
mpu_master_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

mpu_master_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_low_masters_access_rights_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_low_masters_access_rights_t::superAccessRights

mpu_user_access_rights_t mpu_low_masters_access_rights_t::userAccessRights

struct mpu_high_masters_access_rights_t

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

#### Field Documentation

bool mpu_high_masters_access_rights_t::writeEnable

bool mpu_high_masters_access_rights_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 four low master numbers: master 0 ~ master 3. The accessRights2[0] ~ accessRights2[3] are used to configure the four high master numbers: master 4 ~ master 7. 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 kMPU_RegionNum00.

### Data Fields
mpu_region_num_t | regionNum
| MPU region number. More...
uint32_t | startAddress
| Memory region start address. More...
uint32_t | endAddress
| Memory region end address. More...
mpu_low_masters_access_rights_t | accessRights1 [4]
| Low masters access permission. More...
mpu_high_masters_access_rights_t | accessRights2 [4]
| High masters access permission. More...

#### Field Documentation

mpu_region_num_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_low_masters_access_rights_t mpu_region_config_t::accessRights1[4]

mpu_high_masters_access_rights_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, 0, 0))

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

#define MPU_WORD_LOW_MASTER_MASK | ( |  | n | ) | (0x1Fu << MPU_WORD_LOW_MASTER_SHIFT(n))

#define MPU_WORD_LOW_MASTER_WIDTH 5

#define MPU_WORD_LOW_MASTER | ( |  | n,
|  |  | x
| ) |  | (((uint32_t)(((uint32_t)(x)) << MPU_WORD_LOW_MASTER_SHIFT(n))) & MPU_WORD_LOW_MASTER_MASK(n))

#define MPU_LOW_MASTER_PE_SHIFT | ( |  | n | ) | (n * 6 + 5)

#define MPU_LOW_MASTER_PE_MASK | ( |  | n | ) | (0x1u << MPU_LOW_MASTER_PE_SHIFT(n))

#define MPU_WORD_MASTER_PE_WIDTH 1

#define MPU_WORD_MASTER_PE | ( |  | n,
|  |  | x
| ) |  | (((uint32_t)(((uint32_t)(x)) << MPU_LOW_MASTER_PE_SHIFT(n))) & MPU_LOW_MASTER_PE_MASK(n))

#define MPU_WORD_HIGH_MASTER_SHIFT | ( |  | n | ) | (n * 2 + 24)

#define MPU_WORD_HIGH_MASTER_MASK | ( |  | n | ) | (0x03u << MPU_WORD_HIGH_MASTER_SHIFT(n))

#define MPU_WORD_HIGH_MASTER_WIDTH 2

#define MPU_WORD_HIGH_MASTER | ( |  | n,
|  |  | x
| ) |  | (((uint32_t)(((uint32_t)(x)) << MPU_WORD_HIGH_MASTER_SHIFT(n))) & MPU_WORD_HIGH_MASTER_MASK(n))

## Enumeration Type Documentation

enum mpu_region_num_t

enum mpu_master_t

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
| ) |  |
| inline static

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,
|  | mpu_region_num_t | number,
|  | bool | enable
| ) |  |
| inline static

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 kMPU_RegionNum00 does not allow writes from the 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,
|  | mpu_region_num_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 "kMPU_RegionNum00".

Parameters

  base | MPU peripheral base address.
  regionNum | MPU region number.
  startAddr | Region start address.
  endAddr | Region end address.

void MPU_SetRegionLowMasterAccessRights | ( | MPU_Type * | base,
|  | mpu_region_num_t | regionNum,
|  | mpu_master_t | masterNum,
|  | const mpu_low_masters_access_rights_t * | accessRights
| ) |  |

This can be used to change the region access rights for any master port for any region.

Parameters

  base | MPU peripheral base address.
  regionNum | MPU region number.
  masterNum | MPU master number. Should range from kMPU_Master0 ~ kMPU_Master3.
  accessRights | The pointer to the MPU access rights configuration. See "mpu_low_masters_access_rights_t".

void MPU_SetRegionHighMasterAccessRights | ( | MPU_Type * | base,
|  | mpu_region_num_t | regionNum,
|  | mpu_master_t | masterNum,
|  | const mpu_high_masters_access_rights_t * | accessRights
| ) |  |

This can be used to change the region access rights for any master port for any region.

Parameters

  base | MPU peripheral base address.
  regionNum | MPU region number.
  masterNum | MPU master number. Should range from kMPU_Master4 ~ kMPU_Master7.
  accessRights | The pointer to the MPU access rights configuration. See "mpu_high_masters_access_rights_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".