[SOLVED] STM32U5 TFM example (B-U585I-IOT02A) memory mapped PSRAM not accessible
We managed to successfully run TFM_SBSFU example for the IOT02A board. We are using latest STM32Cube_FW_U5_V1.2.0.
The problem is that we do not have access to PSRAM from NonSecure application when in memory mapped mode.
We implemented PSRAM init by importing drivers from BSP package into TFM Example, and initializing PSRAM imidiately in main, adter COM_Init(). For the test, we tried without changing GTZC_TZSC_MPCWM and SAU registers from secure app.
As expected, we are getting Secure Failed handler, with the information that we tried to access protected memory. All as expected.
Than we implemented memory area unprotection in TFM_Appli_Secure, by adding next line of codes in function:
fih_int sau_and_idau_cfg(void)
{
.
.
.
.
for (i = 0; i < ARRAY_SIZE(sau_init_cfg); i++)
{
SAU->RNR = sau_init_cfg[i].RNR;
SAU->RBAR = sau_init_cfg[i].RBAR & SAU_RBAR_BADDR_Msk;
SAU->RLAR = (sau_init_cfg[i].RLAR & SAU_RLAR_LADDR_Msk) |
(sau_init_cfg[i].nsc ? SAU_RLAR_NSC_Msk : 0U) |
SAU_RLAR_ENABLE_Msk;
/* Execution stopped if flow control failed */
FLOW_CONTROL_STEP(uFlowProtectValue, sau_init_cfg[i].flow_step_enable,
sau_init_cfg[i].flow_ctrl_enable);
}
// ADDED LINES TO UNLOCK OCTOSPI1_BASE and OCTOSPI2_BASE
SAU->RNR = (6 & SAU_RNR_REGION_Msk);
SAU->RBAR = (OCTOSPI1_BASE & SAU_RBAR_BADDR_Msk);
SAU->RLAR = ( ((OCTOSPI1_BASE + (8 * 1024 * 1024) - 1) & SAU_RLAR_LADDR_Msk)
| ((0 << SAU_RLAR_NSC_Pos) & SAU_RLAR_NSC_Msk)
| 1U);
SAU->RNR = (7 & SAU_RNR_REGION_Msk);
SAU->RBAR = (OCTOSPI2_BASE & SAU_RBAR_BADDR_Msk);
SAU->RLAR = ( ((OCTOSPI2_BASE + (32 * 1024 * 1024) - 1) & SAU_RLAR_LADDR_Msk)
| ((0 << SAU_RLAR_NSC_Pos) & SAU_RLAR_NSC_Msk)
| 1U);
MPCWM_ConfigTypeDef MPCWM_Desc =
{
.AreaId = GTZC_TZSC_MPCWM_ID1,
.AreaStatus = 1,
.Offset = 0,
.Attribute = GTZC_TZSC_MPCWM_REGION_NSEC | GTZC_TZSC_MPCWM_REGION_NPRIV,
.Length = (8 * 1024 * 1024),
.Lock = GTZC_TZSC_MPCWM_LOCK_ON,
};
HAL_GTZC_TZSC_MPCWM_ConfigMemAttributes(OCTOSPI1_BASE, &MPCWM_Desc);
MPCWM_Desc.Offset = 0;
MPCWM_Desc.Length = (32 * 1024 * 1024);
HAL_GTZC_TZSC_MPCWM_ConfigMemAttributes(OCTOSPI2_BASE, &MPCWM_Desc);
//ADDITION DONE
.
.
.
.
}By our understanding, this should be enough to have OCTOSPI1_BASE, and OCTOSPI2_BASE unlocked for access from TFM_Appli_NonSecure.
We did not changed anything else.
We are able to write to and read from PSRAM in command mode from TFM_Appli_NonSecure.
We are able to see PSRAM content in memory mapped mode from debugger in Memory View from TFM_Appli_NonSecure.
But we are not able to following code to access PSRAM. Exact the same code is working correctly in standalone nonsecure TZEN=0 application.
uint8_t aTxBuffer_ospi[] =
" ****Memory-mapped OSPI communication**** "
"****Memory-mapped OSPI communication**** "
"****Memory-mapped OSPI communication**** "
"****Memory-mapped OSPI communication**** "
"****Memory-mapped OSPI communication**** "
"****Memory-mapped OSPI communication**** ";
uint8_t *mem_addr;
uint8_t temp;
.
.
.
.
ret = BSP_OSPI_RAM_Init(0);
if(ret != BSP_ERROR_NONE)
{
DEBUG_ERROR("Deinitialization failed Failed\n");
return ret;
}
ret = BSP_OSPI_RAM_EnableMemoryMappedMode(0);
if(ret != BSP_ERROR_NONE)
{
DEBUG_ERROR("Enable Memory Mapped Mode: FAILED.\n");
DEBUG_ERROR("Test Aborted.\n");
error_code++;
}
if(error_code == 0)
{
/* Writing Sequence ----------------------------------------------- */
mem_addr = (uint8_t *)(OCTOSPI1_BASE);
for (uint16_t index = 0; index < 0x100; index++)
{
*mem_addr = aTxBuffer_ospi[index]; // <--POINT OF HardFault!!!
mem_addr++;
}
/* In memory-mapped mode, not possible to check if the memory is ready
after the programming. So a delay corresponding to max page programming
time is added */
HAL_Delay(100);
/* Reading Sequence ----------------------------------------------- */
mem_addr = (uint8_t *)(OCTOSPI1_BASE);
for (index = 0; index < 0x100; index++)
{
temp = *mem_addr; //<--POINT OF HardFault IF WRITING SEQUENCE COMMENTED OUT!!!
if (temp != aTxBuffer_ospi[index])
{
DEBUG_ERROR("Memory mapped read failed.\n");
}
mem_addr++;
}
DEBUG_INFO("OSPI RAM Memory Mapped: Successful\n");
}
.
.
.Failure is HardFault, but not able to get anything from stack, it looks like corrupted. HardFault handler is enabled in TFM_Appli_Secure. We tried by increasing stack, and heap for all application, but no luck.
It looks like we are missing important step in enabling NonSecure access to memory area.
Any help?
