Skip to content

Commit 4d9f218

Browse files
kjbraceyCruz Monrreal II
authored andcommitted
Save ROM by specifying initial MPU state
We can omit the need for the "change MPU state" calls from simple images by specifying the initial state at init.
1 parent bb4cb77 commit 4d9f218

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

hal/mpu/mbed_mpu_v8m.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ void mbed_mpu_init()
7878
(3 << MPU_RBAR_AP_Pos) | // RO allowed by all privilege levels
7979
(0 << MPU_RBAR_XN_Pos); // Execute Never disabled
8080
MPU->RLAR = (0x1FFFFFFF & MPU_RLAR_LIMIT_Msk) | // Last address is 0x1FFFFFFF
81-
(region << MPU_RLAR_AttrIndx_Pos); // Attribute index - configured to be the same as the region number
81+
(region << MPU_RLAR_AttrIndx_Pos) | // Attribute index - configured to be the same as the region number
82+
(1 << MPU_RLAR_EN_Pos); // Region enabled
8283

8384
region = 1;
8485
MPU->RNR = region;
@@ -90,7 +91,8 @@ void mbed_mpu_init()
9091
(1 << MPU_RBAR_AP_Pos) | // RW allowed by all privilege levels
9192
(1 << MPU_RBAR_XN_Pos); // Execute Never enabled
9293
MPU->RLAR = (0x3FFFFFFF & MPU_RLAR_LIMIT_Msk) | // Last address is 0x3FFFFFFF
93-
(region << MPU_RLAR_AttrIndx_Pos); // Attribute index - configured to be the same as the region number
94+
(region << MPU_RLAR_AttrIndx_Pos) | // Attribute index - configured to be the same as the region number
95+
(1 << MPU_RLAR_EN_Pos); // Region enabled
9496

9597
region = 2;
9698
MPU->RNR = region;
@@ -102,7 +104,8 @@ void mbed_mpu_init()
102104
(1 << MPU_RBAR_AP_Pos) | // RW allowed by all privilege levels
103105
(1 << MPU_RBAR_XN_Pos); // Execute Never enabled
104106
MPU->RLAR = (0x7FFFFFFF & MPU_RLAR_LIMIT_Msk) | // Last address is 0x7FFFFFFF
105-
(region << MPU_RLAR_AttrIndx_Pos); // Attribute index - configured to be the same as the region number
107+
(region << MPU_RLAR_AttrIndx_Pos) | // Attribute index - configured to be the same as the region number
108+
(1 << MPU_RLAR_EN_Pos); // Region enabled
106109

107110
region = 3;
108111
MPU->RNR = region;
@@ -114,7 +117,8 @@ void mbed_mpu_init()
114117
(1 << MPU_RBAR_AP_Pos) | // RW allowed by all privilege levels
115118
(1 << MPU_RBAR_XN_Pos); // Execute Never enabled
116119
MPU->RLAR = (0x9FFFFFFF & MPU_RLAR_LIMIT_Msk) | // Last address is 0x9FFFFFFF
117-
(region << MPU_RLAR_AttrIndx_Pos); // Attribute index - configured to be the same as the region number
120+
(region << MPU_RLAR_AttrIndx_Pos) | // Attribute index - configured to be the same as the region number
121+
(1 << MPU_RLAR_EN_Pos); // Region enabled
118122

119123
// Enable the MPU
120124
MPU->CTRL =

hal/mpu_api.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ extern "C" {
6464
* Initialize the MPU
6565
*
6666
* Initialize or re-initialize the memory protection unit.
67-
* It is implementation defined what region are protected
68-
* by the MPU after initialization.
67+
* After initialization or re-initialization, ROM and RAM protection
68+
* are both enabled.
6969
*/
7070
void mbed_mpu_init(void);
7171

@@ -75,7 +75,9 @@ void mbed_mpu_init(void);
7575
* This function is used to mark all of ROM as read and execute only.
7676
* When enabled writes to ROM cause a fault.
7777
*
78-
* @param enable true to disable execution in ram, false otherwise
78+
* By default writes to ROM are disabled.
79+
*
80+
* @param enable true to disable writes to ROM, false otherwise
7981
*/
8082
void mbed_mpu_enable_rom_wn(bool enable);
8183

@@ -85,7 +87,9 @@ void mbed_mpu_enable_rom_wn(bool enable);
8587
* This function is used to mark all of RAM as execute never.
8688
* When enabled code is only allowed to execute from flash.
8789
*
88-
* @param enable true to disable execution in ram, false otherwise
90+
* By default execution from RAM is disabled.
91+
*
92+
* @param enable true to disable execution from RAM, false otherwise
8993
*/
9094
void mbed_mpu_enable_ram_xn(bool enable);
9195

rtos/TARGET_CORTEX/mbed_boot.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ uint32_t mbed_stack_isr_size = 0;
8888
void mbed_init(void)
8989
{
9090
mbed_mpu_init();
91-
mbed_mpu_enable_ram_xn(true);
92-
mbed_mpu_enable_rom_wn(true);
9391
mbed_cpy_nvic();
9492
mbed_sdk_init();
9593
mbed_rtos_init();

0 commit comments

Comments
 (0)