em_mpu.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include "em_mpu.h"
  34. #include "em_assert.h"
  35. /***************************************************************************//**
  36. * @addtogroup EM_Library
  37. * @{
  38. ******************************************************************************/
  39. /***************************************************************************//**
  40. * @addtogroup MPU
  41. * @brief Memory Protection Unit (MPU) Peripheral API
  42. * @details
  43. * This module contains functions to enable, disable and setup the MPU.
  44. * The MPU is used to control access attributes and permissions in the
  45. * memory map. The settings that can be controlled are:
  46. *
  47. * @li Executable attribute.
  48. * @li Cachable, bufferable and shareable attributes.
  49. * @li Cache policy.
  50. * @li Access permissions: Priviliged or User state, read or write access,
  51. * and combinations of all these.
  52. *
  53. * The MPU can be activated and deactivated with functions:
  54. * @verbatim
  55. * MPU_Enable(..);
  56. * MPU_Disable();@endverbatim
  57. * The MPU can control 8 memory regions with individual access control
  58. * settings. Section attributes and permissions are set with:
  59. * @verbatim
  60. * MPU_ConfigureRegion(..);@endverbatim
  61. * It is advisable to disable the MPU when altering region settings.
  62. *
  63. *
  64. * @{
  65. ******************************************************************************/
  66. /*******************************************************************************
  67. ************************** GLOBAL FUNCTIONS *******************************
  68. ******************************************************************************/
  69. /***************************************************************************//**
  70. * @brief
  71. * Configure an MPU region.
  72. *
  73. * @details
  74. * Writes to MPU RBAR and RASR registers.
  75. * Refer to Cortex-M3 Reference Manual, MPU chapter for further details.
  76. * To disable a region it is only required to set init->regionNo to the
  77. * desired value and init->regionEnable = false.
  78. *
  79. * @param[in] init
  80. * Pointer to a structure containing MPU region init information.
  81. ******************************************************************************/
  82. void MPU_ConfigureRegion(const MPU_RegionInit_TypeDef *init)
  83. {
  84. EFM_ASSERT(init->regionNo < ((MPU->TYPE & MPU_TYPE_DREGION_Msk) >>
  85. MPU_TYPE_DREGION_Pos));
  86. MPU->RNR = init->regionNo;
  87. if (init->regionEnable)
  88. {
  89. EFM_ASSERT(!(init->baseAddress & ~MPU_RBAR_ADDR_Msk));
  90. EFM_ASSERT(init->tex <= 0x7);
  91. MPU->RBAR = init->baseAddress;
  92. MPU->RASR = ((init->disableExec ? 1 : 0) << MPU_RASR_XN_Pos) |
  93. (init->accessPermission << MPU_RASR_AP_Pos) |
  94. (init->tex << MPU_RASR_TEX_Pos) |
  95. ((init->shareable ? 1 : 0) << MPU_RASR_S_Pos) |
  96. ((init->cacheable ? 1 : 0) << MPU_RASR_C_Pos) |
  97. ((init->bufferable ? 1 : 0) << MPU_RASR_B_Pos) |
  98. (init->srd << MPU_RASR_SRD_Pos) |
  99. (init->size << MPU_RASR_SIZE_Pos) |
  100. (1 << MPU_RASR_ENABLE_Pos);
  101. }
  102. else
  103. {
  104. MPU->RBAR = 0;
  105. MPU->RASR = 0;
  106. }
  107. }
  108. /** @} (end addtogroup CMU) */
  109. /** @} (end addtogroup EM_Library) */