test_diram.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. static void *malloc_block_diram(uint32_t caps)
  11. {
  12. void *attempts[256] = { 0 }; // Allocate up to 256 ALLOC_SZ blocks to exhaust all non-D/IRAM memory temporarily
  13. int count = 0;
  14. void *result;
  15. while(count < sizeof(attempts)/sizeof(void *)) {
  16. result = heap_caps_malloc(ALLOC_SZ, caps);
  17. TEST_ASSERT_NOT_NULL_MESSAGE(result, "not enough free heap to perform test");
  18. if (esp_ptr_in_diram_dram(result) || esp_ptr_in_diram_iram(result)) {
  19. break;
  20. }
  21. attempts[count] = result;
  22. result = NULL;
  23. count++;
  24. }
  25. for (int i = 0; i < count; i++) {
  26. free(attempts[i]);
  27. }
  28. TEST_ASSERT_NOT_NULL_MESSAGE(result, "not enough D/IRAM memory is free");
  29. return result;
  30. }
  31. TEST_CASE("Allocate D/IRAM as DRAM", "[heap]")
  32. {
  33. uint32_t *dram = malloc_block_diram(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  34. for (int i = 0; i < ALLOC_SZ / sizeof(uint32_t); i++) {
  35. uint32_t v = i + 0xAAAA;
  36. dram[i] = v;
  37. volatile uint32_t *iram = esp_ptr_diram_dram_to_iram(dram + i);
  38. TEST_ASSERT_EQUAL(v, dram[i]);
  39. TEST_ASSERT_EQUAL(v, *iram);
  40. *iram = UINT32_MAX;
  41. TEST_ASSERT_EQUAL(UINT32_MAX, *iram);
  42. TEST_ASSERT_EQUAL(UINT32_MAX, dram[i]);
  43. }
  44. free(dram);
  45. }
  46. TEST_CASE("Allocate D/IRAM as IRAM", "[heap]")
  47. {
  48. uint32_t *iram = malloc_block_diram(MALLOC_CAP_EXEC);
  49. for (int i = 0; i < ALLOC_SZ / sizeof(uint32_t); i++) {
  50. uint32_t v = i + 0xEEE;
  51. iram[i] = v;
  52. volatile uint32_t *dram = esp_ptr_diram_iram_to_dram(iram + i);
  53. TEST_ASSERT_EQUAL_HEX32(v, iram[i]);
  54. TEST_ASSERT_EQUAL_HEX32(v, *dram);
  55. *dram = UINT32_MAX;
  56. TEST_ASSERT_EQUAL_HEX32(UINT32_MAX, *dram);
  57. TEST_ASSERT_EQUAL_HEX32(UINT32_MAX, iram[i]);
  58. }
  59. free(iram);
  60. }