/** * @brief MPU region configuration function */ void MPU_Config(void) { MPU_Disable(); /* Configure FLASH region as REGION N0, 1MB of size and R/W region */ MPU_Region_Config(0,FLASH_ADDRESS_START,FLASH_SIZE,MPU_REGION_PRIVILEGED_READ_WRITE); /* Configure RAM region as Region N1, 128kB of size and R/W region */ MPU_Region_Config(1,RAM1_ADDRESS_START,RAM1_SIZE,MPU_REGION_PRIVILEGED_READ_WRITE); /* Configure small RAM region as REGION N2, 32Byte of size, no access region and Execute Never region */ MPU_Region_Config(2,RAM2_ADDRESS_START,RAM2_SIZE,MPU_NO_ACCESS); /* Configure Peripheral region as REGION N3, 128KB of size, R/W region */ MPU_Region_Config(3,PERIPH_ADDRESS_START,PERIPH_SIZE,MPU_REGION_PRIVILEGED_READ_WRITE); /* Enable the memory fault exception */ SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk; /* Enable MPU */ MPU_Enable(); }
/**************************************************************************//** * @brief Main function. *****************************************************************************/ int main( void ) { int i; MPU_RegionInit_TypeDef flashInit = MPU_INIT_FLASH_DEFAULT; MPU_RegionInit_TypeDef sramInit = MPU_INIT_SRAM_DEFAULT; MPU_RegionInit_TypeDef peripheralInit = MPU_INIT_PERIPHERAL_DEFAULT; /* Chip alignment */ CHIP_Init(); /* If first word of user data page is non-zero, enable eA Profiler trace */ BSP_TraceProfilerSetup(); /* Enable LCD without voltage boost */ SegmentLCD_Init( false ); SegmentLCD_AllOff(); SegmentLCD_Symbol(LCD_SYMBOL_GECKO, 1); SegmentLCD_Symbol(LCD_SYMBOL_EFM32, 1); GpioInit(); RTCDRV_Setup( cmuSelect_LFXO, cmuClkDiv_32); ScrollText( " MPU DEMO PRESS Pb0 OR Pb1 " "TO GENERATE MPU EXCEPTIONS " ); MPU_Disable(); /* Flash memory */ MPU_ConfigureRegion( &flashInit ); /* SRAM */ MPU_ConfigureRegion( &sramInit ); /* SRAM, a 4k part with priviledged only access, this regions settings */ /* will override those of the previous region */ sramInit.regionNo = 2; sramInit.baseAddress = RAM_MEM_BASE + 0x2000; sramInit.size = mpuRegionSize4Kb; sramInit.accessPermission = mpuRegionApPRw; MPU_ConfigureRegion( &sramInit ); /* LCD, priviledged only access */ peripheralInit.regionNo = 3; peripheralInit.baseAddress = LCD_BASE; peripheralInit.size = mpuRegionSize128b; peripheralInit.accessPermission = mpuRegionApPRw; MPU_ConfigureRegion( &peripheralInit ); MPU_Enable( MPU_CTRL_PRIVDEFENA ); /* Full access to default memory map */ /* in priviledged state */ i = 0; while ( 1 ) { SegmentLCD_Number( i ); /* Count on numeric diplay */ i = ( i + 1 ) % 101; RTCDRV_Delay( 150 , false); if ( PB0_PUSHED() ) { BullsEye( 1 ); /* Generate an access violation in internal SRAM */ __set_CONTROL( 1 ); /* Enter User (unpriviledged) state */ *(volatile uint32_t *)(RAM_MEM_BASE + 0x2000) = 1; BullsEye( 0 ); } if ( PB1_PUSHED() ) { BullsEye( 1 ); /* Generate an access violation in LCD peripheral */ __set_CONTROL( 1 ); /* Enter User (unpriviledged) state */ BullsEye( 0 ); } } }
/**************************************************************************//** * @brief Main function *****************************************************************************/ int main(void) { int c; MPU_RegionInit_TypeDef flashInit = MPU_INIT_FLASH_DEFAULT; MPU_RegionInit_TypeDef sramInit = MPU_INIT_SRAM_DEFAULT; MPU_RegionInit_TypeDef peripheralInit = MPU_INIT_PERIPHERAL_DEFAULT; /* Chip revision alignment and errata fixes */ CHIP_Init(); /* Initialize DK board register access */ BSP_Init(BSP_INIT_DK_SPI); /* If first word of user data page is non-zero, enable eA Profiler trace */ BSP_TraceProfilerSetup(); /* Enable printf on RS232 port - this example only supports LEUART */ RETARGET_SerialInit(); RETARGET_SerialCrLf(1); printf("\nEFM32 MPU access violation example.\n" "Hit lowercase 'x' to force access violations.\n"); MPU_Disable(); /* Flash memory */ MPU_ConfigureRegion( &flashInit ); /* SRAM */ MPU_ConfigureRegion( &sramInit ); /* SRAM, a 4k part with priviledged only access, this regions settings */ /* will override those of the previous region */ sramInit.regionNo = 2; sramInit.baseAddress = RAM_MEM_BASE + 0x2000; sramInit.size = mpuRegionSize4Kb; sramInit.accessPermission = mpuRegionApPRw; MPU_ConfigureRegion( &sramInit ); /* LEUART, priviledged only access */ peripheralInit.regionNo = 3; peripheralInit.baseAddress = LEUART1_BASE; peripheralInit.size = mpuRegionSize128b; peripheralInit.accessPermission = mpuRegionApPRw; MPU_ConfigureRegion( &peripheralInit ); MPU_Enable( MPU_CTRL_PRIVDEFENA ); /* Full access to default memory map */ /* in priviledged state */ while (1) { EMU_EnterEM2(true); /* Retrieve new character */ c = getchar(); if (c > 0) { if ( c == 'x' ) { /* Generate an access violation in LEUART1 peripheral */ __set_CONTROL( 1 ); /* Enter User (unpriviledged) state */ putchar( c ); /* MemManage_Handler() will set back to priviledged state */ /* Generate an access violation in internal SRAM */ __set_CONTROL( 1 ); /* Enter User (unpriviledged) state */ *(volatile uint32_t *)(RAM_MEM_BASE + 0x2000) = 1; /* MemManage_Handler() will set back to priviledged state */ } else { /* Echo character */ putchar(c); } } /* Most terminals issue CR when pressing enter, add LF */ if (c == '\r') { putchar('\n'); } } }