How do I configure MPU registers in cortex m4?

1

I want to add a protection to a region of a memory, starting from 0x20000000. The size of the region is 64 bytes. Permission is read only, no flag set except xn. Here's how I think it should be,

#define MPU_CTRL         (*((volatile unsigned long*) 0xE000ED94))    // MPU Control register
#define MPU_RNR          (*((volatile unsigned long*) 0xE000ED98))    // MPU Region Number register
#define MPU_RBAR         (*((volatile unsigned long*) 0xE000ED9C))    // MPU Region Base Address Register
#define MPU_RASR         (*((volatile unsigned long*) 0xE000EDA0))    // MPU Region attributes and size register

void Registers_Init(void)
{       
    //MPU Configuring
    MPU_RNR = 0x00000000;                       // use region 0
    MPU_RBAR = 0x20000000;                      // base address is 0x20000000
    MPU_RASR = 0x1608FF0B;                      // enable bit=1, 64 bytes,not subregions, s=c=b=0, xn=1, permission= ro/ro.
    MPU_CTRL = 0x00000005;                      // enable memory protection unit,guaranteeing default priviliged access
}

int main()
{
    Registers_Init();
    return 0;
}

Is this correct? Am I doing it wrong? Please guide.

c
memory-management
cortex-m
cortex-m3
mpu
asked on Stack Overflow Nov 12, 2017 by Muzahir Hussain

1 Answer

1

Yes this looks correct to configure a region. However you have disabled all the sub-regions which mean you will not have any access to this memory block. The sub-region disable bits should be 0 (enabled). You have also set privileged and unprivileged read only.

You don't have to use the RNR register as you can use the VALID and REGION fields in the RBAR register instead.

If at any point you change to unprivileged mode you will not have any access to you code or data memory (other than is what is defined in the region) so you will get an MPU fault. I would suggest adding an MPU fault handler if you have not already and possibly defining a read only region to allow access to all of the flash (although you already have privileged access through the background region).

answered on Stack Overflow Nov 14, 2017 by Realtime Rik

User contributions licensed under CC BY-SA 3.0