test_spi_flash.c 14 KB

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