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 
21 #ifndef MPU_H
22 #define MPU_H
23 
24 #include <stdbool.h>
25 #include <stdint.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
34 #define MPU_NUM_REGIONS ( (MPU->TYPE & MPU_TYPE_DREGION_Msk) >> MPU_TYPE_DREGION_Pos )
35 
39 enum {
40  AP_NO_NO = 0,
41  AP_RW_NO = 1,
42  AP_RW_RO = 2,
43  AP_RW_RW = 3,
44  AP_RO_NO = 5,
45  AP_RO_RO = 6,
46 };
47 
51 enum {
58  MPU_SIZE_2K = 10,
59  MPU_SIZE_4K = 11,
60  MPU_SIZE_8K = 12,
61  MPU_SIZE_16K = 13,
62  MPU_SIZE_32K = 14,
63  MPU_SIZE_64K = 15,
67  MPU_SIZE_1M = 19,
68  MPU_SIZE_2M = 20,
69  MPU_SIZE_4M = 21,
70  MPU_SIZE_8M = 22,
71  MPU_SIZE_16M = 23,
72  MPU_SIZE_32M = 24,
73  MPU_SIZE_64M = 25,
77  MPU_SIZE_1G = 29,
78  MPU_SIZE_2G = 30,
79  MPU_SIZE_4G = 31,
80 };
81 
89 #define MPU_SIZE_TO_BYTES(size) ( (uintptr_t)1 << ((size) + 1) )
90 
104 static inline uint32_t MPU_ATTR(
105  uint32_t xn,
106  uint32_t ap,
107  uint32_t tex,
108  uint32_t c,
109  uint32_t b,
110  uint32_t s,
111  uint32_t size)
112 {
113  return
114  (xn << 28) |
115  (ap << 24) |
116  (tex << 19) |
117  (s << 18) |
118  (c << 17) |
119  (b << 16) |
120  (size << 1);
121 }
122 
129 int mpu_disable(void);
130 
137 int mpu_enable(void);
138 
145 bool mpu_enabled(void);
146 
157 int mpu_configure(uint_fast8_t region, uintptr_t base, uint_fast32_t attr);
158 
159 #ifdef __cplusplus
160 }
161 #endif
162 
163 #endif /* MPU_H */
MPU_SIZE_4G
@ MPU_SIZE_4G
4 gigabytes
Definition: mpu.h:79
AP_NO_NO
@ AP_NO_NO
no access for all levels
Definition: mpu.h:40
MPU_SIZE_4M
@ MPU_SIZE_4M
4 megabytes
Definition: mpu.h:69
mpu_disable
int mpu_disable(void)
disable the MPU
MPU_SIZE_1G
@ MPU_SIZE_1G
1 gigabytes
Definition: mpu.h:77
MPU_SIZE_1K
@ MPU_SIZE_1K
1 kilobytes
Definition: mpu.h:57
MPU_SIZE_8K
@ MPU_SIZE_8K
8 kilobytes
Definition: mpu.h:60
MPU_SIZE_64M
@ MPU_SIZE_64M
64 megabytes
Definition: mpu.h:73
MPU_SIZE_1M
@ MPU_SIZE_1M
1 megabytes
Definition: mpu.h:67
MPU_SIZE_8M
@ MPU_SIZE_8M
8 megabytes
Definition: mpu.h:70
MPU_SIZE_32K
@ MPU_SIZE_32K
32 kilobytes
Definition: mpu.h:62
MPU_SIZE_128M
@ MPU_SIZE_128M
128 megabytes
Definition: mpu.h:74
AP_RW_RW
@ AP_RW_RW
read/write for all levels
Definition: mpu.h:43
MPU_SIZE_128K
@ MPU_SIZE_128K
128 kilobytes
Definition: mpu.h:64
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
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)
generate an MPU attribute word suitable for writing to the RASR register
Definition: mpu.h:104
MPU_SIZE_32M
@ MPU_SIZE_32M
32 megabytes
Definition: mpu.h:72
MPU_SIZE_512K
@ MPU_SIZE_512K
512 kilobytes
Definition: mpu.h:66
MPU_SIZE_512M
@ MPU_SIZE_512M
512 megabytes
Definition: mpu.h:76
mpu_enabled
bool mpu_enabled(void)
test if the MPU is enabled
MPU_SIZE_256K
@ MPU_SIZE_256K
256 kilobytes
Definition: mpu.h:65
MPU_SIZE_32B
@ MPU_SIZE_32B
32 bytes
Definition: mpu.h:52
MPU_SIZE_16K
@ MPU_SIZE_16K
16 kilobytes
Definition: mpu.h:61
MPU_SIZE_512B
@ MPU_SIZE_512B
512 bytes
Definition: mpu.h:56
AP_RW_NO
@ AP_RW_NO
read/write for privileged level, no access from user level
Definition: mpu.h:41
MPU_SIZE_64B
@ MPU_SIZE_64B
64 bytes
Definition: mpu.h:53
MPU_SIZE_16M
@ MPU_SIZE_16M
16 megabytes
Definition: mpu.h:71
MPU_SIZE_64K
@ MPU_SIZE_64K
64 kilobytes
Definition: mpu.h:63
AP_RO_NO
@ AP_RO_NO
read-only for privileged level, no access from user level
Definition: mpu.h:44
MPU_SIZE_256B
@ MPU_SIZE_256B
256 bytes
Definition: mpu.h:55
MPU_SIZE_128B
@ MPU_SIZE_128B
128 bytes
Definition: mpu.h:54
MPU_SIZE_4K
@ MPU_SIZE_4K
4 kilobytes
Definition: mpu.h:59
MPU_SIZE_2K
@ MPU_SIZE_2K
2 kilobytes
Definition: mpu.h:58
MPU_SIZE_256M
@ MPU_SIZE_256M
256 megabytes
Definition: mpu.h:75
MPU_SIZE_2M
@ MPU_SIZE_2M
2 megabytes
Definition: mpu.h:68
AP_RO_RO
@ AP_RO_RO
read-only for all levels
Definition: mpu.h:45
AP_RW_RO
@ AP_RW_RO
read/write for privileged level, read-only for user level
Definition: mpu.h:42
mpu_enable
int mpu_enable(void)
enable the MPU
MPU_SIZE_2G
@ MPU_SIZE_2G
2 gigabytes
Definition: mpu.h:78