mpu.h File Reference

Cortex-M Memory Protection Unit (MPU) Driver Header File. More...

Detailed Description

Cortex-M Memory Protection Unit (MPU) Driver Header File.

Author
Ian Martin ian@l.nosp@m.ocic.nosp@m.ontro.nosp@m.ls.c.nosp@m.om

Definition in file mpu.h.

#include <stdbool.h>
#include <stdint.h>
+ Include dependency graph for mpu.h:

Go to the source code of this file.

Macros

#define MPU_NUM_REGIONS   ( (MPU->TYPE & MPU_TYPE_DREGION_Msk) >> MPU_TYPE_DREGION_Pos )
 Number of MPU regions available (will vary depending on the Cortex-M version)
 
#define MPU_SIZE_TO_BYTES(size)   ( (uintptr_t)1 << ((size) + 1) )
 convert a region size code to a size in bytes More...
 

Enumerations

enum  {
  AP_NO_NO = 0, AP_RW_NO = 1, AP_RW_RO = 2, AP_RW_RW = 3,
  AP_RO_NO = 5, AP_RO_RO = 6
}
 Access Permission words. More...
 
enum  {
  MPU_SIZE_32B = 4, MPU_SIZE_64B = 5, MPU_SIZE_128B = 6, MPU_SIZE_256B = 7,
  MPU_SIZE_512B = 8, MPU_SIZE_1K = 9, MPU_SIZE_2K = 10, MPU_SIZE_4K = 11,
  MPU_SIZE_8K = 12, MPU_SIZE_16K = 13, MPU_SIZE_32K = 14, MPU_SIZE_64K = 15,
  MPU_SIZE_128K = 16, MPU_SIZE_256K = 17, MPU_SIZE_512K = 18, MPU_SIZE_1M = 19,
  MPU_SIZE_2M = 20, MPU_SIZE_4M = 21, MPU_SIZE_8M = 22, MPU_SIZE_16M = 23,
  MPU_SIZE_32M = 24, MPU_SIZE_64M = 25, MPU_SIZE_128M = 26, MPU_SIZE_256M = 27,
  MPU_SIZE_512M = 28, MPU_SIZE_1G = 29, MPU_SIZE_2G = 30, MPU_SIZE_4G = 31
}
 MPU region sizes. More...
 

Functions

static uint32_t MPU_ATTR (uint32_t xn, uint32_t ap, uint32_t tex, uint32_t c, uint32_t b, uint32_t s, uint32_t size)
 generate an MPU attribute word suitable for writing to the RASR register More...
 
int mpu_disable (void)
 disable the MPU More...
 
int mpu_enable (void)
 enable the MPU More...
 
bool mpu_enabled (void)
 test if the MPU is enabled More...
 
int mpu_configure (uint_fast8_t region, uintptr_t base, uint_fast32_t attr)
 configure the base address and attributes for an MPU region More...
 

Macro Definition Documentation

◆ MPU_SIZE_TO_BYTES

#define MPU_SIZE_TO_BYTES (   size)    ( (uintptr_t)1 << ((size) + 1) )

convert a region size code to a size in bytes

Parameters
[in]sizeregion size code, e.g. MPU_SIZE_32B
Returns
region size in bytes

Definition at line 89 of file mpu.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

Access Permission words.

Enumerator
AP_NO_NO 

no access for all levels

AP_RW_NO 

read/write for privileged level, no access from user level

AP_RW_RO 

read/write for privileged level, read-only for user level

AP_RW_RW 

read/write for all levels

AP_RO_NO 

read-only for privileged level, no access from user level

AP_RO_RO 

read-only for all levels

Definition at line 39 of file mpu.h.

◆ anonymous enum

anonymous enum

MPU region sizes.

Enumerator
MPU_SIZE_32B 

32 bytes

MPU_SIZE_64B 

64 bytes

MPU_SIZE_128B 

128 bytes

MPU_SIZE_256B 

256 bytes

MPU_SIZE_512B 

512 bytes

MPU_SIZE_1K 

1 kilobytes

MPU_SIZE_2K 

2 kilobytes

MPU_SIZE_4K 

4 kilobytes

MPU_SIZE_8K 

8 kilobytes

MPU_SIZE_16K 

16 kilobytes

MPU_SIZE_32K 

32 kilobytes

MPU_SIZE_64K 

64 kilobytes

MPU_SIZE_128K 

128 kilobytes

MPU_SIZE_256K 

256 kilobytes

MPU_SIZE_512K 

512 kilobytes

MPU_SIZE_1M 

1 megabytes

MPU_SIZE_2M 

2 megabytes

MPU_SIZE_4M 

4 megabytes

MPU_SIZE_8M 

8 megabytes

MPU_SIZE_16M 

16 megabytes

MPU_SIZE_32M 

32 megabytes

MPU_SIZE_64M 

64 megabytes

MPU_SIZE_128M 

128 megabytes

MPU_SIZE_256M 

256 megabytes

MPU_SIZE_512M 

512 megabytes

MPU_SIZE_1G 

1 gigabytes

MPU_SIZE_2G 

2 gigabytes

MPU_SIZE_4G 

4 gigabytes

Definition at line 51 of file mpu.h.

Function Documentation

◆ MPU_ATTR()

static uint32_t MPU_ATTR ( uint32_t  xn,
uint32_t  ap,
uint32_t  tex,
uint32_t  c,
uint32_t  b,
uint32_t  s,
uint32_t  size 
)
inlinestatic

generate an MPU attribute word suitable for writing to the RASR register

Parameters
[in]xneXecute Never flag (forbids instruction fetches)
[in]apAccess Permission word, e.g. AP_RO_RO
[in]texType Extension Field
[in]cCacheable bit
[in]bBufferable bit
[in]sSub-Region Disable (SRD) field
[in]sizeregion size code, e.g. MPU_SIZE_32B
Returns
combined region attribute word

Definition at line 104 of file mpu.h.

◆ mpu_configure()

int mpu_configure ( uint_fast8_t  region,
uintptr_t  base,
uint_fast32_t  attr 
)

configure the base address and attributes for an MPU region

Parameters
[in]regionMPU region to configure (0 <= region < MPU_NUM_REGIONS)
[in]basebase address in RAM (aligned to the size specified within attr)
[in]attrattribute word generated by MPU_ATTR()
Returns
0 on success
<0 on failure or no MPU present

◆ mpu_disable()

int mpu_disable ( void  )

disable the MPU

Returns
0 on success
<0 on failure or no MPU present

◆ mpu_enable()

int mpu_enable ( void  )

enable the MPU

Returns
0 on success
<0 on failure or no MPU present

◆ mpu_enabled()

bool mpu_enabled ( void  )

test if the MPU is enabled

Returns
true if enabled
false if disabled