使用 Arduino 在 FreeRTOS 中暂停/恢复任务
如果您希望在FreeRTOS中暂停任务vTaskSuspend() ,可以使用一个函数。语法是-
语法
void vTaskSuspend( TaskHandle_t xTaskToSuspend );
如您所见,它以挂起任务的句柄作为参数,不返回任何内容。可以使用恢复暂停的任务。语法是- vTaskResume()
语法
void vTaskResume( TaskHandle_t xTaskToResume );
这再次获取要恢复的任务的句柄,并且不返回任何内容。
为了查看示例,我们将演练-https://exploreembedded.com/wiki/Task_Suspend_and_Resume中给出的代码
如您所见,最初声明了四个任务句柄,并在设置中创建了任务。
/************************************************************************ *************************** ExploreEmbedded Copyright Notice ************************************************************************* *************************** * File: 07-TaskSuspendAndResume * Version: 15.0 * Author: ExploreEmbedded * Website: http://www.exploreembedded.com/wiki * Description: File contains the free rtos example to demonstarte task Suspend and Resume. This code has been developed and tested on ExploreEmbedded boards. We strongly believe that the library works on any of development boards for respective controllers. Check this link http://www.exploreembedded.com/wiki for awesome tutorials on 8051,PIC,AVR,ARM,Robotics,RTOS,IOT. ExploreEmbedded invests substantial time and effort developing open source HW and SW tools, to support consider buying the ExploreEmbedded boards. The ExploreEmbedded libraries and examples are licensed under the terms of the new-bsd license(two-clause bsd license). See also: http://www.opensource.org/licenses/bsd-license.php EXPLOREEMBEDDED DISCLAIMS ANY KIND OF HARDWARE FAILURE RESULTING OUT OF USAGE OF LIBRARIES, DIRECTLY OR INDIRECTLY. FILES MAY BE SUBJECT TO CHANGE WITHOUT PRIOR NOTICE. THE REVISION HISTORY CONTAINS THE INFORMATION RELATED TO UPDATES. Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that this copyright notices appear in all copies and that both those copyright notices and this permission notice appear in supporting documentation. ************************************************************************* *************************/ #includeTaskHandle_t TaskHandle_1; TaskHandle_t TaskHandle_2; TaskHandle_t TaskHandle_3; TaskHandle_t TaskHandle_4; void setup() { Serial.begin(9600); Serial.println(F("In Setup function")); /* Create 4-tasks with priorities 1-4. Capture the Task details to respective handlers */ xTaskCreate(MyTask1, "Task1", 100, NULL, 1, &TaskHandle_1); xTaskCreate(MyTask2, "Task2", 100, NULL, 2, &TaskHandle_2); xTaskCreate(MyTask3, "Task3", 100, NULL, 3, &TaskHandle_3); xTaskCreate(MyTask4, "Task4", 100, NULL, 4, &TaskHandle_4); }
请注意,Task1 的优先级最低,Task4 的优先级最高。
循环只包含一个打印 语句和一个延迟。
void loop() { //挂钩空闲任务,将在CPU空闲时运行 Serial.println(F("Loop function")); delay(50); }
现在,来到各个任务代码,Task4暂停Task2、Task3然后它自己。任务1将这些任务一一恢复,然后自我删除。任务2和3只是在打印语句后删除自己。
/* Task1 with priority 1 */ static void MyTask1(void* pvParameters) { Serial.println(F("Task1 Resuming Task2")); vTaskResume(TaskHandle_2); Serial.println(F("Task1 Resuming Task3")); vTaskResume(TaskHandle_3); Serial.println(F("Task1 Resuming Task4")); vTaskResume(TaskHandle_4); Serial.println(F("Task1 Deleting Itself")); vTaskDelete(TaskHandle_1); } /* Task2 with priority 2 */ static void MyTask2(void* pvParameters) { Serial.println(F("Task2, Deleting itself")); vTaskDelete(NULL); //通过传递NULL删除自己的任务(也可以使用TaskHandle_2) } /* Task3 with priority 3 */ static void MyTask3(void* pvParameters) { Serial.println(F("Task3, Deleting Itself")); vTaskDelete(NULL); //通过传递NULL删除自己的任务(也可以使用TaskHandle_3) } /* Task4 with priority 4 */ static void MyTask4(void* pvParameters) { Serial.println(F("Task4 Running, Suspending all tasks")); vTaskSuspend(TaskHandle_2); //暂停Task2/3 vTaskSuspend(TaskHandle_3); vTaskSuspend(NULL); //暂停自己的任务 Serial.println(F("Back in Task4, Deleting Itself")); vTaskDelete(TaskHandle_4); }
因此,具有最高优先级的Task4首先启动,并挂起Task2、3及其本身。因此,Task1获得控制权。Task1恢复Task2。由于Task2具有更高的优先级,它接管控制权,打印一条语句并删除自己。控制返回到Task1,然后恢复Task3。Task3完成它的工作,删除自身,因此控制权返回到Task1。Task4也会发生同样的事情,一旦Task4被删除,Task1也会删除自己,并且只有循环中的打印语句在串行监视器上可见。