三、代码3.1、案例一
CubeMX上MPU的设置生成的代码如下: void MPU_Config(void) { MPU_Region_InitTypeDef MPU_InitStruct = {0};
/* Disables the MPU */ HAL_MPU_Disable(); /** Initializes and configures the Region and the memory to be protected */ MPU_InitStruct.Enable = MPU_REGION_ENABLE; //MPU Region MPU_InitStruct.Number = MPU_REGION_NUMBER0; //Region 0 Settings MPU_InitStruct.BaseAddress = 0x24000000; //MPU Region Base Address MPU_InitStruct.Size = MPU_REGION_SIZE_512KB; //MPU Region Size MPU_InitStruct.SubRegionDisable = 0x0; //MPU SubRegion Disable MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1; //MPU TEX field level MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS; //MPU Access Permission MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE; //MPU Instruction Access MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE; //MPU Shareability Permission MPU_InitStruct.IsCacheable = MPU_ACCESS_CACHEABLE; //MPU Cacheable Permission MPU_InitStruct.IsBufferable = MPU_ACCESS_BUFFERABLE; //MPU Bufferable Permission
HAL_MPU_ConfigRegion(&MPU_InitStruct); /* Enables the MPU */ HAL_MPU_Enable(MPU_HFNMI_PRIVDEF); //MPU Enable
}
3.2、案例二
void MPU_Config(void) { MPU_Region_InitTypeDef MPU_InitStruct = {0};
/* Disables the MPU */ HAL_MPU_Disable(); /** Initializes and configures the Region and the memory to be protected */ MPU_InitStruct.Enable = MPU_REGION_ENABLE; MPU_InitStruct.Number = MPU_REGION_NUMBER0; MPU_InitStruct.BaseAddress = 0x24000000; MPU_InitStruct.Size = MPU_REGION_SIZE_512KB; MPU_InitStruct.SubRegionDisable = 0x0; MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1; MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS; MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE; MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE; MPU_InitStruct.IsCacheable = MPU_ACCESS_CACHEABLE; MPU_InitStruct.IsBufferable = MPU_ACCESS_BUFFERABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct); /* Enables the MPU */ HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
}
3.3、案例一与案例二的区别
案例一与案例二的区别在于MPU control mode不一样,从而导致函数MPU_Config(void)里,最后一段代码HAL_MPU_Enable( )的入口参数从MPU_HFNMI_PRIVDEF改为MPU_PRIVILEGED_DEFAULT。功能上的区别是程序要不要在硬件错误等中断回调函数里处理MPU。
|