]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MPU_M33F_NXP_LPC55S69_MCUXpresso/NXP_Code/CMSIS/mpu_armv7.h
commit 9f316c246baafa15c542a5aea81a94f26e3d6507
[freertos] / FreeRTOS / Demo / CORTEX_MPU_M33F_NXP_LPC55S69_MCUXpresso / NXP_Code / CMSIS / mpu_armv7.h
1 /******************************************************************************\r
2  * @file     mpu_armv7.h\r
3  * @brief    CMSIS MPU API for Armv7-M MPU\r
4  * @version  V5.1.0\r
5  * @date     08. March 2019\r
6  ******************************************************************************/\r
7 /*\r
8  * Copyright (c) 2017-2019 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_ARMV7_H\r
32 #define ARM_MPU_ARMV7_H\r
33 \r
34 #define ARM_MPU_REGION_SIZE_32B      ((uint8_t)0x04U) ///!< MPU Region Size 32 Bytes\r
35 #define ARM_MPU_REGION_SIZE_64B      ((uint8_t)0x05U) ///!< MPU Region Size 64 Bytes\r
36 #define ARM_MPU_REGION_SIZE_128B     ((uint8_t)0x06U) ///!< MPU Region Size 128 Bytes\r
37 #define ARM_MPU_REGION_SIZE_256B     ((uint8_t)0x07U) ///!< MPU Region Size 256 Bytes\r
38 #define ARM_MPU_REGION_SIZE_512B     ((uint8_t)0x08U) ///!< MPU Region Size 512 Bytes\r
39 #define ARM_MPU_REGION_SIZE_1KB      ((uint8_t)0x09U) ///!< MPU Region Size 1 KByte\r
40 #define ARM_MPU_REGION_SIZE_2KB      ((uint8_t)0x0AU) ///!< MPU Region Size 2 KBytes\r
41 #define ARM_MPU_REGION_SIZE_4KB      ((uint8_t)0x0BU) ///!< MPU Region Size 4 KBytes\r
42 #define ARM_MPU_REGION_SIZE_8KB      ((uint8_t)0x0CU) ///!< MPU Region Size 8 KBytes\r
43 #define ARM_MPU_REGION_SIZE_16KB     ((uint8_t)0x0DU) ///!< MPU Region Size 16 KBytes\r
44 #define ARM_MPU_REGION_SIZE_32KB     ((uint8_t)0x0EU) ///!< MPU Region Size 32 KBytes\r
45 #define ARM_MPU_REGION_SIZE_64KB     ((uint8_t)0x0FU) ///!< MPU Region Size 64 KBytes\r
46 #define ARM_MPU_REGION_SIZE_128KB    ((uint8_t)0x10U) ///!< MPU Region Size 128 KBytes\r
47 #define ARM_MPU_REGION_SIZE_256KB    ((uint8_t)0x11U) ///!< MPU Region Size 256 KBytes\r
48 #define ARM_MPU_REGION_SIZE_512KB    ((uint8_t)0x12U) ///!< MPU Region Size 512 KBytes\r
49 #define ARM_MPU_REGION_SIZE_1MB      ((uint8_t)0x13U) ///!< MPU Region Size 1 MByte\r
50 #define ARM_MPU_REGION_SIZE_2MB      ((uint8_t)0x14U) ///!< MPU Region Size 2 MBytes\r
51 #define ARM_MPU_REGION_SIZE_4MB      ((uint8_t)0x15U) ///!< MPU Region Size 4 MBytes\r
52 #define ARM_MPU_REGION_SIZE_8MB      ((uint8_t)0x16U) ///!< MPU Region Size 8 MBytes\r
53 #define ARM_MPU_REGION_SIZE_16MB     ((uint8_t)0x17U) ///!< MPU Region Size 16 MBytes\r
54 #define ARM_MPU_REGION_SIZE_32MB     ((uint8_t)0x18U) ///!< MPU Region Size 32 MBytes\r
55 #define ARM_MPU_REGION_SIZE_64MB     ((uint8_t)0x19U) ///!< MPU Region Size 64 MBytes\r
56 #define ARM_MPU_REGION_SIZE_128MB    ((uint8_t)0x1AU) ///!< MPU Region Size 128 MBytes\r
57 #define ARM_MPU_REGION_SIZE_256MB    ((uint8_t)0x1BU) ///!< MPU Region Size 256 MBytes\r
58 #define ARM_MPU_REGION_SIZE_512MB    ((uint8_t)0x1CU) ///!< MPU Region Size 512 MBytes\r
59 #define ARM_MPU_REGION_SIZE_1GB      ((uint8_t)0x1DU) ///!< MPU Region Size 1 GByte\r
60 #define ARM_MPU_REGION_SIZE_2GB      ((uint8_t)0x1EU) ///!< MPU Region Size 2 GBytes\r
61 #define ARM_MPU_REGION_SIZE_4GB      ((uint8_t)0x1FU) ///!< MPU Region Size 4 GBytes\r
62 \r
63 #define ARM_MPU_AP_NONE 0U ///!< MPU Access Permission no access\r
64 #define ARM_MPU_AP_PRIV 1U ///!< MPU Access Permission privileged access only\r
65 #define ARM_MPU_AP_URO  2U ///!< MPU Access Permission unprivileged access read-only\r
66 #define ARM_MPU_AP_FULL 3U ///!< MPU Access Permission full access\r
67 #define ARM_MPU_AP_PRO  5U ///!< MPU Access Permission privileged access read-only\r
68 #define ARM_MPU_AP_RO   6U ///!< MPU Access Permission read-only access\r
69 \r
70 /** MPU Region Base Address Register Value\r
71 *\r
72 * \param Region The region to be configured, number 0 to 15.\r
73 * \param BaseAddress The base address for the region.\r
74 */\r
75 #define ARM_MPU_RBAR(Region, BaseAddress) \\r
76   (((BaseAddress) & MPU_RBAR_ADDR_Msk) |  \\r
77    ((Region) & MPU_RBAR_REGION_Msk)    |  \\r
78    (MPU_RBAR_VALID_Msk))\r
79 \r
80 /**\r
81 * MPU Memory Access Attributes\r
82\r
83 * \param TypeExtField      Type extension field, allows you to configure memory access type, for example strongly ordered, peripheral.\r
84 * \param IsShareable       Region is shareable between multiple bus masters.\r
85 * \param IsCacheable       Region is cacheable, i.e. its value may be kept in cache.\r
86 * \param IsBufferable      Region is bufferable, i.e. using write-back caching. Cacheable but non-bufferable regions use write-through policy.\r
87 */  \r
88 #define ARM_MPU_ACCESS_(TypeExtField, IsShareable, IsCacheable, IsBufferable)   \\r
89   ((((TypeExtField) << MPU_RASR_TEX_Pos) & MPU_RASR_TEX_Msk)                  | \\r
90    (((IsShareable)  << MPU_RASR_S_Pos)   & MPU_RASR_S_Msk)                    | \\r
91    (((IsCacheable)  << MPU_RASR_C_Pos)   & MPU_RASR_C_Msk)                    | \\r
92    (((IsBufferable) << MPU_RASR_B_Pos)   & MPU_RASR_B_Msk))\r
93 \r
94 /**\r
95 * MPU Region Attribute and Size Register Value\r
96\r
97 * \param DisableExec       Instruction access disable bit, 1= disable instruction fetches.\r
98 * \param AccessPermission  Data access permissions, allows you to configure read/write access for User and Privileged mode.\r
99 * \param AccessAttributes  Memory access attribution, see \ref ARM_MPU_ACCESS_.\r
100 * \param SubRegionDisable  Sub-region disable field.\r
101 * \param Size              Region size of the region to be configured, for example 4K, 8K.\r
102 */\r
103 #define ARM_MPU_RASR_EX(DisableExec, AccessPermission, AccessAttributes, SubRegionDisable, Size)    \\r
104   ((((DisableExec)      << MPU_RASR_XN_Pos)   & MPU_RASR_XN_Msk)                                  | \\r
105    (((AccessPermission) << MPU_RASR_AP_Pos)   & MPU_RASR_AP_Msk)                                  | \\r
106    (((AccessAttributes) & (MPU_RASR_TEX_Msk | MPU_RASR_S_Msk | MPU_RASR_C_Msk | MPU_RASR_B_Msk))) | \\r
107    (((SubRegionDisable) << MPU_RASR_SRD_Pos)  & MPU_RASR_SRD_Msk)                                 | \\r
108    (((Size)             << MPU_RASR_SIZE_Pos) & MPU_RASR_SIZE_Msk)                                | \\r
109    (((MPU_RASR_ENABLE_Msk))))\r
110 \r
111 /**\r
112 * MPU Region Attribute and Size Register Value\r
113\r
114 * \param DisableExec       Instruction access disable bit, 1= disable instruction fetches.\r
115 * \param AccessPermission  Data access permissions, allows you to configure read/write access for User and Privileged mode.\r
116 * \param TypeExtField      Type extension field, allows you to configure memory access type, for example strongly ordered, peripheral.\r
117 * \param IsShareable       Region is shareable between multiple bus masters.\r
118 * \param IsCacheable       Region is cacheable, i.e. its value may be kept in cache.\r
119 * \param IsBufferable      Region is bufferable, i.e. using write-back caching. Cacheable but non-bufferable regions use write-through policy.\r
120 * \param SubRegionDisable  Sub-region disable field.\r
121 * \param Size              Region size of the region to be configured, for example 4K, 8K.\r
122 */                         \r
123 #define ARM_MPU_RASR(DisableExec, AccessPermission, TypeExtField, IsShareable, IsCacheable, IsBufferable, SubRegionDisable, Size) \\r
124   ARM_MPU_RASR_EX(DisableExec, AccessPermission, ARM_MPU_ACCESS_(TypeExtField, IsShareable, IsCacheable, IsBufferable), SubRegionDisable, Size)\r
125 \r
126 /**\r
127 * MPU Memory Access Attribute for strongly ordered memory.\r
128 *  - TEX: 000b\r
129 *  - Shareable\r
130 *  - Non-cacheable\r
131 *  - Non-bufferable\r
132 */ \r
133 #define ARM_MPU_ACCESS_ORDERED ARM_MPU_ACCESS_(0U, 1U, 0U, 0U)\r
134 \r
135 /**\r
136 * MPU Memory Access Attribute for device memory.\r
137 *  - TEX: 000b (if shareable) or 010b (if non-shareable)\r
138 *  - Shareable or non-shareable\r
139 *  - Non-cacheable\r
140 *  - Bufferable (if shareable) or non-bufferable (if non-shareable)\r
141 *\r
142 * \param IsShareable Configures the device memory as shareable or non-shareable.\r
143 */ \r
144 #define ARM_MPU_ACCESS_DEVICE(IsShareable) ((IsShareable) ? ARM_MPU_ACCESS_(0U, 1U, 0U, 1U) : ARM_MPU_ACCESS_(2U, 0U, 0U, 0U))\r
145 \r
146 /**\r
147 * MPU Memory Access Attribute for normal memory.\r
148 *  - TEX: 1BBb (reflecting outer cacheability rules)\r
149 *  - Shareable or non-shareable\r
150 *  - Cacheable or non-cacheable (reflecting inner cacheability rules)\r
151 *  - Bufferable or non-bufferable (reflecting inner cacheability rules)\r
152 *\r
153 * \param OuterCp Configures the outer cache policy.\r
154 * \param InnerCp Configures the inner cache policy.\r
155 * \param IsShareable Configures the memory as shareable or non-shareable.\r
156 */ \r
157 #define ARM_MPU_ACCESS_NORMAL(OuterCp, InnerCp, IsShareable) ARM_MPU_ACCESS_((4U | (OuterCp)), IsShareable, ((InnerCp) & 2U), ((InnerCp) & 1U))\r
158 \r
159 /**\r
160 * MPU Memory Access Attribute non-cacheable policy.\r
161 */\r
162 #define ARM_MPU_CACHEP_NOCACHE 0U\r
163 \r
164 /**\r
165 * MPU Memory Access Attribute write-back, write and read allocate policy.\r
166 */\r
167 #define ARM_MPU_CACHEP_WB_WRA 1U\r
168 \r
169 /**\r
170 * MPU Memory Access Attribute write-through, no write allocate policy.\r
171 */\r
172 #define ARM_MPU_CACHEP_WT_NWA 2U\r
173 \r
174 /**\r
175 * MPU Memory Access Attribute write-back, no write allocate policy.\r
176 */\r
177 #define ARM_MPU_CACHEP_WB_NWA 3U\r
178 \r
179 \r
180 /**\r
181 * Struct for a single MPU Region\r
182 */\r
183 typedef struct {\r
184   uint32_t RBAR; //!< The region base address register value (RBAR)\r
185   uint32_t RASR; //!< The region attribute and size register value (RASR) \ref MPU_RASR\r
186 } ARM_MPU_Region_t;\r
187     \r
188 /** Enable the MPU.\r
189 * \param MPU_Control Default access permissions for unconfigured regions.\r
190 */\r
191 __STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)\r
192 {\r
193   MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;\r
194 #ifdef SCB_SHCSR_MEMFAULTENA_Msk\r
195   SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;\r
196 #endif\r
197   __DSB();\r
198   __ISB();\r
199 }\r
200 \r
201 /** Disable the MPU.\r
202 */\r
203 __STATIC_INLINE void ARM_MPU_Disable(void)\r
204 {\r
205   __DMB();\r
206 #ifdef SCB_SHCSR_MEMFAULTENA_Msk\r
207   SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;\r
208 #endif\r
209   MPU->CTRL  &= ~MPU_CTRL_ENABLE_Msk;\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   MPU->RNR = rnr;\r
218   MPU->RASR = 0U;\r
219 }\r
220 \r
221 /** Configure an MPU region.\r
222 * \param rbar Value for RBAR register.\r
223 * \param rsar Value for RSAR register.\r
224 */   \r
225 __STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rbar, uint32_t rasr)\r
226 {\r
227   MPU->RBAR = rbar;\r
228   MPU->RASR = rasr;\r
229 }\r
230 \r
231 /** Configure the given MPU region.\r
232 * \param rnr Region number to be configured.\r
233 * \param rbar Value for RBAR register.\r
234 * \param rsar Value for RSAR register.\r
235 */   \r
236 __STATIC_INLINE void ARM_MPU_SetRegionEx(uint32_t rnr, uint32_t rbar, uint32_t rasr)\r
237 {\r
238   MPU->RNR = rnr;\r
239   MPU->RBAR = rbar;\r
240   MPU->RASR = rasr;\r
241 }\r
242 \r
243 /** Memcopy with strictly ordered memory access, e.g. for register targets.\r
244 * \param dst Destination data is copied to.\r
245 * \param src Source data is copied from.\r
246 * \param len Amount of data words to be copied.\r
247 */\r
248 __STATIC_INLINE void ARM_MPU_OrderedMemcpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)\r
249 {\r
250   uint32_t i;\r
251   for (i = 0U; i < len; ++i) \r
252   {\r
253     dst[i] = src[i];\r
254   }\r
255 }\r
256 \r
257 /** Load the given number of MPU regions from a table.\r
258 * \param table Pointer to the MPU configuration table.\r
259 * \param cnt Amount of regions to be configured.\r
260 */\r
261 __STATIC_INLINE void ARM_MPU_Load(ARM_MPU_Region_t const* table, uint32_t cnt) \r
262 {\r
263   const uint32_t rowWordSize = sizeof(ARM_MPU_Region_t)/4U;\r
264   while (cnt > MPU_TYPE_RALIASES) {\r
265     ARM_MPU_OrderedMemcpy(&(MPU->RBAR), &(table->RBAR), MPU_TYPE_RALIASES*rowWordSize);\r
266     table += MPU_TYPE_RALIASES;\r
267     cnt -= MPU_TYPE_RALIASES;\r
268   }\r
269   ARM_MPU_OrderedMemcpy(&(MPU->RBAR), &(table->RBAR), cnt*rowWordSize);\r
270 }\r
271 \r
272 #endif\r