test_spiram_cache_flush.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_USE_CAPS_ALLOC || CONFIG_SPIRAM_USE_MEMMAP
  25. #define TSTSZ (16*1024)
  26. volatile static int res[2], err[2];
  27. void tstMem(void *arg) {
  28. volatile unsigned char *mem=(volatile unsigned char*)arg;
  29. int p=0;
  30. while(1) {
  31. for (int i=0; i<TSTSZ; i++) {
  32. mem[i]=(i^p);
  33. }
  34. for (int i=0; i<TSTSZ; i++) {
  35. if (mem[i]!=((i^p)&0xff)) {
  36. printf("Core %d mem err! Got %x espected %x at addr %p\n", xPortGetCoreID(), mem[i], (i^p)&0xff, &mem[i]);
  37. err[xPortGetCoreID()]++;
  38. }
  39. }
  40. p++;
  41. res[xPortGetCoreID()]++;
  42. }
  43. }
  44. TEST_CASE("Spiram cache flush on mmap", "[spiram][ignore]")
  45. {
  46. void *mem[2];
  47. res[0]=0; res[1]=0;
  48. #if CONFIG_SPIRAM_USE_CAPS_ALLOC
  49. mem[0]=pvPortMallocCaps(TSTSZ, MALLOC_CAP_SPIRAM);
  50. mem[1]=pvPortMallocCaps(TSTSZ, MALLOC_CAP_SPIRAM);
  51. #else
  52. mem[0]=(void*)0x3f800000;
  53. mem[1]=(void*)0x3f800000+TSTSZ;
  54. #endif
  55. TaskHandle_t th[2];
  56. err[0]=0; err[1]=0;
  57. printf("Creating tasks\n");
  58. xTaskCreatePinnedToCore(tstMem , "tskone" , 2048, mem[0], 3, &th[0], 0);
  59. xTaskCreatePinnedToCore(tstMem , "tsktwo" , 2048, mem[1], 3, &th[1], 1);
  60. const esp_partition_t* part = get_test_data_partition();
  61. for (int l=0; l<10; l++) {
  62. for (int p=0; p<4096*1024; p+=65536) {
  63. const void *out;
  64. spi_flash_mmap_handle_t h;
  65. spi_flash_mmap(p, 65536, SPI_FLASH_MMAP_DATA, &out, &h);
  66. spi_flash_munmap(h);
  67. }
  68. printf("%d/10\n", l);
  69. }
  70. printf("Checked memory %d and %d times. Errors: %d and %d\n", res[0], res[1], err[0], err[1]);
  71. vTaskDelete(th[0]);
  72. vTaskDelete(th[1]);
  73. #if CONFIG_SPIRAM_USE_CAPS_ALLOC
  74. free(mem[0]);
  75. free(mem[1]);
  76. #endif
  77. TEST_ASSERT(err[0]==0);
  78. TEST_ASSERT(err[1]==0);
  79. }
  80. #define CYCLES 1024
  81. TEST_CASE("Spiram cache flush on write/read", "[spiram][ignore]")
  82. {
  83. void *mem[2];
  84. res[0]=0; res[1]=0;
  85. #if CONFIG_SPIRAM_USE_CAPS_ALLOC
  86. mem[0]=pvPortMallocCaps(TSTSZ, MALLOC_CAP_SPIRAM);
  87. mem[1]=pvPortMallocCaps(TSTSZ, MALLOC_CAP_SPIRAM);
  88. #else
  89. mem[0]=(void*)0x3f800000;
  90. mem[1]=(void*)0x3f800000+TSTSZ;
  91. #endif
  92. TaskHandle_t th[2];
  93. const esp_partition_t* part = get_test_data_partition();
  94. assert(part!=NULL);
  95. printf("Erasing sector...\n");
  96. esp_partition_erase_range(part, 0, 64*1024);
  97. printf("Erased.\n");
  98. printf("Creating tasks\n");
  99. xTaskCreatePinnedToCore(tstMem , "tskone" , 2048, mem[0], 3, &th[0], 0);
  100. xTaskCreatePinnedToCore(tstMem , "tsktwo" , 2048, mem[1], 3, &th[1], 1);
  101. char buf[512];
  102. const void *out;
  103. spi_flash_mmap_handle_t handle;
  104. esp_partition_mmap(part, 0, 512, SPI_FLASH_MMAP_DATA, &out, &handle);
  105. for (int i=0; i<CYCLES; i++) {
  106. printf("%d/%d\n", i, CYCLES);
  107. esp_partition_write(part, 0, buf, 512);
  108. esp_partition_read(part, 0, buf, 512);
  109. vTaskDelay(1);
  110. }
  111. spi_flash_munmap(handle);
  112. printf("Checked memory %d and %d times.\n", res[0], res[1]);
  113. vTaskDelete(th[0]);
  114. vTaskDelete(th[1]);
  115. #if CONFIG_SPIRAM_USE_CAPS_ALLOC
  116. free(mem[0]);
  117. free(mem[1]);
  118. #endif
  119. }
  120. #endif //CONFIG_SPIRAM_USE_CAPS_ALLOC || CONFIG_SPIRAM_USE_MEMMAP