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