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.mzph.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…