1 #include <stdio.h>
2 #include <stdbool.h>
3 #include "unity.h"
4 
5 #include "esp_attr.h"
6 
7 #include "hal/mpu_hal.h"
8 
9 // TODO ESP32-C3 IDF-2375
10 // LL still not implemented
11 
12 #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C3)
13 
14 volatile static int RTC_NOINIT_ATTR access = 0;
15 
trigger_illegal_access(void)16 static void trigger_illegal_access(void)
17 {
18     access = 0;
19     intptr_t addr = 0x80000000; // MPU region 4
20     volatile int __attribute__((unused)) val = 0;
21 
22     // Marked as an illegal access region at startup in ESP32, ESP32S2.
23     // Make accessible temporarily.
24     mpu_hal_set_region_access(4, MPU_REGION_RW);
25 
26     val = *((int*) addr);
27     ++access;
28     TEST_ASSERT_EQUAL(1, access);
29     printf("Sucessfully accessed location %p\r\n", (void*)addr);
30 
31     // Make access to region illegal again.
32     mpu_hal_set_region_access(4, MPU_REGION_ILLEGAL);
33     ++access;
34 
35     // Since access to region is illegal, this should fail (causing a reset), and the increment
36     // to access count is not performed.
37     val = *((int*) addr);
38     ++access;
39 }
40 
check_access(void)41 void check_access(void)
42 {
43     TEST_ASSERT_EQUAL(2, access);
44 }
45 
46 TEST_CASE_MULTIPLE_STAGES("Can set illegal access regions", "[soc][mpu]",
47         trigger_illegal_access,
48         check_access);
49 
50 #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C3)
51