mpu_armv7.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /******************************************************************************
  2. * @file mpu_armv7.h
  3. * @brief CMSIS MPU API for Armv7-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. #if defined ( __ICCARM__ )
  25. #pragma system_include /* treat file as system include file for MISRA check */
  26. #elif defined (__clang__)
  27. #pragma clang system_header /* treat file as system include file */
  28. #endif
  29. #ifndef ARM_MPU_ARMV7_H
  30. #define ARM_MPU_ARMV7_H
  31. #define ARM_MPU_REGION_SIZE_32B ((uint8_t)0x04U)
  32. #define ARM_MPU_REGION_SIZE_64B ((uint8_t)0x05U)
  33. #define ARM_MPU_REGION_SIZE_128B ((uint8_t)0x06U)
  34. #define ARM_MPU_REGION_SIZE_256B ((uint8_t)0x07U)
  35. #define ARM_MPU_REGION_SIZE_512B ((uint8_t)0x08U)
  36. #define ARM_MPU_REGION_SIZE_1KB ((uint8_t)0x09U)
  37. #define ARM_MPU_REGION_SIZE_2KB ((uint8_t)0x0AU)
  38. #define ARM_MPU_REGION_SIZE_4KB ((uint8_t)0x0BU)
  39. #define ARM_MPU_REGION_SIZE_8KB ((uint8_t)0x0CU)
  40. #define ARM_MPU_REGION_SIZE_16KB ((uint8_t)0x0DU)
  41. #define ARM_MPU_REGION_SIZE_32KB ((uint8_t)0x0EU)
  42. #define ARM_MPU_REGION_SIZE_64KB ((uint8_t)0x0FU)
  43. #define ARM_MPU_REGION_SIZE_128KB ((uint8_t)0x10U)
  44. #define ARM_MPU_REGION_SIZE_256KB ((uint8_t)0x11U)
  45. #define ARM_MPU_REGION_SIZE_512KB ((uint8_t)0x12U)
  46. #define ARM_MPU_REGION_SIZE_1MB ((uint8_t)0x13U)
  47. #define ARM_MPU_REGION_SIZE_2MB ((uint8_t)0x14U)
  48. #define ARM_MPU_REGION_SIZE_4MB ((uint8_t)0x15U)
  49. #define ARM_MPU_REGION_SIZE_8MB ((uint8_t)0x16U)
  50. #define ARM_MPU_REGION_SIZE_16MB ((uint8_t)0x17U)
  51. #define ARM_MPU_REGION_SIZE_32MB ((uint8_t)0x18U)
  52. #define ARM_MPU_REGION_SIZE_64MB ((uint8_t)0x19U)
  53. #define ARM_MPU_REGION_SIZE_128MB ((uint8_t)0x1AU)
  54. #define ARM_MPU_REGION_SIZE_256MB ((uint8_t)0x1BU)
  55. #define ARM_MPU_REGION_SIZE_512MB ((uint8_t)0x1CU)
  56. #define ARM_MPU_REGION_SIZE_1GB ((uint8_t)0x1DU)
  57. #define ARM_MPU_REGION_SIZE_2GB ((uint8_t)0x1EU)
  58. #define ARM_MPU_REGION_SIZE_4GB ((uint8_t)0x1FU)
  59. #define ARM_MPU_AP_NONE 0U
  60. #define ARM_MPU_AP_PRIV 1U
  61. #define ARM_MPU_AP_URO 2U
  62. #define ARM_MPU_AP_FULL 3U
  63. #define ARM_MPU_AP_PRO 5U
  64. #define ARM_MPU_AP_RO 6U
  65. /** MPU Region Base Address Register Value
  66. *
  67. * \param Region The region to be configured, number 0 to 15.
  68. * \param BaseAddress The base address for the region.
  69. */
  70. #define ARM_MPU_RBAR(Region, BaseAddress) \
  71. (((BaseAddress) & MPU_RBAR_ADDR_Msk) | \
  72. ((Region) & MPU_RBAR_REGION_Msk) | \
  73. (MPU_RBAR_VALID_Msk))
  74. /**
  75. * MPU Region Attribute 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. * Struct for a single MPU Region
  98. */
  99. typedef struct {
  100. uint32_t RBAR; //!< The region base address register value (RBAR)
  101. uint32_t RASR; //!< The region attribute and size register value (RASR) \ref MPU_RASR
  102. } ARM_MPU_Region_t;
  103. /** Enable the MPU.
  104. * \param MPU_Control Default access permissions for unconfigured regions.
  105. */
  106. __STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)
  107. {
  108. __DSB();
  109. __ISB();
  110. MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
  111. #ifdef SCB_SHCSR_MEMFAULTENA_Msk
  112. SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
  113. #endif
  114. }
  115. /** Disable the MPU.
  116. */
  117. __STATIC_INLINE void ARM_MPU_Disable(void)
  118. {
  119. __DSB();
  120. __ISB();
  121. #ifdef SCB_SHCSR_MEMFAULTENA_Msk
  122. SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
  123. #endif
  124. MPU->CTRL &= ~MPU_CTRL_ENABLE_Msk;
  125. }
  126. /** Clear and disable the given MPU region.
  127. * \param rnr Region number to be cleared.
  128. */
  129. __STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)
  130. {
  131. MPU->RNR = rnr;
  132. MPU->RASR = 0U;
  133. }
  134. /** Configure an MPU region.
  135. * \param rbar Value for RBAR register.
  136. * \param rsar Value for RSAR register.
  137. */
  138. __STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rbar, uint32_t rasr)
  139. {
  140. MPU->RBAR = rbar;
  141. MPU->RASR = rasr;
  142. }
  143. /** Configure the given MPU region.
  144. * \param rnr Region number to be configured.
  145. * \param rbar Value for RBAR register.
  146. * \param rsar Value for RSAR register.
  147. */
  148. __STATIC_INLINE void ARM_MPU_SetRegionEx(uint32_t rnr, uint32_t rbar, uint32_t rasr)
  149. {
  150. MPU->RNR = rnr;
  151. MPU->RBAR = rbar;
  152. MPU->RASR = rasr;
  153. }
  154. /** Memcopy with strictly ordered memory access, e.g. for register targets.
  155. * \param dst Destination data is copied to.
  156. * \param src Source data is copied from.
  157. * \param len Amount of data words to be copied.
  158. */
  159. __STATIC_INLINE void orderedCpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)
  160. {
  161. uint32_t i;
  162. for (i = 0U; i < len; ++i)
  163. {
  164. dst[i] = src[i];
  165. }
  166. }
  167. /** Load the given number of MPU regions from a table.
  168. * \param table Pointer to the MPU configuration table.
  169. * \param cnt Amount of regions to be configured.
  170. */
  171. __STATIC_INLINE void ARM_MPU_Load(ARM_MPU_Region_t const* table, uint32_t cnt)
  172. {
  173. const uint32_t rowWordSize = sizeof(ARM_MPU_Region_t)/4U;
  174. while (cnt > MPU_TYPE_RALIASES) {
  175. orderedCpy(&(MPU->RBAR), &(table->RBAR), MPU_TYPE_RALIASES*rowWordSize);
  176. table += MPU_TYPE_RALIASES;
  177. cnt -= MPU_TYPE_RALIASES;
  178. }
  179. orderedCpy(&(MPU->RBAR), &(table->RBAR), cnt*rowWordSize);
  180. }
  181. #endif