em_mpu.h 11 KB

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