补第十章的第三部分“10.5 内存保护单元(MPU)的固件库函数”样例
[复制链接]
下面的示例设置了一组基本的保护区域: n 在闪存中,一个28 KB区域的只读代码执行 n 在特权和用户模式下32 KB的RAM读写访问 n 仅使用在特权模式下一个额外的8 KB RAM n 只能在特权模式下访问的1 MB外设空间,除了一个128kb的孔不可访问,另一个128 kb的区域,可以从用户模式访问
以下翻译略-----
// // Define a 28-KBregion of flash from 0x00000000 to 0x00007000. The // region isexecutable, and read-only for both privileged and user // modes. To set upthe region, a 32-KB region (#0) is defined // starting ataddress 0, and then a 4 KB hole removed at the end by // disabling thelast sub-region. The region is initially enabled. // MPURegionSet(0, 0, MPU_RGN_SIZE_32K | MPU_RGN_PERM_EXEC | MPU_RGN_PERM_PRV_RO_USR_RO | MPU_SUB_RGN_DISABLE_7 | MPU_RGN_ENABLE); // // Define a 32-KBregion (#1) of RAM from 0x20000000 to 0x20008000. The // region is notexecutable, and is read/write access for // privileged anduser modes. // MPURegionSet(1,0x20000000, MPU_RGN_SIZE_32K | MPU_RGN_PERM_NOEXEC | MPU_RGN_PERM_PRV_RW_USR_RW | MPU_RGN_ENABLE); // // Define anadditional 8-KB region (#2) in RAM from 0x20008000 to // 0x2000A000 thatis read/write accessible only from privileged // mode. This regionis initially disabled, to be enabled later. // MPURegionSet(2,0x20008000, MPU_RGN_SIZE_8K | MPU_RGN_PERM_NOEXEC | MPU_RGN_PERM_PRV_RW_USR_NO| MPU_RGN_DISABLE); // // Define a region(#3) in peripheral space from 0x40000000 to 0x40100000 // (1 MB). Thisregion is accessible only in privileged mode. There is // an area from0x40020000 to 0x40040000 that has no peripherals and is not // accessible atall. This inaccessible region is created by disabling the // secondsub-region(1) and creating a hole. Further, there is an area // from 0x40080000to 0x400A0000 that should be accessible from user mode // as well. Thisarea is created by disabling the fifth sub-region (4), // and overlaying anadditional region (#4) in that space with the // appropriatepermissions. // MPURegionSet(3,0x40000000, MPU_RGN_SIZE_1M | MPU_RGN_PERM_NOEXEC | MPU_RGN_PERM_PRV_RW_USR_NO | MPU_SUB_RGN_DISABLE_1 | MPU_SUB_RGN_DISABLE_4 | MPU_RGN_ENABLE); MPURegionSet(4,0x40080000, MPU_RGN_SIZE_128K | MPU_RGN_PERM_NOEXEC | MPU_RGN_PERM_PRV_RW_USR_RW | MPU_RGN_ENABLE); // // In this example,compile-time registration of interrupts is used, so the // handler does nothave to be registered. However, it must be enabled. // IntEnable(FAULT_MPU); // // When setting upthe regions, region 2 was initially disabled for some // reason. At somepoint it must be enabled. // MPURegionEnable(2); // // Now the MPU isenabled. It is configured so that a default // map is availablein privileged mode if no regions are defined. The MPU // is not enabledfor the hard fault and NMI handlers, meaning that a // default is notused whenever these handlers are active, effectively // giving the faulthandlers access to all of memory without any // protection. // MPUEnable(MPU_CONFIG_PRIV_DEFAULT); // // At this point,the MPU is configured and enabled and if any code causes // an accessviolation, the memory management fault occurs. // The followingexample shows how to save and restore region configurations. // // The followingarrays provide space for saving the address and // attributes for 4region configurations. // uint32_tui32RegionAddr[4]; uint32_tui32RegionAttr[4]; ... // // At some point inthe system code, we want to save the state of 4 regions // (0-3). // for(ui8Idx = 0;ui8Idx < 4; ui8Idx++) { MPURegionGet(ui8Idx,&ui32RegionAddr[ui8Idx], &ui32RegionAttr[ui8Idx]); } ... // // At some otherpoint, the previously saved regions should be restored. // for(ui8Idx = 0;ui8Idx < 4; ui8Idx++) { MPURegionSet(ui8Idx,ui32RegionAddr[ui8Idx], ui32RegionAttr[ui8Idx]); }
[ 本帖最后由 平湖秋月 于 2013-8-19 20:18 编辑 ]
|