test_spi_flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 <esp_spi_flash.h>
  8. #include <esp_attr.h>
  9. #include "driver/timer.h"
  10. #include "esp_intr_alloc.h"
  11. #include "test_utils.h"
  12. #include "ccomp_timer.h"
  13. #include "esp_log.h"
  14. #include "esp_rom_sys.h"
  15. #include "esp_timer.h"
  16. #include "bootloader_flash.h" //for bootloader_flash_xmc_startup
  17. #include "sdkconfig.h"
  18. #if CONFIG_IDF_TARGET_ESP32
  19. #include "esp32/rom/spi_flash.h"
  20. #elif CONFIG_IDF_TARGET_ESP32S2
  21. #include "esp32s2/rom/spi_flash.h"
  22. #elif CONFIG_IDF_TARGET_ESP32S3
  23. #include "esp32s3/rom/spi_flash.h"
  24. #elif CONFIG_IDF_TARGET_ESP32C3
  25. #include "esp32c3/rom/spi_flash.h"
  26. #endif
  27. struct flash_test_ctx {
  28. uint32_t offset;
  29. bool fail;
  30. SemaphoreHandle_t done;
  31. };
  32. static const char TAG[] = "test_spi_flash";
  33. /* Base offset in flash for tests. */
  34. static size_t start;
  35. static void setup_tests(void)
  36. {
  37. if (start == 0) {
  38. const esp_partition_t *part = get_test_data_partition();
  39. start = part->address;
  40. printf("Test data partition @ 0x%x\n", start);
  41. }
  42. }
  43. static void flash_test_task(void *arg)
  44. {
  45. struct flash_test_ctx *ctx = (struct flash_test_ctx *) arg;
  46. vTaskDelay(100 / portTICK_PERIOD_MS);
  47. const uint32_t sector = start / SPI_FLASH_SEC_SIZE + ctx->offset;
  48. printf("t%d\n", sector);
  49. printf("es%d\n", sector);
  50. if (spi_flash_erase_sector(sector) != ESP_OK) {
  51. ctx->fail = true;
  52. printf("Erase failed\r\n");
  53. xSemaphoreGive(ctx->done);
  54. vTaskDelete(NULL);
  55. }
  56. printf("ed%d\n", sector);
  57. vTaskDelay(0 / portTICK_PERIOD_MS);
  58. uint32_t val = 0xabcd1234;
  59. for (uint32_t offset = 0; offset < SPI_FLASH_SEC_SIZE; offset += 4) {
  60. if (spi_flash_write(sector * SPI_FLASH_SEC_SIZE + offset, (const uint8_t *) &val, 4) != ESP_OK) {
  61. printf("Write failed at offset=%d\r\n", offset);
  62. ctx->fail = true;
  63. break;
  64. }
  65. }
  66. printf("wd%d\n", sector);
  67. vTaskDelay(0 / portTICK_PERIOD_MS);
  68. uint32_t val_read;
  69. for (uint32_t offset = 0; offset < SPI_FLASH_SEC_SIZE; offset += 4) {
  70. if (spi_flash_read(sector * SPI_FLASH_SEC_SIZE + offset, (uint8_t *) &val_read, 4) != ESP_OK) {
  71. printf("Read failed at offset=%d\r\n", offset);
  72. ctx->fail = true;
  73. break;
  74. }
  75. if (val_read != val) {
  76. printf("Read invalid value=%08x at offset=%d\r\n", val_read, offset);
  77. ctx->fail = true;
  78. break;
  79. }
  80. }
  81. printf("td%d\n", sector);
  82. xSemaphoreGive(ctx->done);
  83. vTaskDelete(NULL);
  84. }
  85. TEST_CASE("flash write and erase work both on PRO CPU and on APP CPU", "[spi_flash][ignore]")
  86. {
  87. setup_tests();
  88. SemaphoreHandle_t done = xSemaphoreCreateCounting(4, 0);
  89. struct flash_test_ctx ctx[] = {
  90. { .offset = 0x10 + 6, .done = done },
  91. { .offset = 0x10 + 7, .done = done },
  92. { .offset = 0x10 + 8, .done = done },
  93. #ifndef CONFIG_FREERTOS_UNICORE
  94. { .offset = 0x10 + 9, .done = done }
  95. #endif
  96. };
  97. xTaskCreatePinnedToCore(flash_test_task, "t0", 2048, &ctx[0], 3, NULL, 0);
  98. xTaskCreatePinnedToCore(flash_test_task, "t1", 2048, &ctx[1], 3, NULL, tskNO_AFFINITY);
  99. xTaskCreatePinnedToCore(flash_test_task, "t2", 2048, &ctx[2], 3, NULL, tskNO_AFFINITY);
  100. #ifndef CONFIG_FREERTOS_UNICORE
  101. xTaskCreatePinnedToCore(flash_test_task, "t3", 2048, &ctx[3], 3, NULL, 1);
  102. #endif
  103. const size_t task_count = sizeof(ctx)/sizeof(ctx[0]);
  104. for (int i = 0; i < task_count; ++i) {
  105. xSemaphoreTake(done, portMAX_DELAY);
  106. TEST_ASSERT_FALSE(ctx[i].fail);
  107. }
  108. vSemaphoreDelete(done);
  109. }
  110. typedef struct {
  111. size_t buf_size;
  112. uint8_t* buf;
  113. size_t flash_addr;
  114. size_t repeat_count;
  115. SemaphoreHandle_t done;
  116. } read_task_arg_t;
  117. typedef struct {
  118. size_t delay_time_us;
  119. size_t repeat_count;
  120. } block_task_arg_t;
  121. #ifdef CONFIG_IDF_TARGET_ESP32S2
  122. #define int_clr_timers int_clr
  123. #endif
  124. static void IRAM_ATTR timer_isr(void* varg) {
  125. block_task_arg_t* arg = (block_task_arg_t*) varg;
  126. timer_group_clr_intr_status_in_isr(TIMER_GROUP_0, TIMER_0);
  127. timer_group_enable_alarm_in_isr(TIMER_GROUP_0, TIMER_0);
  128. esp_rom_delay_us(arg->delay_time_us);
  129. arg->repeat_count++;
  130. }
  131. static void read_task(void* varg) {
  132. read_task_arg_t* arg = (read_task_arg_t*) varg;
  133. for (size_t i = 0; i < arg->repeat_count; ++i) {
  134. ESP_ERROR_CHECK( spi_flash_read(arg->flash_addr, arg->buf, arg->buf_size) );
  135. }
  136. xSemaphoreGive(arg->done);
  137. vTaskDelay(1);
  138. vTaskDelete(NULL);
  139. }
  140. TEST_CASE("spi flash functions can run along with IRAM interrupts", "[spi_flash][esp_flash]")
  141. {
  142. const size_t size = 128;
  143. read_task_arg_t read_arg = {
  144. .buf_size = size,
  145. .buf = (uint8_t*) malloc(size),
  146. .flash_addr = 0,
  147. .repeat_count = 1000,
  148. .done = xSemaphoreCreateBinary()
  149. };
  150. timer_config_t config = {
  151. .alarm_en = true,
  152. .counter_en = false,
  153. .intr_type = TIMER_INTR_LEVEL,
  154. .counter_dir = TIMER_COUNT_UP,
  155. .auto_reload = true,
  156. .divider = 80
  157. };
  158. block_task_arg_t block_arg = {
  159. .repeat_count = 0,
  160. .delay_time_us = 100
  161. };
  162. ESP_ERROR_CHECK( timer_init(TIMER_GROUP_0, TIMER_0, &config) );
  163. timer_pause(TIMER_GROUP_0, TIMER_0);
  164. ESP_ERROR_CHECK( timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, 120) );
  165. intr_handle_t handle;
  166. ESP_ERROR_CHECK( timer_isr_register(TIMER_GROUP_0, TIMER_0, &timer_isr, &block_arg, ESP_INTR_FLAG_IRAM, &handle) );
  167. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0);
  168. timer_enable_intr(TIMER_GROUP_0, TIMER_0);
  169. timer_start(TIMER_GROUP_0, TIMER_0);
  170. xTaskCreatePinnedToCore(read_task, "r", 2048, &read_arg, 3, NULL, portNUM_PROCESSORS - 1);
  171. xSemaphoreTake(read_arg.done, portMAX_DELAY);
  172. timer_pause(TIMER_GROUP_0, TIMER_0);
  173. timer_disable_intr(TIMER_GROUP_0, TIMER_0);
  174. esp_intr_free(handle);
  175. vSemaphoreDelete(read_arg.done);
  176. free(read_arg.buf);
  177. }
  178. typedef struct {
  179. uint32_t us_start;
  180. size_t len;
  181. const char* name;
  182. } time_meas_ctx_t;
  183. static void time_measure_start(time_meas_ctx_t* ctx)
  184. {
  185. ctx->us_start = esp_timer_get_time();
  186. ccomp_timer_start();
  187. }
  188. static uint32_t time_measure_end(time_meas_ctx_t* ctx)
  189. {
  190. uint32_t c_time_us = ccomp_timer_stop();
  191. uint32_t time_us = esp_timer_get_time() - ctx->us_start;
  192. 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.));
  193. return ctx->len * 1000 / (c_time_us / 1000);
  194. }
  195. #define TEST_TIMES 20
  196. #define TEST_SECTORS 4
  197. static uint32_t measure_erase(const esp_partition_t* part)
  198. {
  199. const int total_len = SPI_FLASH_SEC_SIZE * TEST_SECTORS;
  200. time_meas_ctx_t time_ctx = {.name = "erase", .len = total_len};
  201. time_measure_start(&time_ctx);
  202. esp_err_t err = spi_flash_erase_range(part->address, total_len);
  203. TEST_ESP_OK(err);
  204. return time_measure_end(&time_ctx);
  205. }
  206. // should called after measure_erase
  207. static uint32_t measure_write(const char* name, const esp_partition_t* part, const uint8_t* data_to_write, int seg_len)
  208. {
  209. const int total_len = SPI_FLASH_SEC_SIZE;
  210. time_meas_ctx_t time_ctx = {.name = name, .len = total_len * TEST_TIMES};
  211. time_measure_start(&time_ctx);
  212. for (int i = 0; i < TEST_TIMES; i ++) {
  213. // Erase one time, but write 100 times the same data
  214. size_t len = total_len;
  215. int offset = 0;
  216. while (len) {
  217. int len_write = MIN(seg_len, len);
  218. esp_err_t err = spi_flash_write(part->address + offset, data_to_write + offset, len_write);
  219. TEST_ESP_OK(err);
  220. offset += len_write;
  221. len -= len_write;
  222. }
  223. }
  224. return time_measure_end(&time_ctx);
  225. }
  226. static uint32_t measure_read(const char* name, const esp_partition_t* part, uint8_t* data_read, int seg_len)
  227. {
  228. const int total_len = SPI_FLASH_SEC_SIZE;
  229. time_meas_ctx_t time_ctx = {.name = name, .len = total_len * TEST_TIMES};
  230. time_measure_start(&time_ctx);
  231. for (int i = 0; i < TEST_TIMES; i ++) {
  232. size_t len = total_len;
  233. int offset = 0;
  234. while (len) {
  235. int len_read = MIN(seg_len, len);
  236. esp_err_t err = spi_flash_read(part->address + offset, data_read + offset, len_read);
  237. TEST_ESP_OK(err);
  238. offset += len_read;
  239. len -= len_read;
  240. }
  241. }
  242. return time_measure_end(&time_ctx);
  243. }
  244. #define MEAS_WRITE(n) (measure_write("write in "#n"-byte chunks", part, data_to_write, n))
  245. #define MEAS_READ(n) (measure_read("read in "#n"-byte chunks", part, data_read, n))
  246. TEST_CASE("Test spi_flash read/write performance", "[spi_flash]")
  247. {
  248. const esp_partition_t *part = get_test_data_partition();
  249. const int total_len = SPI_FLASH_SEC_SIZE;
  250. uint8_t *data_to_write = heap_caps_malloc(total_len, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  251. uint8_t *data_read = heap_caps_malloc(total_len, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  252. srand(777);
  253. for (int i = 0; i < total_len; i++) {
  254. data_to_write[i] = rand();
  255. }
  256. uint32_t erase_1 = measure_erase(part);
  257. uint32_t speed_WR_4B = MEAS_WRITE(4);
  258. uint32_t speed_RD_4B = MEAS_READ(4);
  259. uint32_t erase_2 = measure_erase(part);
  260. uint32_t speed_WR_2KB = MEAS_WRITE(2048);
  261. uint32_t speed_RD_2KB = MEAS_READ(2048);
  262. TEST_ASSERT_EQUAL_HEX8_ARRAY(data_to_write, data_read, total_len);
  263. // Data checks are disabled when PSRAM is used or in Freertos compliance check test
  264. #if !CONFIG_SPIRAM && !CONFIG_FREERTOS_CHECK_PORT_CRITICAL_COMPLIANCE
  265. # define CHECK_DATA(suffix) TEST_PERFORMANCE_CCOMP_GREATER_THAN(FLASH_SPEED_BYTE_PER_SEC_LEGACY_##suffix, "%d", speed_##suffix)
  266. # define CHECK_ERASE(var) TEST_PERFORMANCE_CCOMP_GREATER_THAN(FLASH_SPEED_BYTE_PER_SEC_LEGACY_ERASE, "%d", var)
  267. #else
  268. # define CHECK_DATA(suffix) ((void)speed_##suffix)
  269. # define CHECK_ERASE(var) ((void)var)
  270. #endif
  271. CHECK_DATA(WR_4B);
  272. CHECK_DATA(RD_4B);
  273. CHECK_DATA(WR_2KB);
  274. CHECK_DATA(RD_2KB);
  275. // Erase time may vary a lot, can increase threshold if this fails with a reasonable speed
  276. CHECK_ERASE(erase_1);
  277. CHECK_ERASE(erase_2);
  278. free(data_to_write);
  279. free(data_read);
  280. }
  281. #if portNUM_PROCESSORS > 1
  282. TEST_CASE("spi_flash deadlock with high priority busy-waiting task", "[spi_flash][esp_flash]")
  283. {
  284. typedef struct {
  285. QueueHandle_t queue;
  286. volatile bool done;
  287. } deadlock_test_arg_t;
  288. /* Create two tasks: high-priority consumer on CPU0, low-priority producer on CPU1.
  289. * Consumer polls the queue until it gets some data, then yields.
  290. * Run flash operation on CPU0. Check that when IPC1 task blocks out the producer,
  291. * the task which does flash operation does not get blocked by the consumer.
  292. */
  293. void producer_task(void* varg)
  294. {
  295. int dummy = 0;
  296. deadlock_test_arg_t* arg = (deadlock_test_arg_t*) varg;
  297. while (!arg->done) {
  298. xQueueSend(arg->queue, &dummy, 0);
  299. vTaskDelay(1);
  300. }
  301. vTaskDelete(NULL);
  302. }
  303. void consumer_task(void* varg)
  304. {
  305. int dummy;
  306. deadlock_test_arg_t* arg = (deadlock_test_arg_t*) varg;
  307. while (!arg->done) {
  308. if (xQueueReceive(arg->queue, &dummy, 0) == pdTRUE) {
  309. vTaskDelay(1);
  310. }
  311. }
  312. vTaskDelete(NULL);
  313. }
  314. deadlock_test_arg_t arg = {
  315. .queue = xQueueCreate(32, sizeof(int)),
  316. .done = false
  317. };
  318. TEST_ASSERT(xTaskCreatePinnedToCore(&producer_task, "producer", 4096, &arg, 5, NULL, 1));
  319. TEST_ASSERT(xTaskCreatePinnedToCore(&consumer_task, "consumer", 4096, &arg, 10, NULL, 0));
  320. for (int i = 0; i < 1000; i++) {
  321. uint32_t dummy;
  322. TEST_ESP_OK(spi_flash_read(0, &dummy, sizeof(dummy)));
  323. }
  324. arg.done = true;
  325. vTaskDelay(5);
  326. vQueueDelete(arg.queue);
  327. /* Check that current task priority is still correct */
  328. TEST_ASSERT_EQUAL_INT(uxTaskPriorityGet(NULL), UNITY_FREERTOS_PRIORITY);
  329. }
  330. #endif // portNUM_PROCESSORS > 1
  331. TEST_CASE("WEL is cleared after boot", "[spi_flash]")
  332. {
  333. esp_rom_spiflash_chip_t *legacy_chip = &g_rom_flashchip;
  334. uint32_t status;
  335. esp_rom_spiflash_read_status(legacy_chip, &status);
  336. TEST_ASSERT((status & 0x2) == 0);
  337. }
  338. #if CONFIG_ESPTOOLPY_FLASHMODE_QIO
  339. // ISSI chip has its QE bit on other chips' BP4, which may get cleared by accident
  340. TEST_CASE("rom unlock will not erase QE bit", "[spi_flash]")
  341. {
  342. esp_rom_spiflash_chip_t *legacy_chip = &g_rom_flashchip;
  343. uint32_t status;
  344. printf("dev_id: %08X \n", legacy_chip->device_id);
  345. if (((legacy_chip->device_id >> 16) & 0xff) != 0x9D) {
  346. TEST_IGNORE_MESSAGE("This test is only for ISSI chips. Ignore.");
  347. }
  348. esp_rom_spiflash_unlock();
  349. esp_rom_spiflash_read_status(legacy_chip, &status);
  350. printf("status: %08x\n", status);
  351. TEST_ASSERT(status & 0x40);
  352. }
  353. #endif
  354. static IRAM_ATTR NOINLINE_ATTR void test_xmc_startup(void)
  355. {
  356. extern void spi_flash_disable_interrupts_caches_and_other_cpu(void);
  357. extern void spi_flash_enable_interrupts_caches_and_other_cpu(void);
  358. esp_err_t ret = ESP_OK;
  359. spi_flash_disable_interrupts_caches_and_other_cpu();
  360. ret = bootloader_flash_xmc_startup();
  361. spi_flash_enable_interrupts_caches_and_other_cpu();
  362. TEST_ASSERT_EQUAL(ESP_OK, ret);
  363. }
  364. TEST_CASE("bootloader_flash_xmc_startup can be called when cache disabled", "[spi_flash]")
  365. {
  366. test_xmc_startup();
  367. }