freeRTOS源碼解析4--tasks.c 5

来源:https://www.cnblogs.com/freeManX1807/p/18425725
-Advertisement-
Play Games

想著做個輪腿的機器人玩玩,但是如果光用PID做演算法,對於輪子加腿的結構似乎效果並不好,為了實現輪腿本身能夠飛坡在一定高度下能夠跳躍,我想著上個模擬模型來調試和學習LQR演算法 機器人模擬的軟體似乎挺多,我查到比較常用的有ROS套件的一個,還有就是webots 本著界面簡單,開源(還有校園網方便下載)的 ...


4.2.13 繼續任務--vTaskResume

介面:
void vTaskResume( TaskHandle_t xTaskToResume )
形參1:xTaskToResume ,想要繼續的任務handle;

首先是vTaskResume調用的一個內部函數:static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ),用於檢查任務是否是掛起狀態,只有掛起的任務才能繼續,否則是等待事件、通知等處於阻塞態,那就不能放到就緒列表,必須繼續等待。這個介面邏輯非常簡單,就直接看代碼即可。

 1 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
 2 {
 3     BaseType_t xReturn = pdFALSE;
 4     const TCB_t * const pxTCB = xTask;
 5 
 6     /* Accesses xPendingReadyList so must be called from a critical
 7      * section. */
 8 
 9     /* It does not make sense to check if the calling task is suspended. */
10     configASSERT( xTask );
11 
12     /* Is the task being resumed actually in the suspended list? */
13     /* 檢查任務是否在掛起列表, 不在則說明任務未掛起 */
14     if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
15     {
16         /* Has the task already been resumed from within an ISR? */
17         /* 任務在掛起列表裡, 但在調度器暫停時被移至等待就緒列表中, 則說明任務未掛起 */
18         if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
19         {
20             /* Is it in the suspended list because it is in the Suspended
21              * state, or because it is blocked with no timeout? */
22             /* 任務在掛起列表裡, 但在等待事件, 則說明任務未掛起 */
23             if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE )
24             {
25                 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
26                 {
27                     BaseType_t x;
28 
29                     /* The task does not appear on the event list item of
30                      * and of the RTOS objects, but could still be in the
31                      * blocked state if it is waiting on its notification
32                      * rather than waiting on an object.  If not, is
33                      * suspended. */
34                     /* 任務在掛起列表裡, 且未等待事件, 那麼如果在等待通知則未掛起, 否則掛起 */
35                     xReturn = pdTRUE;
36 
37                     for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
38                     {
39                         if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
40                         {
41                             xReturn = pdFALSE;
42                             break;
43                         }
44                     }
45                 }
46                 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
47                 {
48                     xReturn = pdTRUE;
49                 }
50                 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
51             }
52             else
53             {
54                 mtCOVERAGE_TEST_MARKER();
55             }
56         }
57         else
58         {
59             mtCOVERAGE_TEST_MARKER();
60         }
61     }
62     else
63     {
64         mtCOVERAGE_TEST_MARKER();
65     }
66 
67     /* 總結下來就是任務若在掛起列表裡, 如果不是在等待事件或通知, 則是掛起狀態, 否則不是 */
68 
69     return xReturn;
70 }
prvTaskIsTaskSuspended
 1 void vTaskResume( TaskHandle_t xTaskToResume )
 2 {
 3     TCB_t * const pxTCB = xTaskToResume;
 4 
 5     /* It does not make sense to resume the calling task. */
 6     configASSERT( xTaskToResume );
 7 
 8     #if ( configNUMBER_OF_CORES == 1 )
 9         /* The parameter cannot be NULL as it is impossible to resume the
10          * currently executing task. */
11         if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
12     #endif
13     {
14         taskENTER_CRITICAL();
15         {
16             if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
17             {
18                 /* The ready list can be accessed even if the scheduler is
19                  * suspended because this is inside a critical section. */
20                 /* 只有任務處於掛起狀態才能繼續 */
21                 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
22                 prvAddTaskToReadyList( pxTCB );
23 
24                 /* This yield may not cause the task just resumed to run,
25                  * but will leave the lists in the correct state for the
26                  * next yield. */
27                 /* 保證各任務列表處於正常的狀態 */
28                 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
29             }
30             else
31             {
32                 mtCOVERAGE_TEST_MARKER();
33             }
34         }
35         taskEXIT_CRITICAL();
36     }
37     else
38     {
39         mtCOVERAGE_TEST_MARKER();
40     }
41 }
vTaskResume

