mpu_armv7.h 6.7 KB

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