Cortex-M3 Memory Protection Configuration(MPU)
I want to protect a memory region from writing. I've configured MPU, but it is not generating any faults. The base address of the region that I want to protect is 0x20000000. The region size is 64 bytes.
Here's how I did it,
//Memory Protection Unit Registers
#define MPU_CTRL (*((volatile unsigned long*) 0xE000ED94)) #define MPU_RNR (*((volatile unsigned long*) 0xE000ED98))#define MPU_RBAR (*((volatile unsigned long*) 0xE000ED9C))
#define MPU_RASR (*((volatile unsigned long*) 0xE000EDA0))#define SCB_SHCSR (*((volatile unsigned long*) 0xE000ED24))
void Registers_Init(void) { //MPU Configuring MPU_RNR = 0x00000000; //region is 0 MPU_RBAR = 0x20000000; //base address is 0x20000000 MPU_RASR = 0x1608FF0B; // enable bit=1, 64 bytes, no subregions, s=c=b=0, xn=1, permission=ro,ro SCB_SHCSR |= 0x00010000; // enable MemManage FaultMPU_CTRL = 0x00000005; // enable memory protection unit,guaranteeing default priviliged access
}void MemManage_Handler(void)
{ __asm( 'MOV R4, 0x77777777\n\t' 'MOV R5, 0x77777777\n\t' ); } void HardFault_Handler(void) { __asm( 'MOV R4, 0x77777777\n\t' 'MOV R5, 0x77777777\n\t' ); } int main(void) { Registers_Init();while(1)
{ __asm( 'LDR R0, =0x20000000\n\t' 'MOV R1, 0x77777777\n\t' 'STR R1, [R0,#0]' ); } return (1); }void SystemInit(void)
{ }So, in main function, I am writing in restricted area i.e. 0x20000000, but MPU is not generating any fault and instead of calling MemManage_Handler()or HardFault_Handler(), it writes successfully.
Why is that? Please help.
The micro controller I am using is STM32F103RB.
#mpu #stm32f103rb #memory-protection #stm32 #cortex-m3