]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MPU_STM32L4_Discovery_Keil_STM32Cube/ST_Code/Drivers/CMSIS/Include/mpu_armv7.h
Make vSetupTimerInterrupt weak in the RVDS M4 MPU port to give the
[freertos] / FreeRTOS / Demo / CORTEX_MPU_STM32L4_Discovery_Keil_STM32Cube / ST_Code / Drivers / CMSIS / Include / mpu_armv7.h
1 /******************************************************************************\r
2  * @file     mpu_armv7.h\r
3  * @brief    CMSIS MPU API for Armv7-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_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   \r
108 /**\r
109 * MPU Region Attribute and Size Register Value\r
110\r
111 * \param DisableExec       Instruction access disable bit, 1= disable instruction fetches.\r
112 * \param AccessPermission  Data access permissions, allows you to configure read/write access for User and Privileged mode.\r
113 * \param TypeExtField      Type extension field, allows you to configure memory access type, for example strongly ordered, peripheral.\r
114 * \param IsShareable       Region is shareable between multiple bus masters.\r
115 * \param IsCacheable       Region is cacheable, i.e. its value may be kept in cache.\r
116 * \param IsBufferable      Region is bufferable, i.e. using write-back caching. Cacheable but non-bufferable regions use write-through policy.\r
117 * \param SubRegionDisable  Sub-region disable field.\r
118 * \param Size              Region size of the region to be configured, for example 4K, 8K.\r
119 */                         \r
120 #define ARM_MPU_RASR(DisableExec, AccessPermission, TypeExtField, IsShareable, IsCacheable, IsBufferable, SubRegionDisable, Size) \\r
121   ARM_MPU_RASR_EX(DisableExec, AccessPermission, ARM_MPU_ACCESS_(TypeExtField, IsShareable, IsCacheable, IsBufferable), SubRegionDisable, Size)\r
122 \r
123 /**\r
124 * MPU Memory Access Attribute for strongly ordered memory.\r
125 *  - TEX: 000b\r
126 *  - Shareable\r
127 *  - Non-cacheable\r
128 *  - Non-bufferable\r
129 */ \r
130 #define ARM_MPU_ACCESS_ORDERED ARM_MPU_ACCESS_(0U, 1U, 0U, 0U)\r
131 \r
132 /**\r
133 * MPU Memory Access Attribute for device memory.\r
134 *  - TEX: 000b (if non-shareable) or 010b (if shareable)\r
135 *  - Shareable or non-shareable\r
136 *  - Non-cacheable\r
137 *  - Bufferable (if shareable) or non-bufferable (if non-shareable)\r
138 *\r
139 * \param IsShareable Configures the device memory as shareable or non-shareable.\r
140 */ \r
141 #define ARM_MPU_ACCESS_DEVICE(IsShareable) ((IsShareable) ? ARM_MPU_ACCESS_(0U, 1U, 0U, 1U) : ARM_MPU_ACCESS_(2U, 0U, 0U, 0U))\r
142 \r
143 /**\r
144 * MPU Memory Access Attribute for normal memory.\r
145 *  - TEX: 1BBb (reflecting outer cacheability rules)\r
146 *  - Shareable or non-shareable\r
147 *  - Cacheable or non-cacheable (reflecting inner cacheability rules)\r
148 *  - Bufferable or non-bufferable (reflecting inner cacheability rules)\r
149 *\r
150 * \param OuterCp Configures the outer cache policy.\r
151 * \param InnerCp Configures the inner cache policy.\r
152 * \param IsShareable Configures the memory as shareable or non-shareable.\r
153 */ \r
154 #define ARM_MPU_ACCESS_NORMAL(OuterCp, InnerCp, IsShareable) ARM_MPU_ACCESS_((4U | (OuterCp)), IsShareable, ((InnerCp) & 2U), ((InnerCp) & 1U))\r
155 \r
156 /**\r
157 * MPU Memory Access Attribute non-cacheable policy.\r
158 */\r
159 #define ARM_MPU_CACHEP_NOCACHE 0U\r
160 \r
161 /**\r
162 * MPU Memory Access Attribute write-back, write and read allocate policy.\r
163 */\r
164 #define ARM_MPU_CACHEP_WB_WRA 1U\r
165 \r
166 /**\r
167 * MPU Memory Access Attribute write-through, no write allocate policy.\r
168 */\r
169 #define ARM_MPU_CACHEP_WT_NWA 2U\r
170 \r
171 /**\r
172 * MPU Memory Access Attribute write-back, no write allocate policy.\r
173 */\r
174 #define ARM_MPU_CACHEP_WB_NWA 3U\r
175 \r
176 \r
177 /**\r
178 * Struct for a single MPU Region\r
179 */\r
180 typedef struct {\r
181   uint32_t RBAR; //!< The region base address register value (RBAR)\r
182   uint32_t RASR; //!< The region attribute and size register value (RASR) \ref MPU_RASR\r
183 } ARM_MPU_Region_t;\r
184     \r
185 /** Enable the MPU.\r
186 * \param MPU_Control Default access permissions for unconfigured regions.\r
187 */\r
188 __STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)\r
189 {\r
190   __DSB();\r
191   __ISB();\r
192   MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;\r
193 #ifdef SCB_SHCSR_MEMFAULTENA_Msk\r
194   SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;\r
195 #endif\r
196 }\r
197 \r
198 /** Disable the MPU.\r
199 */\r
200 __STATIC_INLINE void ARM_MPU_Disable(void)\r
201 {\r
202   __DSB();\r
203   __ISB();\r
204 #ifdef SCB_SHCSR_MEMFAULTENA_Msk\r
205   SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;\r
206 #endif\r
207   MPU->CTRL  &= ~MPU_CTRL_ENABLE_Msk;\r
208 }\r
209 \r
210 /** Clear and disable the given MPU region.\r
211 * \param rnr Region number to be cleared.\r
212 */\r
213 __STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)\r
214 {\r
215   MPU->RNR = rnr;\r
216   MPU->RASR = 0U;\r
217 }\r
218 \r
219 /** Configure an MPU region.\r
220 * \param rbar Value for RBAR register.\r
221 * \param rsar Value for RSAR register.\r
222 */   \r
223 __STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rbar, uint32_t rasr)\r
224 {\r
225   MPU->RBAR = rbar;\r
226   MPU->RASR = rasr;\r
227 }\r
228 \r
229 /** Configure the given MPU region.\r
230 * \param rnr Region number to be configured.\r
231 * \param rbar Value for RBAR register.\r
232 * \param rsar Value for RSAR register.\r
233 */   \r
234 __STATIC_INLINE void ARM_MPU_SetRegionEx(uint32_t rnr, uint32_t rbar, uint32_t rasr)\r
235 {\r
236   MPU->RNR = rnr;\r
237   MPU->RBAR = rbar;\r
238   MPU->RASR = rasr;\r
239 }\r
240 \r
241 /** Memcopy with strictly ordered memory access, e.g. for register targets.\r
242 * \param dst Destination data is copied to.\r
243 * \param src Source data is copied from.\r
244 * \param len Amount of data words to be copied.\r
245 */\r
246 __STATIC_INLINE void orderedCpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)\r
247 {\r
248   uint32_t i;\r
249   for (i = 0U; i < len; ++i) \r
250   {\r
251     dst[i] = src[i];\r
252   }\r
253 }\r
254 \r
255 /** Load the given number of MPU regions from a table.\r
256 * \param table Pointer to the MPU configuration table.\r
257 * \param cnt Amount of regions to be configured.\r
258 */\r
259 __STATIC_INLINE void ARM_MPU_Load(ARM_MPU_Region_t const* table, uint32_t cnt) \r
260 {\r
261   const uint32_t rowWordSize = sizeof(ARM_MPU_Region_t)/4U;\r
262   while (cnt > MPU_TYPE_RALIASES) {\r
263     orderedCpy(&(MPU->RBAR), &(table->RBAR), MPU_TYPE_RALIASES*rowWordSize);\r
264     table += MPU_TYPE_RALIASES;\r
265     cnt -= MPU_TYPE_RALIASES;\r
266   }\r
267   orderedCpy(&(MPU->RBAR), &(table->RBAR), cnt*rowWordSize);\r
268 }\r
269 \r
270 #endif\r