test_diram.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. /*
  7. Tests for D/IRAM support in heap capability allocator
  8. */
  9. #include <esp_types.h>
  10. #include <stdio.h>
  11. #include "unity.h"
  12. #include "esp_heap_caps.h"
  13. #include "soc/soc_memory_layout.h"
  14. #define ALLOC_SZ 1024
  15. #ifndef CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
  16. static void *malloc_block_diram(uint32_t caps)
  17. {
  18. void *attempts[256] = { 0 }; // Allocate up to 256 ALLOC_SZ blocks to exhaust all non-D/IRAM memory temporarily
  19. int count = 0;
  20. void *result;
  21. while(count < sizeof(attempts)/sizeof(void *)) {
  22. result = heap_caps_malloc(ALLOC_SZ, caps);
  23. TEST_ASSERT_NOT_NULL_MESSAGE(result, "not enough free heap to perform test");
  24. if (esp_ptr_in_diram_dram(result) || esp_ptr_in_diram_iram(result)) {
  25. break;
  26. }
  27. attempts[count] = result;
  28. result = NULL;
  29. count++;
  30. }
  31. for (int i = 0; i < count; i++) {
  32. free(attempts[i]);
  33. }
  34. TEST_ASSERT_NOT_NULL_MESSAGE(result, "not enough D/IRAM memory is free");
  35. return result;
  36. }
  37. TEST_CASE("Allocate D/IRAM as DRAM", "[heap][qemu-ignore]")
  38. {
  39. uint32_t *dram = malloc_block_diram(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  40. for (int i = 0; i < ALLOC_SZ / sizeof(uint32_t); i++) {
  41. uint32_t v = i + 0xAAAA;
  42. dram[i] = v;
  43. volatile uint32_t *iram = esp_ptr_diram_dram_to_iram(dram + i);
  44. TEST_ASSERT_EQUAL(v, dram[i]);
  45. TEST_ASSERT_EQUAL(v, *iram);
  46. *iram = UINT32_MAX;
  47. TEST_ASSERT_EQUAL(UINT32_MAX, *iram);
  48. TEST_ASSERT_EQUAL(UINT32_MAX, dram[i]);
  49. }
  50. free(dram);
  51. }
  52. TEST_CASE("Allocate D/IRAM as IRAM", "[heap][qemu-ignore]")
  53. {
  54. uint32_t *iram = malloc_block_diram(MALLOC_CAP_EXEC);
  55. for (int i = 0; i < ALLOC_SZ / sizeof(uint32_t); i++) {
  56. uint32_t v = i + 0xEEE;
  57. iram[i] = v;
  58. volatile uint32_t *dram = esp_ptr_diram_iram_to_dram(iram + i);
  59. TEST_ASSERT_EQUAL_HEX32(v, iram[i]);
  60. TEST_ASSERT_EQUAL_HEX32(v, *dram);
  61. *dram = UINT32_MAX;
  62. TEST_ASSERT_EQUAL_HEX32(UINT32_MAX, *dram);
  63. TEST_ASSERT_EQUAL_HEX32(UINT32_MAX, iram[i]);
  64. }
  65. free(iram);
  66. }
  67. #endif // CONFIG_ESP_SYSTEM_MEMPROT_FEATURE