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