1 /******************************************************************************
\r
3 * @brief CMSIS MPU API for Armv7-M MPU
\r
5 * @date 10. January 2018
\r
6 ******************************************************************************/
\r
8 * Copyright (c) 2017-2018 Arm Limited. All rights reserved.
\r
10 * SPDX-License-Identifier: Apache-2.0
\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
16 * www.apache.org/licenses/LICENSE-2.0
\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
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
31 #ifndef ARM_MPU_ARMV7_H
\r
32 #define ARM_MPU_ARMV7_H
\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
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
70 /** MPU Region Base Address Register Value
\r
72 * \param Region The region to be configured, number 0 to 15.
\r
73 * \param BaseAddress The base address for the region.
\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
81 * MPU Memory Access Attributes
\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
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
95 * MPU Region Attribute and Size Register Value
\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
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
109 * MPU Region Attribute and Size Register Value
\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
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
124 * MPU Memory Access Attribute for strongly ordered memory.
\r
130 #define ARM_MPU_ACCESS_ORDERED ARM_MPU_ACCESS_(0U, 1U, 0U, 0U)
\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
137 * - Bufferable (if shareable) or non-bufferable (if non-shareable)
\r
139 * \param IsShareable Configures the device memory as shareable or non-shareable.
\r
141 #define ARM_MPU_ACCESS_DEVICE(IsShareable) ((IsShareable) ? ARM_MPU_ACCESS_(0U, 1U, 0U, 1U) : ARM_MPU_ACCESS_(2U, 0U, 0U, 0U))
\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
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
154 #define ARM_MPU_ACCESS_NORMAL(OuterCp, InnerCp, IsShareable) ARM_MPU_ACCESS_((4U | (OuterCp)), IsShareable, ((InnerCp) & 2U), ((InnerCp) & 1U))
\r
157 * MPU Memory Access Attribute non-cacheable policy.
\r
159 #define ARM_MPU_CACHEP_NOCACHE 0U
\r
162 * MPU Memory Access Attribute write-back, write and read allocate policy.
\r
164 #define ARM_MPU_CACHEP_WB_WRA 1U
\r
167 * MPU Memory Access Attribute write-through, no write allocate policy.
\r
169 #define ARM_MPU_CACHEP_WT_NWA 2U
\r
172 * MPU Memory Access Attribute write-back, no write allocate policy.
\r
174 #define ARM_MPU_CACHEP_WB_NWA 3U
\r
178 * Struct for a single MPU Region
\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
185 /** Enable the MPU.
\r
186 * \param MPU_Control Default access permissions for unconfigured regions.
\r
188 __STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)
\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
198 /** Disable the MPU.
\r
200 __STATIC_INLINE void ARM_MPU_Disable(void)
\r
204 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
\r
205 SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
\r
207 MPU->CTRL &= ~MPU_CTRL_ENABLE_Msk;
\r
210 /** Clear and disable the given MPU region.
\r
211 * \param rnr Region number to be cleared.
\r
213 __STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)
\r
219 /** Configure an MPU region.
\r
220 * \param rbar Value for RBAR register.
\r
221 * \param rsar Value for RSAR register.
\r
223 __STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rbar, uint32_t rasr)
\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
234 __STATIC_INLINE void ARM_MPU_SetRegionEx(uint32_t rnr, uint32_t rbar, uint32_t rasr)
\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
246 __STATIC_INLINE void orderedCpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)
\r
249 for (i = 0U; i < len; ++i)
\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
259 __STATIC_INLINE void ARM_MPU_Load(ARM_MPU_Region_t const* table, uint32_t cnt)
\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
267 orderedCpy(&(MPU->RBAR), &(table->RBAR), cnt*rowWordSize);
\r