test_spiram_cache_flush.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. This code tests the interaction between PSRAM and SPI flash routines.
  3. */
  4. #include <esp_types.h>
  5. #include <stdio.h>
  6. #include "esp32/rom/ets_sys.h"
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "freertos/semphr.h"
  10. #include "freertos/queue.h"
  11. #include "freertos/xtensa_api.h"
  12. #include "unity.h"
  13. #include <stdint.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "esp32/rom/ets_sys.h"
  18. #include "esp_heap_caps.h"
  19. #include "esp_spi_flash.h"
  20. #include "esp_partition.h"
  21. #include "test_utils.h"
  22. #if CONFIG_ESP32_SPIRAM_SUPPORT
  23. #if CONFIG_SPIRAM_USE_CAPS_ALLOC || CONFIG_SPIRAM_USE_MALLOC
  24. #define USE_CAPS_ALLOC 1
  25. #endif // CONFIG_SPIRAM_USE_CAPS_ALLOC || CONFIG_SPIRAM_USE_MALLOC
  26. #define TSTSZ (16*1024)
  27. #if !CONFIG_FREERTOS_UNICORE
  28. volatile static int res[2], err[2];
  29. void tstMem(void *arg) {
  30. volatile unsigned char *mem=(volatile unsigned char*)arg;
  31. int p=0;
  32. while(1) {
  33. for (int i=0; i<TSTSZ; i++) {
  34. mem[i]=(i^p);
  35. }
  36. for (int i=0; i<TSTSZ; i++) {
  37. if (mem[i]!=((i^p)&0xff)) {
  38. printf("Core %d mem err! Got %x espected %x at addr %p\n", xPortGetCoreID(), mem[i], (i^p)&0xff, &mem[i]);
  39. err[xPortGetCoreID()]++;
  40. }
  41. }
  42. p++;
  43. res[xPortGetCoreID()]++;
  44. }
  45. }
  46. TEST_CASE("Spiram cache flush on mmap", "[spiram]")
  47. {
  48. void *mem[2];
  49. res[0]=0; res[1]=0;
  50. #if USE_CAPS_ALLOC
  51. printf("Allocating SPI RAM chunk...\n");
  52. mem[0]=heap_caps_malloc(TSTSZ, MALLOC_CAP_SPIRAM);
  53. mem[1]=heap_caps_malloc(TSTSZ, MALLOC_CAP_SPIRAM);
  54. #else
  55. mem[0]=(void*)0x3f800000;
  56. mem[1]=(void*)0x3f800000+TSTSZ;
  57. #endif
  58. assert(mem[0]);
  59. assert(mem[1]);
  60. TaskHandle_t th[2];
  61. err[0]=0; err[1]=0;
  62. printf("Creating tasks\n");
  63. xTaskCreatePinnedToCore(tstMem , "tskone" , 2048, mem[0], 3, &th[0], 0);
  64. xTaskCreatePinnedToCore(tstMem , "tsktwo" , 2048, mem[1], 3, &th[1], 1);
  65. for (int l=0; l<10; l++) {
  66. for (int p=0; p<4096*1024; p+=65536) {
  67. const void *out;
  68. spi_flash_mmap_handle_t h;
  69. spi_flash_mmap(p, 65536, SPI_FLASH_MMAP_DATA, &out, &h);
  70. spi_flash_munmap(h);
  71. }
  72. }
  73. printf("Checked memory %d and %d times. Errors: %d and %d\n", res[0], res[1], err[0], err[1]);
  74. vTaskDelete(th[0]);
  75. vTaskDelete(th[1]);
  76. #if USE_CAPS_ALLOC
  77. free(mem[0]);
  78. free(mem[1]);
  79. #endif
  80. TEST_ASSERT(err[0]==0);
  81. TEST_ASSERT(err[1]==0);
  82. }
  83. #define CYCLES 1024
  84. TEST_CASE("Spiram cache flush on write/read", "[spiram]")
  85. {
  86. void *mem[2];
  87. res[0]=0; res[1]=0;
  88. #if USE_CAPS_ALLOC
  89. printf("Allocating SPI RAM chunk...\n");
  90. mem[0]=heap_caps_malloc(TSTSZ, MALLOC_CAP_SPIRAM);
  91. mem[1]=heap_caps_malloc(TSTSZ, MALLOC_CAP_SPIRAM);
  92. #else
  93. mem[0]=(void*)0x3f800000;
  94. mem[1]=(void*)0x3f800000+TSTSZ;
  95. #endif
  96. assert(mem[0]);
  97. assert(mem[1]);
  98. TaskHandle_t th[2];
  99. const esp_partition_t* part = get_test_data_partition();
  100. assert(part!=NULL);
  101. printf("Erasing sector...\n");
  102. esp_partition_erase_range(part, 0, 64*1024);
  103. printf("Erased.\n");
  104. printf("Creating tasks\n");
  105. xTaskCreatePinnedToCore(tstMem , "tskone" , 2048, mem[0], 3, &th[0], 0);
  106. xTaskCreatePinnedToCore(tstMem , "tsktwo" , 2048, mem[1], 3, &th[1], 1);
  107. char buf[512];
  108. const void *out;
  109. spi_flash_mmap_handle_t handle;
  110. esp_partition_mmap(part, 0, 512, SPI_FLASH_MMAP_DATA, &out, &handle);
  111. for (int i=0; i<CYCLES; i++) {
  112. esp_partition_write(part, 0, buf, 512);
  113. esp_partition_read(part, 0, buf, 512);
  114. vTaskDelay(1);
  115. }
  116. spi_flash_munmap(handle);
  117. printf("Checked memory %d and %d times.\n", res[0], res[1]);
  118. vTaskDelete(th[0]);
  119. vTaskDelete(th[1]);
  120. #if USE_CAPS_ALLOC
  121. free(mem[0]);
  122. free(mem[1]);
  123. #endif
  124. }
  125. #endif // !CONFIG_FREERTOS_UNICORE
  126. IRAM_ATTR TEST_CASE("Spiram memcmp weirdness at 80MHz", "[spiram]") {
  127. char *mem1=malloc(0x10000);
  128. #if USE_CAPS_ALLOC
  129. char *mem2=heap_caps_malloc(0x10000, MALLOC_CAP_SPIRAM);
  130. #else
  131. char *mem2=(void*)0x3f800000;
  132. #endif
  133. #if !CONFIG_SPIRAM_SPEED_80M
  134. printf("**** WARNING **** Spi memory isn't running at 80MHz, so this test is somewhat meaningless.\n");
  135. #endif
  136. printf("RAM: Got %p and %p\n", mem1, mem2);
  137. assert(mem1);
  138. assert(mem2);
  139. for (int i=0; i<0x10000; i++) mem1[i]=i^0xAAAAAAAA;
  140. for (int cycle=1; cycle<100; cycle++) {
  141. memcpy(mem2, mem1, 0x10000);
  142. if (memcmp(mem1, mem2, 0x10000)!=0) {
  143. printf("Memcmp failed! Cycle %d\n", cycle);
  144. for (int i=0; i<0x10000; i++) {
  145. if (mem1[i]!=mem2[i]) {
  146. printf("Found real difference at index %d: 0x%x vs 0x%x\n", i, mem1[i], mem2[i]);
  147. break;
  148. }
  149. }
  150. }
  151. }
  152. free(mem1);
  153. #if USE_CAPS_ALLOC
  154. free(mem2);
  155. #endif
  156. }
  157. #endif // CONFIG_ESP32_SPIRAM_SUPPORT