test_himem.c 3.0 KB

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