test_flash_psram.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 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. #include "esp_flash.h"
  15. #include "esp_partition.h"
  16. #if CONFIG_IDF_TARGET_ESP32S3
  17. #include "esp32s3/rom/spi_flash.h"
  18. #include "esp32s3/rom/opi_flash.h"
  19. #endif
  20. //-----------------------------------------SPI0 PSRAM TEST-----------------------------------------------//
  21. #if CONFIG_SPIRAM
  22. #if CONFIG_SPIRAM_MODE_OCT
  23. #define SPI0_PSRAM_TEST_LEN (512 * 1024)
  24. #define LENGTH_PER_TIME 1024
  25. #else
  26. #define SPI0_PSRAM_TEST_LEN (128 * 1024)
  27. #define LENGTH_PER_TIME 1024
  28. #endif
  29. static esp_err_t spi0_psram_test(void)
  30. {
  31. printf("----------SPI0 PSRAM Test----------\n");
  32. uint8_t *psram_wr_buf = (uint8_t *)heap_caps_malloc(LENGTH_PER_TIME, MALLOC_CAP_32BIT | MALLOC_CAP_SPIRAM);
  33. if (!psram_wr_buf) {
  34. printf("no memory\n");
  35. abort();
  36. }
  37. uint32_t *psram_rd_buf = (uint32_t *)heap_caps_malloc(SPI0_PSRAM_TEST_LEN, MALLOC_CAP_32BIT | MALLOC_CAP_SPIRAM);
  38. if (!psram_rd_buf) {
  39. printf("no memory\n");
  40. abort();
  41. }
  42. srand(399);
  43. for (int i = 0; i < SPI0_PSRAM_TEST_LEN / LENGTH_PER_TIME; i++) {
  44. for (int j = 0; j < sizeof(psram_wr_buf); j++) {
  45. psram_wr_buf[j] = rand();
  46. }
  47. memcpy(psram_rd_buf + i * LENGTH_PER_TIME, psram_wr_buf, LENGTH_PER_TIME);
  48. if (memcmp(psram_rd_buf + i * LENGTH_PER_TIME, psram_wr_buf, LENGTH_PER_TIME) != 0) {
  49. printf("Fail\n");
  50. free(psram_rd_buf);
  51. free(psram_wr_buf);
  52. return ESP_FAIL;
  53. }
  54. }
  55. free(psram_rd_buf);
  56. free(psram_wr_buf);
  57. printf(DRAM_STR("----------SPI0 PSRAM Test Success----------\n\n"));
  58. return ESP_OK;
  59. }
  60. #endif
  61. //-----------------------------------------SPI1 FLASH TEST-----------------------------------------------//
  62. #define SPI1_FLASH_TEST_LEN 512
  63. #define SECTOR_LEN 4096
  64. #define SPI1_FLASH_TEST_NUM (SECTOR_LEN / SPI1_FLASH_TEST_LEN)
  65. #define SPI1_FLASH_TEST_ADDR 0x2a0000
  66. extern void spi_flash_disable_interrupts_caches_and_other_cpu(void);
  67. extern void spi_flash_enable_interrupts_caches_and_other_cpu(void);
  68. static uint8_t rd_buf[SPI1_FLASH_TEST_LEN];
  69. static uint8_t wr_buf[SPI1_FLASH_TEST_LEN];
  70. static const esp_partition_t *get_test_flash_partition(void)
  71. {
  72. /* This finds "flash_test" partition defined in partition_table_unit_test_app.csv */
  73. const esp_partition_t *result = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
  74. ESP_PARTITION_SUBTYPE_ANY, "flash_test");
  75. assert(result != NULL); /* means partition table set wrong */
  76. return result;
  77. }
  78. static NOINLINE_ATTR IRAM_ATTR esp_err_t spi1_flash_test(void)
  79. {
  80. printf(DRAM_STR("----------SPI1 Flash Test----------\n"));
  81. //We need to use SPI1
  82. const esp_partition_t* part = get_test_flash_partition();
  83. esp_flash_erase_region(part->flash_chip, part->address, part->size);
  84. for (int i = 0; i < SPI1_FLASH_TEST_NUM; i++) {
  85. for (int j = i + 10; j < SPI1_FLASH_TEST_LEN; j++) {
  86. wr_buf[j] = j;
  87. }
  88. uint32_t test_flash_addr = SPI1_FLASH_TEST_ADDR;
  89. esp_flash_write(part->flash_chip, wr_buf, part->address, sizeof(wr_buf));
  90. esp_flash_read(part->flash_chip, rd_buf, part->address, sizeof(rd_buf));
  91. if (memcmp(wr_buf, rd_buf, SPI1_FLASH_TEST_LEN) != 0) {
  92. printf(DRAM_STR("error happened between 0x%x and 0x%x!!!!\n"), test_flash_addr, test_flash_addr + SPI1_FLASH_TEST_LEN);
  93. for (int i = 0; i < SPI1_FLASH_TEST_LEN; i++) {
  94. if (wr_buf[i] != rd_buf[i]) {
  95. printf(DRAM_STR("err: wr[%d]: 0x%02x -- rd[%d]: 0x%02x\n"), i, wr_buf[i], i, rd_buf[i]);
  96. }
  97. }
  98. return ESP_FAIL;
  99. }
  100. memset(rd_buf, 0x0, SPI1_FLASH_TEST_LEN);
  101. }
  102. printf(DRAM_STR("----------SPI1 Flash Test Success----------\n\n"));
  103. return ESP_OK;
  104. }
  105. //-----------------------------------------SPI0 FLASH TEST-----------------------------------------------//
  106. #define SPI0_FLASH_TEST_LEN 32
  107. #define SPI0_FLASH_TEST_BUF {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, \
  108. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F}
  109. static const uint8_t flash_rd_buf[SPI0_FLASH_TEST_LEN] = SPI0_FLASH_TEST_BUF;
  110. extern int _flash_rodata_start;
  111. extern int _rodata_reserved_end;
  112. static IRAM_ATTR esp_err_t spi0_flash_test(void)
  113. {
  114. printf("----------SPI0 Flash Test----------\n");
  115. //Check if the flash_rd_buf is in .rodata
  116. assert(((intptr_t)flash_rd_buf >= (intptr_t)&_flash_rodata_start) && ((intptr_t)flash_rd_buf < (intptr_t)&_rodata_reserved_end));
  117. uint8_t cmp_buf[SPI0_FLASH_TEST_LEN] = SPI0_FLASH_TEST_BUF;
  118. for (int i = 0; i < SPI0_FLASH_TEST_LEN; i++) {
  119. if (flash_rd_buf[i] != cmp_buf[i]) {
  120. return ESP_FAIL;
  121. }
  122. }
  123. printf(DRAM_STR("----------SPI0 Flash Test Success----------\n\n"));
  124. return ESP_OK;
  125. }
  126. void app_main(void)
  127. {
  128. ESP_ERROR_CHECK(spi0_flash_test());
  129. #if CONFIG_SPIRAM
  130. ESP_ERROR_CHECK(spi0_psram_test());
  131. #endif
  132. ESP_ERROR_CHECK(spi1_flash_test());
  133. printf("flash psram test success\n");
  134. }