FreeRTOS任務基礎知識

任務特性

在RTOS中,一個實時應用可以作為一個獨立的任務,支持搶占,支持優先級,每個任務都有自己的堆棧,當任務切換時將上下文環境保存在堆棧中,再次調用任務時,取出上下文信息,繼續運行。

任務優先級

每一個任務都可以分配一個從0~(configMAX_PRIORITIES-1)的優先級,configMAX_PRIORITIES在FreeRTOSConfig中有定義
在這里插入圖片描述
如果所使用的硬件平臺支持類似計算前導零這樣的指令(Cortex-M處理器支持)。并且configUSE_PORT_OPTIMISED_TASK_SELECTION設置為1,那么configMAX_PRIORITIES不能超過32。
在這里插入圖片描述
其他情況下configMAX_PRIORITIES可以設置成任意值,但是考慮到RAM的消耗,configMAX_PRIORITIES最好設置為一個滿足應用的最小值。
優先級數字越低表示任務的優先級越低,0的優先級最低,configMAX_PRIORITIES-1的優先級最高,空閑任務的優先級最低,為0。當宏configUSE_TIME_SLICING=1時,多個任務可以共用一個優先級,數量不限。此時處于就緒態的優先級相同的任務就會使用時間片輪轉調度器獲取運行時間。

任務控制塊

FreeRTOS每個任務都有一些屬性需要存儲,FreeRTOS把這些屬性集合到一起一起用一個結構體表示,這個結構體叫做任務控制塊:TCB_t,創建任務的時候自動給每個任務分配一個任務控制塊,此結構體在task.c中定義:如下:

