mpu_v7m.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file common/ARMCMx/mpu_v7m.h
  15. * @brief ARMv7-M MPU support macros and structures.
  16. *
  17. * @addtogroup COMMON_ARMCMx_MPUv7M
  18. * @{
  19. */
  20. #ifndef MPUV7M_H
  21. #define MPUV7M_H
  22. /* Other layers may include another header named mpu.h which is perfectly
  23. compatible, doing a check here to avoid name conflicts.*/
  24. #ifndef MPU_H
  25. /*===========================================================================*/
  26. /* Driver constants. */
  27. /*===========================================================================*/
  28. /**
  29. * @name MPU registers definitions
  30. * @{
  31. */
  32. #define MPU_TYPE_SEPARATED (1U << 0U)
  33. #define MPU_TYPE_DREGION(n) (((n) >> 8U) & 255U)
  34. #define MPU_TYPE_IREGION(n) (((n) >> 16U) & 255U)
  35. #define MPU_CTRL_ENABLE (1U << 0U)
  36. #define MPU_CTRL_HFNMIENA (1U << 1U)
  37. #define MPU_CTRL_PRIVDEFENA (1U << 2U)
  38. #define MPU_RNR_REGION_MASK (255U << 0U)
  39. #define MPU_RNR_REGION(n) ((n) << 0U)
  40. #define MPU_RBAR_REGION_MASK (15U << 0U)
  41. #define MPU_RBAR_REGION(n) ((n) << 0U)
  42. #define MPU_RBAR_VALID (1U << 4U)
  43. #define MPU_RBAR_ADDR_MASK 0xFFFFFFE0U
  44. #define MPU_RBAR_ADDR(n) ((n) << 5U)
  45. #define MPU_RASR_ENABLE (1U << 0U)
  46. #define MPU_RASR_SIZE_MASK (31U << 1U)
  47. #define MPU_RASR_SIZE(n) ((n) << 1U)
  48. #define MPU_RASR_SIZE_32 MPU_RASR_SIZE(4U)
  49. #define MPU_RASR_SIZE_64 MPU_RASR_SIZE(5U)
  50. #define MPU_RASR_SIZE_128 MPU_RASR_SIZE(6U)
  51. #define MPU_RASR_SIZE_256 MPU_RASR_SIZE(7U)
  52. #define MPU_RASR_SIZE_512 MPU_RASR_SIZE(8U)
  53. #define MPU_RASR_SIZE_1K MPU_RASR_SIZE(9U)
  54. #define MPU_RASR_SIZE_2K MPU_RASR_SIZE(10U)
  55. #define MPU_RASR_SIZE_4K MPU_RASR_SIZE(11U)
  56. #define MPU_RASR_SIZE_8K MPU_RASR_SIZE(12U)
  57. #define MPU_RASR_SIZE_16K MPU_RASR_SIZE(13U)
  58. #define MPU_RASR_SIZE_32K MPU_RASR_SIZE(14U)
  59. #define MPU_RASR_SIZE_64K MPU_RASR_SIZE(15U)
  60. #define MPU_RASR_SIZE_128K MPU_RASR_SIZE(16U)
  61. #define MPU_RASR_SIZE_256K MPU_RASR_SIZE(17U)
  62. #define MPU_RASR_SIZE_512K MPU_RASR_SIZE(18U)
  63. #define MPU_RASR_SIZE_1M MPU_RASR_SIZE(19U)
  64. #define MPU_RASR_SIZE_2M MPU_RASR_SIZE(20U)
  65. #define MPU_RASR_SIZE_4M MPU_RASR_SIZE(21U)
  66. #define MPU_RASR_SIZE_8M MPU_RASR_SIZE(22U)
  67. #define MPU_RASR_SIZE_16M MPU_RASR_SIZE(23U)
  68. #define MPU_RASR_SIZE_32M MPU_RASR_SIZE(24U)
  69. #define MPU_RASR_SIZE_64M MPU_RASR_SIZE(25U)
  70. #define MPU_RASR_SIZE_128M MPU_RASR_SIZE(26U)
  71. #define MPU_RASR_SIZE_256M MPU_RASR_SIZE(27U)
  72. #define MPU_RASR_SIZE_512M MPU_RASR_SIZE(28U)
  73. #define MPU_RASR_SIZE_1G MPU_RASR_SIZE(29U)
  74. #define MPU_RASR_SIZE_2G MPU_RASR_SIZE(30U)
  75. #define MPU_RASR_SIZE_4G MPU_RASR_SIZE(31U)
  76. #define MPU_RASR_SRD_MASK (255U << 8U)
  77. #define MPU_RASR_SRD(n) ((n) << 8U)
  78. #define MPU_RASR_SRD_ALL (0U << 8U)
  79. #define MPU_RASR_SRD_DISABLE_SUB0 (1U << 8U)
  80. #define MPU_RASR_SRD_DISABLE_SUB1 (2U << 8U)
  81. #define MPU_RASR_SRD_DISABLE_SUB2 (4U << 8U)
  82. #define MPU_RASR_SRD_DISABLE_SUB3 (8U << 8U)
  83. #define MPU_RASR_SRD_DISABLE_SUB4 (16U << 8U)
  84. #define MPU_RASR_SRD_DISABLE_SUB5 (32U << 8U)
  85. #define MPU_RASR_SRD_DISABLE_SUB6 (64U << 8U)
  86. #define MPU_RASR_SRD_DISABLE_SUB7 (128U << 8U)
  87. #define MPU_RASR_ATTR_B (1U << 16U)
  88. #define MPU_RASR_ATTR_C (1U << 17U)
  89. #define MPU_RASR_ATTR_S (1U << 18U)
  90. #define MPU_RASR_ATTR_TEX_MASK (7U << 19U)
  91. #define MPU_RASR_ATTR_TEX(n) ((n) << 19U)
  92. #define MPU_RASR_ATTR_AP_MASK (7U << 24U)
  93. #define MPU_RASR_ATTR_AP(n) ((n) << 24U)
  94. #define MPU_RASR_ATTR_AP_NA_NA (0U << 24U)
  95. #define MPU_RASR_ATTR_AP_RW_NA (1U << 24U)
  96. #define MPU_RASR_ATTR_AP_RW_RO (2U << 24U)
  97. #define MPU_RASR_ATTR_AP_RW_RW (3U << 24U)
  98. #define MPU_RASR_ATTR_AP_RO_NA (5U << 24U)
  99. #define MPU_RASR_ATTR_AP_RO_RO (6U << 24U)
  100. #define MPU_RASR_ATTR_XN (1U << 28U)
  101. /** @} */
  102. /**
  103. * @name Region attributes
  104. * @{
  105. */
  106. #define MPU_RASR_ATTR_STRONGLY_ORDERED (MPU_RASR_ATTR_TEX(0))
  107. #define MPU_RASR_ATTR_SHARED_DEVICE (MPU_RASR_ATTR_TEX(0) | MPU_RASR_ATTR_B)
  108. #define MPU_RASR_ATTR_CACHEABLE_WT_NWA (MPU_RASR_ATTR_TEX(0) | MPU_RASR_ATTR_C)
  109. #define MPU_RASR_ATTR_CACHEABLE_WB_NWA (MPU_RASR_ATTR_TEX(0) | MPU_RASR_ATTR_B | MPU_RASR_ATTR_C)
  110. #define MPU_RASR_ATTR_NON_CACHEABLE (MPU_RASR_ATTR_TEX(1))
  111. #define MPU_RASR_ATTR_CACHEABLE_WB_WA (MPU_RASR_ATTR_TEX(1) | MPU_RASR_ATTR_B | MPU_RASR_ATTR_C)
  112. #define MPU_RASR_ATTR_NON_SHARED_DEVICE (MPU_RASR_ATTR_TEX(2))
  113. /** @} */
  114. /**
  115. * @name Region identifiers
  116. * @{
  117. */
  118. #define MPU_REGION_0 0U
  119. #define MPU_REGION_1 1U
  120. #define MPU_REGION_2 2U
  121. #define MPU_REGION_3 3U
  122. #define MPU_REGION_4 4U
  123. #define MPU_REGION_5 5U
  124. #define MPU_REGION_6 6U
  125. #define MPU_REGION_7 7U
  126. /** @} */
  127. /*===========================================================================*/
  128. /* Driver pre-compile time settings. */
  129. /*===========================================================================*/
  130. /*===========================================================================*/
  131. /* Derived constants and error checks. */
  132. /*===========================================================================*/
  133. /*===========================================================================*/
  134. /* Driver data structures and types. */
  135. /*===========================================================================*/
  136. /*===========================================================================*/
  137. /* Driver macros. */
  138. /*===========================================================================*/
  139. /**
  140. * @brief Enables the MPU.
  141. * @note MEMFAULENA is enabled in SCB_SHCSR.
  142. *
  143. * @param[in] ctrl MPU control modes as defined in @p MPU_CTRL register,
  144. * the enable bit is enforced
  145. *
  146. * @api
  147. */
  148. #define mpuEnable(ctrl) { \
  149. MPU->CTRL = ((uint32_t)ctrl) | MPU_CTRL_ENABLE; \
  150. SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk; \
  151. }
  152. /**
  153. * @brief Disables the MPU.
  154. * @note MEMFAULENA is disabled in SCB_SHCSR.
  155. *
  156. * @api
  157. */
  158. #define mpuDisable() { \
  159. SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk; \
  160. MPU->CTRL = 0; \
  161. }
  162. /**
  163. * @brief Configures an MPU region.
  164. *
  165. * @param[in] region the region number
  166. * @param[in] address start address of the region, note, there are alignment
  167. * constraints
  168. * @param[in] attribs attributes mask as defined in @p MPU_RASR register
  169. *
  170. * @api
  171. */
  172. #define mpuConfigureRegion(region, addr, attribs) { \
  173. MPU->RNR = ((uint32_t)region); \
  174. MPU->RBAR = ((uint32_t)addr); \
  175. MPU->RASR = ((uint32_t)attribs); \
  176. }
  177. /*===========================================================================*/
  178. /* External declarations. */
  179. /*===========================================================================*/
  180. #ifdef __cplusplus
  181. extern "C" {
  182. #endif
  183. #ifdef __cplusplus
  184. }
  185. #endif
  186. #endif /* MPU_H */
  187. #endif /* MPUV7M_H */
  188. /** @} */