cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L496ZG MPU test

JBiVTSU
Associate

Hello,

 

I'm trying to implement a MPU test on a STM32L496ZG MCU that will be part of some self-tests in our application.

 

The MPU is working as expected and an interrupt occurs when I try to write to a protected memory region. But now I need to "catch" this interrupt and continue the code execution like nothing happened.

I know I can override the MemManage_Handler and do some stuff in it but my issue is that when I return from the handler, it immediately enters again. 

Is there any way I can exit MemManage_Handler and continue code execution normally ?

 

Here's my code:

 

int main(void)
{
  // HAL init + clock init...

  // Enable MemManage_Handler
  SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;

  // Configure MPU
  HAL_MPU_Disable();
  MPU_Region_InitTypeDef mpu_struct;
  mpu_struct.Enable = MPU_REGION_ENABLE;
  mpu_struct.BaseAddress = 0x081FF000;
  mpu_struct.Size = MPU_REGION_SIZE_4KB;
  mpu_struct.AccessPermission = MPU_REGION_PRIV_RO;
  mpu_struct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
  mpu_struct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
  mpu_struct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
  mpu_struct.Number = MPU_REGION_NUMBER0;
  mpu_struct.TypeExtField = MPU_TEX_LEVEL0;
  mpu_struct.SubRegionDisable = 0x00;
  mpu_struct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
  HAL_MPU_ConfigRegion(&mpu_struct);
  HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);

  // Test
  uint32_t* p_write_addr = reinterpret_cast<uint32_t*>(0x081FF000);
  *p_write_addr = 0xBADC0DE;

  while (1)
  {
    HAL_GPIO_TogglePin(DO_LED_GPIO_Port, DO_LED_Pin);
    HAL_Delay(1000);
  }
}

 

Thanks !

0 REPLIES 0