test_spiram_cache_flush.c 4.8 KB

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