硬汉嵌入式论坛

 找回密码
 立即注册
查看: 2614|回复: 0
收起左侧

[MPU] STM32H7的MPU支持的几个参数,方便以后直接查看配置

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106833
QQ
发表于 2018-11-7 00:38:08 | 显示全部楼层 |阅读模式
结构体:
  1. #if (__MPU_PRESENT == 1)
  2. /** @defgroup CORTEX_MPU_Region_Initialization_Structure_definition MPU Region Initialization Structure Definition
  3.   * @brief  MPU Region initialization structure
  4.   * @{
  5.   */
  6. typedef struct
  7. {
  8.   uint8_t                Enable;                /*!< Specifies the status of the region.
  9.                                                      This parameter can be a value of @ref CORTEX_MPU_Region_Enable                 */
  10.   uint8_t                Number;                /*!< Specifies the number of the region to protect.
  11.                                                      This parameter can be a value of @ref CORTEX_MPU_Region_Number                 */
  12.   uint32_t               BaseAddress;           /*!< Specifies the base address of the region to protect.                           */
  13.   uint8_t                Size;                  /*!< Specifies the size of the region to protect.
  14.                                                      This parameter can be a value of @ref CORTEX_MPU_Region_Size                   */
  15.   uint8_t                SubRegionDisable;      /*!< Specifies the number of the subregion protection to disable.
  16.                                                      This parameter must be a number between Min_Data = 0x00 and Max_Data = 0xFF    */
  17.   uint8_t                TypeExtField;          /*!< Specifies the TEX field level.
  18.                                                      This parameter can be a value of @ref CORTEX_MPU_TEX_Levels                    */
  19.   uint8_t                AccessPermission;      /*!< Specifies the region access permission type.
  20.                                                      This parameter can be a value of @ref CORTEX_MPU_Region_Permission_Attributes  */
  21.   uint8_t                DisableExec;           /*!< Specifies the instruction access status.
  22.                                                      This parameter can be a value of @ref CORTEX_MPU_Instruction_Access            */
  23.   uint8_t                IsShareable;           /*!< Specifies the shareability status of the protected region.
  24.                                                      This parameter can be a value of @ref CORTEX_MPU_Access_Shareable              */
  25.   uint8_t                IsCacheable;           /*!< Specifies the cacheable status of the region protected.
  26.                                                      This parameter can be a value of @ref CORTEX_MPU_Access_Cacheable              */
  27.   uint8_t                IsBufferable;          /*!< Specifies the bufferable status of the protected region.
  28.                                                      This parameter can be a value of @ref CORTEX_MPU_Access_Bufferable             */
  29. }MPU_Region_InitTypeDef;
  30. /**
  31.   * @}
  32.   */
  33. #endif /* __MPU_PRESENT */
复制代码



