Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:03

0001 /******************************************************************************
0002  * @file     mpu_armv7.h
0003  * @brief    CMSIS MPU API for Armv7-M MPU
0004  * @version  V5.1.2
0005  * @date     25. May 2020
0006  ******************************************************************************/
0007 /*
0008  * Copyright (c) 2017-2020 Arm Limited. All rights reserved.
0009  *
0010  * SPDX-License-Identifier: Apache-2.0
0011  *
0012  * Licensed under the Apache License, Version 2.0 (the License); you may
0013  * not use this file except in compliance with the License.
0014  * You may obtain a copy of the License at
0015  *
0016  * www.apache.org/licenses/LICENSE-2.0
0017  *
0018  * Unless required by applicable law or agreed to in writing, software
0019  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
0020  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0021  * See the License for the specific language governing permissions and
0022  * limitations under the License.
0023  */
0024  
0025 #if   defined ( __ICCARM__ )
0026   #pragma system_include         /* treat file as system include file for MISRA check */
0027 #elif defined (__clang__)
0028   #pragma clang system_header    /* treat file as system include file */
0029 #endif
0030  
0031 #ifndef ARM_MPU_ARMV7_H
0032 #define ARM_MPU_ARMV7_H
0033 
0034 #define ARM_MPU_REGION_SIZE_32B      ((uint8_t)0x04U) ///!< MPU Region Size 32 Bytes
0035 #define ARM_MPU_REGION_SIZE_64B      ((uint8_t)0x05U) ///!< MPU Region Size 64 Bytes
0036 #define ARM_MPU_REGION_SIZE_128B     ((uint8_t)0x06U) ///!< MPU Region Size 128 Bytes
0037 #define ARM_MPU_REGION_SIZE_256B     ((uint8_t)0x07U) ///!< MPU Region Size 256 Bytes
0038 #define ARM_MPU_REGION_SIZE_512B     ((uint8_t)0x08U) ///!< MPU Region Size 512 Bytes
0039 #define ARM_MPU_REGION_SIZE_1KB      ((uint8_t)0x09U) ///!< MPU Region Size 1 KByte
0040 #define ARM_MPU_REGION_SIZE_2KB      ((uint8_t)0x0AU) ///!< MPU Region Size 2 KBytes
0041 #define ARM_MPU_REGION_SIZE_4KB      ((uint8_t)0x0BU) ///!< MPU Region Size 4 KBytes
0042 #define ARM_MPU_REGION_SIZE_8KB      ((uint8_t)0x0CU) ///!< MPU Region Size 8 KBytes
0043 #define ARM_MPU_REGION_SIZE_16KB     ((uint8_t)0x0DU) ///!< MPU Region Size 16 KBytes
0044 #define ARM_MPU_REGION_SIZE_32KB     ((uint8_t)0x0EU) ///!< MPU Region Size 32 KBytes
0045 #define ARM_MPU_REGION_SIZE_64KB     ((uint8_t)0x0FU) ///!< MPU Region Size 64 KBytes
0046 #define ARM_MPU_REGION_SIZE_128KB    ((uint8_t)0x10U) ///!< MPU Region Size 128 KBytes
0047 #define ARM_MPU_REGION_SIZE_256KB    ((uint8_t)0x11U) ///!< MPU Region Size 256 KBytes
0048 #define ARM_MPU_REGION_SIZE_512KB    ((uint8_t)0x12U) ///!< MPU Region Size 512 KBytes
0049 #define ARM_MPU_REGION_SIZE_1MB      ((uint8_t)0x13U) ///!< MPU Region Size 1 MByte
0050 #define ARM_MPU_REGION_SIZE_2MB      ((uint8_t)0x14U) ///!< MPU Region Size 2 MBytes
0051 #define ARM_MPU_REGION_SIZE_4MB      ((uint8_t)0x15U) ///!< MPU Region Size 4 MBytes
0052 #define ARM_MPU_REGION_SIZE_8MB      ((uint8_t)0x16U) ///!< MPU Region Size 8 MBytes
0053 #define ARM_MPU_REGION_SIZE_16MB     ((uint8_t)0x17U) ///!< MPU Region Size 16 MBytes
0054 #define ARM_MPU_REGION_SIZE_32MB     ((uint8_t)0x18U) ///!< MPU Region Size 32 MBytes
0055 #define ARM_MPU_REGION_SIZE_64MB     ((uint8_t)0x19U) ///!< MPU Region Size 64 MBytes
0056 #define ARM_MPU_REGION_SIZE_128MB    ((uint8_t)0x1AU) ///!< MPU Region Size 128 MBytes
0057 #define ARM_MPU_REGION_SIZE_256MB    ((uint8_t)0x1BU) ///!< MPU Region Size 256 MBytes
0058 #define ARM_MPU_REGION_SIZE_512MB    ((uint8_t)0x1CU) ///!< MPU Region Size 512 MBytes
0059 #define ARM_MPU_REGION_SIZE_1GB      ((uint8_t)0x1DU) ///!< MPU Region Size 1 GByte
0060 #define ARM_MPU_REGION_SIZE_2GB      ((uint8_t)0x1EU) ///!< MPU Region Size 2 GBytes
0061 #define ARM_MPU_REGION_SIZE_4GB      ((uint8_t)0x1FU) ///!< MPU Region Size 4 GBytes
0062 
0063 #define ARM_MPU_AP_NONE 0U ///!< MPU Access Permission no access
0064 #define ARM_MPU_AP_PRIV 1U ///!< MPU Access Permission privileged access only
0065 #define ARM_MPU_AP_URO  2U ///!< MPU Access Permission unprivileged access read-only
0066 #define ARM_MPU_AP_FULL 3U ///!< MPU Access Permission full access
0067 #define ARM_MPU_AP_PRO  5U ///!< MPU Access Permission privileged access read-only
0068 #define ARM_MPU_AP_RO   6U ///!< MPU Access Permission read-only access
0069 
0070 /** MPU Region Base Address Register Value
0071 *
0072 * \param Region The region to be configured, number 0 to 15.
0073 * \param BaseAddress The base address for the region.
0074 */
0075 #define ARM_MPU_RBAR(Region, BaseAddress) \
0076   (((BaseAddress) & MPU_RBAR_ADDR_Msk) |  \
0077    ((Region) & MPU_RBAR_REGION_Msk)    |  \
0078    (MPU_RBAR_VALID_Msk))
0079 
0080 /**
0081 * MPU Memory Access Attributes
0082 * 
0083 * \param TypeExtField      Type extension field, allows you to configure memory access type, for example strongly ordered, peripheral.
0084 * \param IsShareable       Region is shareable between multiple bus masters.
0085 * \param IsCacheable       Region is cacheable, i.e. its value may be kept in cache.
0086 * \param IsBufferable      Region is bufferable, i.e. using write-back caching. Cacheable but non-bufferable regions use write-through policy.
0087 */  
0088 #define ARM_MPU_ACCESS_(TypeExtField, IsShareable, IsCacheable, IsBufferable)   \
0089   ((((TypeExtField) << MPU_RASR_TEX_Pos) & MPU_RASR_TEX_Msk)                  | \
0090    (((IsShareable)  << MPU_RASR_S_Pos)   & MPU_RASR_S_Msk)                    | \
0091    (((IsCacheable)  << MPU_RASR_C_Pos)   & MPU_RASR_C_Msk)                    | \
0092    (((IsBufferable) << MPU_RASR_B_Pos)   & MPU_RASR_B_Msk))
0093 
0094 /**
0095 * MPU Region Attribute and Size Register Value
0096 * 
0097 * \param DisableExec       Instruction access disable bit, 1= disable instruction fetches.
0098 * \param AccessPermission  Data access permissions, allows you to configure read/write access for User and Privileged mode.
0099 * \param AccessAttributes  Memory access attribution, see \ref ARM_MPU_ACCESS_.
0100 * \param SubRegionDisable  Sub-region disable field.
0101 * \param Size              Region size of the region to be configured, for example 4K, 8K.
0102 */
0103 #define ARM_MPU_RASR_EX(DisableExec, AccessPermission, AccessAttributes, SubRegionDisable, Size)    \
0104   ((((DisableExec)      << MPU_RASR_XN_Pos)   & MPU_RASR_XN_Msk)                                  | \
0105    (((AccessPermission) << MPU_RASR_AP_Pos)   & MPU_RASR_AP_Msk)                                  | \
0106    (((AccessAttributes) & (MPU_RASR_TEX_Msk | MPU_RASR_S_Msk | MPU_RASR_C_Msk | MPU_RASR_B_Msk))) | \
0107    (((SubRegionDisable) << MPU_RASR_SRD_Pos)  & MPU_RASR_SRD_Msk)                                 | \
0108    (((Size)             << MPU_RASR_SIZE_Pos) & MPU_RASR_SIZE_Msk)                                | \
0109    (((MPU_RASR_ENABLE_Msk))))
0110 
0111 /**
0112 * MPU Region Attribute and Size Register Value
0113 * 
0114 * \param DisableExec       Instruction access disable bit, 1= disable instruction fetches.
0115 * \param AccessPermission  Data access permissions, allows you to configure read/write access for User and Privileged mode.
0116 * \param TypeExtField      Type extension field, allows you to configure memory access type, for example strongly ordered, peripheral.
0117 * \param IsShareable       Region is shareable between multiple bus masters.
0118 * \param IsCacheable       Region is cacheable, i.e. its value may be kept in cache.
0119 * \param IsBufferable      Region is bufferable, i.e. using write-back caching. Cacheable but non-bufferable regions use write-through policy.
0120 * \param SubRegionDisable  Sub-region disable field.
0121 * \param Size              Region size of the region to be configured, for example 4K, 8K.
0122 */                         
0123 #define ARM_MPU_RASR(DisableExec, AccessPermission, TypeExtField, IsShareable, IsCacheable, IsBufferable, SubRegionDisable, Size) \
0124   ARM_MPU_RASR_EX(DisableExec, AccessPermission, ARM_MPU_ACCESS_(TypeExtField, IsShareable, IsCacheable, IsBufferable), SubRegionDisable, Size)
0125 
0126 /**
0127 * MPU Memory Access Attribute for strongly ordered memory.
0128 *  - TEX: 000b
0129 *  - Shareable
0130 *  - Non-cacheable
0131 *  - Non-bufferable
0132 */ 
0133 #define ARM_MPU_ACCESS_ORDERED ARM_MPU_ACCESS_(0U, 1U, 0U, 0U)
0134 
0135 /**
0136 * MPU Memory Access Attribute for device memory.
0137 *  - TEX: 000b (if shareable) or 010b (if non-shareable)
0138 *  - Shareable or non-shareable
0139 *  - Non-cacheable
0140 *  - Bufferable (if shareable) or non-bufferable (if non-shareable)
0141 *
0142 * \param IsShareable Configures the device memory as shareable or non-shareable.
0143 */ 
0144 #define ARM_MPU_ACCESS_DEVICE(IsShareable) ((IsShareable) ? ARM_MPU_ACCESS_(0U, 1U, 0U, 1U) : ARM_MPU_ACCESS_(2U, 0U, 0U, 0U))
0145 
0146 /**
0147 * MPU Memory Access Attribute for normal memory.
0148 *  - TEX: 1BBb (reflecting outer cacheability rules)
0149 *  - Shareable or non-shareable
0150 *  - Cacheable or non-cacheable (reflecting inner cacheability rules)
0151 *  - Bufferable or non-bufferable (reflecting inner cacheability rules)
0152 *
0153 * \param OuterCp Configures the outer cache policy.
0154 * \param InnerCp Configures the inner cache policy.
0155 * \param IsShareable Configures the memory as shareable or non-shareable.
0156 */ 
0157 #define ARM_MPU_ACCESS_NORMAL(OuterCp, InnerCp, IsShareable) ARM_MPU_ACCESS_((4U | (OuterCp)), IsShareable, ((InnerCp) >> 1U), ((InnerCp) & 1U))
0158 
0159 /**
0160 * MPU Memory Access Attribute non-cacheable policy.
0161 */
0162 #define ARM_MPU_CACHEP_NOCACHE 0U
0163 
0164 /**
0165 * MPU Memory Access Attribute write-back, write and read allocate policy.
0166 */
0167 #define ARM_MPU_CACHEP_WB_WRA 1U
0168 
0169 /**
0170 * MPU Memory Access Attribute write-through, no write allocate policy.
0171 */
0172 #define ARM_MPU_CACHEP_WT_NWA 2U
0173 
0174 /**
0175 * MPU Memory Access Attribute write-back, no write allocate policy.
0176 */
0177 #define ARM_MPU_CACHEP_WB_NWA 3U
0178 
0179 
0180 /**
0181 * Struct for a single MPU Region
0182 */
0183 typedef struct {
0184   uint32_t RBAR; //!< The region base address register value (RBAR)
0185   uint32_t RASR; //!< The region attribute and size register value (RASR) \ref MPU_RASR
0186 } ARM_MPU_Region_t;
0187     
0188 /** Enable the MPU.
0189 * \param MPU_Control Default access permissions for unconfigured regions.
0190 */
0191 __STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)
0192 {
0193   __DMB();
0194   MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
0195 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
0196   SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
0197 #endif
0198   __DSB();
0199   __ISB();
0200 }
0201 
0202 /** Disable the MPU.
0203 */
0204 __STATIC_INLINE void ARM_MPU_Disable(void)
0205 {
0206   __DMB();
0207 #ifdef SCB_SHCSR_MEMFAULTENA_Msk
0208   SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
0209 #endif
0210   MPU->CTRL  &= ~MPU_CTRL_ENABLE_Msk;
0211   __DSB();
0212   __ISB();
0213 }
0214 
0215 /** Clear and disable the given MPU region.
0216 * \param rnr Region number to be cleared.
0217 */
0218 __STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)
0219 {
0220   MPU->RNR = rnr;
0221   MPU->RASR = 0U;
0222 }
0223 
0224 /** Configure an MPU region.
0225 * \param rbar Value for RBAR register.
0226 * \param rasr Value for RASR register.
0227 */   
0228 __STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rbar, uint32_t rasr)
0229 {
0230   MPU->RBAR = rbar;
0231   MPU->RASR = rasr;
0232 }
0233 
0234 /** Configure the given MPU region.
0235 * \param rnr Region number to be configured.
0236 * \param rbar Value for RBAR register.
0237 * \param rasr Value for RASR register.
0238 */   
0239 __STATIC_INLINE void ARM_MPU_SetRegionEx(uint32_t rnr, uint32_t rbar, uint32_t rasr)
0240 {
0241   MPU->RNR = rnr;
0242   MPU->RBAR = rbar;
0243   MPU->RASR = rasr;
0244 }
0245 
0246 /** Memcpy with strictly ordered memory access, e.g. used by code in ARM_MPU_Load().
0247 * \param dst Destination data is copied to.
0248 * \param src Source data is copied from.
0249 * \param len Amount of data words to be copied.
0250 */
0251 __STATIC_INLINE void ARM_MPU_OrderedMemcpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)
0252 {
0253   uint32_t i;
0254   for (i = 0U; i < len; ++i) 
0255   {
0256     dst[i] = src[i];
0257   }
0258 }
0259 
0260 /** Load the given number of MPU regions from a table.
0261 * \param table Pointer to the MPU configuration table.
0262 * \param cnt Amount of regions to be configured.
0263 */
0264 __STATIC_INLINE void ARM_MPU_Load(ARM_MPU_Region_t const* table, uint32_t cnt) 
0265 {
0266   const uint32_t rowWordSize = sizeof(ARM_MPU_Region_t)/4U;
0267   while (cnt > MPU_TYPE_RALIASES) {
0268     ARM_MPU_OrderedMemcpy(&(MPU->RBAR), &(table->RBAR), MPU_TYPE_RALIASES*rowWordSize);
0269     table += MPU_TYPE_RALIASES;
0270     cnt -= MPU_TYPE_RALIASES;
0271   }
0272   ARM_MPU_OrderedMemcpy(&(MPU->RBAR), &(table->RBAR), cnt*rowWordSize);
0273 }
0274 
0275 #endif