I made the MPU demo program on the KSDK V1.1 using FRDM-K64F. I'm sorry but it is not for K22F.
#include <stdio.h>
#include <stdlib.h>
#include "fsl_device_registers.h"
#include "fsl_uart_driver.h"
#include "fsl_clock_manager.h"
#include "board.h"
#ifdef DEBUG
#include "fsl_debug_console.h"
#endif
#define M0_r ((3<<3)|(4))
#define M0_w ((3<<3)|(2))
#define M0_x ((3<<3)|(1))
void HardFault_Handler(void)
{
printf("MPU_EAR0=0x%08x\n\r",MPU_EAR0);
printf("MPU_EDR0=0x%08x\n\r",MPU_EDR0);
printf("MPU_EAR1=0x%08x\n\r",MPU_EAR1);
printf("MPU_EDR1=0x%08x\n\r",MPU_EDR1);
printf("MPU_EAR2=0x%08x\n\r",MPU_EAR2);
printf("MPU_EDR2=0x%08x\n\r",MPU_EDR2);
printf("MPU_EAR3=0x%08x\n\r",MPU_EAR3);
printf("MPU_EDR3=0x%08x\n\r",MPU_EDR3);
printf("MPU_EAR4=0x%08x\n\r",MPU_EAR4);
printf("MPU_EDR4=0x%08x\n\r",MPU_EDR4);
if(((int)MPU_EDR3 & 1)==0){ // If the read error causes,
MPU_RGDAAC4 |= M0_r; // emable read for the region tempory.
}
MPU_CESR |= MPU_CESR_SPERR_MASK; // clear interrupts
}
void MPU_Setup(void)
{
SIM_SCGC7 = SIM_SCGC7_MPU_MASK | SIM_SCGC7_FLEXBUS_MASK;
MPU_CESR &= ~MPU_CESR_VLD_MASK; // disable MPU
MPU_CESR |= MPU_CESR_SPERR_MASK; // clear interrupts
MPU_RGD0_WORD2 = 0; // ignore default protection for RGD0
MPU_RGD1_WORD0 = 0x00000000;// until SRAM area
MPU_RGD1_WORD1 = 0x1FFEFFFF;// Read/Write/Execute
MPU_RGD1_WORD2 = M0_r | M0_w | M0_x;
MPU_RGD1_WORD3 = 0x00000001;
MPU_RGD2_WORD0 = 0x1FFF0000; // SRAM_L
MPU_RGD2_WORD1 = 0x1FFFFFFF; // Read/Write
MPU_RGD2_WORD2 = M0_r | M0_w;
MPU_RGD2_WORD3 = 0x00000001;
MPU_RGD3_WORD0 = 0x20000000; // SRAM_U(Part1)
MPU_RGD3_WORD1 = 0x200000FF; // only Read
MPU_RGD3_WORD2 = M0_r ;
MPU_RGD3_WORD3 = 0x00000001;
MPU_RGD4_WORD0 = 0x20000100; // SRAM_U(part2)
MPU_RGD4_WORD1 = 0x200001FF; // only Write
MPU_RGD4_WORD2 = M0_w;;
MPU_RGD4_WORD3 = 0x00000001;
MPU_RGD5_WORD0 = 0x20000200; // SRAM_U(Part3)
MPU_RGD5_WORD1 = 0x2002FFFF; // Read/Write/Execute
MPU_RGD5_WORD2 = M0_r | M0_w | M0_x;
MPU_RGD5_WORD3 = 0x00000001;
MPU_RGD6_WORD0 = 0x20030000; // The rest address spaces
MPU_RGD6_WORD1 = 0xFFFFFFFF; // Read/Write/Execute
MPU_RGD6_WORD2 = M0_r | M0_w | M0_x;
MPU_RGD6_WORD3 = 0x00000001;
MPU_CESR |= MPU_CESR_VLD_MASK; // enabe MPU
}
volatile int a;
volatile int *p;
int main (void)
{
hardware_init();
configure_uart_pins(BOARD_DEBUG_UART_INSTANCE);
dbg_uart_init();
MPU_Setup(); // MPU Setting
// Tests of the protection errors
p = (int*)0x20000080;
a = *p;
printf("Passed - 1\n\r");
*p = a;
printf("Passed - 2\n\r");
p = (int*)0x20000100;
a= *p;
printf("Passed - 3\n\r");
*p = a;
printf("Passed - 4\n\r");
}
Basically returning from the protection fault (i.e. Hard Fault) has no meaning and OS should abort the task. In the sample program the store error can recover form the exception because the error PC pointed after the store instruction due to the store buffer. However, the read error causes repeatedly because the error PC pointed to the load instruction.
Yasuhiko Koumoto.