最近学习白问网韦东山老师在B站开源的freeRTOS课程,网址:韦东山直播公开课:RTOS实战项目之实现多任务系统 第1节:裸机程序框架和缺陷_哔哩哔哩_bilibili和7天物联网训练营【第2期】7天物联网智能家居实战训练营
在学习过程中按照韦老师的方法分析了下freeRTOS源码,如果有不对的地方请指证。
任务创建源码分析:
//任务控制块结构体主要成员
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. */
//状态列表、事件列表
ListItem_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. */
} tskTCB;
//任务创建函数
BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
const char * const pcName,
const uint16_t usStackDepth,
void * const pvParameters,
UBaseType_t uxPriority,
TaskHandle_t * const pxCreatedTask ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
{
TCB_t *pxNewTCB;
BaseType_t xReturn;
/* 根据你使用的硬件平台的区别*/
这个区别是什么呢?
指的 硬件平台栈增长方式
我们选择M4,分析M4的栈增长方式就可以
在M4的权威指南里 4.4.3-栈存储分析
#define portSTACK_GROWTH ( -1 ) 表示满减栈
*/
#if( portSTACK_GROWTH > 0 )
{
}
#else /* portSTACK_GROWTH */
{
StackType_t *pxStack;
/* 任务栈内存分配*/
pxStack = ( StackType_t * ) pvPortMalloc( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
if( pxStack != NULL )
{

本文详细解析了FreeRTOS操作系统中任务的创建、删除、挂起和恢复的源码实现。通过分析任务控制块结构体,了解任务的状态管理和栈管理。在任务创建时,涉及栈分配、任务优先级设置以及任务初始化。任务删除则包括从任务列表中移除并释放资源。挂起任务会将其移到挂起列表,而恢复任务则是将其重新放入就绪列表。这些操作都是基于临界区保护和优先级管理进行的。
最低0.47元/天 解锁文章
1157

被折叠的 条评论
为什么被折叠?



