test_spiram_cache_flush.c 4.8 KB

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