mpu.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Loci Controls Inc.
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
9 #pragma once
10 
23 #include <stdbool.h>
24 #include <stdint.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
33 #define MPU_NUM_REGIONS ( (MPU->TYPE & MPU_TYPE_DREGION_Msk) >> MPU_TYPE_DREGION_Pos )
34 
38 enum {
39  AP_NO_NO = 0,
40  AP_RW_NO = 1,
41  AP_RW_RO = 2,
42  AP_RW_RW = 3,
43  AP_RO_NO = 5,
44  AP_RO_RO = 6,
45 };
46 
50 enum {
57  MPU_SIZE_2K = 10,
58  MPU_SIZE_4K = 11,
59  MPU_SIZE_8K = 12,
60  MPU_SIZE_16K = 13,
61  MPU_SIZE_32K = 14,
62  MPU_SIZE_64K = 15,
66  MPU_SIZE_1M = 19,
67  MPU_SIZE_2M = 20,
68  MPU_SIZE_4M = 21,
69  MPU_SIZE_8M = 22,
70  MPU_SIZE_16M = 23,
71  MPU_SIZE_32M = 24,
72  MPU_SIZE_64M = 25,
76  MPU_SIZE_1G = 29,
77  MPU_SIZE_2G = 30,
78  MPU_SIZE_4G = 31,
79 };
80 
88 #define MPU_SIZE_TO_BYTES(size) ( (uintptr_t)1 << ((size) + 1) )
89 
103 static inline uint32_t MPU_ATTR(
104  uint32_t xn,
105  uint32_t ap,
106  uint32_t tex,
107  uint32_t c,
108  uint32_t b,
109  uint32_t s,
110  uint32_t size)
111 {
112  return
113  (xn << 28) |
114  (ap << 24) |
115  (tex << 19) |
116  (s << 18) |
117  (c << 17) |
118  (b << 16) |
119  (size << 1);
120 }
121 
128 int mpu_disable(void);
129 
136 int mpu_enable(void);
137 
144 bool mpu_enabled(void);
145 
156 int mpu_configure(uint_fast8_t region, uintptr_t base, uint_fast32_t attr);
157 
158 #ifdef __cplusplus
159 }
160 #endif
int mpu_configure(uint_fast8_t region, uintptr_t base, uint_fast32_t attr)
configure the base address and attributes for an MPU region
bool mpu_enabled(void)
test if the MPU is enabled
@ AP_NO_NO
no access for all levels
Definition: mpu.h:39
@ AP_RW_NO
read/write for privileged level, no access from user level
Definition: mpu.h:40
@ AP_RW_RW
read/write for all levels
Definition: mpu.h:42
@ AP_RO_NO
read-only for privileged level, no access from user level
Definition: mpu.h:43
@ AP_RW_RO
read/write for privileged level, read-only for user level
Definition: mpu.h:41
@ AP_RO_RO
read-only for all levels
Definition: mpu.h:44
int mpu_disable(void)
disable the MPU
int mpu_enable(void)
enable the MPU
@ MPU_SIZE_256K
256 kilobytes
Definition: mpu.h:64
@ MPU_SIZE_512K
512 kilobytes
Definition: mpu.h:65
@ MPU_SIZE_2M
2 megabytes
Definition: mpu.h:67
@ MPU_SIZE_4M
4 megabytes
Definition: mpu.h:68
@ MPU_SIZE_64K
64 kilobytes
Definition: mpu.h:62
@ MPU_SIZE_2K
2 kilobytes
Definition: mpu.h:57
@ MPU_SIZE_32M
32 megabytes
Definition: mpu.h:71
@ MPU_SIZE_2G
2 gigabytes
Definition: mpu.h:77
@ MPU_SIZE_256B
256 bytes
Definition: mpu.h:54
@ MPU_SIZE_64B
64 bytes
Definition: mpu.h:52
@ MPU_SIZE_8M
8 megabytes
Definition: mpu.h:69
@ MPU_SIZE_4G
4 gigabytes
Definition: mpu.h:78
@ MPU_SIZE_1G
1 gigabytes
Definition: mpu.h:76
@ MPU_SIZE_128B
128 bytes
Definition: mpu.h:53
@ MPU_SIZE_1M
1 megabytes
Definition: mpu.h:66
@ MPU_SIZE_16K
16 kilobytes
Definition: mpu.h:60
@ MPU_SIZE_32B
32 bytes
Definition: mpu.h:51
@ MPU_SIZE_16M
16 megabytes
Definition: mpu.h:70
@ MPU_SIZE_32K
32 kilobytes
Definition: mpu.h:61
@ MPU_SIZE_4K
4 kilobytes
Definition: mpu.h:58
@ MPU_SIZE_128K
128 kilobytes
Definition: mpu.h:63
@ MPU_SIZE_256M
256 megabytes
Definition: mpu.h:74
@ MPU_SIZE_128M
128 megabytes
Definition: mpu.h:73
@ MPU_SIZE_1K
1 kilobytes
Definition: mpu.h:56
@ MPU_SIZE_64M
64 megabytes
Definition: mpu.h:72
@ MPU_SIZE_512B
512 bytes
Definition: mpu.h:55
@ MPU_SIZE_8K
8 kilobytes
Definition: mpu.h:59
@ MPU_SIZE_512M
512 megabytes
Definition: mpu.h:75
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
Definition: mpu.h:103