参数:
  1. #if (__MPU_PRESENT == 1)
  2. /** @defgroup CORTEX_MPU_HFNMI_PRIVDEF_Control MPU HFNMI and PRIVILEGED Access control
  3.   * @{
  4.   */
  5. #define  MPU_HFNMI_PRIVDEF_NONE      ((uint32_t)0x00000000)
  6. #define  MPU_HARDFAULT_NMI           ((uint32_t)0x00000002)
  7. #define  MPU_PRIVILEGED_DEFAULT      ((uint32_t)0x00000004)
  8. #define  MPU_HFNMI_PRIVDEF           ((uint32_t)0x00000006)
  9. /**
  10.   * @}
  11.   */

  12. /** @defgroup CORTEX_MPU_Region_Enable CORTEX MPU Region Enable
  13.   * @{
  14.   */
  15. #define  MPU_REGION_ENABLE     ((uint8_t)0x01)
  16. #define  MPU_REGION_DISABLE    ((uint8_t)0x00)
  17. /**
  18.   * @}
  19.   */

  20. /** @defgroup CORTEX_MPU_Instruction_Access CORTEX MPU Instruction Access
  21.   * @{
  22.   */
  23. #define  MPU_INSTRUCTION_ACCESS_ENABLE      ((uint8_t)0x00)
  24. #define  MPU_INSTRUCTION_ACCESS_DISABLE     ((uint8_t)0x01)
  25. /**
  26.   * @}
  27.   */

  28. /** @defgroup CORTEX_MPU_Access_Shareable CORTEX MPU Instruction Access Shareable
  29.   * @{
  30.   */
  31. #define  MPU_ACCESS_SHAREABLE        ((uint8_t)0x01)
  32. #define  MPU_ACCESS_NOT_SHAREABLE    ((uint8_t)0x00)
  33. /**
  34.   * @}
  35.   */

  36. /** @defgroup CORTEX_MPU_Access_Cacheable CORTEX MPU Instruction Access Cacheable
  37.   * @{
  38.   */
  39. #define  MPU_ACCESS_CACHEABLE         ((uint8_t)0x01)
  40. #define  MPU_ACCESS_NOT_CACHEABLE     ((uint8_t)0x00)
  41. /**
  42.   * @}
  43.   */

  44. /** @defgroup CORTEX_MPU_Access_Bufferable CORTEX MPU Instruction Access Bufferable
  45.   * @{
  46.   */
  47. #define  MPU_ACCESS_BUFFERABLE         ((uint8_t)0x01)
  48. #define  MPU_ACCESS_NOT_BUFFERABLE     ((uint8_t)0x00)
  49. /**
  50.   * @}
  51.   */

  52. /** @defgroup CORTEX_MPU_TEX_Levels MPU TEX Levels
  53.   * @{
  54.   */
  55. #define  MPU_TEX_LEVEL0    ((uint8_t)0x00)
  56. #define  MPU_TEX_LEVEL1    ((uint8_t)0x01)
  57. #define  MPU_TEX_LEVEL2    ((uint8_t)0x02)
  58. /**
  59.   * @}
  60.   */

  61. /** @defgroup CORTEX_MPU_Region_Size CORTEX MPU Region Size
  62.   * @{
  63.   */
  64. #define   MPU_REGION_SIZE_32B      ((uint8_t)0x04)
  65. #define   MPU_REGION_SIZE_64B      ((uint8_t)0x05)
  66. #define   MPU_REGION_SIZE_128B     ((uint8_t)0x06)
  67. #define   MPU_REGION_SIZE_256B     ((uint8_t)0x07)
  68. #define   MPU_REGION_SIZE_512B     ((uint8_t)0x08)
  69. #define   MPU_REGION_SIZE_1KB      ((uint8_t)0x09)
  70. #define   MPU_REGION_SIZE_2KB      ((uint8_t)0x0A)
  71. #define   MPU_REGION_SIZE_4KB      ((uint8_t)0x0B)
  72. #define   MPU_REGION_SIZE_8KB      ((uint8_t)0x0C)
  73. #define   MPU_REGION_SIZE_16KB     ((uint8_t)0x0D)
  74. #define   MPU_REGION_SIZE_32KB     ((uint8_t)0x0E)
  75. #define   MPU_REGION_SIZE_64KB     ((uint8_t)0x0F)
  76. #define   MPU_REGION_SIZE_128KB    ((uint8_t)0x10)
  77. #define   MPU_REGION_SIZE_256KB    ((uint8_t)0x11)
  78. #define   MPU_REGION_SIZE_512KB    ((uint8_t)0x12)
  79. #define   MPU_REGION_SIZE_1MB      ((uint8_t)0x13)
  80. #define   MPU_REGION_SIZE_2MB      ((uint8_t)0x14)
  81. #define   MPU_REGION_SIZE_4MB      ((uint8_t)0x15)
  82. #define   MPU_REGION_SIZE_8MB      ((uint8_t)0x16)
  83. #define   MPU_REGION_SIZE_16MB     ((uint8_t)0x17)
  84. #define   MPU_REGION_SIZE_32MB     ((uint8_t)0x18)
  85. #define   MPU_REGION_SIZE_64MB     ((uint8_t)0x19)
  86. #define   MPU_REGION_SIZE_128MB    ((uint8_t)0x1A)
  87. #define   MPU_REGION_SIZE_256MB    ((uint8_t)0x1B)
  88. #define   MPU_REGION_SIZE_512MB    ((uint8_t)0x1C)
  89. #define   MPU_REGION_SIZE_1GB      ((uint8_t)0x1D)
  90. #define   MPU_REGION_SIZE_2GB      ((uint8_t)0x1E)
  91. #define   MPU_REGION_SIZE_4GB      ((uint8_t)0x1F)
  92. /**
  93.   * @}
  94.   */

  95. /** @defgroup CORTEX_MPU_Region_Permission_Attributes CORTEX MPU Region Permission Attributes
  96.   * @{
  97.   */
  98. #define  MPU_REGION_NO_ACCESS      ((uint8_t)0x00)
  99. #define  MPU_REGION_PRIV_RW        ((uint8_t)0x01)
  100. #define  MPU_REGION_PRIV_RW_URO    ((uint8_t)0x02)
  101. #define  MPU_REGION_FULL_ACCESS    ((uint8_t)0x03)
  102. #define  MPU_REGION_PRIV_RO        ((uint8_t)0x05)
  103. #define  MPU_REGION_PRIV_RO_URO    ((uint8_t)0x06)
  104. /**
  105.   * @}
  106.   */

  107. /** @defgroup CORTEX_MPU_Region_Number CORTEX MPU Region Number
  108.   * @{
  109.   */
  110. #define  MPU_REGION_NUMBER0    ((uint8_t)0x00)
  111. #define  MPU_REGION_NUMBER1    ((uint8_t)0x01)
  112. #define  MPU_REGION_NUMBER2    ((uint8_t)0x02)
  113. #define  MPU_REGION_NUMBER3    ((uint8_t)0x03)
  114. #define  MPU_REGION_NUMBER4    ((uint8_t)0x04)
  115. #define  MPU_REGION_NUMBER5    ((uint8_t)0x05)
  116. #define  MPU_REGION_NUMBER6    ((uint8_t)0x06)
  117. #define  MPU_REGION_NUMBER7    ((uint8_t)0x07)
  118. #define  MPU_REGION_NUMBER8    ((uint8_t)0x08)
  119. #define  MPU_REGION_NUMBER9    ((uint8_t)0x09)
  120. #define  MPU_REGION_NUMBER10   ((uint8_t)0x0A)
  121. #define  MPU_REGION_NUMBER11   ((uint8_t)0x0B)
  122. #define  MPU_REGION_NUMBER12   ((uint8_t)0x0C)
  123. #define  MPU_REGION_NUMBER13   ((uint8_t)0x0D)
  124. #define  MPU_REGION_NUMBER14   ((uint8_t)0x0E)
  125. #define  MPU_REGION_NUMBER15   ((uint8_t)0x0F)

  126. /**
  127.   * @}
  128.   */
  129. #endif /* __MPU_PRESENT */
