test_flash_psram.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "sdkconfig.h"
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "esp_system.h"
  12. #include "esp_check.h"
  13. #include "esp_attr.h"
  14. #if CONFIG_IDF_TARGET_ESP32S3
  15. #include "esp32s3/rom/spi_flash.h"
  16. #include "esp32s3/rom/opi_flash.h"
  17. #endif
  18. //-----------------------------------------SPI0 PSRAM TEST-----------------------------------------------//
  19. #if CONFIG_SPIRAM
  20. #if CONFIG_SPIRAM_MODE_OCT
  21. #define SPI0_PSRAM_TEST_LEN (512 * 1024)
  22. #define LENGTH_PER_TIME 1024
  23. #else
  24. #define SPI0_PSRAM_TEST_LEN (128 * 1024)
  25. #define LENGTH_PER_TIME 1024
  26. #endif
  27. static esp_err_t spi0_psram_test(void)
  28. {
  29. printf("----------SPI0 PSRAM Test----------\n");
  30. uint8_t *psram_wr_buf = (uint8_t *)heap_caps_malloc(LENGTH_PER_TIME, MALLOC_CAP_32BIT | MALLOC_CAP_SPIRAM);
  31. if (!psram_wr_buf) {
  32. printf("no memory\n");
  33. abort();
  34. }
  35. uint32_t *psram_rd_buf = (uint32_t *)heap_caps_malloc(SPI0_PSRAM_TEST_LEN, MALLOC_CAP_32BIT | MALLOC_CAP_SPIRAM);
  36. if (!psram_rd_buf) {
  37. printf("no memory\n");
  38. abort();
  39. }
  40. srand(399);
  41. for (int i = 0; i < SPI0_PSRAM_TEST_LEN / LENGTH_PER_TIME; i++) {
  42. for (int j = 0; j < sizeof(psram_wr_buf); j++) {
  43. psram_wr_buf[j] = rand();
  44. }
  45. memcpy(psram_rd_buf + i * LENGTH_PER_TIME, psram_wr_buf, LENGTH_PER_TIME);
  46. if (memcmp(psram_rd_buf + i * LENGTH_PER_TIME, psram_wr_buf, LENGTH_PER_TIME) != 0) {
  47. printf("Fail\n");
  48. free(psram_rd_buf);
  49. free(psram_wr_buf);
  50. return ESP_FAIL;
  51. }
  52. }
  53. free(psram_rd_buf);
  54. free(psram_wr_buf);
  55. printf(DRAM_STR("----------SPI0 PSRAM Test Success----------\n\n"));
  56. return ESP_OK;
  57. }
  58. #endif
  59. //-----------------------------------------SPI1 FLASH TEST-----------------------------------------------//
  60. #define SPI1_FLASH_TEST_LEN 512
  61. #define SECTOR_LEN 4096
  62. #define SPI1_FLASH_TEST_NUM (SECTOR_LEN / SPI1_FLASH_TEST_LEN)
  63. #define SPI1_FLASH_TEST_ADDR 0x200000
  64. extern void spi_flash_disable_interrupts_caches_and_other_cpu(void);
  65. extern void spi_flash_enable_interrupts_caches_and_other_cpu(void);
  66. static DRAM_ATTR uint8_t rd_buf[SPI1_FLASH_TEST_LEN];
  67. static DRAM_ATTR uint8_t wr_buf[SPI1_FLASH_TEST_LEN];
  68. static NOINLINE_ATTR IRAM_ATTR esp_err_t spi1_flash_test(void)
  69. {
  70. printf(DRAM_STR("----------SPI1 Flash Test----------\n"));
  71. //We need to use SPI1
  72. spi_flash_disable_interrupts_caches_and_other_cpu();
  73. uint32_t sector_num = SPI1_FLASH_TEST_ADDR / SECTOR_LEN;
  74. esp_rom_spiflash_erase_sector(sector_num);
  75. spi_flash_enable_interrupts_caches_and_other_cpu();
  76. for (int i = 0; i < SPI1_FLASH_TEST_NUM; i++) {
  77. for (int j = i + 10; j < SPI1_FLASH_TEST_LEN; j++) {
  78. wr_buf[j] = j;
  79. }
  80. spi_flash_disable_interrupts_caches_and_other_cpu();
  81. uint32_t test_flash_addr = SPI1_FLASH_TEST_ADDR + i * SPI1_FLASH_TEST_LEN;
  82. esp_rom_spiflash_write(test_flash_addr, (uint32_t*)wr_buf, SPI1_FLASH_TEST_LEN);
  83. esp_rom_spiflash_read(test_flash_addr, (uint32_t*)rd_buf, SPI1_FLASH_TEST_LEN);
  84. spi_flash_enable_interrupts_caches_and_other_cpu();
  85. if (memcmp(wr_buf, rd_buf, SPI1_FLASH_TEST_LEN) != 0) {
  86. printf(DRAM_STR("error happened between 0x%x and 0x%x!!!!\n"), test_flash_addr, test_flash_addr + SPI1_FLASH_TEST_LEN);
  87. for (int i = 0; i < SPI1_FLASH_TEST_LEN; i++) {
  88. if (wr_buf[i] != rd_buf[i]) {
  89. printf(DRAM_STR("err: wr[%d]: 0x%02x -- rd[%d]: 0x%02x\n"), i, wr_buf[i], i, rd_buf[i]);
  90. }
  91. }
  92. return ESP_FAIL;
  93. }
  94. memset(rd_buf, 0x0, SPI1_FLASH_TEST_LEN);
  95. }
  96. printf(DRAM_STR("----------SPI1 Flash Test Success----------\n\n"));
  97. return ESP_OK;
  98. }
  99. //-----------------------------------------SPI0 FLASH TEST-----------------------------------------------//
  100. #define SPI0_FLASH_TEST_LEN 32
  101. #define SPI0_FLASH_TEST_BUF {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, \
  102. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F}
  103. static const uint8_t flash_rd_buf[SPI0_FLASH_TEST_LEN] = SPI0_FLASH_TEST_BUF;
  104. extern int _flash_rodata_start;
  105. extern int _rodata_reserved_end;
  106. static IRAM_ATTR esp_err_t spi0_flash_test(void)
  107. {
  108. printf("----------SPI0 Flash Test----------\n");
  109. //Check if the flash_rd_buf is in .rodata
  110. assert(((intptr_t)flash_rd_buf >= (intptr_t)&_flash_rodata_start) && ((intptr_t)flash_rd_buf < (intptr_t)&_rodata_reserved_end));
  111. uint8_t cmp_buf[SPI0_FLASH_TEST_LEN] = SPI0_FLASH_TEST_BUF;
  112. for (int i = 0; i < SPI0_FLASH_TEST_LEN; i++) {
  113. if (flash_rd_buf[i] != cmp_buf[i]) {
  114. return ESP_FAIL;
  115. }
  116. }
  117. printf(DRAM_STR("----------SPI0 Flash Test Success----------\n\n"));
  118. return ESP_OK;
  119. }
  120. void app_main(void)
  121. {
  122. ESP_ERROR_CHECK(spi0_flash_test());
  123. #if CONFIG_SPIRAM
  124. ESP_ERROR_CHECK(spi0_psram_test());
  125. #endif
  126. ESP_ERROR_CHECK(spi1_flash_test());
  127. printf("flash psram test success\n");
  128. }