EFM32 Happy Gecko Software Documentation  efm32hg-doc-5.1.2
em_mpu.h
Go to the documentation of this file.
1 /***************************************************************************/
33 #ifndef EM_MPU_H
34 #define EM_MPU_H
35 
36 #include "em_device.h"
37 
38 #if defined(__MPU_PRESENT) && (__MPU_PRESENT == 1)
39 #include "em_assert.h"
40 
41 #include <stdbool.h>
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 /***************************************************************************/
52 /***************************************************************************/
60 #define MPU_CTRL_PRIVDEFENA MPU_CTRL_PRIVDEFENA_Msk
61 
65 #define MPU_CTRL_HFNMIENA MPU_CTRL_HFNMIENA_Msk
66 
67 /*******************************************************************************
68  ******************************** ENUMS ************************************
69  ******************************************************************************/
70 
74 typedef enum
75 {
76  mpuRegionSize32b = 4,
77  mpuRegionSize64b = 5,
78  mpuRegionSize128b = 6,
79  mpuRegionSize256b = 7,
80  mpuRegionSize512b = 8,
81  mpuRegionSize1Kb = 9,
82  mpuRegionSize2Kb = 10,
83  mpuRegionSize4Kb = 11,
84  mpuRegionSize8Kb = 12,
85  mpuRegionSize16Kb = 13,
86  mpuRegionSize32Kb = 14,
87  mpuRegionSize64Kb = 15,
88  mpuRegionSize128Kb = 16,
89  mpuRegionSize256Kb = 17,
90  mpuRegionSize512Kb = 18,
91  mpuRegionSize1Mb = 19,
92  mpuRegionSize2Mb = 20,
93  mpuRegionSize4Mb = 21,
94  mpuRegionSize8Mb = 22,
95  mpuRegionSize16Mb = 23,
96  mpuRegionSize32Mb = 24,
97  mpuRegionSize64Mb = 25,
98  mpuRegionSize128Mb = 26,
99  mpuRegionSize256Mb = 27,
100  mpuRegionSize512Mb = 28,
101  mpuRegionSize1Gb = 29,
102  mpuRegionSize2Gb = 30,
103  mpuRegionSize4Gb = 31
104 } MPU_RegionSize_TypeDef;
105 
109 typedef enum
110 {
111  mpuRegionNoAccess = 0,
112  mpuRegionApPRw = 1,
113  mpuRegionApPRwURo = 2,
114  mpuRegionApFullAccess = 3,
115  mpuRegionApPRo = 5,
116  mpuRegionApPRo_URo = 6
117 } MPU_RegionAp_TypeDef;
118 
119 
120 /*******************************************************************************
121  ******************************* STRUCTS ***********************************
122  ******************************************************************************/
123 
125 typedef struct
126 {
127  bool regionEnable;
128  uint8_t regionNo;
129  uint32_t baseAddress;
130  MPU_RegionSize_TypeDef size;
131  MPU_RegionAp_TypeDef accessPermission;
132  bool disableExec;
133  bool shareable;
134  bool cacheable;
135  bool bufferable;
136  uint8_t srd;
137  uint8_t tex;
138 } MPU_RegionInit_TypeDef;
139 
141 #define MPU_INIT_FLASH_DEFAULT \
142 { \
143  true, /* Enable MPU region. */ \
144  0, /* MPU Region number. */ \
145  FLASH_MEM_BASE, /* Flash base address. */ \
146  mpuRegionSize1Mb, /* Size - Set to max. */ \
147  mpuRegionApFullAccess, /* Access permissions. */ \
148  false, /* Execution allowed. */ \
149  false, /* Not shareable. */ \
150  true, /* Cacheable. */ \
151  false, /* Not bufferable. */ \
152  0, /* No subregions. */ \
153  0 /* No TEX attributes. */ \
154 }
155 
156 
158 #define MPU_INIT_SRAM_DEFAULT \
159 { \
160  true, /* Enable MPU region. */ \
161  1, /* MPU Region number. */ \
162  RAM_MEM_BASE, /* SRAM base address. */ \
163  mpuRegionSize128Kb, /* Size - Set to max. */ \
164  mpuRegionApFullAccess, /* Access permissions. */ \
165  false, /* Execution allowed. */ \
166  true, /* Shareable. */ \
167  true, /* Cacheable. */ \
168  false, /* Not bufferable. */ \
169  0, /* No subregions. */ \
170  0 /* No TEX attributes. */ \
171 }
172 
173 
175 #define MPU_INIT_PERIPHERAL_DEFAULT \
176 { \
177  true, /* Enable MPU region. */ \
178  0, /* MPU Region number. */ \
179  0, /* Region base address. */ \
180  mpuRegionSize32b, /* Size - Set to minimum */ \
181  mpuRegionApFullAccess, /* Access permissions. */ \
182  true, /* Execution not allowed. */ \
183  true, /* Shareable. */ \
184  false, /* Not cacheable. */ \
185  true, /* Bufferable. */ \
186  0, /* No subregions. */ \
187  0 /* No TEX attributes. */ \
188 }
189 
190 
191 /*******************************************************************************
192  ***************************** PROTOTYPES **********************************
193  ******************************************************************************/
194 
195 
196 void MPU_ConfigureRegion(const MPU_RegionInit_TypeDef *init);
197 
198 
199 /***************************************************************************/
205 __STATIC_INLINE void MPU_Disable(void)
206 {
207  SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk; /* Disable fault exceptions */
208  MPU->CTRL &= ~MPU_CTRL_ENABLE_Msk; /* Disable the MPU */
209 }
210 
211 
212 /***************************************************************************/
221 __STATIC_INLINE void MPU_Enable(uint32_t flags)
222 {
223  EFM_ASSERT(!(flags & ~(MPU_CTRL_PRIVDEFENA_Msk
224  | MPU_CTRL_HFNMIENA_Msk
225  | MPU_CTRL_ENABLE_Msk)));
226 
227  MPU->CTRL = flags | MPU_CTRL_ENABLE_Msk; /* Enable the MPU */
228  SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk; /* Enable fault exceptions */
229 }
230 
231 
235 #ifdef __cplusplus
236 }
237 #endif
238 
239 #endif /* defined(__MPU_PRESENT) && (__MPU_PRESENT == 1) */
240 
241 #endif /* EM_MPU_H */
Emlib peripheral API "assert" implementation.
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.