test_task_suspend_resume.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* Tests for FreeRTOS task suspend & resume */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "sdkconfig.h"
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/task.h"
  7. #include "freertos/semphr.h"
  8. #include "freertos/timers.h"
  9. #include "freertos/queue.h"
  10. #include "freertos/xtensa_api.h"
  11. #include "unity.h"
  12. #include "soc/cpu.h"
  13. #include "test_utils.h"
  14. #include "driver/timer.h"
  15. #ifndef CONFIG_FREERTOS_UNICORE
  16. #include "esp_ipc.h"
  17. #endif
  18. #include "esp_freertos_hooks.h"
  19. #ifdef CONFIG_IDF_TARGET_ESP32S2
  20. #define int_clr_timers int_clr
  21. #define update update.update
  22. #define int_st_timers int_st
  23. #endif
  24. /* Counter task counts a target variable forever */
  25. static void task_count(void *vp_counter)
  26. {
  27. volatile unsigned *counter = (volatile unsigned *)vp_counter;
  28. *counter = 0;
  29. while (1) {
  30. (*counter)++;
  31. vTaskDelay(1);
  32. }
  33. }
  34. static void test_suspend_resume(int target_core)
  35. {
  36. volatile unsigned counter = 0;
  37. TaskHandle_t counter_task;
  38. xTaskCreatePinnedToCore(task_count, "Count", 2048,
  39. (void *)&counter, UNITY_FREERTOS_PRIORITY + 1,
  40. &counter_task, target_core);
  41. vTaskDelay(10);
  42. /* check some counting has happened */
  43. TEST_ASSERT_NOT_EQUAL(0, counter);
  44. // Do the next part a few times, just to be sure multiple suspends & resumes
  45. // work as expected...
  46. const int TEST_ITERATIONS = 5;
  47. for (int i = 0; i < TEST_ITERATIONS; i++) {
  48. vTaskSuspend(counter_task);
  49. unsigned suspend_count = counter;
  50. printf("Suspending @ %d\n", suspend_count);
  51. vTaskDelay(2);
  52. printf("Still suspended @ %d\n", counter);
  53. /* check the counter hasn't gone up while the task is suspended */
  54. TEST_ASSERT_EQUAL(suspend_count, counter);
  55. vTaskResume(counter_task);
  56. vTaskDelay(2);
  57. printf("Resumed @ %d\n", counter);
  58. /* check the counter is going up again now the task is resumed */
  59. TEST_ASSERT_NOT_EQUAL(suspend_count, counter);
  60. }
  61. vTaskDelete(counter_task);
  62. }
  63. TEST_CASE("Suspend/resume task on same core", "[freertos]")
  64. {
  65. test_suspend_resume(UNITY_FREERTOS_CPU);
  66. }
  67. #ifndef CONFIG_FREERTOS_UNICORE
  68. TEST_CASE("Suspend/resume task on other core", "[freertos]")
  69. {
  70. test_suspend_resume(!UNITY_FREERTOS_CPU);
  71. }
  72. #endif
  73. /* Task suspends itself, then sets a flag and deletes itself */
  74. static void task_suspend_self(void *vp_resumed)
  75. {
  76. volatile bool *resumed = (volatile bool *)vp_resumed;
  77. *resumed = false;
  78. vTaskSuspend(NULL);
  79. *resumed = true;
  80. vTaskDelete(NULL);
  81. }
  82. TEST_CASE("Suspend the current running task", "[freertos]")
  83. {
  84. volatile bool resumed = false;
  85. TaskHandle_t suspend_task;
  86. xTaskCreatePinnedToCore(task_suspend_self, "suspend_self", 2048,
  87. (void *)&resumed, UNITY_FREERTOS_PRIORITY + 1,
  88. &suspend_task, UNITY_FREERTOS_CPU);
  89. vTaskDelay(1);
  90. TEST_ASSERT_FALSE(resumed);
  91. vTaskResume(suspend_task);
  92. // Shouldn't need any delay here, as task should resume on this CPU immediately
  93. TEST_ASSERT_TRUE(resumed);
  94. }
  95. volatile bool timer_isr_fired;
  96. /* Timer ISR clears interrupt, sets flag, then resumes the task supplied in the
  97. * callback argument.
  98. */
  99. void IRAM_ATTR timer_group0_isr(void *vp_arg)
  100. {
  101. // Clear interrupt
  102. timer_group_clr_intr_status_in_isr(TIMER_GROUP_0, TIMER_0);
  103. timer_isr_fired = true;
  104. TaskHandle_t handle = vp_arg;
  105. BaseType_t higherPriorityTaskWoken = pdFALSE;
  106. higherPriorityTaskWoken = xTaskResumeFromISR(handle);
  107. if (higherPriorityTaskWoken == pdTRUE) {
  108. portYIELD_FROM_ISR();
  109. }
  110. }
  111. /* Create a task which suspends itself, then resume it from a timer
  112. * interrupt. */
  113. static void test_resume_task_from_isr(int target_core)
  114. {
  115. volatile bool resumed = false;
  116. TaskHandle_t suspend_task;
  117. xTaskCreatePinnedToCore(task_suspend_self, "suspend_self", 2048,
  118. (void *)&resumed, UNITY_FREERTOS_PRIORITY + 1,
  119. &suspend_task, target_core);
  120. vTaskDelay(1);
  121. TEST_ASSERT_FALSE(resumed);
  122. /* Configure timer ISR */
  123. const timer_config_t config = {
  124. .alarm_en = 1,
  125. .auto_reload = 0,
  126. .counter_dir = TIMER_COUNT_UP,
  127. .divider = 2, //Range is 2 to 65536
  128. .intr_type = TIMER_INTR_LEVEL,
  129. .counter_en = TIMER_PAUSE,
  130. };
  131. /*Configure timer*/
  132. timer_init(TIMER_GROUP_0, TIMER_0, &config);
  133. timer_pause(TIMER_GROUP_0, TIMER_0);
  134. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0);
  135. timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, 1000);
  136. timer_enable_intr(TIMER_GROUP_0, TIMER_0);
  137. timer_isr_register(TIMER_GROUP_0, TIMER_0, timer_group0_isr, (void*)suspend_task, ESP_INTR_FLAG_IRAM, NULL);
  138. timer_isr_fired = false;
  139. timer_start(TIMER_GROUP_0, TIMER_0);
  140. vTaskDelay(1);
  141. timer_deinit(TIMER_GROUP_0, TIMER_0);
  142. TEST_ASSERT_TRUE(timer_isr_fired);
  143. TEST_ASSERT_TRUE(resumed);
  144. }
  145. TEST_CASE("Resume task from ISR (same core)", "[freertos]")
  146. {
  147. test_resume_task_from_isr(UNITY_FREERTOS_CPU);
  148. }
  149. #ifndef CONFIG_FREERTOS_UNICORE
  150. TEST_CASE("Resume task from ISR (other core)", "[freertos]")
  151. {
  152. test_resume_task_from_isr(!UNITY_FREERTOS_CPU);
  153. }
  154. static volatile bool block;
  155. static bool suspend_both_cpus;
  156. static void IRAM_ATTR suspend_scheduler_while_block_set(void* arg)
  157. {
  158. vTaskSuspendAll();
  159. while (block) { };
  160. ets_delay_us(1);
  161. xTaskResumeAll();
  162. }
  163. static void IRAM_ATTR suspend_scheduler_on_both_cpus(void)
  164. {
  165. block = true;
  166. if (suspend_both_cpus) {
  167. TEST_ESP_OK(esp_ipc_call((xPortGetCoreID() == 0) ? 1 : 0, &suspend_scheduler_while_block_set, NULL));
  168. }
  169. vTaskSuspendAll();
  170. }
  171. static void IRAM_ATTR resume_scheduler_on_both_cpus(void)
  172. {
  173. block = false;
  174. xTaskResumeAll();
  175. }
  176. static const int waiting_ms = 2000;
  177. static const int delta_ms = 100;
  178. static int duration_wait_task_ms;
  179. static int duration_ctrl_task_ms;
  180. static void waiting_task(void *pvParameters)
  181. {
  182. int cpu_id = xPortGetCoreID();
  183. int64_t start_time = esp_timer_get_time();
  184. printf("Start waiting_task cpu=%d\n", cpu_id);
  185. vTaskDelay(waiting_ms / portTICK_PERIOD_MS);
  186. duration_wait_task_ms = (esp_timer_get_time() - start_time) / 1000;
  187. printf("Finish waiting_task cpu=%d, time=%d ms\n", cpu_id, duration_wait_task_ms);
  188. vTaskDelete(NULL);
  189. }
  190. static void control_task(void *pvParameters)
  191. {
  192. int cpu_id = xPortGetCoreID();
  193. ets_delay_us(2000); // let to start the waiting_task first
  194. printf("Start control_task cpu=%d\n", cpu_id);
  195. int64_t start_time = esp_timer_get_time();
  196. suspend_scheduler_on_both_cpus();
  197. ets_delay_us(waiting_ms * 1000 + delta_ms * 1000);
  198. resume_scheduler_on_both_cpus();
  199. duration_ctrl_task_ms = (esp_timer_get_time() - start_time) / 1000;
  200. printf("Finish control_task cpu=%d, time=%d ms\n", cpu_id, duration_ctrl_task_ms);
  201. vTaskDelete(NULL);
  202. }
  203. static void test_scheduler_suspend1(int cpu)
  204. {
  205. /* This test tests a case then both CPUs were in suspend state and then resume CPUs back.
  206. * A task for which a wait time has been set and this time has elapsed in the suspended state should in any case be ready to start.
  207. * (In an old implementation of xTaskIncrementTick function the counting for waiting_task() will be continued
  208. * (excluding time in suspended) after control_task() is finished.)
  209. */
  210. duration_wait_task_ms = 0;
  211. duration_ctrl_task_ms = 0;
  212. printf("Test for CPU%d\n", cpu);
  213. int other_cpu = (cpu == 0) ? 1 : 0;
  214. xTaskCreatePinnedToCore(&waiting_task, "waiting_task", 8192, NULL, 5, NULL, other_cpu);
  215. xTaskCreatePinnedToCore(&control_task, "control_task", 8192, NULL, 5, NULL, cpu);
  216. vTaskDelay(waiting_ms * 2 / portTICK_PERIOD_MS);
  217. TEST_ASSERT_INT_WITHIN(4, waiting_ms + delta_ms + 4, duration_ctrl_task_ms);
  218. if (suspend_both_cpus == false && cpu == 1) {
  219. // CPU0 continues to increase the TickCount and the wait_task does not depend on Suspended Scheduler on CPU1
  220. TEST_ASSERT_INT_WITHIN(2, waiting_ms, duration_wait_task_ms);
  221. } else {
  222. TEST_ASSERT_INT_WITHIN(4, waiting_ms + delta_ms + 4, duration_wait_task_ms);
  223. }
  224. printf("\n");
  225. }
  226. TEST_CASE("Test the waiting task not missed due to scheduler suspension on both CPUs", "[freertos]")
  227. {
  228. printf("Suspend both CPUs:\n");
  229. suspend_both_cpus = true;
  230. test_scheduler_suspend1(0);
  231. test_scheduler_suspend1(1);
  232. }
  233. TEST_CASE("Test the waiting task not missed due to scheduler suspension on one CPU", "[freertos]")
  234. {
  235. printf("Suspend only one CPU:\n");
  236. suspend_both_cpus = false;
  237. test_scheduler_suspend1(0);
  238. test_scheduler_suspend1(1);
  239. }
  240. static uint32_t count_tick[2];
  241. static void IRAM_ATTR tick_hook(void)
  242. {
  243. ++count_tick[xPortGetCoreID()];
  244. }
  245. static void test_scheduler_suspend2(int cpu)
  246. {
  247. esp_register_freertos_tick_hook_for_cpu(tick_hook, 0);
  248. esp_register_freertos_tick_hook_for_cpu(tick_hook, 1);
  249. memset(count_tick, 0, sizeof(count_tick));
  250. printf("Test for CPU%d\n", cpu);
  251. xTaskCreatePinnedToCore(&control_task, "control_task", 8192, NULL, 5, NULL, cpu);
  252. vTaskDelay(waiting_ms * 2 / portTICK_PERIOD_MS);
  253. esp_deregister_freertos_tick_hook(tick_hook);
  254. printf("count_tick[cpu0] = %d, count_tick[cpu1] = %d\n", count_tick[0], count_tick[1]);
  255. TEST_ASSERT_INT_WITHIN(1, waiting_ms * 2, count_tick[0]);
  256. TEST_ASSERT_INT_WITHIN(1, waiting_ms * 2, count_tick[1]);
  257. printf("\n");
  258. }
  259. TEST_CASE("Test suspend-resume CPU. The number of tick_hook should be the same for both CPUs", "[freertos]")
  260. {
  261. printf("Suspend both CPUs:\n");
  262. suspend_both_cpus = true;
  263. test_scheduler_suspend2(0);
  264. test_scheduler_suspend2(1);
  265. printf("Suspend only one CPU:\n");
  266. suspend_both_cpus = false;
  267. test_scheduler_suspend2(0);
  268. test_scheduler_suspend2(1);
  269. }
  270. static int duration_timer_ms;
  271. static void timer_callback(void *arg)
  272. {
  273. ++duration_timer_ms;
  274. }
  275. static void test_scheduler_suspend3(int cpu)
  276. {
  277. duration_timer_ms = 0;
  278. duration_ctrl_task_ms = 0;
  279. printf("Test for CPU%d\n", cpu);
  280. TimerHandle_t count_time = xTimerCreate("count_time", 1, pdTRUE, NULL, timer_callback);
  281. xTimerStart( count_time, portMAX_DELAY);
  282. xTaskCreatePinnedToCore(&control_task, "control_task", 8192, NULL, 5, NULL, cpu);
  283. vTaskDelay(waiting_ms * 2 / portTICK_PERIOD_MS);
  284. xTimerDelete(count_time, portMAX_DELAY);
  285. printf("Finish duration_timer_ms=%d ms\n", duration_timer_ms);
  286. TEST_ASSERT_INT_WITHIN(2, waiting_ms * 2, duration_timer_ms);
  287. TEST_ASSERT_INT_WITHIN(5, waiting_ms + delta_ms, duration_ctrl_task_ms);
  288. printf("\n");
  289. }
  290. TEST_CASE("Test suspend-resume CPU works with xTimer", "[freertos]")
  291. {
  292. printf("Suspend both CPUs:\n");
  293. suspend_both_cpus = true;
  294. test_scheduler_suspend3(0);
  295. test_scheduler_suspend3(1);
  296. printf("Suspend only one CPU:\n");
  297. suspend_both_cpus = false;
  298. test_scheduler_suspend3(0);
  299. test_scheduler_suspend3(1);
  300. }
  301. #endif // CONFIG_FREERTOS_UNICORE