test_diram.c 2.1 KB

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