4.2.14 啟動調度--vTaskStartScheduler

 這個介面會創建空閑任務和軟定時器任務,之後就是允許第一個任務,這個介面永遠也不會返回,也是main中調用的最後一個介面。

 1 void vTaskStartScheduler( void )
 2 {
 3     BaseType_t xReturn;
 4 
 5     /* 創建空閑任務 */
 6     xReturn = prvCreateIdleTasks();
 7 
 8     #if ( configUSE_TIMERS == 1 )
 9     {
10         if( xReturn == pdPASS )
11         {
12             xReturn = xTimerCreateTimerTask();    // 創建軟定時器任務
13         }
14         else
15         {
16             mtCOVERAGE_TEST_MARKER();
17         }
18     }
19     #endif /* configUSE_TIMERS */
20 
21     if( xReturn == pdPASS )
22     {
23         /* Interrupts are turned off here, to ensure a tick does not occur
24          * before or during the call to xPortStartScheduler().  The stacks of
25          * the created tasks contain a status word with interrupts switched on
26          * so interrupts will automatically get re-enabled when the first task
27          * starts to run. */
28         /* 關閉中斷, 保證在調用xPortStartScheduler前不會發生tick中斷, 所有創建
29          * 的任務的棧中都包含一個中斷開啟的狀態字, 所以第一個任務運行時, 中斷
30          * 會自動開啟 */
31         /* 暫時沒看懂這個註釋的意思, 但這個介面只是設置了中斷優先順序屏蔽寄存器,
32          * 實際的中斷並未關閉, 系統中斷被屏蔽了 */
33         portDISABLE_INTERRUPTS();
34 
35         xNextTaskUnblockTime = portMAX_DELAY;
36         xSchedulerRunning = pdTRUE;
37         xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
38 
39         /* If configGENERATE_RUN_TIME_STATS is defined then the following
40          * macro must be defined to configure the timer/counter used to generate
41          * the run time counter time base.   NOTE:  If configGENERATE_RUN_TIME_STATS
42          * is set to 0 and the following line fails to build then ensure you do not
43          * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
44          * FreeRTOSConfig.h file. */
45         portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
46 
47         /* Setting up the timer tick is hardware specific and thus in the
48          * portable interface. */
49 
50         /* The return value for xPortStartScheduler is not required
51          * hence using a void datatype. */
52         /* 真正的啟動調度器, 即啟動了第一個任務 */
53         ( void ) xPortStartScheduler();
54 
55         /* In most cases, xPortStartScheduler() will not return. If it
56          * returns pdTRUE then there was not enough heap memory available
57          * to create either the Idle or the Timer task. If it returned
58          * pdFALSE, then the application called xTaskEndScheduler().
59          * Most ports don't implement xTaskEndScheduler() as there is
60          * nothing to return to. */
61     }
62     else
63     {
64         /* This line will only be reached if the kernel could not be started,
65          * because there was not enough FreeRTOS heap to create the idle task
66          * or the timer task. */
67         configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
68     }
69 
70     /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
71      * meaning xIdleTaskHandles are not used anywhere else. */
72     ( void ) xIdleTaskHandles;
73 
74     /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
75      * from getting optimized out as it is no longer used by the kernel. */
76     ( void ) uxTopUsedPriority;
77 }
vTaskStartScheduler

 

4.2.15 停止調度--vTaskEndScheduler

這個介面和上一個相對,而且這個介面一定是在某個任務中調用的,因為系統不會主動停止調度器,而正常運行中始終只有某個任務和系統在運行,所以必然是某個任務主動調用的。

 1 void vTaskEndScheduler( void )
 2 {
 3     #if ( INCLUDE_vTaskDelete == 1 )
 4     {
 5         BaseType_t xCoreID;
 6 
 7         #if ( configUSE_TIMERS == 1 )
 8         {
 9             /* Delete the timer task created by the kernel. */
10             /* 刪除軟定時器任務 */
11             vTaskDelete( xTimerGetTimerDaemonTaskHandle() );
12         }
13         #endif /* #if ( configUSE_TIMERS == 1 ) */
14 
15         /* Delete Idle tasks created by the kernel.*/
16         for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
17         {
18             vTaskDelete( xIdleTaskHandles[ xCoreID ] );    // 刪除空閑任務
19         }
20 
21         /* Idle task is responsible for reclaiming the resources of the tasks in
22          * xTasksWaitingTermination list. Since the idle task is now deleted and
23          * no longer going to run, we need to reclaim resources of all the tasks
24          * in the xTasksWaitingTermination list. */
25         /* 本應由空閑任務回收待刪除任務的資源, 但現在空閑任務被刪除了, 就在這裡處理 */
26         prvCheckTasksWaitingTermination();
27     }
28     #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */
29 
30     /* Stop the scheduler interrupts and call the portable scheduler end
31      * routine so the original ISRs can be restored if necessary.  The port
32      * layer must ensure interrupts enable  bit is left in the correct state. */
33     /* 這跟啟動調度器時關中斷一樣的疑問 */
34     portDISABLE_INTERRUPTS();
35     xSchedulerRunning = pdFALSE;
36 
37     /* This function must be called from a task and the application is
38      * responsible for deleting that task after the scheduler is stopped. */
39     vPortEndScheduler();
40 }
vTaskEndScheduler

 

  好了,下一篇講xTaskAbortDelay和xTaskIncrementTick介面,這兩個介面較為複雜。

  下篇再見。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • 徒弟不懂git怎麼用, 於是寫了篇文章, 把本地git操作從頭寫了一遍, 自己去看吧! 0、基本概念 •Git是一個免費、開源的、分散式版本控制系統 •它使用一個特殊的叫做倉庫的資料庫來記錄文件的變化 •倉庫中的每個文件都有一個完整的版本歷史記錄 1)安裝 sudo apt-update sudo ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...