简体   繁体   English

如何在 cortex m4 中配置 MPU 寄存器?

[英]How do I configure MPU registers in cortex m4?

I want to add a protection to a region of a memory, starting from 0x20000000.我想对从 0x20000000 开始的内存区域添加保护。 The size of the region is 64 bytes.该区域的大小为 64 字节。 Permission is read only, no flag set except xn.权限是只读的,除 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.请指导。

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).子区域禁用位应为 0(启用)。 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.您不必使用 RNR 寄存器,因为您可以改用 RBAR 寄存器中的 VALID 和 REGION 字段。

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.如果在任何时候您更改为非特权模式,您将无法访问您的代码或数据内存(区域中定义的除外),因此您将遇到 MPU 故障。 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).如果您还没有定义一个只读区域以允许访问所有闪存,我建议添加一个 MPU 故障处理程序(尽管您已经拥有通过后台区域的特权访问)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM