]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MPU_STM32L4_Discovery_GCC_IAR_Keil/ST_Code/Drivers/CMSIS/Include/mpu_armv8.h
Rename STM32Cube to GCC for STM32L4 Discovery projects as GCC is
[freertos] / FreeRTOS / Demo / CORTEX_MPU_STM32L4_Discovery_GCC_IAR_Keil / ST_Code / Drivers / CMSIS / Include / mpu_armv8.h
1 /******************************************************************************\r
2  * @file     mpu_armv8.h\r
3  * @brief    CMSIS MPU API for Armv8-M MPU\r
4  * @version  V5.0.4\r
5  * @date     10. January 2018\r
6  ******************************************************************************/\r
7 /*\r
8  * Copyright (c) 2017-2018 Arm Limited. All rights reserved.\r
9  *\r
10  * SPDX-License-Identifier: Apache-2.0\r
11  *\r
12  * Licensed under the Apache License, Version 2.0 (the License); you may\r
13  * not use this file except in compliance with the License.\r
14  * You may obtain a copy of the License at\r
15  *\r
16  * www.apache.org/licenses/LICENSE-2.0\r
17  *\r
18  * Unless required by applicable law or agreed to in writing, software\r
19  * distributed under the License is distributed on an AS IS BASIS, WITHOUT\r
20  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
21  * See the License for the specific language governing permissions and\r
22  * limitations under the License.\r
23  */\r
24 \r
25 #if   defined ( __ICCARM__ )\r
26   #pragma system_include         /* treat file as system include file for MISRA check */\r
27 #elif defined (__clang__)\r
28   #pragma clang system_header    /* treat file as system include file */\r
29 #endif\r
30 \r
31 #ifndef ARM_MPU_ARMV8_H\r
32 #define ARM_MPU_ARMV8_H\r
33 \r
34 /** \brief Attribute for device memory (outer only) */\r
35 #define ARM_MPU_ATTR_DEVICE                           ( 0U )\r
36 \r
37 /** \brief Attribute for non-cacheable, normal memory */\r
38 #define ARM_MPU_ATTR_NON_CACHEABLE                    ( 4U )\r
39 \r
40 /** \brief Attribute for normal memory (outer and inner)\r
41 * \param NT Non-Transient: Set to 1 for non-transient data.\r
42 * \param WB Write-Back: Set to 1 to use write-back update policy.\r
43 * \param RA Read Allocation: Set to 1 to use cache allocation on read miss.\r
44 * \param WA Write Allocation: Set to 1 to use cache allocation on write miss.\r
45 */\r
46 #define ARM_MPU_ATTR_MEMORY_(NT, WB, RA, WA) \\r
47   (((NT & 1U) << 3U) | ((WB & 1U) << 2U) | ((RA & 1U) << 1U) | (WA & 1U))\r
48 \r
49 /** \brief Device memory type non Gathering, non Re-ordering, non Early Write Acknowledgement */\r
50 #define ARM_MPU_ATTR_DEVICE_nGnRnE (0U)\r
51 \r
52 /** \brief Device memory type non Gathering, non Re-ordering, Early Write Acknowledgement */\r
53 #define ARM_MPU_ATTR_DEVICE_nGnRE  (1U)\r
54 \r
55 /** \brief Device memory type non Gathering, Re-ordering, Early Write Acknowledgement */\r
56 #define ARM_MPU_ATTR_DEVICE_nGRE   (2U)\r
57 \r
58 /** \brief Device memory type Gathering, Re-ordering, Early Write Acknowledgement */\r
59 #define ARM_MPU_ATTR_DEVICE_GRE    (3U)\r
60 \r
61 /** \brief Memory Attribute\r
62 * \param O Outer memory attributes\r
63 * \param I O == ARM_MPU_ATTR_DEVICE: Device memory attributes, else: Inner memory attributes\r
64 */\r
65 #define ARM_MPU_ATTR(O, I) (((O & 0xFU) << 4U) | (((O & 0xFU) != 0U) ? (I & 0xFU) : ((I & 0x3U) << 2U)))\r
66 \r
67 /** \brief Normal memory non-shareable  */\r
68 #define ARM_MPU_SH_NON   (0U)\r
69 \r
70 /** \brief Normal memory outer shareable  */\r
71 #define ARM_MPU_SH_OUTER (2U)\r
72 \r
73 /** \brief Normal memory inner shareable  */\r
74 #define ARM_MPU_SH_INNER (3U)\r
75 \r
76 /** \brief Memory access permissions\r
77 * \param RO Read-Only: Set to 1 for read-only memory.\r
78 * \param NP Non-Privileged: Set to 1 for non-privileged memory.\r
79 */\r
80 #define ARM_MPU_AP_(RO, NP) (((RO & 1U) << 1U) | (NP & 1U))\r
81 \r
82 /** \brief Region Base Address Register value\r
83 * \param BASE The base address bits [31:5] of a memory region. The value is zero extended. Effective address gets 32 byte aligned.\r
84 * \param SH Defines the Shareability domain for this memory region.\r
85 * \param RO Read-Only: Set to 1 for a read-only memory region.\r
86 * \param NP Non-Privileged: Set to 1 for a non-privileged memory region.\r
87 * \oaram XN eXecute Never: Set to 1 for a non-executable memory region.\r
88 */\r
89 #define ARM_MPU_RBAR(BASE, SH, RO, NP, XN) \\r
90   ((BASE & MPU_RBAR_BASE_Msk) | \\r
91   ((SH << MPU_RBAR_SH_Pos) & MPU_RBAR_SH_Msk) | \\r
92   ((ARM_MPU_AP_(RO, NP) << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk) | \\r
93   ((XN << MPU_RBAR_XN_Pos) & MPU_RBAR_XN_Msk))\r
94 \r
95 /** \brief Region Limit Address Register value\r
96 * \param LIMIT The limit address bits [31:5] for this memory region. The value is one extended.\r
97 * \param IDX The attribute index to be associated with this memory region.\r
98 */\r
99 #define ARM_MPU_RLAR(LIMIT, IDX) \\r
100   ((LIMIT & MPU_RLAR_LIMIT_Msk) | \\r
101   ((IDX << MPU_RLAR_AttrIndx_Pos) & MPU_RLAR_AttrIndx_Msk) | \\r
102   (MPU_RLAR_EN_Msk))\r
103 \r
104 /**\r
105 * Struct for a single MPU Region\r
106 */\r
107 typedef struct {\r
108   uint32_t RBAR;                   /*!< Region Base Address Register value */\r
109   uint32_t RLAR;                   /*!< Region Limit Address Register value */\r
110 } ARM_MPU_Region_t;\r
111     \r
112 /** Enable the MPU.\r
113 * \param MPU_Control Default access permissions for unconfigured regions.\r
114 */\r
115 __STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)\r
116 {\r
117   __DSB();\r
118   __ISB();\r
119   MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;\r
120 #ifdef SCB_SHCSR_MEMFAULTENA_Msk\r
121   SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;\r
122 #endif\r
123 }\r
124 \r
125 /** Disable the MPU.\r
126 */\r
127 __STATIC_INLINE void ARM_MPU_Disable(void)\r
128 {\r
129   __DSB();\r
130   __ISB();\r
131 #ifdef SCB_SHCSR_MEMFAULTENA_Msk\r
132   SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;\r
133 #endif\r
134   MPU->CTRL  &= ~MPU_CTRL_ENABLE_Msk;\r
135 }\r
136 \r
137 #ifdef MPU_NS\r
138 /** Enable the Non-secure MPU.\r
139 * \param MPU_Control Default access permissions for unconfigured regions.\r
140 */\r
141 __STATIC_INLINE void ARM_MPU_Enable_NS(uint32_t MPU_Control)\r
142 {\r
143   __DSB();\r
144   __ISB();\r
145   MPU_NS->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;\r
146 #ifdef SCB_SHCSR_MEMFAULTENA_Msk\r
147   SCB_NS->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;\r
148 #endif\r
149 }\r
150 \r
151 /** Disable the Non-secure MPU.\r
152 */\r
153 __STATIC_INLINE void ARM_MPU_Disable_NS(void)\r
154 {\r
155   __DSB();\r
156   __ISB();\r
157 #ifdef SCB_SHCSR_MEMFAULTENA_Msk\r
158   SCB_NS->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;\r
159 #endif\r
160   MPU_NS->CTRL  &= ~MPU_CTRL_ENABLE_Msk;\r
161 }\r
162 #endif\r
163 \r
164 /** Set the memory attribute encoding to the given MPU.\r
165 * \param mpu Pointer to the MPU to be configured.\r
166 * \param idx The attribute index to be set [0-7]\r
167 * \param attr The attribute value to be set.\r
168 */\r
169 __STATIC_INLINE void ARM_MPU_SetMemAttrEx(MPU_Type* mpu, uint8_t idx, uint8_t attr)\r
170 {\r
171   const uint8_t reg = idx / 4U;\r
172   const uint32_t pos = ((idx % 4U) * 8U);\r
173   const uint32_t mask = 0xFFU << pos;\r
174   \r
175   if (reg >= (sizeof(mpu->MAIR) / sizeof(mpu->MAIR[0]))) {\r
176     return; // invalid index\r
177   }\r
178   \r
179   mpu->MAIR[reg] = ((mpu->MAIR[reg] & ~mask) | ((attr << pos) & mask));\r
180 }\r
181 \r
182 /** Set the memory attribute encoding.\r
183 * \param idx The attribute index to be set [0-7]\r
184 * \param attr The attribute value to be set.\r
185 */\r
186 __STATIC_INLINE void ARM_MPU_SetMemAttr(uint8_t idx, uint8_t attr)\r
187 {\r
188   ARM_MPU_SetMemAttrEx(MPU, idx, attr);\r
189 }\r
190 \r
191 #ifdef MPU_NS\r
192 /** Set the memory attribute encoding to the Non-secure MPU.\r
193 * \param idx The attribute index to be set [0-7]\r
194 * \param attr The attribute value to be set.\r
195 */\r
196 __STATIC_INLINE void ARM_MPU_SetMemAttr_NS(uint8_t idx, uint8_t attr)\r
197 {\r
198   ARM_MPU_SetMemAttrEx(MPU_NS, idx, attr);\r
199 }\r
200 #endif\r
201 \r
202 /** Clear and disable the given MPU region of the given MPU.\r
203 * \param mpu Pointer to MPU to be used.\r
204 * \param rnr Region number to be cleared.\r
205 */\r
206 __STATIC_INLINE void ARM_MPU_ClrRegionEx(MPU_Type* mpu, uint32_t rnr)\r
207 {\r
208   mpu->RNR = rnr;\r
209   mpu->RLAR = 0U;\r
210 }\r
211 \r
212 /** Clear and disable the given MPU region.\r
213 * \param rnr Region number to be cleared.\r
214 */\r
215 __STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)\r
216 {\r
217   ARM_MPU_ClrRegionEx(MPU, rnr);\r
218 }\r
219 \r
220 #ifdef MPU_NS\r
221 /** Clear and disable the given Non-secure MPU region.\r
222 * \param rnr Region number to be cleared.\r
223 */\r
224 __STATIC_INLINE void ARM_MPU_ClrRegion_NS(uint32_t rnr)\r
225 {  \r
226   ARM_MPU_ClrRegionEx(MPU_NS, rnr);\r
227 }\r
228 #endif\r
229 \r
230 /** Configure the given MPU region of the given MPU.\r
231 * \param mpu Pointer to MPU to be used.\r
232 * \param rnr Region number to be configured.\r
233 * \param rbar Value for RBAR register.\r
234 * \param rlar Value for RLAR register.\r
235 */   \r
236 __STATIC_INLINE void ARM_MPU_SetRegionEx(MPU_Type* mpu, uint32_t rnr, uint32_t rbar, uint32_t rlar)\r
237 {\r
238   mpu->RNR = rnr;\r
239   mpu->RBAR = rbar;\r
240   mpu->RLAR = rlar;\r
241 }\r
242 \r
243 /** Configure the given MPU region.\r
244 * \param rnr Region number to be configured.\r
245 * \param rbar Value for RBAR register.\r
246 * \param rlar Value for RLAR register.\r
247 */   \r
248 __STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rnr, uint32_t rbar, uint32_t rlar)\r
249 {\r
250   ARM_MPU_SetRegionEx(MPU, rnr, rbar, rlar);\r
251 }\r
252 \r
253 #ifdef MPU_NS\r
254 /** Configure the given Non-secure MPU region.\r
255 * \param rnr Region number to be configured.\r
256 * \param rbar Value for RBAR register.\r
257 * \param rlar Value for RLAR register.\r
258 */   \r
259 __STATIC_INLINE void ARM_MPU_SetRegion_NS(uint32_t rnr, uint32_t rbar, uint32_t rlar)\r
260 {\r
261   ARM_MPU_SetRegionEx(MPU_NS, rnr, rbar, rlar);  \r
262 }\r
263 #endif\r
264 \r
265 /** Memcopy with strictly ordered memory access, e.g. for register targets.\r
266 * \param dst Destination data is copied to.\r
267 * \param src Source data is copied from.\r
268 * \param len Amount of data words to be copied.\r
269 */\r
270 __STATIC_INLINE void orderedCpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)\r
271 {\r
272   uint32_t i;\r
273   for (i = 0U; i < len; ++i) \r
274   {\r
275     dst[i] = src[i];\r
276   }\r
277 }\r
278 \r
279 /** Load the given number of MPU regions from a table to the given MPU.\r
280 * \param mpu Pointer to the MPU registers to be used.\r
281 * \param rnr First region number to be configured.\r
282 * \param table Pointer to the MPU configuration table.\r
283 * \param cnt Amount of regions to be configured.\r
284 */\r
285 __STATIC_INLINE void ARM_MPU_LoadEx(MPU_Type* mpu, uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt) \r
286 {\r
287   const uint32_t rowWordSize = sizeof(ARM_MPU_Region_t)/4U;\r
288   if (cnt == 1U) {\r
289     mpu->RNR = rnr;\r
290     orderedCpy(&(mpu->RBAR), &(table->RBAR), rowWordSize);\r
291   } else {\r
292     uint32_t rnrBase   = rnr & ~(MPU_TYPE_RALIASES-1U);\r
293     uint32_t rnrOffset = rnr % MPU_TYPE_RALIASES;\r
294     \r
295     mpu->RNR = rnrBase;\r
296     while ((rnrOffset + cnt) > MPU_TYPE_RALIASES) {\r
297       uint32_t c = MPU_TYPE_RALIASES - rnrOffset;\r
298       orderedCpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), c*rowWordSize);\r
299       table += c;\r
300       cnt -= c;\r
301       rnrOffset = 0U;\r
302       rnrBase += MPU_TYPE_RALIASES;\r
303       mpu->RNR = rnrBase;\r
304     }\r
305     \r
306     orderedCpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), cnt*rowWordSize);\r
307   }\r
308 }\r
309 \r
310 /** Load the given number of MPU regions from a table.\r
311 * \param rnr First region number to be configured.\r
312 * \param table Pointer to the MPU configuration table.\r
313 * \param cnt Amount of regions to be configured.\r
314 */\r
315 __STATIC_INLINE void ARM_MPU_Load(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt) \r
316 {\r
317   ARM_MPU_LoadEx(MPU, rnr, table, cnt);\r
318 }\r
319 \r
320 #ifdef MPU_NS\r
321 /** Load the given number of MPU regions from a table to the Non-secure MPU.\r
322 * \param rnr First region number to be configured.\r
323 * \param table Pointer to the MPU configuration table.\r
324 * \param cnt Amount of regions to be configured.\r
325 */\r
326 __STATIC_INLINE void ARM_MPU_Load_NS(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt) \r
327 {\r
328   ARM_MPU_LoadEx(MPU_NS, rnr, table, cnt);\r
329 }\r
330 #endif\r
331 \r
332 #endif\r
333 \r