This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

MPU enabling triggers hard fault

Hi, I'm struggling with debugging a weird behaviour of my ARM-M3 board. I generated the following code with the ST IDE and what it does is configuring the MPU inside the SVC handler and then enabling it. However, as soon as the MPU is enabled (actually, as soon as the instruction "SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;" is executed) the MCU generates a hard fault.

I really don't know what to do about it and, since the code is pretty much it, I don't know how to get to the bottom of it. I would really appreciate any help. Thank you

void main(){
    //Jump to SVC handler
    __asm("SVC #0x00");
}
...
void SVC_Handler(void){
	//Privileged code
	ARM_MPU_ClrRegion(1);
	//Set first region over a RAM section with full privileges
	ARM_MPU_SetRegionEx(1UL,0x20004000UL,ARM_MPU_RASR(0UL,ARM_MPU_AP_FULL,0UL,0UL,1UL,1UL,0x00UL,ARM_MPU_REGION_SIZE_1MB));
	ARM_MPU_Enable(0);
}
...
__STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control){
  __DSB();
  __ISB();
  MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
#ifdef SCB_SHCSR_MEMFAULTENA_Msk
  SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk; //<-- Generates hard fault
#endif
}
...
void HardFault_Handler(void){
    //Code resumes here after MPU enable.
    while (1);
}