test_himem.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "unity.h"
  7. #include <stdio.h>
  8. #include <stdbool.h>
  9. #include <stdint.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "esp_system.h"
  13. #include "esp32/rom/cache.h"
  14. #include "sdkconfig.h"
  15. #include "esp32/himem.h"
  16. #if CONFIG_IDF_TARGET_ESP32
  17. #if CONFIG_SPIRAM_BANKSWITCH_ENABLE
  18. //Fill memory with pseudo-random data generated from the given seed.
  19. static void fill_mem_seed(int seed, void *mem, int len)
  20. {
  21. uint32_t *p = (uint32_t *)mem;
  22. unsigned int rseed = seed ^ 0xa5a5a5a5;
  23. for (int i = 0; i < len / 4; i++) {
  24. *p++ = rand_r(&rseed);
  25. }
  26. }
  27. //Check the memory filled by fill_mem_seed. Returns true if the data is still intact.
  28. static bool check_mem_seed(int seed, void *mem, int len)
  29. {
  30. uint32_t *p = (uint32_t *)mem;
  31. unsigned int rseed = seed ^ 0xa5a5a5a5;
  32. for (int i = 0; i < len / 4; i++) {
  33. uint32_t ex = rand_r(&rseed);
  34. if (ex != *p) {
  35. printf("check_mem_seed: %p @ %p has 0x%08x expected 0x%08x\n", mem, p, *p, ex);
  36. return false;
  37. }
  38. p++;
  39. }
  40. return true;
  41. }
  42. //Allocate a himem region, fill it with data, check it and release it.
  43. static bool test_region(int check_size, int seed)
  44. {
  45. esp_himem_handle_t mh;
  46. esp_himem_rangehandle_t rh;
  47. bool ret = true;
  48. ESP_ERROR_CHECK(esp_himem_alloc(check_size, &mh));
  49. ESP_ERROR_CHECK(esp_himem_alloc_map_range(ESP_HIMEM_BLKSZ * 2, &rh));
  50. for (int i = 0; i < check_size; i += ESP_HIMEM_BLKSZ) {
  51. uint32_t *ptr = NULL;
  52. ESP_ERROR_CHECK(esp_himem_map(mh, rh, i, 0, ESP_HIMEM_BLKSZ, 0, (void**)&ptr));
  53. fill_mem_seed(i ^ seed, ptr, ESP_HIMEM_BLKSZ);
  54. ESP_ERROR_CHECK(esp_himem_unmap(rh, ptr, ESP_HIMEM_BLKSZ));
  55. }
  56. for (int i = 0; i < check_size; i += ESP_HIMEM_BLKSZ) {
  57. uint32_t *ptr;
  58. ESP_ERROR_CHECK(esp_himem_map(mh, rh, i, 0, ESP_HIMEM_BLKSZ, 0, (void**)&ptr));
  59. if (!check_mem_seed(i ^ seed, ptr, ESP_HIMEM_BLKSZ)) {
  60. printf("Error in block %d\n", i / ESP_HIMEM_BLKSZ);
  61. ret = false;
  62. break;
  63. }
  64. ESP_ERROR_CHECK(esp_himem_unmap(rh, ptr, ESP_HIMEM_BLKSZ));
  65. }
  66. ESP_ERROR_CHECK(esp_himem_free(mh));
  67. ESP_ERROR_CHECK(esp_himem_free_map_range(rh));
  68. return ret;
  69. }
  70. static volatile int testsDone;
  71. static void memtest_thread(void *arg)
  72. {
  73. int d = (int)arg;
  74. for (int i = 0; i < 4; i++) {
  75. printf("d=%d check=%d\n", d, i);
  76. test_region(2 * 1024 * 1024, d + (i << 16));
  77. vTaskDelay(d);
  78. }
  79. testsDone++; //note possible race here... not really an issue if the two tasks have different vTaskDelay args
  80. vTaskDelete(NULL);
  81. }
  82. TEST_CASE("high psram memory test", "[himem]")
  83. {
  84. printf("Doing single-core test\n");
  85. assert(test_region(4 * 1024 * 1024, 0xaaaa));
  86. testsDone = 0;
  87. printf("Doing dual-core test...\n");
  88. xTaskCreatePinnedToCore(&memtest_thread, "th2", 1024 * 2, (void *)2, 5, NULL, 1);
  89. xTaskCreatePinnedToCore(&memtest_thread, "th1", 1024 * 2, (void *)5, 5, NULL, 0);
  90. while (testsDone != 2) {
  91. vTaskDelay(10);
  92. }
  93. printf("Done!\n");
  94. vTaskDelay(100);
  95. }
  96. #endif
  97. #endif // CONFIG_IDF_TARGET_ESP32