2 \defgroup mpu8_functions MPU Functions for Armv8-M
3 \brief Functions that relate to the Memory Protection Unit.
5 The following functions support the optional Memory Protection Unit (MPU) that is available on the Cortex-M23, M33, M35P processor.
7 The MPU is used to prevent from illegal memory accesses that are typically caused by errors in an application software.
13 // Set Region 0 using Attr 0
14 ARM_MPU_SetMemAttr(0UL, ARM_MPU_ATTR( /* Normal memory */
15 ARM_MPU_ATTR_MEMORY_(0UL, 1UL, 1UL, 1UL), /* Outer Write-Back transient with read and write allocate */
16 ARM_MPU_ATTR_MEMORY_(0UL, 0UL, 1UL, 1UL) /* Inner Write-Through transient with read and write allocate */
19 ARM_MPU_SetRegion(0UL,
20 ARM_MPU_RBAR(0x08000000UL, ARM_MPU_SH_NON, 0UL, 1UL, 1UL), /* Non-shareable, read/write, non-privileged, execute-never */
21 ARM_MPU_RLAR(0x080FFFFFUL, 0UL) /* 1MB memory block using Attr 0 */
26 // Execute application code that is access protected by the MPU
35 /** \brief Attribute for device memory (outer only) */
36 #define ARM_MPU_ATTR_DEVICE ( 0U )
38 /** \brief Attribute for non-cacheable, normal memory */
39 #define ARM_MPU_ATTR_NON_CACHEABLE ( 4U )
41 /** \brief Attribute for normal memory (outer and inner)
42 * \param NT Non-Transient: Set to 1 for non-transient data.
43 * \param WB Write-Back: Set to 1 to use write-back update policy.
44 * \param RA Read Allocation: Set to 1 to use cache allocation on read miss.
45 * \param WA Write Allocation: Set to 1 to use cache allocation on write miss.
47 #define ARM_MPU_ATTR_MEMORY_(NT, WB, RA, WA)
49 /** \brief Device memory type non Gathering, non Re-ordering, non Early Write Acknowledgement */
50 #define ARM_MPU_ATTR_DEVICE_nGnRnE
52 /** \brief Device memory type non Gathering, non Re-ordering, Early Write Acknowledgement */
53 #define ARM_MPU_ATTR_DEVICE_nGnRE
55 /** \brief Device memory type non Gathering, Re-ordering, Early Write Acknowledgement */
56 #define ARM_MPU_ATTR_DEVICE_nGRE
58 /** \brief Device memory type Gathering, Re-ordering, Early Write Acknowledgement */
59 #define ARM_MPU_ATTR_DEVICE_GRE
61 /** \brief Memory Attribute
62 * \param O Outer memory attributes
63 * \param I O == ARM_MPU_ATTR_DEVICE: Device memory attributes, else: Inner memory attributes
65 #define ARM_MPU_ATTR(O, I)
67 /** \brief Normal memory non-shareable */
68 #define ARM_MPU_SH_NON
70 /** \brief Normal memory outer shareable */
71 #define ARM_MPU_SH_OUTER
73 /** \brief Normal memory inner shareable */
74 #define ARM_MPU_SH_INNER
76 /** \brief Memory access permissions
77 * \param RO Read-Only: Set to 1 for read-only memory.
78 * \param NP Non-Privileged: Set to 1 for non-privileged memory.
80 #define ARM_MPU_AP_(RO, NP)
82 /** \brief Region Base Address Register value
83 * \param BASE The base address bits [31:5] of a memory region. The value is zero extended. Effective address gets 32 byte aligned.
84 * \param SH Defines the Shareability domain for this memory region.
85 * \param RO Read-Only: Set to 1 for a read-only memory region.
86 * \param NP Non-Privileged: Set to 1 for a non-privileged memory region.
87 * \param XN eXecute Never: Set to 1 for a non-executable memory region.
89 #define ARM_MPU_RBAR(BASE, SH, RO, NP, XN) \
90 ((BASE & MPU_RBAR_BASE_Msk) | \
91 ((SH << MPU_RBAR_SH_Pos) & MPU_RBAR_SH_Msk) | \
92 ((ARM_MPU_AP_(RO, NP) << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk) | \
93 ((XN << MPU_RBAR_XN_Pos) & MPU_RBAR_XN_Msk))
95 /** \brief Region Limit Address Register value
96 * \param LIMIT The limit address bits [31:5] for this memory region. The value is one extended.
97 * \param IDX The attribute index to be associated with this memory region.
99 #define ARM_MPU_RLAR(LIMIT, IDX)
102 * Struct for a single MPU Region
105 uint32_t RBAR; /*!< Region Base Address Register value */
106 uint32_t RLAR; /*!< Region Limit Address Register value */
110 * \param MPU_Control Default access permissions for unconfigured regions.
112 __STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control);
116 __STATIC_INLINE void ARM_MPU_Disable(void);
118 /** Enable the Non-secure MPU.
119 * \param MPU_Control Default access permissions for unconfigured regions.
121 __STATIC_INLINE ARM_MPU_Enable_NS(uint32_t MPU_Control);
123 /** Disable the Non-secure MPU.
125 __STATIC_INLINE void ARM_MPU_Disable_NS(void);
127 /** Set the memory attribute encoding to the given MPU.
128 * \param mpu Pointer to the MPU to be configured.
129 * \param idx The attribute index to be set [0-7]
130 * \param attr The attribute value to be set.
132 __STATIC_INLINE void ARM_MPU_SetMemAttrEx(MPU_Type* mpu, uint8_t idx, uint8_t attr);
134 /** Set the memory attribute encoding.
135 * \param idx The attribute index to be set [0-7]
136 * \param attr The attribute value to be set.
138 __STATIC_INLINE void ARM_MPU_SetMemAttr(uint8_t idx, uint8_t attr);
140 /** Set the memory attribute encoding to the Non-secure MPU.
141 * \param idx The attribute index to be set [0-7]
142 * \param attr The attribute value to be set.
144 __STATIC_INLINE void ARM_MPU_SetMemAttr_NS(uint8_t idx, uint8_t attr);
146 /** Clear and disable the given MPU region of the given MPU.
147 * \param mpu Pointer to MPU to be used.
148 * \param rnr Region number to be cleared.
150 __STATIC_INLINE void ARM_MPU_ClrRegionEx(MPU_Type* mpu, uint32_t rnr);
152 /** Clear and disable the given MPU region.
153 * \param rnr Region number to be cleared.
155 __STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr);
157 /** Clear and disable the given Non-secure MPU region.
158 * \param rnr Region number to be cleared.
160 __STATIC_INLINE void ARM_MPU_ClrRegion_NS(uint32_t rnr);
162 /** Configure the given MPU region of the given MPU.
163 * \param mpu Pointer to MPU to be used.
164 * \param rnr Region number to be configured.
165 * \param rbar Value for RBAR register.
166 * \param rlar Value for RLAR register.
168 __STATIC_INLINE void ARM_MPU_SetRegionEx(MPU_Type* mpu, uint32_t rnr, uint32_t rbar, uint32_t rlar);
170 /** Configure the given MPU region.
171 * \param rnr Region number to be configured.
172 * \param rbar Value for RBAR register.
173 * \param rlar Value for RLAR register.
175 __STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rnr, uint32_t rbar, uint32_t rlar);
177 /** Configure the given Non-secure MPU region.
178 * \param rnr Region number to be configured.
179 * \param rbar Value for RBAR register.
180 * \param rlar Value for RLAR register.
182 __STATIC_INLINE void ARM_MPU_SetRegion_NS(uint32_t rnr, uint32_t rbar, uint32_t rlar);
184 /** Memcpy with strictly ordered memory access, e.g. used by code in \ref ARM_MPU_LoadEx.
185 * \param dst Destination data is copied to.
186 * \param src Source data is copied from.
187 * \param len Amount of data words to be copied.
189 __STATIC_INLINE void ARM_MPU_OrderedMemcpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len);
191 /** Load the given number of MPU regions from a table to the given MPU.
192 * \param mpu Pointer to the MPU registers to be used.
193 * \param rnr First region number to be configured.
194 * \param table Pointer to the MPU configuration table.
195 * \param cnt Amount of regions to be configured.
197 __STATIC_INLINE void ARM_MPU_LoadEx(MPU_Type* mpu, uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt);
199 /** Load the given number of MPU regions from a table.
200 * \param rnr First region number to be configured.
201 * \param table Pointer to the MPU configuration table.
202 * \param cnt Amount of regions to be configured.
206 * const ARM_MPU_Region_t mpuTable[1][4] = {
208 * // BASE SH RO NP XN LIMIT ATTR
209 * { .RBAR = ARM_MPU_RBAR(0x08000000UL, ARM_MPU_SH_NON, 0UL, 1UL, 0UL), .RLAR = ARM_MPU_RLAR(0x080FFFFFUL, 0UL) },
210 * { .RBAR = ARM_MPU_RBAR(0x20000000UL, ARM_MPU_SH_NON, 0UL, 1UL, 1UL), .RLAR = ARM_MPU_RLAR(0x20007FFFUL, 0UL) },
211 * { .RBAR = ARM_MPU_RBAR(0x40020000UL, ARM_MPU_SH_NON, 0UL, 1UL, 1UL), .RLAR = ARM_MPU_RLAR(0x40021FFFUL, 1UL) },
212 * { .RBAR = ARM_MPU_RBAR(0x40022000UL, ARM_MPU_SH_NON, 0UL, 1UL, 1UL), .RLAR = ARM_MPU_RLAR(0x40022FFFUL, 1UL) }
216 * void UpdateMpu(uint32_t idx)
218 * ARM_MPU_Load(0, mpuTable[idx], 4);
222 __STATIC_INLINE void ARM_MPU_Load(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt);
224 /** Load the given number of MPU regions from a table to the Non-secure MPU.
225 * \param rnr First region number to be configured.
226 * \param table Pointer to the MPU configuration table.
227 * \param cnt Amount of regions to be configured.
229 __STATIC_INLINE void ARM_MPU_Load_NS(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt);