test_spi_flash.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include <stdio.h>
  2. #include <freertos/FreeRTOS.h>
  3. #include <freertos/task.h>
  4. #include <freertos/semphr.h>
  5. #include <unity.h>
  6. #include <esp_spi_flash.h>
  7. #include <esp_attr.h>
  8. #include "driver/timer.h"
  9. #include "esp_intr_alloc.h"
  10. #include "test_utils.h"
  11. struct flash_test_ctx {
  12. uint32_t offset;
  13. bool fail;
  14. SemaphoreHandle_t done;
  15. };
  16. static void flash_test_task(void *arg)
  17. {
  18. struct flash_test_ctx *ctx = (struct flash_test_ctx *) arg;
  19. vTaskDelay(100 / portTICK_PERIOD_MS);
  20. const uint32_t sector = ctx->offset;
  21. printf("t%d\n", sector);
  22. printf("es%d\n", sector);
  23. if (spi_flash_erase_sector(sector) != ESP_OK) {
  24. ctx->fail = true;
  25. printf("Erase failed\r\n");
  26. xSemaphoreGive(ctx->done);
  27. vTaskDelete(NULL);
  28. }
  29. printf("ed%d\n", sector);
  30. vTaskDelay(0 / portTICK_PERIOD_MS);
  31. uint32_t val = 0xabcd1234;
  32. for (uint32_t offset = 0; offset < SPI_FLASH_SEC_SIZE; offset += 4) {
  33. if (spi_flash_write(sector * SPI_FLASH_SEC_SIZE + offset, (const uint8_t *) &val, 4) != ESP_OK) {
  34. printf("Write failed at offset=%d\r\n", offset);
  35. ctx->fail = true;
  36. break;
  37. }
  38. }
  39. printf("wd%d\n", sector);
  40. vTaskDelay(0 / portTICK_PERIOD_MS);
  41. uint32_t val_read;
  42. for (uint32_t offset = 0; offset < SPI_FLASH_SEC_SIZE; offset += 4) {
  43. if (spi_flash_read(sector * SPI_FLASH_SEC_SIZE + offset, (uint8_t *) &val_read, 4) != ESP_OK) {
  44. printf("Read failed at offset=%d\r\n", offset);
  45. ctx->fail = true;
  46. break;
  47. }
  48. if (val_read != val) {
  49. printf("Read invalid value=%08x at offset=%d\r\n", val_read, offset);
  50. ctx->fail = true;
  51. break;
  52. }
  53. }
  54. printf("td%d\n", sector);
  55. xSemaphoreGive(ctx->done);
  56. vTaskDelete(NULL);
  57. }
  58. TEST_CASE("flash write and erase work both on PRO CPU and on APP CPU", "[spi_flash][ignore]")
  59. {
  60. SemaphoreHandle_t done = xSemaphoreCreateCounting(4, 0);
  61. struct flash_test_ctx ctx[] = {
  62. { .offset = 0x100 + 6, .done = done },
  63. { .offset = 0x100 + 7, .done = done },
  64. { .offset = 0x100 + 8, .done = done },
  65. #ifndef CONFIG_FREERTOS_UNICORE
  66. { .offset = 0x100 + 9, .done = done }
  67. #endif
  68. };
  69. xTaskCreatePinnedToCore(flash_test_task, "t0", 2048, &ctx[0], 3, NULL, 0);
  70. xTaskCreatePinnedToCore(flash_test_task, "t1", 2048, &ctx[1], 3, NULL, tskNO_AFFINITY);
  71. xTaskCreatePinnedToCore(flash_test_task, "t2", 2048, &ctx[2], 3, NULL, tskNO_AFFINITY);
  72. #ifndef CONFIG_FREERTOS_UNICORE
  73. xTaskCreatePinnedToCore(flash_test_task, "t3", 2048, &ctx[3], 3, NULL, 1);
  74. #endif
  75. const size_t task_count = sizeof(ctx)/sizeof(ctx[0]);
  76. for (int i = 0; i < task_count; ++i) {
  77. xSemaphoreTake(done, portMAX_DELAY);
  78. TEST_ASSERT_FALSE(ctx[i].fail);
  79. }
  80. vSemaphoreDelete(done);
  81. }
  82. typedef struct {
  83. size_t buf_size;
  84. uint8_t* buf;
  85. size_t flash_addr;
  86. size_t repeat_count;
  87. SemaphoreHandle_t done;
  88. } read_task_arg_t;
  89. typedef struct {
  90. size_t delay_time_us;
  91. size_t repeat_count;
  92. } block_task_arg_t;
  93. static void IRAM_ATTR timer_isr(void* varg) {
  94. block_task_arg_t* arg = (block_task_arg_t*) varg;
  95. TIMERG0.int_clr_timers.t0 = 1;
  96. TIMERG0.hw_timer[0].config.alarm_en = 1;
  97. ets_delay_us(arg->delay_time_us);
  98. arg->repeat_count++;
  99. }
  100. static void read_task(void* varg) {
  101. read_task_arg_t* arg = (read_task_arg_t*) varg;
  102. for (size_t i = 0; i < arg->repeat_count; ++i) {
  103. ESP_ERROR_CHECK( spi_flash_read(arg->flash_addr, arg->buf, arg->buf_size) );
  104. }
  105. xSemaphoreGive(arg->done);
  106. vTaskDelay(1);
  107. vTaskDelete(NULL);
  108. }
  109. TEST_CASE("spi flash functions can run along with IRAM interrupts", "[spi_flash]")
  110. {
  111. const size_t size = 128;
  112. read_task_arg_t read_arg = {
  113. .buf_size = size,
  114. .buf = (uint8_t*) malloc(size),
  115. .flash_addr = 0,
  116. .repeat_count = 1000,
  117. .done = xSemaphoreCreateBinary()
  118. };
  119. timer_config_t config = {
  120. .alarm_en = true,
  121. .counter_en = false,
  122. .intr_type = TIMER_INTR_LEVEL,
  123. .counter_dir = TIMER_COUNT_UP,
  124. .auto_reload = true,
  125. .divider = 80
  126. };
  127. block_task_arg_t block_arg = {
  128. .repeat_count = 0,
  129. .delay_time_us = 100
  130. };
  131. ESP_ERROR_CHECK( timer_init(TIMER_GROUP_0, TIMER_0, &config) );
  132. timer_pause(TIMER_GROUP_0, TIMER_0);
  133. ESP_ERROR_CHECK( timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, 120) );
  134. intr_handle_t handle;
  135. ESP_ERROR_CHECK( timer_isr_register(TIMER_GROUP_0, TIMER_0, &timer_isr, &block_arg, ESP_INTR_FLAG_IRAM, &handle) );
  136. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0);
  137. timer_enable_intr(TIMER_GROUP_0, TIMER_0);
  138. timer_start(TIMER_GROUP_0, TIMER_0);
  139. xTaskCreatePinnedToCore(read_task, "r", 2048, &read_arg, 3, NULL, portNUM_PROCESSORS - 1);
  140. xSemaphoreTake(read_arg.done, portMAX_DELAY);
  141. timer_pause(TIMER_GROUP_0, TIMER_0);
  142. timer_disable_intr(TIMER_GROUP_0, TIMER_0);
  143. esp_intr_free(handle);
  144. vSemaphoreDelete(read_arg.done);
  145. free(read_arg.buf);
  146. }
  147. #if portNUM_PROCESSORS > 1
  148. TEST_CASE("spi_flash deadlock with high priority busy-waiting task", "[spi_flash]")
  149. {
  150. typedef struct {
  151. QueueHandle_t queue;
  152. volatile bool done;
  153. } deadlock_test_arg_t;
  154. /* Create two tasks: high-priority consumer on CPU0, low-priority producer on CPU1.
  155. * Consumer polls the queue until it gets some data, then yields.
  156. * Run flash operation on CPU0. Check that when IPC1 task blocks out the producer,
  157. * the task which does flash operation does not get blocked by the consumer.
  158. */
  159. void producer_task(void* varg)
  160. {
  161. int dummy = 0;
  162. deadlock_test_arg_t* arg = (deadlock_test_arg_t*) varg;
  163. while (!arg->done) {
  164. xQueueSend(arg->queue, &dummy, 0);
  165. vTaskDelay(1);
  166. }
  167. vTaskDelete(NULL);
  168. }
  169. void consumer_task(void* varg)
  170. {
  171. int dummy;
  172. deadlock_test_arg_t* arg = (deadlock_test_arg_t*) varg;
  173. while (!arg->done) {
  174. if (xQueueReceive(arg->queue, &dummy, 0) == pdTRUE) {
  175. vTaskDelay(1);
  176. }
  177. }
  178. vTaskDelete(NULL);
  179. }
  180. deadlock_test_arg_t arg = {
  181. .queue = xQueueCreate(32, sizeof(int)),
  182. .done = false
  183. };
  184. TEST_ASSERT(xTaskCreatePinnedToCore(&producer_task, "producer", 4096, &arg, 5, NULL, 1));
  185. TEST_ASSERT(xTaskCreatePinnedToCore(&consumer_task, "consumer", 4096, &arg, 10, NULL, 0));
  186. for (int i = 0; i < 1000; i++) {
  187. uint32_t dummy;
  188. TEST_ESP_OK(spi_flash_read(0, &dummy, sizeof(dummy)));
  189. }
  190. arg.done = true;
  191. vTaskDelay(5);
  192. vQueueDelete(arg.queue);
  193. /* Check that current task priority is still correct */
  194. TEST_ASSERT_EQUAL_INT(uxTaskPriorityGet(NULL), UNITY_FREERTOS_PRIORITY);
  195. }
  196. #endif // portNUM_PROCESSORS > 1