Skip to content

Commit 2b5518a

Browse files
committed
Reduce dynamic MPU code size with a loop
1 parent e585eac commit 2b5518a

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

hal/mpu/mbed_mpu_v7m.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,18 @@ void mbed_mpu_free()
195195
__ISB();
196196
}
197197

198+
static void enable_region(bool enable, uint32_t region)
199+
{
200+
MPU->RNR = region;
201+
MPU->RASR = (MPU->RASR & ~MPU_RASR_ENABLE_Msk) | (enable << MPU_RASR_ENABLE_Pos);
202+
}
203+
198204
void mbed_mpu_enable_rom_wn(bool enable)
199205
{
200206
// Flush memory writes before configuring the MPU.
201207
__DMB();
202208

203-
MPU->RNR = 0;
204-
MPU->RASR = (MPU->RASR & ~MPU_RASR_ENABLE_Msk) | (enable ? MPU_RASR_ENABLE_Msk : 0);
209+
enable_region(enable, 0);
205210

206211
// Ensure changes take effect
207212
__DSB();
@@ -213,14 +218,9 @@ void mbed_mpu_enable_ram_xn(bool enable)
213218
// Flush memory writes before configuring the MPU.
214219
__DMB();
215220

216-
MPU->RNR = 1;
217-
MPU->RASR = (MPU->RASR & ~MPU_RASR_ENABLE_Msk) | (enable ? MPU_RASR_ENABLE_Msk : 0);
218-
219-
MPU->RNR = 2;
220-
MPU->RASR = (MPU->RASR & ~MPU_RASR_ENABLE_Msk) | (enable ? MPU_RASR_ENABLE_Msk : 0);
221-
222-
MPU->RNR = 3;
223-
MPU->RASR = (MPU->RASR & ~MPU_RASR_ENABLE_Msk) | (enable ? MPU_RASR_ENABLE_Msk : 0);
221+
for (uint32_t region = 1; region <= 3; region++) {
222+
enable_region(enable, region);
223+
}
224224

225225
// Ensure changes take effect
226226
__DSB();

hal/mpu/mbed_mpu_v8m.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,18 @@ void mbed_mpu_free()
144144
__ISB();
145145
}
146146

147+
static void enable_region(bool enable, uint32_t region)
148+
{
149+
MPU->RNR = region;
150+
MPU->RLAR = (MPU->RLAR & ~MPU_RLAR_EN_Msk) | (enable << MPU_RLAR_EN_Pos);
151+
}
152+
147153
void mbed_mpu_enable_rom_wn(bool enable)
148154
{
149155
// Flush memory writes before configuring the MPU.
150156
__DMB();
151157

152-
MPU->RNR = 0;
153-
MPU->RLAR = (MPU->RLAR & ~MPU_RLAR_EN_Msk) | (enable ? MPU_RLAR_EN_Msk : 0);
158+
enable_region(enable, 0);
154159

155160
// Ensure changes take effect
156161
__DSB();
@@ -162,14 +167,9 @@ void mbed_mpu_enable_ram_xn(bool enable)
162167
// Flush memory writes before configuring the MPU.
163168
__DMB();
164169

165-
MPU->RNR = 1;
166-
MPU->RLAR = (MPU->RLAR & ~MPU_RLAR_EN_Msk) | (enable ? MPU_RLAR_EN_Msk : 0);
167-
168-
MPU->RNR = 2;
169-
MPU->RLAR = (MPU->RLAR & ~MPU_RLAR_EN_Msk) | (enable ? MPU_RLAR_EN_Msk : 0);
170-
171-
MPU->RNR = 3;
172-
MPU->RLAR = (MPU->RLAR & ~MPU_RLAR_EN_Msk) | (enable ? MPU_RLAR_EN_Msk : 0);
170+
for (uint32_t region = 1; region <= 3; region++) {
171+
enable_region(enable, region);
172+
}
173173

174174
// Ensure changes take effect
175175
__DSB();

0 commit comments

Comments
 (0)