复制代码



函数:
  1. #if (__MPU_PRESENT == 1)
  2. /**
  3.   * @brief  Disables the MPU
  4.   * @retval None
  5.   */
  6. void HAL_MPU_Disable(void)
  7. {
  8.   /* Make sure outstanding transfers are done */
  9.   __DMB();

  10.   /* Disable fault exceptions */
  11.   SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;

  12.   /* Disable the MPU and clear the control register*/
  13.   MPU->CTRL = 0;
  14. }

  15. /**
  16.   * @brief  Enables the MPU
  17.   * @param  MPU_Control Specifies the control mode of the MPU during hard fault,
  18.   *         NMI, FAULTMASK and privileged access to the default memory
  19.   *         This parameter can be one of the following values:
  20.   *            @arg MPU_HFNMI_PRIVDEF_NONE
  21.   *            @arg MPU_HARDFAULT_NMI
  22.   *            @arg MPU_PRIVILEGED_DEFAULT
  23.   *            @arg MPU_HFNMI_PRIVDEF
  24.   * @retval None
  25.   */
  26. void HAL_MPU_Enable(uint32_t MPU_Control)
  27. {
  28.   /* Enable the MPU */
  29.   MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;

  30.   /* Enable fault exceptions */
  31.   SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;

  32.   /* Ensure MPU setting take effects */
  33.   __DSB();
  34.   __ISB();
  35. }
  36. /**
  37.   * @brief  Initializes and configures the Region and the memory to be protected.
  38.   * @param  MPU_Init Pointer to a MPU_Region_InitTypeDef structure that contains
  39.   *                the initialization and configuration information.
  40.   * @retval None
  41.   */
  42. void HAL_MPU_ConfigRegion(MPU_Region_InitTypeDef *MPU_Init)
  43. {
  44.   /* Check the parameters */
  45.   assert_param(IS_MPU_REGION_NUMBER(MPU_Init->Number));
  46.   assert_param(IS_MPU_REGION_ENABLE(MPU_Init->Enable));

  47.   /* Set the Region number */
  48.   MPU->RNR = MPU_Init->Number;

  49.   if ((MPU_Init->Enable) != RESET)
  50.   {
  51.     /* Check the parameters */
  52.     assert_param(IS_MPU_INSTRUCTION_ACCESS(MPU_Init->DisableExec));
  53.     assert_param(IS_MPU_REGION_PERMISSION_ATTRIBUTE(MPU_Init->AccessPermission));
  54.     assert_param(IS_MPU_TEX_LEVEL(MPU_Init->TypeExtField));
  55.     assert_param(IS_MPU_ACCESS_SHAREABLE(MPU_Init->IsShareable));
  56.     assert_param(IS_MPU_ACCESS_CACHEABLE(MPU_Init->IsCacheable));
  57.     assert_param(IS_MPU_ACCESS_BUFFERABLE(MPU_Init->IsBufferable));
  58.     assert_param(IS_MPU_SUB_REGION_DISABLE(MPU_Init->SubRegionDisable));
  59.     assert_param(IS_MPU_REGION_SIZE(MPU_Init->Size));

  60.     MPU->RBAR = MPU_Init->BaseAddress;
  61.     MPU->RASR = ((uint32_t)MPU_Init->DisableExec             << MPU_RASR_XN_Pos)   |
  62.                 ((uint32_t)MPU_Init->AccessPermission        << MPU_RASR_AP_Pos)   |
  63.                 ((uint32_t)MPU_Init->TypeExtField            << MPU_RASR_TEX_Pos)  |
  64.                 ((uint32_t)MPU_Init->IsShareable             << MPU_RASR_S_Pos)    |
  65.                 ((uint32_t)MPU_Init->IsCacheable             << MPU_RASR_C_Pos)    |
  66.                 ((uint32_t)MPU_Init->IsBufferable            << MPU_RASR_B_Pos)    |
  67.                 ((uint32_t)MPU_Init->SubRegionDisable        << MPU_RASR_SRD_Pos)  |
  68.                 ((uint32_t)MPU_Init->Size                    << MPU_RASR_SIZE_Pos) |
  69.                 ((uint32_t)MPU_Init->Enable                  << MPU_RASR_ENABLE_Pos);
  70.   }
  71.   else
  72.   {
  73.     MPU->RBAR = 0x00;
  74.     MPU->RASR = 0x00;
  75.   }
  76. }
  77. #endif /* __MPU_PRESENT */
