test_spiram_cache_flush.c 4.8 KB

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