test_spiram_cache_flush.c 4.9 KB

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