]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MPU_M33F_NXP_LPC55S69_MCUXpresso/NXP_Code/CMSIS/mpu_armv7.h
8fde91eb96692b9d8a502c3ccf7f874740aa9d3b
[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.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)\r
35 #define ARM_MPU_REGION_SIZE_64B      ((uint8_t)0x05U)\r
36 #define ARM_MPU_REGION_SIZE_128B     ((uint8_t)0x06U)\r
37 #define ARM_MPU_REGION_SIZE_256B     ((uint8_t)0x07U)\r
38 #define ARM_MPU_REGION_SIZE_512B     ((uint8_t)0x08U)\r
39 #define ARM_MPU_REGION_SIZE_1KB      ((uint8_t)0x09U)\r
40 #define ARM_MPU_REGION_SIZE_2KB      ((uint8_t)0x0AU)\r
41 #define ARM_MPU_REGION_SIZE_4KB      ((uint8_t)0x0BU)\r
42 #define ARM_MPU_REGION_SIZE_8KB      ((uint8_t)0x0CU)\r
43 #define ARM_MPU_REGION_SIZE_16KB     ((uint8_t)0x0DU)\r
44 #define ARM_MPU_REGION_SIZE_32KB     ((uint8_t)0x0EU)\r
45 #define ARM_MPU_REGION_SIZE_64KB     ((uint8_t)0x0FU)\r
46 #define ARM_MPU_REGION_SIZE_128KB    ((uint8_t)0x10U)\r
47 #define ARM_MPU_REGION_SIZE_256KB    ((uint8_t)0x11U)\r
48 #define ARM_MPU_REGION_SIZE_512KB    ((uint8_t)0x12U)\r
49 #define ARM_MPU_REGION_SIZE_1MB      ((uint8_t)0x13U)\r
50 #define ARM_MPU_REGION_SIZE_2MB      ((uint8_t)0x14U)\r
51 #define ARM_MPU_REGION_SIZE_4MB      ((uint8_t)0x15U)\r
52 #define ARM_MPU_REGION_SIZE_8MB      ((uint8_t)0x16U)\r
53 #define ARM_MPU_REGION_SIZE_16MB     ((uint8_t)0x17U)\r
54 #define ARM_MPU_REGION_SIZE_32MB     ((uint8_t)0x18U)\r
55 #define ARM_MPU_REGION_SIZE_64MB     ((uint8_t)0x19U)\r
56 #define ARM_MPU_REGION_SIZE_128MB    ((uint8_t)0x1AU)\r
57 #define ARM_MPU_REGION_SIZE_256MB    ((uint8_t)0x1BU)\r
58 #define ARM_MPU_REGION_SIZE_512MB    ((uint8_t)0x1CU)\r
59 #define ARM_MPU_REGION_SIZE_1GB      ((uint8_t)0x1DU)\r
60 #define ARM_MPU_REGION_SIZE_2GB      ((uint8_t)0x1EU)\r
61 #define ARM_MPU_REGION_SIZE_4GB      ((uint8_t)0x1FU)\r
62 \r
63 #define ARM_MPU_AP_NONE 0U \r
64 #define ARM_MPU_AP_PRIV 1U\r
65 #define ARM_MPU_AP_URO  2U\r
66 #define ARM_MPU_AP_FULL 3U\r
67 #define ARM_MPU_AP_PRO  5U\r
68 #define ARM_MPU_AP_RO   6U\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 Region Attribute and Size Register Value\r
82\r
83 * \param DisableExec       Instruction access disable bit, 1= disable instruction fetches.\r
84 * \param AccessPermission  Data access permissions, allows you to configure read/write access for User and Privileged mode.\r
85 * \param TypeExtField      Type extension field, allows you to configure memory access type, for example strongly ordered, peripheral.\r
86 * \param IsShareable       Region is shareable between multiple bus masters.\r
87 * \param IsCacheable       Region is cacheable, i.e. its value may be kept in cache.\r
88 * \param IsBufferable      Region is bufferable, i.e. using write-back caching. Cacheable but non-bufferable regions use write-through policy.\r
89 * \param SubRegionDisable  Sub-region disable field.\r
90 * \param Size              Region size of the region to be configured, for example 4K, 8K.\r
91 */                         \r
92 #define ARM_MPU_RASR(DisableExec, AccessPermission, TypeExtField, IsShareable, IsCacheable, IsBufferable, SubRegionDisable, Size) \\r
93   ((((DisableExec     ) << MPU_RASR_XN_Pos)     & MPU_RASR_XN_Msk)     | \\r
94    (((AccessPermission) << MPU_RASR_AP_Pos)     & MPU_RASR_AP_Msk)     | \\r
95    (((TypeExtField    ) << MPU_RASR_TEX_Pos)    & MPU_RASR_TEX_Msk)    | \\r
96    (((IsShareable     ) << MPU_RASR_S_Pos)      & MPU_RASR_S_Msk)      | \\r
97    (((IsCacheable     ) << MPU_RASR_C_Pos)      & MPU_RASR_C_Msk)      | \\r
98    (((IsBufferable    ) << MPU_RASR_B_Pos)      & MPU_RASR_B_Msk)      | \\r
99    (((SubRegionDisable) << MPU_RASR_SRD_Pos)    & MPU_RASR_SRD_Msk)    | \\r
100    (((Size            ) << MPU_RASR_SIZE_Pos)   & MPU_RASR_SIZE_Msk)   | \\r
101    (MPU_RASR_ENABLE_Msk))\r
102 \r
103 \r
104 /**\r
105 * Struct for a single MPU Region\r
106 */\r
107 typedef struct {\r
108   uint32_t RBAR; //!< The region base address register value (RBAR)\r
109   uint32_t RASR; //!< The region attribute and size register value (RASR) \ref MPU_RASR\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 /** Clear and disable the given MPU region.\r
138 * \param rnr Region number to be cleared.\r
139 */\r
140 __STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)\r
141 {\r
142   MPU->RNR = rnr;\r
143   MPU->RASR = 0U;\r
144 }\r
145 \r
146 /** Configure an MPU region.\r
147 * \param rbar Value for RBAR register.\r
148 * \param rsar Value for RSAR register.\r
149 */   \r
150 __STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rbar, uint32_t rasr)\r
151 {\r
152   MPU->RBAR = rbar;\r
153   MPU->RASR = rasr;\r
154 }\r
155 \r
156 /** Configure the given MPU region.\r
157 * \param rnr Region number to be configured.\r
158 * \param rbar Value for RBAR register.\r
159 * \param rsar Value for RSAR register.\r
160 */   \r
161 __STATIC_INLINE void ARM_MPU_SetRegionEx(uint32_t rnr, uint32_t rbar, uint32_t rasr)\r
162 {\r
163   MPU->RNR = rnr;\r
164   MPU->RBAR = rbar;\r
165   MPU->RASR = rasr;\r
166 }\r
167 \r
168 /** Memcopy with strictly ordered memory access, e.g. for register targets.\r
169 * \param dst Destination data is copied to.\r
170 * \param src Source data is copied from.\r
171 * \param len Amount of data words to be copied.\r
172 */\r
173 __STATIC_INLINE void orderedCpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)\r
174 {\r
175   uint32_t i;\r
176   for (i = 0U; i < len; ++i) \r
177   {\r
178     dst[i] = src[i];\r
179   }\r
180 }\r
181 \r
182 /** Load the given number of MPU regions from a table.\r
183 * \param table Pointer to the MPU configuration table.\r
184 * \param cnt Amount of regions to be configured.\r
185 */\r
186 __STATIC_INLINE void ARM_MPU_Load(ARM_MPU_Region_t const* table, uint32_t cnt) \r
187 {\r
188   const uint32_t rowWordSize = sizeof(ARM_MPU_Region_t)/4U;\r
189   while (cnt > MPU_TYPE_RALIASES) {\r
190     orderedCpy(&(MPU->RBAR), &(table->RBAR), MPU_TYPE_RALIASES*rowWordSize);\r
191     table += MPU_TYPE_RALIASES;\r
192     cnt -= MPU_TYPE_RALIASES;\r
193   }\r
194   orderedCpy(&(MPU->RBAR), &(table->RBAR), cnt*rowWordSize);\r
195 }\r
196 \r
197 #endif\r