test_mpu.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include "unity.h"
  4. #include "esp_attr.h"
  5. #include "hal/mpu_hal.h"
  6. volatile static int RTC_NOINIT_ATTR access = 0;
  7. static void trigger_illegal_access(void)
  8. {
  9. access = 0;
  10. intptr_t addr = 0x80000000; // MPU region 4
  11. volatile int __attribute__((unused)) val = 0;
  12. // Marked as an illegal access region at startup in ESP32, ESP32S2.
  13. // Make accessible temporarily.
  14. mpu_hal_set_region_access(4, MPU_REGION_RW);
  15. val = *((int*) addr);
  16. ++access;
  17. TEST_ASSERT_EQUAL(1, access);
  18. printf("Sucessfully accessed location %p\r\n", (void*)addr);
  19. // Make access to region illegal again.
  20. mpu_hal_set_region_access(4, MPU_REGION_ILLEGAL);
  21. ++access;
  22. // Since access to region is illegal, this should fail (causing a reset), and the increment
  23. // to access count is not performed.
  24. val = *((int*) addr);
  25. ++access;
  26. }
  27. void check_access(void)
  28. {
  29. TEST_ASSERT_EQUAL(2, access);
  30. }
  31. TEST_CASE_MULTIPLE_STAGES("Can set illegal access regions", "[soc][mpu]",
  32. trigger_illegal_access,
  33. check_access);