My Project
Loading...
Searching...
No Matches
mpu_armv8.h
Go to the documentation of this file.
1/******************************************************************************
2 * @file mpu_armv8.h
3 * @brief CMSIS MPU API for Armv8-M MPU
4 * @version V5.0.4
5 * @date 10. January 2018
6 ******************************************************************************/
7/*
8 * Copyright (c) 2017-2018 Arm Limited. All rights reserved.
9 *
10 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the License); you may
13 * not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
20 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 */
24
25#if defined ( __ICCARM__ )
26 #pragma system_include /* treat file as system include file for MISRA check */
27#elif defined (__clang__)
28 #pragma clang system_header /* treat file as system include file */
29#endif
30
31#ifndef ARM_MPU_ARMV8_H
32#define ARM_MPU_ARMV8_H
33
35#define ARM_MPU_ATTR_DEVICE ( 0U )
36
38#define ARM_MPU_ATTR_NON_CACHEABLE ( 4U )
39
46#define ARM_MPU_ATTR_MEMORY_(NT, WB, RA, WA) \
47 (((NT & 1U) << 3U) | ((WB & 1U) << 2U) | ((RA & 1U) << 1U) | (WA & 1U))
48
50#define ARM_MPU_ATTR_DEVICE_nGnRnE (0U)
51
53#define ARM_MPU_ATTR_DEVICE_nGnRE (1U)
54
56#define ARM_MPU_ATTR_DEVICE_nGRE (2U)
57
59#define ARM_MPU_ATTR_DEVICE_GRE (3U)
60
65#define ARM_MPU_ATTR(O, I) (((O & 0xFU) << 4U) | (((O & 0xFU) != 0U) ? (I & 0xFU) : ((I & 0x3U) << 2U)))
66
68#define ARM_MPU_SH_NON (0U)
69
71#define ARM_MPU_SH_OUTER (2U)
72
74#define ARM_MPU_SH_INNER (3U)
75
80#define ARM_MPU_AP_(RO, NP) (((RO & 1U) << 1U) | (NP & 1U))
81
89#define ARM_MPU_RBAR(BASE, SH, RO, NP, XN) \
90 ((BASE & MPU_RBAR_BASE_Msk) | \
91 ((SH << MPU_RBAR_SH_Pos) & MPU_RBAR_SH_Msk) | \
92 ((ARM_MPU_AP_(RO, NP) << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk) | \
93 ((XN << MPU_RBAR_XN_Pos) & MPU_RBAR_XN_Msk))
94
99#define ARM_MPU_RLAR(LIMIT, IDX) \
100 ((LIMIT & MPU_RLAR_LIMIT_Msk) | \
101 ((IDX << MPU_RLAR_AttrIndx_Pos) & MPU_RLAR_AttrIndx_Msk) | \
102 (MPU_RLAR_EN_Msk))
103
107typedef struct {
108 uint32_t RBAR;
109 uint32_t RLAR;
111
115__STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)
116{
117 __DSB();
118 __ISB();
119 MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
120#ifdef SCB_SHCSR_MEMFAULTENA_Msk
122#endif
123}
124
128{
129 __DSB();
130 __ISB();
131#ifdef SCB_SHCSR_MEMFAULTENA_Msk
132 SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
133#endif
134 MPU->CTRL &= ~MPU_CTRL_ENABLE_Msk;
135}
136
137#ifdef MPU_NS
141__STATIC_INLINE void ARM_MPU_Enable_NS(uint32_t MPU_Control)
142{
143 __DSB();
144 __ISB();
145 MPU_NS->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
146#ifdef SCB_SHCSR_MEMFAULTENA_Msk
147 SCB_NS->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
148#endif
149}
150
153__STATIC_INLINE void ARM_MPU_Disable_NS(void)
154{
155 __DSB();
156 __ISB();
157#ifdef SCB_SHCSR_MEMFAULTENA_Msk
158 SCB_NS->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
159#endif
160 MPU_NS->CTRL &= ~MPU_CTRL_ENABLE_Msk;
161}
162#endif
163
169__STATIC_INLINE void ARM_MPU_SetMemAttrEx(MPU_Type* mpu, uint8_t idx, uint8_t attr)
170{
171 const uint8_t reg = idx / 4U;
172 const uint32_t pos = ((idx % 4U) * 8U);
173 const uint32_t mask = 0xFFU << pos;
174
175 if (reg >= (sizeof(mpu->MAIR) / sizeof(mpu->MAIR[0]))) {
176 return; // invalid index
177 }
178
179 mpu->MAIR[reg] = ((mpu->MAIR[reg] & ~mask) | ((attr << pos) & mask));
180}
181
186__STATIC_INLINE void ARM_MPU_SetMemAttr(uint8_t idx, uint8_t attr)
187{
188 ARM_MPU_SetMemAttrEx(MPU, idx, attr);
189}
190
191#ifdef MPU_NS
196__STATIC_INLINE void ARM_MPU_SetMemAttr_NS(uint8_t idx, uint8_t attr)
197{
198 ARM_MPU_SetMemAttrEx(MPU_NS, idx, attr);
199}
200#endif
201
206__STATIC_INLINE void ARM_MPU_ClrRegionEx(MPU_Type* mpu, uint32_t rnr)
207{
208 mpu->RNR = rnr;
209 mpu->RLAR = 0U;
210}
211
216{
217 ARM_MPU_ClrRegionEx(MPU, rnr);
218}
219
220#ifdef MPU_NS
224__STATIC_INLINE void ARM_MPU_ClrRegion_NS(uint32_t rnr)
225{
226 ARM_MPU_ClrRegionEx(MPU_NS, rnr);
227}
228#endif
229
236__STATIC_INLINE void ARM_MPU_SetRegionEx(MPU_Type* mpu, uint32_t rnr, uint32_t rbar, uint32_t rlar)
237{
238 mpu->RNR = rnr;
239 mpu->RBAR = rbar;
240 mpu->RLAR = rlar;
241}
242
248__STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rnr, uint32_t rbar, uint32_t rlar)
249{
250 ARM_MPU_SetRegionEx(MPU, rnr, rbar, rlar);
251}
252
253#ifdef MPU_NS
259__STATIC_INLINE void ARM_MPU_SetRegion_NS(uint32_t rnr, uint32_t rbar, uint32_t rlar)
260{
261 ARM_MPU_SetRegionEx(MPU_NS, rnr, rbar, rlar);
262}
263#endif
264
270__STATIC_INLINE void orderedCpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)
271{
272 uint32_t i;
273 for (i = 0U; i < len; ++i)
274 {
275 dst[i] = src[i];
276 }
277}
278
285__STATIC_INLINE void ARM_MPU_LoadEx(MPU_Type* mpu, uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
286{
287 const uint32_t rowWordSize = sizeof(ARM_MPU_Region_t)/4U;
288 if (cnt == 1U) {
289 mpu->RNR = rnr;
290 orderedCpy(&(mpu->RBAR), &(table->RBAR), rowWordSize);
291 } else {
292 uint32_t rnrBase = rnr & ~(MPU_TYPE_RALIASES-1U);
293 uint32_t rnrOffset = rnr % MPU_TYPE_RALIASES;
294
295 mpu->RNR = rnrBase;
296 while ((rnrOffset + cnt) > MPU_TYPE_RALIASES) {
297 uint32_t c = MPU_TYPE_RALIASES - rnrOffset;
298 orderedCpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), c*rowWordSize);
299 table += c;
300 cnt -= c;
301 rnrOffset = 0U;
302 rnrBase += MPU_TYPE_RALIASES;
303 mpu->RNR = rnrBase;
304 }
305
306 orderedCpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), cnt*rowWordSize);
307 }
308}
309
315__STATIC_INLINE void ARM_MPU_Load(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
316{
317 ARM_MPU_LoadEx(MPU, rnr, table, cnt);
318}
319
320#ifdef MPU_NS
326__STATIC_INLINE void ARM_MPU_Load_NS(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
327{
328 ARM_MPU_LoadEx(MPU_NS, rnr, table, cnt);
329}
330#endif
331
332#endif
333
#define __RESTRICT
Definition: cmsis_armcc.h:101
#define __STATIC_INLINE
Definition: cmsis_armcc.h:59
#define __DSB()
Data Synchronization Barrier.
Definition: cmsis_armcc.h:429
#define __ISB()
Instruction Synchronization Barrier.
Definition: cmsis_armcc.h:418
#define SCB_SHCSR_MEMFAULTENA_Msk
Definition: core_armv8mml.h:694
#define SCB
Definition: core_armv8mbl.h:1122
__STATIC_INLINE void ARM_MPU_ClrRegionEx(MPU_Type *mpu, uint32_t rnr)
Clear and disable the given MPU region of the given MPU.
Definition: mpu_armv8.h:206
__STATIC_INLINE void ARM_MPU_SetMemAttrEx(MPU_Type *mpu, uint8_t idx, uint8_t attr)
Set the memory attribute encoding to the given MPU.
Definition: mpu_armv8.h:169
__STATIC_INLINE void ARM_MPU_SetRegionEx(MPU_Type *mpu, uint32_t rnr, uint32_t rbar, uint32_t rlar)
Configure the given MPU region of the given MPU.
Definition: mpu_armv8.h:236
__STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)
Enable the MPU.
Definition: mpu_armv8.h:115
__STATIC_INLINE void ARM_MPU_Disable(void)
Disable the MPU.
Definition: mpu_armv8.h:127
__STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rnr, uint32_t rbar, uint32_t rlar)
Configure the given MPU region.
Definition: mpu_armv8.h:248
__STATIC_INLINE void orderedCpy(volatile uint32_t *dst, const uint32_t *__RESTRICT src, uint32_t len)
Memcopy with strictly ordered memory access, e.g.
Definition: mpu_armv8.h:270
__STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)
Clear and disable the given MPU region.
Definition: mpu_armv8.h:215
__STATIC_INLINE void ARM_MPU_SetMemAttr(uint8_t idx, uint8_t attr)
Set the memory attribute encoding.
Definition: mpu_armv8.h:186
__STATIC_INLINE void ARM_MPU_LoadEx(MPU_Type *mpu, uint32_t rnr, ARM_MPU_Region_t const *table, uint32_t cnt)
Load the given number of MPU regions from a table to the given MPU.
Definition: mpu_armv8.h:285
__STATIC_INLINE void ARM_MPU_Load(uint32_t rnr, ARM_MPU_Region_t const *table, uint32_t cnt)
Load the given number of MPU regions from a table.
Definition: mpu_armv8.h:315
Struct for a single MPU Region.
Definition: mpu_armv7.h:180
uint32_t RLAR
Definition: mpu_armv8.h:109
uint32_t RBAR
The region base address register value (RBAR)
Definition: mpu_armv7.h:181