喜欢1次
                                #include "main.h"
#include 
#include "bsp.h"
/** @addtogroup CM32M4xxR_StdPeriph_Examples
 * @{
 */
#define MACHINE_MODE_STACK_SIZE            0x400
static uint8_t sMachineModeStack[MACHINE_MODE_STACK_SIZE] = {0};
//Configure test_array Acess Permission on PMP
void PMP_Config() {
    PMP_Region_InitTypeDef pmp_init;
    pmp_init.Number = PMP_REGION_NUMBER0;
    pmp_init.Enable = PMP_REGION_ENABLE;   // Enable Configuration
    pmp_init.Lock = PMP_REGION_UNLOCK; //
    pmp_init.BaseAddress = 0;  //
    pmp_init.Size = PMP_REGION_SIZE_4GB;    //Setting array size
    pmp_init.AddressMatching = PMP_ADDRESS_MATCHING_NAPOT; //Setting PMP Size to NAPOT mode -> 2^n
    pmp_init.AccessPermission = PMP_ACCESS_RWX; //Setting array permission is Read Only
    PMP_ConfigRegion(&pmp_init);
    sPMP_Region_InitTypeDef spmp_init;
    spmp_init.Number = SPMP_REGION_NUMBER0;
    spmp_init.Enable = SPMP_REGION_ENABLE;
    spmp_init.Lock = SPMP_REGION_UNLOCK;
    spmp_init.BaseAddress = 0;
    spmp_init.Size = SPMP_REGION_SIZE_4GB;
    spmp_init.AddressMatching = SPMP_ADDRESS_MATCHING_NAPOT;
    spmp_init.UserMode = SPMP_USERMODE_RESET;
    spmp_init.AccessPermission = SPMP_ACCESS_RWX;
    sPMP_ConfigRegion(&spmp_init);
}
/**
 * @brief Jump User Mode
 */
void JumpUserMode(uint32_t func)
{
    __RV_CSR_WRITE(CSR_MSCRATCH, sMachineModeStack + MACHINE_MODE_STACK_SIZE);
    __RV_CSR_CLEAR(CSR_MSTATUS, MSTATUS_MPP);
    __RV_CSR_WRITE(CSR_MEPC, func);
    __ASM  volatile ( "mret ");
}
/**
 * @brief User Mode Application
 */
void Led()
{
    LedInit(LED2_PORT, LED2_PIN);
    while (1)
    {
        LedBlink(LED2_PORT, LED2_PIN);
        delay_ms(1000);
    }
}
/**
 * @brief Main function
 */
int main(void) {
    /* System Clocks Configuration */
    PMP_Config();
    JumpUserMode((uint32_t)Led);
    while (1) {
    }
}
 Projects\CM32M433R-START\Examples\RISC-V\UserMode这个目录下的例程,为什么分别设置了pmp和spmp对整个区间的限制?难道只设置一个不可以?(只设置一个确实会触发异常,只设置spmp时是异常1,只设置pmp异常12且mdcause为6也就是spmp加载异常,但是,why?)这块很奇怪,暂且不说把整个指令地址空间设置保护相当于没保护,那这个例程只涉及了M和U模式为什么还要动SPMP??想问一下这块板子是不是开了虚拟页管理?我只用到PMP的话应该如何设置??