复制代码


配置举例:
  1. static void MPU_Config( void )
  2. {
  3.         MPU_Region_InitTypeDef MPU_InitStruct;

  4.         /* 禁止 MPU */
  5.         HAL_MPU_Disable();

  6.         /* 配置AXI SRAM的MPU属性为Write through */
  7.         MPU_InitStruct.Enable           = MPU_REGION_ENABLE;
  8.         MPU_InitStruct.BaseAddress      = 0x24000000;
  9.         MPU_InitStruct.Size             = MPU_REGION_SIZE_512KB;
  10.         MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
  11.         MPU_InitStruct.IsBufferable     = MPU_ACCESS_NOT_BUFFERABLE;
  12.         MPU_InitStruct.IsCacheable      = MPU_ACCESS_CACHEABLE;
  13.         MPU_InitStruct.IsShareable      = MPU_ACCESS_NOT_SHAREABLE;
  14.         MPU_InitStruct.Number           = MPU_REGION_NUMBER0;
  15.         MPU_InitStruct.TypeExtField     = MPU_TEX_LEVEL0;
  16.         MPU_InitStruct.SubRegionDisable = 0x00;
  17.         MPU_InitStruct.DisableExec      = MPU_INSTRUCTION_ACCESS_ENABLE;

  18.         HAL_MPU_ConfigRegion(&MPU_InitStruct);

  19.         /*使能 MPU */
  20.         HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
  21. }
复制代码




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-6 19:08 , Processed in 0.261011 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表