深入理解FreeRTOS_学习笔记(2.任务的本质)

**

文章大部分内容摘抄至B站的韦东山老师的深入了解FreeRTOS操作系统教程,若有不理解的地方,可点击链接学习韦老师的视频。

**

**

上一章已经了解了CPU内部的结构,也了解了一部分的汇编代码,这一章要开始学习一下操作系统的任务。如果有错误的地方,请批评指正,不胜感激!!

**


前言

任务的创建大概都已经会使用了,可以使用静态和动态创建,使用静态创建需要自己定义栈空间大小,还需要设定相对应的宏。本章全程使用动态创建,不需要自定义栈空间大小。

一、创建任务的核心

总共有三个核心,一是任务需要执行的函数,其中运行过程中的局部变量存储在哪里?在切换瞬间保存在哪里?都保存在他自己的栈空间里面。二是怎么来表示这个任务,这就是任务的任务结构体。我们先从创建任务的函数来说明。

二、任务函数

1.内部参数

BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
                            const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
                            const configSTACK_DEPTH_TYPE usStackDepth,
                            void * const pvParameters,
                            UBaseType_t uxPriority,
                            TaskHandle_t * const pxCreatedTask )

内部使用的是malloc函数来申请堆空间来作为任务的栈,可以发现参数有6个,第一个是函数任务入口,第二个是名称,第三个是栈的头部位置,第四个是传递的参数,第五个是优先级,第六个是指向的任务结构体控制块。

2.任务TCB

代码如下(示例):

typedef struct tskTaskControlBlock       /* The old naming convention is used to prevent breaking kernel aware debuggers. */
{
    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. */
    #endif

    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. */

    #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
        StackType_t * pxEndOfStack; /*< Points to the highest valid address for the stack. */
    #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 not
         * used by the FreeRTOS maintainers themselves.  FreeRTOS is not
         * responsible for resulting newlib operation.  User must be familiar with
         * newlib and must provide system-wide implementations of the necessary
         * stubs. Be warned that (at the time of writing) the current newlib design
         * implements a system-wide malloc() that must be provided with locks.
         *
         * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
         * for additional information. */
        struct  _reent xNewLib_reent;
    #endif

    #if ( configUSE_TASK_NOTIFICATIONS == 1 )
        volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
        volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
    #endif

    /* See the comments in FreeRTOS.h with the definition of
     * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
    #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
        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

    #if ( configUSE_POSIX_ERRNO == 1 )
        int iTaskErrno;
    #endif
} tskTCB;

可以看出任务控制块中有五个具体参数:第一个指向栈顶,第二个是状态链表,第三个是事件链表,第四个是优先级,第五个是指向的栈,第六个是名称。不难发现其实和任务创建的参数相似,但是任务创建所需要的函数入口地址却在TCB中没有发现,那么TCB中要怎么知道运行哪个任务函数呢??之后我们便能知晓了,现在先了解一下,任务创建中的栈空间究竟是存储在哪里的,其大小是如何确定。

3.任务中的栈空间

在任务函数中添加了一个容量为100的数组,查看反汇编代码:
在这里插入图片描述
可以看出创建了数组之后,sp=sp-0x194,100*int占四个字节=400–>0x190
加上0x04就是整个的局部变量,可以看出栈空间的大小具体是由局部变量来
确定的,当然如果在这个任务中调用了另一个函数,也能决定栈空间的大小,准确来说,应该是取决于局部变量和调用深度,调用的任务中局部变量也存储在任务的栈空间中。而任务占用的栈空间是从哪里分配的呢?在代码中可以看出,在heap2.c中可以看出,其实是创建了一个大容量的数组,任务创建就会向这个堆空间申请出一份内存用作栈空间。这个数组的容量和自定义。
在这里插入图片描述

4.任务入口函数

任务创建中,我们发现TCB中没有可存储任务函数的变量,那么任务函数是怎么跑起来的呢?实际在FreeRTOS中,在创建时候传递的函数地址,要想运行这个任务函数,只需要将PC寄存器的值=函数的地址就可以了,这就让CPU来运行这个函数了。
在这里插入图片描述
这张图就充分展示了TCB每一个变量是怎么存储在所存储的栈空间的。

总结

这章主要是讲了一下任务在内存中是怎么样的,下一期主要是写一下任务的调度原理,这就会用上TCB中的两个状态链表了,这需要去了解一下数据结构了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值