efm32_mpu.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Memory protection unit (MPU) peripheral API for EFM32.
  4. * @author Energy Micro AS
  5. * @version 1.3.0
  6. *******************************************************************************
  7. * @section License
  8. * <b>(C) Copyright 2010 Energy Micro AS, http://www.energymicro.com</b>
  9. *******************************************************************************
  10. *
  11. * This source code is the property of Energy Micro AS. The source and compiled
  12. * code may only be used on Energy Micro "EFM32" microcontrollers.
  13. *
  14. * This copyright notice may not be removed from the source code nor changed.
  15. *
  16. * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
  17. * obligation to support this Software. Energy Micro AS is providing the
  18. * Software "AS IS", with no express or implied warranties of any kind,
  19. * including, but not limited to, any implied warranties of merchantability
  20. * or fitness for any particular purpose or warranties against infringement
  21. * of any proprietary rights of a third party.
  22. *
  23. * Energy Micro AS will not be liable for any consequential, incidental, or
  24. * special damages, or any other relief, or for any claim by any third party,
  25. * arising from your use of this Software.
  26. *
  27. ******************************************************************************/
  28. #ifndef __EFM32_MPU_H
  29. #define __EFM32_MPU_H
  30. #include "efm32.h"
  31. #if defined(__MPU_PRESENT) && (__MPU_PRESENT == 1)
  32. #include "efm32_assert.h"
  33. #include <stdbool.h>
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /***************************************************************************//**
  38. * @addtogroup EFM32_Library
  39. * @{
  40. ******************************************************************************/
  41. /***************************************************************************//**
  42. * @addtogroup MPU
  43. * @{
  44. ******************************************************************************/
  45. /** @anchor MPU_CTRL_PRIVDEFENA
  46. * Argument to MPU_enable(). Enables priviledged
  47. * access to default memory map. */
  48. #define MPU_CTRL_PRIVDEFENA MPU_CTRL_PRIVDEFENA_Msk
  49. /** @anchor MPU_CTRL_HFNMIENA
  50. * Argument to MPU_enable(). Enables MPU during hard fault,
  51. * NMI, and FAULTMASK handlers. */
  52. #define MPU_CTRL_HFNMIENA MPU_CTRL_HFNMIENA_Msk
  53. /*******************************************************************************
  54. ******************************** ENUMS ************************************
  55. ******************************************************************************/
  56. /**
  57. * Size of an MPU region.
  58. */
  59. typedef enum
  60. {
  61. mpuRegionSize32b = 4, /**< 32 byte region size. */
  62. mpuRegionSize64b = 5, /**< 64 byte region size. */
  63. mpuRegionSize128b = 6, /**< 128 byte region size. */
  64. mpuRegionSize256b = 7, /**< 256 byte region size. */
  65. mpuRegionSize512b = 8, /**< 512 byte region size. */
  66. mpuRegionSize1Kb = 9, /**< 1K byte region size. */
  67. mpuRegionSize2Kb = 10, /**< 2K byte region size. */
  68. mpuRegionSize4Kb = 11, /**< 4K byte region size. */
  69. mpuRegionSize8Kb = 12, /**< 8K byte region size. */
  70. mpuRegionSize16Kb = 13, /**< 16K byte region size. */
  71. mpuRegionSize32Kb = 14, /**< 32K byte region size. */
  72. mpuRegionSize64Kb = 15, /**< 64K byte region size. */
  73. mpuRegionSize128Kb = 16, /**< 128K byte region size. */
  74. mpuRegionSize256Kb = 17, /**< 256K byte region size. */
  75. mpuRegionSize512Kb = 18, /**< 512K byte region size. */
  76. mpuRegionSize1Mb = 19, /**< 1M byte region size. */
  77. mpuRegionSize2Mb = 20, /**< 2M byte region size. */
  78. mpuRegionSize4Mb = 21, /**< 4M byte region size. */
  79. mpuRegionSize8Mb = 22, /**< 8M byte region size. */
  80. mpuRegionSize16Mb = 23, /**< 16M byte region size. */
  81. mpuRegionSize32Mb = 24, /**< 32M byte region size. */
  82. mpuRegionSize64Mb = 25, /**< 64M byte region size. */
  83. mpuRegionSize128Mb = 26, /**< 128M byte region size. */
  84. mpuRegionSize256Mb = 27, /**< 256M byte region size. */
  85. mpuRegionSize512Mb = 28, /**< 512M byte region size. */
  86. mpuRegionSize1Gb = 29, /**< 1G byte region size. */
  87. mpuRegionSize2Gb = 30, /**< 2G byte region size. */
  88. mpuRegionSize4Gb = 31 /**< 4G byte region size. */
  89. } MPU_RegionSize_TypeDef;
  90. /**
  91. * MPU region access permission attributes.
  92. */
  93. typedef enum
  94. {
  95. mpuRegionNoAccess = 0, /**< No access what so ever. */
  96. mpuRegionApPRw = 1, /**< Priviledged state R/W only. */
  97. mpuRegionApPRwURo = 2, /**< Priviledged state R/W, User state R only. */
  98. mpuRegionApFullAccess = 3, /**< R/W in Priviledged and User state. */
  99. mpuRegionApPRo = 5, /**< Priviledged R only. */
  100. mpuRegionApPRo_URo = 6 /**< R only in Priviledged and User state. */
  101. } MPU_RegionAp_TypeDef;
  102. /*******************************************************************************
  103. ******************************* STRUCTS ***********************************
  104. ******************************************************************************/
  105. /** MPU Region init structure. */
  106. typedef struct
  107. {
  108. bool regionEnable; /**< MPU region enable. */
  109. uint8_t regionNo; /**< MPU region number. */
  110. uint32_t baseAddress; /**< Region baseaddress. */
  111. MPU_RegionSize_TypeDef size; /**< Memory region size. */
  112. MPU_RegionAp_TypeDef accessPermission; /**< Memory access permissions. */
  113. bool disableExec; /**< Disable execution. */
  114. bool shareable; /**< Memory shareable attribute. */
  115. bool cacheable; /**< Memory cacheable attribute. */
  116. bool bufferable; /**< Memory bufferable attribute. */
  117. uint8_t srd; /**< Memory subregion disable bits. */
  118. uint8_t tex; /**< Memory type extension attributes. */
  119. } MPU_RegionInit_TypeDef;
  120. /** Default configuration of MPU region init structure for flash memory. */
  121. #define MPU_INIT_FLASH_DEFAULT \
  122. { \
  123. true, /* Enable MPU region. */ \
  124. 0, /* MPU Region number. */ \
  125. FLASH_MEM_BASE, /* Flash base address. */ \
  126. mpuRegionSize1Mb, /* Size - Set to max. for EFM32. */ \
  127. mpuRegionApFullAccess, /* Access permissions. */ \
  128. false, /* Execution allowed. */ \
  129. false, /* Not shareable. */ \
  130. true, /* Cacheable. */ \
  131. false, /* Not bufferable. */ \
  132. 0, /* No subregions. */ \
  133. 0 /* No TEX attributes. */ \
  134. }
  135. /** Default configuration of MPU region init structure for sram memory. */
  136. #define MPU_INIT_SRAM_DEFAULT \
  137. { \
  138. true, /* Enable MPU region. */ \
  139. 1, /* MPU Region number. */ \
  140. RAM_MEM_BASE, /* SRAM base address. */ \
  141. mpuRegionSize128Kb, /* Size - Set to max. for EFM32. */ \
  142. mpuRegionApFullAccess, /* Access permissions. */ \
  143. false, /* Execution allowed. */ \
  144. true, /* Shareable. */ \
  145. true, /* Cacheable. */ \
  146. false, /* Not bufferable. */ \
  147. 0, /* No subregions. */ \
  148. 0 /* No TEX attributes. */ \
  149. }
  150. /** Default configuration of MPU region init structure for onchip peripherals.*/
  151. #define MPU_INIT_PERIPHERAL_DEFAULT \
  152. { \
  153. true, /* Enable MPU region. */ \
  154. 0, /* MPU Region number. */ \
  155. 0, /* Region base address. */ \
  156. mpuRegionSize32b, /* Size - Set to minimum */ \
  157. mpuRegionApFullAccess, /* Access permissions. */ \
  158. true, /* Execution not allowed. */ \
  159. true, /* Shareable. */ \
  160. false, /* Not cacheable. */ \
  161. true, /* Bufferable. */ \
  162. 0, /* No subregions. */ \
  163. 0 /* No TEX attributes. */ \
  164. }
  165. /*******************************************************************************
  166. ***************************** PROTOTYPES **********************************
  167. ******************************************************************************/
  168. void MPU_ConfigureRegion(MPU_RegionInit_TypeDef *init);
  169. /***************************************************************************//**
  170. * @brief
  171. * Disable the MPU
  172. * @details
  173. * Disable MPU and MPU fault exceptions.
  174. ******************************************************************************/
  175. static __INLINE void MPU_Disable(void)
  176. {
  177. SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk; /* Disable fault exceptions */
  178. MPU->CTRL &= ~MPU_CTRL_ENABLE_Msk; /* Disable the MPU */
  179. }
  180. /***************************************************************************//**
  181. * @brief
  182. * Enable the MPU
  183. * @details
  184. * Enable MPU and MPU fault exceptions.
  185. * @param[in] flags
  186. * Use a logical OR of @ref MPU_CTRL_PRIVDEFENA and
  187. * @ref MPU_CTRL_HFNMIENA as needed.
  188. ******************************************************************************/
  189. static __INLINE void MPU_Enable(uint32_t flags)
  190. {
  191. EFM_ASSERT(!(flags & ~(MPU_CTRL_PRIVDEFENA_Msk |
  192. MPU_CTRL_HFNMIENA_Msk |
  193. MPU_CTRL_ENABLE_Msk)));
  194. MPU->CTRL = flags | MPU_CTRL_ENABLE_Msk; /* Enable the MPU */
  195. SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk; /* Enable fault exceptions */
  196. }
  197. /** @} (end addtogroup MPU) */
  198. /** @} (end addtogroup EFM32_Library) */
  199. #ifdef __cplusplus
  200. }
  201. #endif
  202. #endif /* defined(__MPU_PRESENT) && (EBI_COUNT == 1) */
  203. #endif /* __EFM32_MPU_H */