test_spi_flash.c 12 KB

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