test_task_suspend_resume.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 "unity.h"
  11. #include "soc/cpu.h"
  12. #include "test_utils.h"
  13. #include "driver/timer.h"
  14. #ifndef CONFIG_FREERTOS_UNICORE
  15. #include "esp_ipc.h"
  16. #endif
  17. #include "esp_freertos_hooks.h"
  18. #include "esp_rom_sys.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. /* Task suspends itself, then sets parameter value to the current timer group counter and deletes itself */
  112. static void task_suspend_self_with_timer(void *vp_resumed)
  113. {
  114. volatile uint64_t *resumed_counter = vp_resumed;
  115. *resumed_counter = 0;
  116. vTaskSuspend(NULL);
  117. timer_get_counter_value(TIMER_GROUP_0, TIMER_0, vp_resumed);
  118. vTaskDelete(NULL);
  119. }
  120. /* Create a task which suspends itself, then resume it from a timer
  121. * interrupt. */
  122. static void test_resume_task_from_isr(int target_core)
  123. {
  124. volatile uint64_t resumed_counter = 99;
  125. TaskHandle_t suspend_task;
  126. xTaskCreatePinnedToCore(task_suspend_self_with_timer, "suspend_self", 2048,
  127. (void *)&resumed_counter, UNITY_FREERTOS_PRIORITY + 1,
  128. &suspend_task, target_core);
  129. vTaskDelay(1);
  130. TEST_ASSERT_EQUAL(0, resumed_counter);
  131. const unsigned APB_CYCLES_PER_TICK = TIMER_BASE_CLK / configTICK_RATE_HZ;
  132. const unsigned TEST_TIMER_DIV = 2;
  133. const unsigned TEST_TIMER_CYCLES_PER_TICK = APB_CYCLES_PER_TICK / TEST_TIMER_DIV;
  134. const unsigned TEST_TIMER_CYCLES_PER_MS = TIMER_BASE_CLK / 1000 / TEST_TIMER_DIV;
  135. const unsigned TEST_TIMER_ALARM = TEST_TIMER_CYCLES_PER_TICK / 2; // half an RTOS tick
  136. /* Configure timer ISR */
  137. timer_isr_handle_t isr_handle;
  138. const timer_config_t config = {
  139. .alarm_en = 1,
  140. .auto_reload = 0,
  141. .counter_dir = TIMER_COUNT_UP,
  142. .divider = TEST_TIMER_DIV,
  143. .intr_type = TIMER_INTR_LEVEL,
  144. .counter_en = TIMER_PAUSE,
  145. };
  146. /*Configure timer*/
  147. ESP_ERROR_CHECK( timer_init(TIMER_GROUP_0, TIMER_0, &config) );
  148. ESP_ERROR_CHECK( timer_pause(TIMER_GROUP_0, TIMER_0) );
  149. ESP_ERROR_CHECK( timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0) );
  150. ESP_ERROR_CHECK( timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, TEST_TIMER_ALARM) );
  151. ESP_ERROR_CHECK( timer_enable_intr(TIMER_GROUP_0, TIMER_0) );
  152. ESP_ERROR_CHECK( timer_isr_register(TIMER_GROUP_0, TIMER_0, timer_group0_isr, (void*)suspend_task, ESP_INTR_FLAG_IRAM, &isr_handle) );
  153. timer_isr_fired = false;
  154. vTaskDelay(1); // Make sure we're at the start of a new tick
  155. ESP_ERROR_CHECK( timer_start(TIMER_GROUP_0, TIMER_0) );
  156. vTaskDelay(1); // We expect timer group will fire half-way through this delay
  157. TEST_ASSERT_TRUE(timer_isr_fired);
  158. TEST_ASSERT_NOT_EQUAL(0, resumed_counter);
  159. // The task should have woken within 500us of the timer interrupt event (note: task may be a flash cache miss)
  160. printf("alarm value %u task resumed at %u\n", TEST_TIMER_ALARM, (unsigned)resumed_counter);
  161. TEST_ASSERT_UINT32_WITHIN(TEST_TIMER_CYCLES_PER_MS/2, TEST_TIMER_ALARM, (unsigned)resumed_counter);
  162. // clean up
  163. timer_deinit(TIMER_GROUP_0, TIMER_0);
  164. ESP_ERROR_CHECK( esp_intr_free(isr_handle) );
  165. }
  166. TEST_CASE("Resume task from ISR (same core)", "[freertos]")
  167. {
  168. test_resume_task_from_isr(UNITY_FREERTOS_CPU);
  169. }
  170. #ifndef CONFIG_FREERTOS_UNICORE
  171. TEST_CASE("Resume task from ISR (other core)", "[freertos]")
  172. {
  173. test_resume_task_from_isr(!UNITY_FREERTOS_CPU);
  174. }
  175. static volatile bool block;
  176. static bool suspend_both_cpus;
  177. static void IRAM_ATTR suspend_scheduler_while_block_set(void* arg)
  178. {
  179. vTaskSuspendAll();
  180. while (block) { };
  181. esp_rom_delay_us(1);
  182. xTaskResumeAll();
  183. }
  184. static void IRAM_ATTR suspend_scheduler_on_both_cpus(void)
  185. {
  186. block = true;
  187. if (suspend_both_cpus) {
  188. TEST_ESP_OK(esp_ipc_call((xPortGetCoreID() == 0) ? 1 : 0, &suspend_scheduler_while_block_set, NULL));
  189. }
  190. vTaskSuspendAll();
  191. }
  192. static void IRAM_ATTR resume_scheduler_on_both_cpus(void)
  193. {
  194. block = false;
  195. xTaskResumeAll();
  196. }
  197. static const int waiting_ms = 2000;
  198. static const int delta_ms = 100;
  199. static int duration_wait_task_ms;
  200. static int duration_ctrl_task_ms;
  201. static void waiting_task(void *pvParameters)
  202. {
  203. int cpu_id = xPortGetCoreID();
  204. int64_t start_time = esp_timer_get_time();
  205. printf("Start waiting_task cpu=%d\n", cpu_id);
  206. vTaskDelay(waiting_ms / portTICK_PERIOD_MS);
  207. duration_wait_task_ms = (esp_timer_get_time() - start_time) / 1000;
  208. printf("Finish waiting_task cpu=%d, time=%d ms\n", cpu_id, duration_wait_task_ms);
  209. vTaskDelete(NULL);
  210. }
  211. static void control_task(void *pvParameters)
  212. {
  213. int cpu_id = xPortGetCoreID();
  214. esp_rom_delay_us(2000); // let to start the waiting_task first
  215. printf("Start control_task cpu=%d\n", cpu_id);
  216. int64_t start_time = esp_timer_get_time();
  217. suspend_scheduler_on_both_cpus();
  218. esp_rom_delay_us(waiting_ms * 1000 + delta_ms * 1000);
  219. resume_scheduler_on_both_cpus();
  220. duration_ctrl_task_ms = (esp_timer_get_time() - start_time) / 1000;
  221. printf("Finish control_task cpu=%d, time=%d ms\n", cpu_id, duration_ctrl_task_ms);
  222. vTaskDelete(NULL);
  223. }
  224. static void test_scheduler_suspend1(int cpu)
  225. {
  226. /* This test tests a case then both CPUs were in suspend state and then resume CPUs back.
  227. * 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.
  228. * (In an old implementation of xTaskIncrementTick function the counting for waiting_task() will be continued
  229. * (excluding time in suspended) after control_task() is finished.)
  230. */
  231. duration_wait_task_ms = 0;
  232. duration_ctrl_task_ms = 0;
  233. printf("Test for CPU%d\n", cpu);
  234. int other_cpu = (cpu == 0) ? 1 : 0;
  235. xTaskCreatePinnedToCore(&waiting_task, "waiting_task", 8192, NULL, 5, NULL, other_cpu);
  236. xTaskCreatePinnedToCore(&control_task, "control_task", 8192, NULL, 5, NULL, cpu);
  237. vTaskDelay(waiting_ms * 2 / portTICK_PERIOD_MS);
  238. TEST_ASSERT_INT_WITHIN(4, waiting_ms + delta_ms + 4, duration_ctrl_task_ms);
  239. if (suspend_both_cpus == false && cpu == 1) {
  240. // CPU0 continues to increase the TickCount and the wait_task does not depend on Suspended Scheduler on CPU1
  241. TEST_ASSERT_INT_WITHIN(2, waiting_ms, duration_wait_task_ms);
  242. } else {
  243. TEST_ASSERT_INT_WITHIN(4, waiting_ms + delta_ms + 4, duration_wait_task_ms);
  244. }
  245. printf("\n");
  246. }
  247. TEST_CASE("Test the waiting task not missed due to scheduler suspension on both CPUs", "[freertos]")
  248. {
  249. printf("Suspend both CPUs:\n");
  250. suspend_both_cpus = true;
  251. test_scheduler_suspend1(0);
  252. test_scheduler_suspend1(1);
  253. }
  254. TEST_CASE("Test the waiting task not missed due to scheduler suspension on one CPU", "[freertos]")
  255. {
  256. printf("Suspend only one CPU:\n");
  257. suspend_both_cpus = false;
  258. test_scheduler_suspend1(0);
  259. test_scheduler_suspend1(1);
  260. }
  261. static uint32_t tick_hook_ms[2];
  262. static void IRAM_ATTR tick_hook(void)
  263. {
  264. tick_hook_ms[xPortGetCoreID()] += portTICK_PERIOD_MS;
  265. }
  266. static void test_scheduler_suspend2(int cpu)
  267. {
  268. esp_register_freertos_tick_hook_for_cpu(tick_hook, 0);
  269. esp_register_freertos_tick_hook_for_cpu(tick_hook, 1);
  270. memset(tick_hook_ms, 0, sizeof(tick_hook_ms));
  271. printf("Test for CPU%d\n", cpu);
  272. xTaskCreatePinnedToCore(&control_task, "control_task", 8192, NULL, 5, NULL, cpu);
  273. vTaskDelay(waiting_ms * 2 / portTICK_PERIOD_MS);
  274. esp_deregister_freertos_tick_hook(tick_hook);
  275. printf("tick_hook_ms[cpu0] = %d, tick_hook_ms[cpu1] = %d\n", tick_hook_ms[0], tick_hook_ms[1]);
  276. TEST_ASSERT_INT_WITHIN(portTICK_PERIOD_MS * 2, waiting_ms * 2, tick_hook_ms[0]);
  277. TEST_ASSERT_INT_WITHIN(portTICK_PERIOD_MS * 2, waiting_ms * 2, tick_hook_ms[1]);
  278. printf("\n");
  279. }
  280. TEST_CASE("Test suspend-resume CPU. The number of tick_hook should be the same for both CPUs", "[freertos]")
  281. {
  282. printf("Suspend both CPUs:\n");
  283. suspend_both_cpus = true;
  284. test_scheduler_suspend2(0);
  285. test_scheduler_suspend2(1);
  286. printf("Suspend only one CPU:\n");
  287. suspend_both_cpus = false;
  288. test_scheduler_suspend2(0);
  289. test_scheduler_suspend2(1);
  290. }
  291. static int duration_timer_ms;
  292. static void timer_callback(void *arg)
  293. {
  294. duration_timer_ms += portTICK_PERIOD_MS;
  295. }
  296. static void test_scheduler_suspend3(int cpu)
  297. {
  298. duration_timer_ms = 0;
  299. duration_ctrl_task_ms = 0;
  300. printf("Test for CPU%d\n", cpu);
  301. TimerHandle_t count_time = xTimerCreate("count_time", 1, pdTRUE, NULL, timer_callback);
  302. xTimerStart( count_time, portMAX_DELAY);
  303. xTaskCreatePinnedToCore(&control_task, "control_task", 8192, NULL, 5, NULL, cpu);
  304. vTaskDelay(waiting_ms * 2 / portTICK_PERIOD_MS);
  305. xTimerDelete(count_time, portMAX_DELAY);
  306. printf("Finish duration_timer_ms=%d ms\n", duration_timer_ms);
  307. TEST_ASSERT_INT_WITHIN(2, waiting_ms * 2, duration_timer_ms);
  308. TEST_ASSERT_INT_WITHIN(5, waiting_ms + delta_ms, duration_ctrl_task_ms);
  309. printf("\n");
  310. }
  311. TEST_CASE("Test suspend-resume CPU works with xTimer", "[freertos]")
  312. {
  313. printf("Suspend both CPUs:\n");
  314. suspend_both_cpus = true;
  315. test_scheduler_suspend3(0);
  316. test_scheduler_suspend3(1);
  317. printf("Suspend only one CPU:\n");
  318. suspend_both_cpus = false;
  319. test_scheduler_suspend3(0);
  320. test_scheduler_suspend3(1);
  321. }
  322. #endif // CONFIG_FREERTOS_UNICORE