1 /***************************************************************************//**
\r
3 * @brief Memory protection unit (MPU) peripheral API
\r
5 *******************************************************************************
\r
7 * <b>(C) Copyright 2015 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
33 #ifndef __SILICON_LABS_EM_MPU_H__
\r
34 #define __SILICON_LABS_EM_MPU_H__
\r
36 #include "em_device.h"
\r
38 #if defined(__MPU_PRESENT) && (__MPU_PRESENT == 1)
\r
39 #include "em_assert.h"
\r
41 #include <stdbool.h>
\r
47 /***************************************************************************//**
\r
48 * @addtogroup EM_Library
\r
50 ******************************************************************************/
\r
52 /***************************************************************************//**
\r
55 ******************************************************************************/
\r
57 /** @anchor MPU_CTRL_PRIVDEFENA
\r
58 * Argument to MPU_enable(). Enables priviledged
\r
59 * access to default memory map. */
\r
60 #define MPU_CTRL_PRIVDEFENA MPU_CTRL_PRIVDEFENA_Msk
\r
62 /** @anchor MPU_CTRL_HFNMIENA
\r
63 * Argument to MPU_enable(). Enables MPU during hard fault,
\r
64 * NMI, and FAULTMASK handlers. */
\r
65 #define MPU_CTRL_HFNMIENA MPU_CTRL_HFNMIENA_Msk
\r
67 /*******************************************************************************
\r
68 ******************************** ENUMS ************************************
\r
69 ******************************************************************************/
\r
72 * Size of an MPU region.
\r
76 mpuRegionSize32b = 4, /**< 32 byte region size. */
\r
77 mpuRegionSize64b = 5, /**< 64 byte region size. */
\r
78 mpuRegionSize128b = 6, /**< 128 byte region size. */
\r
79 mpuRegionSize256b = 7, /**< 256 byte region size. */
\r
80 mpuRegionSize512b = 8, /**< 512 byte region size. */
\r
81 mpuRegionSize1Kb = 9, /**< 1K byte region size. */
\r
82 mpuRegionSize2Kb = 10, /**< 2K byte region size. */
\r
83 mpuRegionSize4Kb = 11, /**< 4K byte region size. */
\r
84 mpuRegionSize8Kb = 12, /**< 8K byte region size. */
\r
85 mpuRegionSize16Kb = 13, /**< 16K byte region size. */
\r
86 mpuRegionSize32Kb = 14, /**< 32K byte region size. */
\r
87 mpuRegionSize64Kb = 15, /**< 64K byte region size. */
\r
88 mpuRegionSize128Kb = 16, /**< 128K byte region size. */
\r
89 mpuRegionSize256Kb = 17, /**< 256K byte region size. */
\r
90 mpuRegionSize512Kb = 18, /**< 512K byte region size. */
\r
91 mpuRegionSize1Mb = 19, /**< 1M byte region size. */
\r
92 mpuRegionSize2Mb = 20, /**< 2M byte region size. */
\r
93 mpuRegionSize4Mb = 21, /**< 4M byte region size. */
\r
94 mpuRegionSize8Mb = 22, /**< 8M byte region size. */
\r
95 mpuRegionSize16Mb = 23, /**< 16M byte region size. */
\r
96 mpuRegionSize32Mb = 24, /**< 32M byte region size. */
\r
97 mpuRegionSize64Mb = 25, /**< 64M byte region size. */
\r
98 mpuRegionSize128Mb = 26, /**< 128M byte region size. */
\r
99 mpuRegionSize256Mb = 27, /**< 256M byte region size. */
\r
100 mpuRegionSize512Mb = 28, /**< 512M byte region size. */
\r
101 mpuRegionSize1Gb = 29, /**< 1G byte region size. */
\r
102 mpuRegionSize2Gb = 30, /**< 2G byte region size. */
\r
103 mpuRegionSize4Gb = 31 /**< 4G byte region size. */
\r
104 } MPU_RegionSize_TypeDef;
\r
107 * MPU region access permission attributes.
\r
111 mpuRegionNoAccess = 0, /**< No access what so ever. */
\r
112 mpuRegionApPRw = 1, /**< Priviledged state R/W only. */
\r
113 mpuRegionApPRwURo = 2, /**< Priviledged state R/W, User state R only. */
\r
114 mpuRegionApFullAccess = 3, /**< R/W in Priviledged and User state. */
\r
115 mpuRegionApPRo = 5, /**< Priviledged R only. */
\r
116 mpuRegionApPRo_URo = 6 /**< R only in Priviledged and User state. */
\r
117 } MPU_RegionAp_TypeDef;
\r
120 /*******************************************************************************
\r
121 ******************************* STRUCTS ***********************************
\r
122 ******************************************************************************/
\r
124 /** MPU Region init structure. */
\r
127 bool regionEnable; /**< MPU region enable. */
\r
128 uint8_t regionNo; /**< MPU region number. */
\r
129 uint32_t baseAddress; /**< Region baseaddress. */
\r
130 MPU_RegionSize_TypeDef size; /**< Memory region size. */
\r
131 MPU_RegionAp_TypeDef accessPermission; /**< Memory access permissions. */
\r
132 bool disableExec; /**< Disable execution. */
\r
133 bool shareable; /**< Memory shareable attribute. */
\r
134 bool cacheable; /**< Memory cacheable attribute. */
\r
135 bool bufferable; /**< Memory bufferable attribute. */
\r
136 uint8_t srd; /**< Memory subregion disable bits. */
\r
137 uint8_t tex; /**< Memory type extension attributes. */
\r
138 } MPU_RegionInit_TypeDef;
\r
140 /** Default configuration of MPU region init structure for flash memory. */
\r
141 #define MPU_INIT_FLASH_DEFAULT \
\r
143 true, /* Enable MPU region. */ \
\r
144 0, /* MPU Region number. */ \
\r
145 FLASH_MEM_BASE, /* Flash base address. */ \
\r
146 mpuRegionSize1Mb, /* Size - Set to max. */ \
\r
147 mpuRegionApFullAccess, /* Access permissions. */ \
\r
148 false, /* Execution allowed. */ \
\r
149 false, /* Not shareable. */ \
\r
150 true, /* Cacheable. */ \
\r
151 false, /* Not bufferable. */ \
\r
152 0, /* No subregions. */ \
\r
153 0 /* No TEX attributes. */ \
\r
157 /** Default configuration of MPU region init structure for sram memory. */
\r
158 #define MPU_INIT_SRAM_DEFAULT \
\r
160 true, /* Enable MPU region. */ \
\r
161 1, /* MPU Region number. */ \
\r
162 RAM_MEM_BASE, /* SRAM base address. */ \
\r
163 mpuRegionSize128Kb, /* Size - Set to max. */ \
\r
164 mpuRegionApFullAccess, /* Access permissions. */ \
\r
165 false, /* Execution allowed. */ \
\r
166 true, /* Shareable. */ \
\r
167 true, /* Cacheable. */ \
\r
168 false, /* Not bufferable. */ \
\r
169 0, /* No subregions. */ \
\r
170 0 /* No TEX attributes. */ \
\r
174 /** Default configuration of MPU region init structure for onchip peripherals.*/
\r
175 #define MPU_INIT_PERIPHERAL_DEFAULT \
\r
177 true, /* Enable MPU region. */ \
\r
178 0, /* MPU Region number. */ \
\r
179 0, /* Region base address. */ \
\r
180 mpuRegionSize32b, /* Size - Set to minimum */ \
\r
181 mpuRegionApFullAccess, /* Access permissions. */ \
\r
182 true, /* Execution not allowed. */ \
\r
183 true, /* Shareable. */ \
\r
184 false, /* Not cacheable. */ \
\r
185 true, /* Bufferable. */ \
\r
186 0, /* No subregions. */ \
\r
187 0 /* No TEX attributes. */ \
\r
191 /*******************************************************************************
\r
192 ***************************** PROTOTYPES **********************************
\r
193 ******************************************************************************/
\r
196 void MPU_ConfigureRegion(const MPU_RegionInit_TypeDef *init);
\r
199 /***************************************************************************//**
\r
203 * Disable MPU and MPU fault exceptions.
\r
204 ******************************************************************************/
\r
205 __STATIC_INLINE void MPU_Disable(void)
\r
207 SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk; /* Disable fault exceptions */
\r
208 MPU->CTRL &= ~MPU_CTRL_ENABLE_Msk; /* Disable the MPU */
\r
212 /***************************************************************************//**
\r
216 * Enable MPU and MPU fault exceptions.
\r
218 * Use a logical OR of @ref MPU_CTRL_PRIVDEFENA and
\r
219 * @ref MPU_CTRL_HFNMIENA as needed.
\r
220 ******************************************************************************/
\r
221 __STATIC_INLINE void MPU_Enable(uint32_t flags)
\r
223 EFM_ASSERT(!(flags & ~(MPU_CTRL_PRIVDEFENA_Msk
\r
224 | MPU_CTRL_HFNMIENA_Msk
\r
225 | MPU_CTRL_ENABLE_Msk)));
\r
227 MPU->CTRL = flags | MPU_CTRL_ENABLE_Msk; /* Enable the MPU */
\r
228 SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk; /* Enable fault exceptions */
\r
232 /** @} (end addtogroup MPU) */
\r
233 /** @} (end addtogroup EM_Library) */
\r
239 #endif /* defined(__MPU_PRESENT) && (__MPU_PRESENT == 1) */
\r
241 #endif /* __SILICON_LABS_EM_MPU_H__ */
\r