test_spi_flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #include <stdio.h>
  2. #include <sys/param.h>
  3. #include <freertos/FreeRTOS.h>
  4. #include <freertos/task.h>
  5. #include <freertos/semphr.h>
  6. #include <unity.h>
  7. #include <spi_flash_mmap.h>
  8. #include <esp_attr.h>
  9. #include "esp_intr_alloc.h"
  10. #include "test_utils.h"
  11. #include "ccomp_timer.h"
  12. #include "esp_log.h"
  13. #include "esp_rom_sys.h"
  14. #include "esp_rom_spiflash.h"
  15. #include "esp_timer.h"
  16. #include "bootloader_flash.h" //for bootloader_flash_xmc_startup
  17. #include "sdkconfig.h"
  18. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
  19. // TODO: SPI_FLASH IDF-4025
  20. struct flash_test_ctx {
  21. uint32_t offset;
  22. bool fail;
  23. SemaphoreHandle_t done;
  24. };
  25. /* Base offset in flash for tests. */
  26. static size_t start;
  27. static void setup_tests(void)
  28. {
  29. if (start == 0) {
  30. const esp_partition_t *part = get_test_data_partition();
  31. start = part->address;
  32. printf("Test data partition @ 0x%x\n", start);
  33. }
  34. }
  35. static void flash_test_task(void *arg)
  36. {
  37. struct flash_test_ctx *ctx = (struct flash_test_ctx *) arg;
  38. vTaskDelay(100 / portTICK_PERIOD_MS);
  39. const uint32_t sector = start / SPI_FLASH_SEC_SIZE + ctx->offset;
  40. printf("t%d\n", sector);
  41. printf("es%d\n", sector);
  42. if (esp_flash_erase_region(NULL, sector * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE) != ESP_OK) {
  43. ctx->fail = true;
  44. printf("Erase failed\r\n");
  45. xSemaphoreGive(ctx->done);
  46. vTaskDelete(NULL);
  47. }
  48. printf("ed%d\n", sector);
  49. vTaskDelay(0 / portTICK_PERIOD_MS);
  50. uint32_t val = 0xabcd1234;
  51. for (uint32_t offset = 0; offset < SPI_FLASH_SEC_SIZE; offset += 4) {
  52. if (esp_flash_write(NULL, (const uint8_t *) &val, sector * SPI_FLASH_SEC_SIZE + offset, 4) != ESP_OK) {
  53. printf("Write failed at offset=%d\r\n", offset);
  54. ctx->fail = true;
  55. break;
  56. }
  57. }
  58. printf("wd%d\n", sector);
  59. vTaskDelay(0 / portTICK_PERIOD_MS);
  60. uint32_t val_read;
  61. for (uint32_t offset = 0; offset < SPI_FLASH_SEC_SIZE; offset += 4) {
  62. if (esp_flash_read(NULL, (uint8_t *) &val_read, sector * SPI_FLASH_SEC_SIZE + offset, 4) != ESP_OK) {
  63. printf("Read failed at offset=%d\r\n", offset);
  64. ctx->fail = true;
  65. break;
  66. }
  67. if (val_read != val) {
  68. printf("Read invalid value=%08x at offset=%d\r\n", val_read, offset);
  69. ctx->fail = true;
  70. break;
  71. }
  72. }
  73. printf("td%d\n", sector);
  74. xSemaphoreGive(ctx->done);
  75. vTaskDelete(NULL);
  76. }
  77. TEST_CASE("flash write and erase work both on PRO CPU and on APP CPU", "[spi_flash][ignore]")
  78. {
  79. setup_tests();
  80. SemaphoreHandle_t done = xSemaphoreCreateCounting(4, 0);
  81. struct flash_test_ctx ctx[] = {
  82. { .offset = 0x10 + 6, .done = done },
  83. { .offset = 0x10 + 7, .done = done },
  84. { .offset = 0x10 + 8, .done = done },
  85. #ifndef CONFIG_FREERTOS_UNICORE
  86. { .offset = 0x10 + 9, .done = done }
  87. #endif
  88. };
  89. xTaskCreatePinnedToCore(flash_test_task, "t0", 2048, &ctx[0], 3, NULL, 0);
  90. xTaskCreatePinnedToCore(flash_test_task, "t1", 2048, &ctx[1], 3, NULL, tskNO_AFFINITY);
  91. xTaskCreatePinnedToCore(flash_test_task, "t2", 2048, &ctx[2], 3, NULL, tskNO_AFFINITY);
  92. #ifndef CONFIG_FREERTOS_UNICORE
  93. xTaskCreatePinnedToCore(flash_test_task, "t3", 2048, &ctx[3], 3, NULL, 1);
  94. #endif
  95. const size_t task_count = sizeof(ctx)/sizeof(ctx[0]);
  96. for (int i = 0; i < task_count; ++i) {
  97. xSemaphoreTake(done, portMAX_DELAY);
  98. TEST_ASSERT_FALSE(ctx[i].fail);
  99. }
  100. vSemaphoreDelete(done);
  101. }
  102. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3)
  103. // TODO ESP32-S3 IDF-2021
  104. static const char TAG[] = "test_spi_flash";
  105. typedef struct {
  106. uint32_t us_start;
  107. size_t len;
  108. const char* name;
  109. } time_meas_ctx_t;
  110. static void time_measure_start(time_meas_ctx_t* ctx)
  111. {
  112. ctx->us_start = esp_timer_get_time();
  113. ccomp_timer_start();
  114. }
  115. static uint32_t time_measure_end(time_meas_ctx_t* ctx)
  116. {
  117. uint32_t c_time_us = ccomp_timer_stop();
  118. uint32_t time_us = esp_timer_get_time() - ctx->us_start;
  119. ESP_LOGI(TAG, "%s: compensated: %.2lf kB/s, typical: %.2lf kB/s", ctx->name, ctx->len / (c_time_us/1000.), ctx->len / (time_us/1000.));
  120. return ctx->len * 1000 / (c_time_us / 1000);
  121. }
  122. #define TEST_TIMES 20
  123. #define TEST_SECTORS 4
  124. static uint32_t measure_erase(const esp_partition_t* part)
  125. {
  126. const int total_len = SPI_FLASH_SEC_SIZE * TEST_SECTORS;
  127. time_meas_ctx_t time_ctx = {.name = "erase", .len = total_len};
  128. time_measure_start(&time_ctx);
  129. esp_err_t err = esp_flash_erase_region(NULL, part->address, total_len);
  130. TEST_ESP_OK(err);
  131. return time_measure_end(&time_ctx);
  132. }
  133. // should called after measure_erase
  134. static uint32_t measure_write(const char* name, const esp_partition_t* part, const uint8_t* data_to_write, int seg_len)
  135. {
  136. const int total_len = SPI_FLASH_SEC_SIZE;
  137. time_meas_ctx_t time_ctx = {.name = name, .len = total_len * TEST_TIMES};
  138. time_measure_start(&time_ctx);
  139. for (int i = 0; i < TEST_TIMES; i ++) {
  140. // Erase one time, but write 100 times the same data
  141. size_t len = total_len;
  142. int offset = 0;
  143. while (len) {
  144. int len_write = MIN(seg_len, len);
  145. esp_err_t err = esp_flash_write(NULL, data_to_write + offset, part->address + offset, len_write);
  146. TEST_ESP_OK(err);
  147. offset += len_write;
  148. len -= len_write;
  149. }
  150. }
  151. return time_measure_end(&time_ctx);
  152. }
  153. static uint32_t measure_read(const char* name, const esp_partition_t* part, uint8_t* data_read, int seg_len)
  154. {
  155. const int total_len = SPI_FLASH_SEC_SIZE;
  156. time_meas_ctx_t time_ctx = {.name = name, .len = total_len * TEST_TIMES};
  157. time_measure_start(&time_ctx);
  158. for (int i = 0; i < TEST_TIMES; i ++) {
  159. size_t len = total_len;
  160. int offset = 0;
  161. while (len) {
  162. int len_read = MIN(seg_len, len);
  163. esp_err_t err = esp_flash_read(NULL, data_read + offset, part->address + offset, len_read);
  164. TEST_ESP_OK(err);
  165. offset += len_read;
  166. len -= len_read;
  167. }
  168. }
  169. return time_measure_end(&time_ctx);
  170. }
  171. #define MEAS_WRITE(n) (measure_write("write in "#n"-byte chunks", part, data_to_write, n))
  172. #define MEAS_READ(n) (measure_read("read in "#n"-byte chunks", part, data_read, n))
  173. TEST_CASE("Test spi_flash read/write performance", "[spi_flash]")
  174. {
  175. const esp_partition_t *part = get_test_data_partition();
  176. const int total_len = SPI_FLASH_SEC_SIZE;
  177. uint8_t *data_to_write = heap_caps_malloc(total_len, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  178. uint8_t *data_read = heap_caps_malloc(total_len, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  179. srand(777);
  180. for (int i = 0; i < total_len; i++) {
  181. data_to_write[i] = rand();
  182. }
  183. uint32_t erase_1 = measure_erase(part);
  184. uint32_t speed_WR_4B = MEAS_WRITE(4);
  185. uint32_t speed_RD_4B = MEAS_READ(4);
  186. uint32_t erase_2 = measure_erase(part);
  187. uint32_t speed_WR_2KB = MEAS_WRITE(2048);
  188. uint32_t speed_RD_2KB = MEAS_READ(2048);
  189. TEST_ASSERT_EQUAL_HEX8_ARRAY(data_to_write, data_read, total_len);
  190. #define LOG_DATA(suffix) IDF_LOG_PERFORMANCE("FLASH_SPEED_BYTE_PER_SEC_LEGACY_"#suffix, "%d", speed_##suffix)
  191. #define LOG_ERASE(var) IDF_LOG_PERFORMANCE("FLASH_SPEED_BYTE_PER_SEC_LEGACY_ERASE", "%d", var)
  192. LOG_DATA(WR_4B);
  193. LOG_DATA(RD_4B);
  194. LOG_DATA(WR_2KB);
  195. LOG_DATA(RD_2KB);
  196. // Erase time may vary a lot, can increase threshold if this fails with a reasonable speed
  197. LOG_ERASE(erase_1);
  198. LOG_ERASE(erase_2);
  199. free(data_to_write);
  200. free(data_read);
  201. }
  202. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3)
  203. // TODO: This test is disabled on S3 with legacy impl - IDF-3505
  204. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32, ESP32S2, ESP32S3, ESP32C3)
  205. #if portNUM_PROCESSORS > 1
  206. TEST_CASE("spi_flash deadlock with high priority busy-waiting task", "[spi_flash][esp_flash]")
  207. {
  208. typedef struct {
  209. QueueHandle_t queue;
  210. volatile bool done;
  211. } deadlock_test_arg_t;
  212. /* Create two tasks: high-priority consumer on CPU0, low-priority producer on CPU1.
  213. * Consumer polls the queue until it gets some data, then yields.
  214. * Run flash operation on CPU0. Check that when IPC1 task blocks out the producer,
  215. * the task which does flash operation does not get blocked by the consumer.
  216. */
  217. void producer_task(void* varg)
  218. {
  219. int dummy = 0;
  220. deadlock_test_arg_t* arg = (deadlock_test_arg_t*) varg;
  221. while (!arg->done) {
  222. xQueueSend(arg->queue, &dummy, 0);
  223. vTaskDelay(1);
  224. }
  225. vTaskDelete(NULL);
  226. }
  227. void consumer_task(void* varg)
  228. {
  229. int dummy;
  230. deadlock_test_arg_t* arg = (deadlock_test_arg_t*) varg;
  231. while (!arg->done) {
  232. if (xQueueReceive(arg->queue, &dummy, 0) == pdTRUE) {
  233. vTaskDelay(1);
  234. }
  235. }
  236. vTaskDelete(NULL);
  237. }
  238. deadlock_test_arg_t arg = {
  239. .queue = xQueueCreate(32, sizeof(int)),
  240. .done = false
  241. };
  242. TEST_ASSERT(xTaskCreatePinnedToCore(&producer_task, "producer", 4096, &arg, 5, NULL, 1));
  243. TEST_ASSERT(xTaskCreatePinnedToCore(&consumer_task, "consumer", 4096, &arg, 10, NULL, 0));
  244. for (int i = 0; i < 1000; i++) {
  245. uint32_t dummy;
  246. TEST_ESP_OK(esp_flash_read(NULL, &dummy, 0, sizeof(dummy)));
  247. }
  248. arg.done = true;
  249. vTaskDelay(5);
  250. vQueueDelete(arg.queue);
  251. /* Check that current task priority is still correct */
  252. TEST_ASSERT_EQUAL_INT(uxTaskPriorityGet(NULL), UNITY_FREERTOS_PRIORITY);
  253. }
  254. #endif // portNUM_PROCESSORS > 1
  255. #endif // !TEMPORARY_DISABLED_FOR_TARGETS(ESP32, ESP32S2, ESP32S3, ESP32C3)
  256. TEST_CASE("WEL is cleared after boot", "[spi_flash]")
  257. {
  258. esp_rom_spiflash_chip_t *legacy_chip = &g_rom_flashchip;
  259. uint32_t status;
  260. esp_rom_spiflash_read_status(legacy_chip, &status);
  261. TEST_ASSERT((status & 0x2) == 0);
  262. }
  263. #if CONFIG_ESPTOOLPY_FLASHMODE_QIO
  264. // ISSI chip has its QE bit on other chips' BP4, which may get cleared by accident
  265. TEST_CASE("rom unlock will not erase QE bit", "[spi_flash]")
  266. {
  267. esp_rom_spiflash_chip_t *legacy_chip = &g_rom_flashchip;
  268. uint32_t status;
  269. printf("dev_id: %08X \n", legacy_chip->device_id);
  270. if (((legacy_chip->device_id >> 16) & 0xff) != 0x9D) {
  271. TEST_IGNORE_MESSAGE("This test is only for ISSI chips. Ignore.");
  272. }
  273. bootloader_flash_unlock();
  274. esp_rom_spiflash_read_status(legacy_chip, &status);
  275. printf("status: %08x\n", status);
  276. TEST_ASSERT(status & 0x40);
  277. }
  278. #endif
  279. static IRAM_ATTR NOINLINE_ATTR void test_xmc_startup(void)
  280. {
  281. extern void spi_flash_disable_interrupts_caches_and_other_cpu(void);
  282. extern void spi_flash_enable_interrupts_caches_and_other_cpu(void);
  283. esp_err_t ret = ESP_OK;
  284. spi_flash_disable_interrupts_caches_and_other_cpu();
  285. ret = bootloader_flash_xmc_startup();
  286. spi_flash_enable_interrupts_caches_and_other_cpu();
  287. TEST_ASSERT_EQUAL(ESP_OK, ret);
  288. }
  289. TEST_CASE("bootloader_flash_xmc_startup can be called when cache disabled", "[spi_flash]")
  290. {
  291. test_xmc_startup();
  292. }
  293. #endif //#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)