]> begriffs open source - cmsis/blob - CMSIS/Core/Include/mpu_armv7.h
Core: Fixed minor compiler and MISRA warnings.
[cmsis] / CMSIS / Core / Include / mpu_armv7.h
1 /******************************************************************************
2  * @file     mpu_armv7.h
3  * @brief    CMSIS MPU API for ARMv7 MPU
4  * @version  V5.0.3
5  * @date     09. August 2017
6  ******************************************************************************/
7 /*
8  * Copyright (c) 2017 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 #ifndef ARM_MPU_ARMV7_H
26 #define ARM_MPU_ARMV7_H
27
28 #define ARM_MPU_REGION_SIZE_32B      ((uint8_t)0x04U)
29 #define ARM_MPU_REGION_SIZE_64B      ((uint8_t)0x05U)
30 #define ARM_MPU_REGION_SIZE_128B     ((uint8_t)0x06U)
31 #define ARM_MPU_REGION_SIZE_256B     ((uint8_t)0x07U)
32 #define ARM_MPU_REGION_SIZE_512B     ((uint8_t)0x08U)
33 #define ARM_MPU_REGION_SIZE_1KB      ((uint8_t)0x09U)
34 #define ARM_MPU_REGION_SIZE_2KB      ((uint8_t)0x0AU)
35 #define ARM_MPU_REGION_SIZE_4KB      ((uint8_t)0x0BU)
36 #define ARM_MPU_REGION_SIZE_8KB      ((uint8_t)0x0CU)
37 #define ARM_MPU_REGION_SIZE_16KB     ((uint8_t)0x0DU)
38 #define ARM_MPU_REGION_SIZE_32KB     ((uint8_t)0x0EU)
39 #define ARM_MPU_REGION_SIZE_64KB     ((uint8_t)0x0FU)
40 #define ARM_MPU_REGION_SIZE_128KB    ((uint8_t)0x10U)
41 #define ARM_MPU_REGION_SIZE_256KB    ((uint8_t)0x11U)
42 #define ARM_MPU_REGION_SIZE_512KB    ((uint8_t)0x12U)
43 #define ARM_MPU_REGION_SIZE_1MB      ((uint8_t)0x13U)
44 #define ARM_MPU_REGION_SIZE_2MB      ((uint8_t)0x14U)
45 #define ARM_MPU_REGION_SIZE_4MB      ((uint8_t)0x15U)
46 #define ARM_MPU_REGION_SIZE_8MB      ((uint8_t)0x16U)
47 #define ARM_MPU_REGION_SIZE_16MB     ((uint8_t)0x17U)
48 #define ARM_MPU_REGION_SIZE_32MB     ((uint8_t)0x18U)
49 #define ARM_MPU_REGION_SIZE_64MB     ((uint8_t)0x19U)
50 #define ARM_MPU_REGION_SIZE_128MB    ((uint8_t)0x1AU)
51 #define ARM_MPU_REGION_SIZE_256MB    ((uint8_t)0x1BU)
52 #define ARM_MPU_REGION_SIZE_512MB    ((uint8_t)0x1CU)
53 #define ARM_MPU_REGION_SIZE_1GB      ((uint8_t)0x1DU)
54 #define ARM_MPU_REGION_SIZE_2GB      ((uint8_t)0x1EU)
55 #define ARM_MPU_REGION_SIZE_4GB      ((uint8_t)0x1FU)
56
57 #define ARM_MPU_AP_NONE 0U 
58 #define ARM_MPU_AP_PRIV 1U
59 #define ARM_MPU_AP_URO  2U
60 #define ARM_MPU_AP_FULL 3U
61 #define ARM_MPU_AP_PRO  5U
62 #define ARM_MPU_AP_RO   6U
63
64 /** MPU Region Base Address Register Value
65 *
66 * \param Region The region to be configured, number 0 to 15.
67 * \param BaseAddress The base address for the region.
68 */
69 #define ARM_MPU_RBAR(Region, BaseAddress) \
70   (((BaseAddress) & MPU_RBAR_ADDR_Msk) |  \
71    ((Region) & MPU_RBAR_REGION_Msk)    |  \
72    (MPU_RBAR_VALID_Msk))
73
74 /**
75 * MPU Region Attribut and Size Register Value
76
77 * \param DisableExec       Instruction access disable bit, 1= disable instruction fetches.
78 * \param AccessPermission  Data access permissions, allows you to configure read/write access for User and Privileged mode.
79 * \param TypeExtField      Type extension field, allows you to configure memory access type, for example strongly ordered, peripheral.
80 * \param IsShareable       Region is shareable between multiple bus masters.
81 * \param IsCacheable       Region is cacheable, i.e. its value may be kept in cache.
82 * \param IsBufferable      Region is bufferable, i.e. using write-back caching. Cacheable but non-bufferable regions use write-through policy.
83 * \param SubRegionDisable  Sub-region disable field.
84 * \param Size              Region size of the region to be configured, for example 4K, 8K.
85 */                         
86 #define ARM_MPU_RASR(DisableExec, AccessPermission, TypeExtField, IsShareable, IsCacheable, IsBufferable, SubRegionDisable, Size) \
87   ((((DisableExec     ) << MPU_RASR_XN_Pos)     & MPU_RASR_XN_Msk)     | \
88    (((AccessPermission) << MPU_RASR_AP_Pos)     & MPU_RASR_AP_Msk)     | \
89    (((TypeExtField    ) << MPU_RASR_TEX_Pos)    & MPU_RASR_TEX_Msk)    | \
90    (((IsShareable     ) << MPU_RASR_S_Pos)      & MPU_RASR_S_Msk)      | \
91    (((IsCacheable     ) << MPU_RASR_C_Pos)      & MPU_RASR_C_Msk)      | \
92    (((IsBufferable    ) << MPU_RASR_B_Pos)      & MPU_RASR_B_Msk)      | \
93    (((SubRegionDisable) << MPU_RASR_SRD_Pos)    & MPU_RASR_SRD_Msk)    | \
94    (((Size            ) << MPU_RASR_SIZE_Pos)   & MPU_RASR_SIZE_Msk)   | \
95    (MPU_RASR_ENABLE_Msk))
96
97
98 /**
99 * Struct for a single MPU Region
100 */
101 typedef struct _ARM_MPU_Region_t {
102   uint32_t RBAR; //!< The region base address register value (RBAR)
103   uint32_t RASR; //!< The region attribute and size register value (RASR) \ref MPU_RASR
104 } ARM_MPU_Region_t;
105     
106 /** Enable the MPU.
107 * \param MPU_Control Default access permissions for unconfigured regions.
108 */
109 __STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)
110 {
111   __DSB();
112   __ISB();
113   MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
114 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
115   SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
116 #endif
117 }
118
119 /** Disable the MPU.
120 */
121 __STATIC_INLINE void ARM_MPU_Disable(void)
122 {
123   __DSB();
124   __ISB();
125 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
126   SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
127 #endif
128   MPU->CTRL  &= ~MPU_CTRL_ENABLE_Msk;
129 }
130
131 /** Clear and disable the given MPU region.
132 * \param rnr Region number to be cleared.
133 */
134 __STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)
135 {
136   MPU->RNR = rnr;
137   MPU->RASR = 0U;
138 }
139
140 /** Configure an MPU region.
141 * \param rbar Value for RBAR register.
142 * \param rsar Value for RSAR register.
143 */   
144 __STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rbar, uint32_t rasr)
145 {
146   MPU->RBAR = rbar;
147   MPU->RASR = rasr;
148 }
149
150 /** Configure the given MPU region.
151 * \param rnr Region number to be configured.
152 * \param rbar Value for RBAR register.
153 * \param rsar Value for RSAR register.
154 */   
155 __STATIC_INLINE void ARM_MPU_SetRegionEx(uint32_t rnr, uint32_t rbar, uint32_t rasr)
156 {
157   MPU->RNR = rnr;
158   MPU->RBAR = rbar;
159   MPU->RASR = rasr;
160 }
161
162 /** Memcopy with strictly ordered memory access, e.g. for register targets.
163 * \param dst Destination data is copied to.
164 * \param src Source data is copied from.
165 * \param len Amount of data words to be copied.
166 */
167 __STATIC_INLINE void orderedCpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)
168 {
169   uint32_t i;
170   for (i = 0U; i < len; ++i) 
171   {
172     dst[i] = src[i];
173   }
174 }
175
176 /** Load the given number of MPU regions from a table.
177 * \param table Pointer to the MPU configuration table.
178 * \param cnt Amount of regions to be configured.
179 */
180 __STATIC_INLINE void ARM_MPU_Load(ARM_MPU_Region_t const* table, uint32_t cnt) 
181 {
182   static const uint32_t rowWordSize = sizeof(ARM_MPU_Region_t)/4U;
183   if (cnt > MPU_TYPE_RALIASES) {
184     orderedCpy(&(MPU->RBAR), &(table->RBAR), MPU_TYPE_RALIASES*rowWordSize);
185     ARM_MPU_Load(table+MPU_TYPE_RALIASES, cnt-MPU_TYPE_RALIASES);
186   } else {
187     orderedCpy(&(MPU->RBAR), &(table->RBAR), cnt*rowWordSize);
188   }
189 }
190
191 #endif