typedef struct tskTaskControlBlock
{volatile StackType_t	*pxTopOfStack;	/*< Points to the location of the last item placed on the tasks stack.  THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */#if ( portUSING_MPU_WRAPPERS == 1 )xMPU_SETTINGS	xMPUSettings;		/*< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */#endifListItem_t			xStateListItem;	/*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */ListItem_t			xEventListItem;		/*< Used to reference a task from an event list. */UBaseType_t			uxPriority;			/*< The priority of the task.  0 is the lowest priority. */StackType_t			*pxStack;			/*< Points to the start of the stack. */char				pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created.  Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */#if ( portSTACK_GROWTH > 0 )StackType_t		*pxEndOfStack;		/*< Points to the end of the stack on architectures where the stack grows up from low memory. */#endif#if ( portCRITICAL_NESTING_IN_TCB == 1 )UBaseType_t		uxCriticalNesting;	/*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */#endif#if ( configUSE_TRACE_FACILITY == 1 )UBaseType_t		uxTCBNumber;		/*< Stores a number that increments each time a TCB is created.  It allows debuggers to determine when a task has been deleted and then recreated. */UBaseType_t		uxTaskNumber;		/*< Stores a number specifically for use by third party trace code. */#endif#if ( configUSE_MUTEXES == 1 )UBaseType_t		uxBasePriority;		/*< The priority last assigned to the task - used by the priority inheritance mechanism. */UBaseType_t		uxMutexesHeld;#endif#if ( configUSE_APPLICATION_TASK_TAG == 1 )TaskHookFunction_t pxTaskTag;#endif#if( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )void *pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];#endif#if( configGENERATE_RUN_TIME_STATS == 1 )uint32_t		ulRunTimeCounter;	/*< Stores the amount of time the task has spent in the Running state. */#endif#if ( configUSE_NEWLIB_REENTRANT == 1 )/* Allocate a Newlib reent structure that is specific to this task.Note Newlib support has been included by popular demand, but is notused by the FreeRTOS maintainers themselves.  FreeRTOS is notresponsible for resulting newlib operation.  User must be familiar withnewlib and must provide system-wide implementations of the necessarystubs. Be warned that (at the time of writing) the current newlib designimplements a system-wide malloc() that must be provided with locks. */struct	_reent xNewLib_reent;#endif#if( configUSE_TASK_NOTIFICATIONS == 1 )volatile uint32_t ulNotifiedValue;volatile uint8_t ucNotifyState;#endif/* See the comments above the definition oftskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */#if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )uint8_t	ucStaticallyAllocated; 		/*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */#endif#if( INCLUDE_xTaskAbortDelay == 1 )uint8_t ucDelayAborted;#endif} tskTCB;/* The old tskTCB name is maintained above then typedefed to the new TCB_t name
below to enable the use of older kernel aware debuggers. */
typedef tskTCB TCB_t;

任務堆棧

FreeRTOS之所以能正確恢復一個任務的運行就是有任務堆棧,創建任務的時候需要給任務指定堆棧,如果使用xTaskCreate創建任務的話,任務堆棧就會由函數xTaskCreate自動創建。如果使用xTaskCreateStatic創建任務,就需要我們自行定義任務堆棧。
任務堆棧的數據類型是StackType_t,StackType_t本質上是uint32_t類型,在portmacro.h中有定義,所以StackType_t是4字節,實際堆棧大小是我們定義的4倍。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379765.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379765.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379765.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

測試Rockey 4 Smart加密鎖的C語言代碼

測試Rockey 4 Smart加密鎖的C語言代碼 // win32Console_dog_test.cpp : Defines the entry point for the console application. /// // //測試Rockey 4 Smart加密鎖的C語言代碼 // /// #include "stdafx.h" #include <conio.h> #include "time.h" #…

C——任意一個偶數分解兩個素數

題目&#xff1a;一個偶數總能表示為兩個素數之和 以上實例運行輸出結果為&#xff1a; 請輸入一個偶數: 4 偶數4可以分解成1和3兩個素數的和 #include <stdio.h> #include <stdlib.h> int Isprimer(int n); int main() {int n,i;do{printf("請輸入一個偶數&…

c#委托調用另一窗口函數_在C#中使用委托調用成員函數

c#委托調用另一窗口函數Prerequisite: Delegates in C# 先決條件&#xff1a; C&#xff03;中的代表 We can also call a member function of a class using delegates. It is similar to static function calls, here we have to pass member function using an object on t…

Java版AVG游戲開發入門[0]——游戲模式轉換中的事件交互

Java版AVG游戲開發入門[0]——游戲模式轉換中的事件交互 示例程序下載地址&#xff1a;http://download.csdn.net/source/999273&#xff08;源碼在jar內&#xff09; AVG&#xff0c;即Adventure Game&#xff0c;可以直譯為[冒險游戲]。但是通常情況下我們說AVG是指[文字冒險…

FreeRTOS任務創建和刪除

任務創建和刪除的API函數 xTaskCreate()&#xff1a;使用動態方法創建一個任務xTaskCreateStatic()&#xff1a;使用靜態方法創建一個任務xTaskCreateRestricated()&#xff1a;創建一個使用MPU進行限制的任務&#xff0c;相關內存使用動態內存分配vTaskDelete()&#xff1a;刪…

Delphi 調試

調試&#xff1a;F9執行F8逐過程單步調試F7逐語句單步調試轉載于:https://www.cnblogs.com/JackShao/archive/2012/04/30/2476931.html

1.創建單項鏈表

# include <stdio.h> # include <malloc.h> # include <stdlib.h>typedef struct Node{int data;//數據域struct Node *pNext;//指針域}NODE, *PNODE; //NODE等價于struct Node //PNOD等價于struct Node * //函數聲明PNODE create_list(void); void traverse…

python 日本就業_日本的繪圖標志 Python中的圖像處理

python 日本就業Read basics of the drawing/image processing in python: Drawing flag of Thailand 閱讀python中繪圖/圖像處理的基礎知識&#xff1a; 泰國的繪圖標志 The national flag of Japan is a rectangular white banner bearing a crimson-red disc at its center…

[windows phone 7 ]查看已安裝程序GUID

首先介紹下wp7RootToolsSDK,這個功能相當強大&#xff0c;適合研究wp7高級功能。 它支持File&#xff0c;Register操作&#xff0c;比之前的COM調用要簡單&#xff0c;方便。 功能:查看已安裝程序的guid 開發心得: 用的是mozart,rom多&#xff0c;刷機吧&#xff0c;最麻煩的是…

FreeRTOS任務掛起和恢復

任務掛起&#xff1a;暫停某個任務的執行 任務恢復&#xff1a;讓暫停的任務繼續執行 通過任務掛起和恢復&#xff0c;可以達到讓任務停止一段時間后重新運行。 相關API函數&#xff1a; vTaskSuspend void vTaskSuspend( TaskHandle_t xTaskToSuspend );xTaskToSuspend &am…

向oracle存儲過程中傳參值出現亂碼

在頁面中加入<meta http-equiv"Content-Type" content"text ml;charsetUTF-8"/>就可以解決這一問題 適用情況&#xff1a; 1.中文 2.特殊符號 轉載于:https://www.cnblogs.com/GoalRyan/archive/2009/02/16/1391348.html

Scala程序將多行字符串轉換為數組

Scala | 多行字符串到數組 (Scala | Multiline strings to an array) Scala programming language is employed in working with data logs and their manipulation. Data logs are entered into the code as a single string which might contain multiple lines of code and …

SQL 異常處理 Begin try end try begin catch end catch--轉

SQL 異常處理 Begin try end try begin catch end catch 總結了一下錯誤捕捉方法:try catch ,error, raiserror 這是在數據庫轉換的時候用的的異常處理, Begin TryInsert into SDT.dbo.DYEmpLostTM(LogDate,ProdGroup,ShiftCode,EmployeeNo,MONo,OpNo,OTFlag,LostTypeID,OffStd…

FreeRTOS中斷配置與臨界段

Cortex-M中斷 中斷是指計算機運行過程中&#xff0c;出現某些意外情況需主機干預時&#xff0c;機器能自動停止正在運行的程序并轉入處理新情況的程序&#xff08;中斷服務程序&#xff09;&#xff0c;處理完畢后又返回原被暫停的程序繼續運行。Cortex-M內核的MCU提供了一個用…

vector向量容器

一、vector向量容器 簡介&#xff1a; Vector向量容器可以簡單的理解為一個數組&#xff0c;它的下標也是從0開始的&#xff0c;使用時可以不用確定大小&#xff0c;但是它可以對于元素的插入和刪除&#xff0c;可以進行動態調整所占用的內存空間&#xff0c;它里面有很多系統…

netsh(二)

netsh 來自微軟的網絡管理看家法寶很多時候&#xff0c;我們可能需要在不同的網絡中工作&#xff0c;一遍又一遍地重復修改IP地址是一件比較麻煩的事。另外&#xff0c;系統崩潰了&#xff0c;重新配置網卡等相關參數也比較煩人&#xff08;尤其是無線網卡&#xff09;。事實上…

java uuid靜態方法_Java UUID getLeastSignificantBits()方法與示例

java uuid靜態方法UUID類getLeastSignificantBits()方法 (UUID Class getLeastSignificantBits() method) getLeastSignificantBits() method is available in java.util package. getLeastSignificantBits()方法在java.util包中可用。 getLeastSignificantBits() method is us…

Google C2Dm相關文章

Android C2DM學習——云端推送&#xff1a;http://blog.csdn.net/ichliebephone/article/details/6591071 Android C2DM學習——客戶端代碼開發&#xff1a;http://blog.csdn.net/ichliebephone/article/details/6626864 Android C2DM學習——服務器端代碼開發&#xff1a;http…

FreeRTOS的列表和列表項

列表和列表項 列表 列表是FreeRTOS中的一個數據結構&#xff0c;概念上和鏈表有點類型&#xff0c;是一個循環雙向鏈表&#xff0c;列表被用來跟蹤FreeRTOS中的任務。列表的類型是List_T&#xff0c;具體定義如下&#xff1a; typedef struct xLIST {listFIRST_LIST_INTEGRI…

string基本字符系列容器

二、string基本字符系列容器 簡介&#xff1a;C語言只提供了一個char類型來處理字符&#xff0c;而對于字符串&#xff0c;只能通過字符串數組來處理&#xff0c;顯得十分不方便。CSTL提供了string基本字符系列容器來處理字符串&#xff0c;可以把string理解為字符串類&#x…