MPU not triggering faults in cortex M4

400 Views Asked by At

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 a compiling code that demonstrates the issue.

#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_RNR =  0x00000000;       // using region 0
   MPU_RBAR = 0x20000000;       // base address is 0x20000000
   MPU_RASR = 0x0700110B;       // Size is 64 bytes, no sub-regions, permission=7(ro,ro), s=b=c= 0, tex=0
   MPU_CTRL = 0x00000001;       // enable MPU
   SCB_SHCSR = 0x00010000;      // enable MemManage Fault
}

void MemManage_Handler(void)
{ 
   __asm(
         "MOV R4, 0x77777777\n\t"
         "MOV R5, 0x77777777\n\t"
      );
}

int main(void)
{
    Registers_Init();

   __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(), it writes successfully.

1

There are 1 best solutions below

1
On

This looks fine to me. Make sure your hardware have a MPU. MPU has a register called MPU_TYPE Register. This is a read-only register that tells you if you have a MPU or not. If bits 15:8 in MPU_TYPE register read 0, there's no MPU.

And never use numbers when dealing with registers. This makes it really hard for you and other person to read your code. Instead, define a number of bit masks. See tutorials on how to do that.