您好!
我正在尝试找到有关如何设置和使用 MPU 的示例。 SDK 文档详细介绍了如何设置 MPU 区域、以及如何设置访问函数的权限。 您能否提供一个简单的示例代码、其中在特定的 MPU 保护区域有一个全局变量、还有三个函数、其中一个函数具有读取/写入访问权限、一个函数仅具有读取访问权限、一个函数没有读取和写入访问权限。
此致、
哈沙
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
您好!
我正在尝试找到有关如何设置和使用 MPU 的示例。 SDK 文档详细介绍了如何设置 MPU 区域、以及如何设置访问函数的权限。 您能否提供一个简单的示例代码、其中在特定的 MPU 保护区域有一个全局变量、还有三个函数、其中一个函数具有读取/写入访问权限、一个函数仅具有读取访问权限、一个函数没有读取和写入访问权限。
此致、
哈沙
您好、Harsha、
请查找文件-
e2e.ti.com/.../hello_5F00_world_5F00_mpu_5F00_function_5F00_test.c
这里还会提到该模块的 MPU 设置:

希望它有所帮助。
此致、
Aakash
尊敬的 Aakash:
非常感谢您的立即回应,并很抱歉我的延迟回复。 从您的示例代码中、我了解如何设置和使用 MPU。 我只有一个问题:
请告诉我、我的理解是否正确?
每次必须执行以下操作时访问受保护区域:
1. 禁用 MPU
2. 更改访问权限(以获取访问权限)
3. 启用 MPU
访问之后、我应该再次:
1. 禁用 MPU
2. 更改访问权限(恢复访问权限)
3. 启用 MPU
这是我通常应该如何使用 MPU 吗?另外、是否有必要执行第二部分、即恢复访问? 如果我不撤消访问、会发生什么情况?
非常感谢您的帮助。
此致、
哈沙
尊敬的 Aakash:
我尝试了以上示例、但似乎失败了。 请让我知道我在这里犯了什么错。
/*
* Copyright (C) 2021 Texas Instruments Incorporated
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <kernel/dpl/DebugP.h>
#include <kernel/dpl/MpuP_armv7.h>
#include "ti_drivers_config.h"
#include "ti_drivers_open_close.h"
#include "ti_board_open_close.h"
/*
* This is an empty project provided for all cores present in the device.
* User can use this project to start their application by adding more SysConfig modules.
*
* This application does driver and board init and just prints the pass string on the console.
* In case of the main core, the print is redirected to the UART console.
* For all other cores, CCS prints are used.
*/
MpuP_RegionConfig localMpuRegionConfig = {
.baseAddr = 0x70080000,
.size = MpuP_RegionSize_1K,
.attrs = {
.isEnable = 1,
.isCacheable = 1,
.isBufferable = 0,
.isSharable = 1,
.isExecuteNever = 1,
.tex = 0,
.accessPerm = MpuP_AP_ALL_BLOCK,
.subregionDisableMask = 0x0u
},
};
uint8_t secure_variable[1024] __attribute__((section(".data.secure_variable"))) = {0x01, 0x02, 0x03, 0x04, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
void access_permission_set(uint16_t perm)
{
/* Disable MPU */
localMpuRegionConfig.attrs.isEnable = 0;
localMpuRegionConfig.attrs.isCacheable = 0;
MpuP_setRegion(6, (void *)localMpuRegionConfig.baseAddr, localMpuRegionConfig.size, (MpuP_RegionAttrs *)&localMpuRegionConfig.attrs);
/* Change the Access Permission */
localMpuRegionConfig.attrs.accessPerm = (uint8_t)perm;
MpuP_setRegion(6, (void *)localMpuRegionConfig.baseAddr, localMpuRegionConfig.size, (MpuP_RegionAttrs *)&localMpuRegionConfig.attrs);
/* Enable MPU */
localMpuRegionConfig.attrs.isEnable = 1;
localMpuRegionConfig.attrs.isCacheable = 1;
MpuP_setRegion(6, (void *)localMpuRegionConfig.baseAddr, localMpuRegionConfig.size, (MpuP_RegionAttrs *)&localMpuRegionConfig.attrs);
}
void function_a(void)
{
access_permission_set(MpuP_AP_ALL_RW);
DebugP_log("Function A\r\n");
/* Testing the Read Access */
DebugP_log("Variable Function Data %x\r\n", secure_variable[0]);
/* Testing the Write Access */
secure_variable[0] = 0xFF;
/* Testing the Read Access again to prevent optimization */
DebugP_log("Variable Function Data %x\r\n", secure_variable[0]);
access_permission_set(MpuP_AP_ALL_BLOCK);
}
void function_b(void)
{
access_permission_set(MpuP_AP_ALL_R);
DebugP_log("Function B\r\n");
DebugP_log("Variable Function Data %x\r\n", secure_variable[0]);
/* Writing will lead to abort, so commented */
secure_variable[0] = 0xAA;
DebugP_log("Variable Function Data %x\r\n", secure_variable[0]);
access_permission_set(MpuP_AP_ALL_BLOCK);
}
void function_c(void)
{
DebugP_log("Function C\r\n");
/* Reading will lead to abort, so commented */
DebugP_log("Variable Function Data %x\r\n", secure_variable[0]);
/* Writing will lead to abort, so commented */
secure_variable[0] = 0xCC;
DebugP_log("Variable Function Data %x\r\n", secure_variable[0]);
}
void empty_main(void *args)
{
/* Open drivers to open the UART driver for console */
Drivers_open();
Board_driversOpen();
function_a();
function_b();
function_c();
DebugP_log("All tests have passed!!\r\n");
while(1)
{
}
Board_driversClose();
Drivers_close();
}
syscfg 中的 MPU 配置

UART 端子上的输出:

函数 b 和 c 应该已经触发中止、但它们都不应该触发。
电源发展
此致、
哈沙
大家好、 Harsha Dsouza、
您能分享您的地图文件吗? 您的 .data.secure_variable 是否与1K 存储器对齐? MPU 仅根据区域的大小对齐内存、因此.data.secure_variable 应处于0x70080000。
如果上面的设置没问题的话、您能在调试器中读出 CPR15寄存器以分享更多信息吗? 此外、请在 函数 B/C 中完成每次 ACCESS_PERMISSION_SET 调用后执行此实验?
此致、
Aakash