美文网首页RTOS和GUI_基于英飞凌tc2x及stm32开发板
FreeRTOS的第一个任务跳转源码分析—Apple的学习笔记

FreeRTOS的第一个任务跳转源码分析—Apple的学习笔记

作者: applecai | 来源:发表于2021-04-15 22:17 被阅读0次

    一,前言

    ubuntu下Nuttx OS调试环境搭建--Apple的学习笔记中我已经开始了nuttx os的学习了。但是我觉得有必要先复习下freertos的任务切换机制,这样nuttx os可以用对比的方式学习。我在自己移植的cortex-M0的芯片基础上进行的分析,为了验证理解,我进行了调试及截图。

    二,准备工作:pxCurrentTCB和栈的关系

    1. pxCurrentTCB的路径和stack有什么关系,先要弄清楚,否则后面是无法理解的。
    static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
    {
        /* Ensure interrupts don't access the task lists while the lists are being
         * updated. */
        taskENTER_CRITICAL();
        {
            uxCurrentNumberOfTasks++;
    
            if( pxCurrentTCB == NULL )
            {
                /* There are no other tasks, or all the other tasks are in
                 * the suspended state - make this the current task. */
                pxCurrentTCB = pxNewTCB;
    ….
    }
    

    pxCurrentTCB就是一个最高优先级的TCB的地址。

    1. 接着一个重要的内容是pxTopOfStack。
    typedef struct tskTaskControlBlock       
    {
        volatile StackType_t * pxTopOfStack;
    …
    }
    

    这就把pxCurrentTCB和pxTopOfStack的地址联系起来了,因为pxTopOfStack是第一个结构体成员,所以从地址上来看pxCurrentTCB的地址等于pxTopOfStack的地址。

    1. 那么看下pxTopOfStack在创建task的时候是如何赋值的。
    StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
                                         TaskFunction_t pxCode,
                                         void * pvParameters )
    {
        /* Simulate the stack frame as it would be created by a context switch
         * interrupt. */
        pxTopOfStack--;                                   /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */
        *pxTopOfStack = portINITIAL_XPSR;                 /* xPSR */
        pxTopOfStack--;
        *pxTopOfStack = ( StackType_t ) pxCode;           /* PC */
        pxTopOfStack--;
        *pxTopOfStack = ( StackType_t ) prvTaskExitError; /* LR */
        pxTopOfStack -= 5;                                /* R12, R3, R2 and R1. */
        *pxTopOfStack = ( StackType_t ) pvParameters;     /* R0 */
        pxTopOfStack -= 8;                                /* R11..R4. */
    
        return pxTopOfStack;
    }
    

    上面的代码看的是否很熟悉。这就是创建task的时候,自己构造的一个任务栈。与下图对应。每个地址是4个字节的32bit内容。前8个地址是出栈入栈上下文切换的主要内容,R4到R11的8个通用寄存器是保存函数内的临时数据。官员上下文切换的出栈入栈标准截图如下:


    image.png
    1. 最后看下pxCurrentTCB指针的存储位置。
      PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
      pxCurrentTCB是一个指针类型的数据,它的地址查看map文件,得到的是0x18000004。pxCurrentTCB 0x18000004 Data 4 tasks.o(.data)

    三,prvPortStartFirstTask源码分析

    上一章节的地址及地址中构造的内容理解后,本章节就简单了,只需要会看汇编语言即可。

    __asm void prvPortStartFirstTask( void )
    {
        extern pxCurrentTCB;
    
        PRESERVE8
    
        /* The MSP stack is not reset as, unlike on M3/4 parts, there is no vector
         * table offset register that can be used to locate the initial stack value.
         * Not all M0 parts have the application vector table at address 0. */
    /* *INDENT-OFF* */
    
        ldr r3, = pxCurrentTCB /* Obtain location of pxCurrentTCB. */
        ldr r1, [ r3 ]
        ldr r0, [ r1 ]         /* The first item in pxCurrentTCB is the task top of stack. */
        adds r0, # 32          /* Discard everything up to r0. */
        msr psp, r0            /* This is now the new top of stack to use in the task. */
        movs r0, # 2           /* Switch to the psp stack. */
        msr CONTROL, r0
        isb
        pop { r0 - r5 } /* Pop the registers that are saved automatically. */
        mov lr, r5 /* lr is now in r5. */
        pop { r3 } /* The return address is now in r3. */
        pop { r2 } /* Pop and discard the XPSR. */
        cpsie i /* The first task has its context and interrupts can be enabled. */
        bx r3 /* Finally, jump to the user defined task code. */
    
        ALIGN
    /* *INDENT-ON* */
    }
    

    我一句句来分析

    1. ldr r3, = pxCurrentTCB获取pxCurrentTCB指针的地到r3寄存器,可以看到单步执行后r3的值变为了0x18000004。这个值与第二章节的map文件存储地址值一致。
      image.png
    2. ldr r1, [ r3 ]取r3地址中的值放入r1。也就是pxCurrentTCB中的值,就是pxCurrentTCB指针指向的地址值放入r1。pxCurrentTCB的值是0x180004D0。
      image.png
    3. ldr r0, [ r1 ]取r1地址中的值放入r0。可以到内存窗口看到0x180004D0中的值是0x18000480,将其放入了r1。
      image.png
    4. adds r0, # 32 r0的值加0x20变成了0x180004A0。至于为什么要加32字节的原因就是32/4=8,pxCurrentTCB的地址是自己构造的stack的地址,而stack的地址包括16个寄存器,在最上面已经分析过,地址最小的是R4R11的8个寄存器,r0+32就是跳过R4R11指向了自定义stack中的含义为r0的位置。
      image.png
    5. msr psp, r0就是将r0保存到PSR。
      通用寄存器:R0-R12, 特殊寄存器:如PSR, SP
      MRS:Move from Spential register to general Register(加载特殊寄存器的值到通用寄存器)
      MSR:Move from general Register to Spential register(存储通用寄存器的值到特殊寄存器)
      image.png
    6. movs r0, # 2r0设置为2
      image.png
    7. msr CONTROL, r0 把r0的值2保存到控制寄存器。说明用的是PSP。左边还是MSP,单步运行后control寄存器值变成2,栈选用了PSP中的地址。PSP和MSP都是栈地址,只是PSP是用户权限,MSP是系统权限。
      image.png
    8. Isb 该指令是同步屏障,清除流水线并且确保在新指令执行时,之前的指令都已经执行完毕。
    9. pop { r0 - r5 } PSP地址0x180004A0中的地址pop出来,就是把自定义构造为r0含义的内容通过栈的pop方式出栈到当前r0r5寄存器。这样的意思就是自己造了一个栈,现在开始出栈咯
      image.png
    10. mov lr, r5 把r5的值保存到lr(R14寄存器)
      image.png
      11.pop { r3 }Stack内容继续pop,这里pop的目的地址是r3寄存器,r3的值变成0x110011A9
      image.png
    11. pop { r2 }同上继续pop 0x01000000到r2
      image.png
    12. cpsie i使能全局中断。
    13. bx r3跳转到r3地址开始执行。等于切换到新的函数去运行咯~
      image.png
      至于为什么r3是PC的值,再对照下面的注释,初始化时候pxCode就是PC,下面的注释从下向上看,就是pop到寄存器的值。一开始含义为自定义栈r0的地址放入PSP栈开始执行pop。先pop r0~r5,所以r5就是LR,然后pop r3,r3就是PC,最后pop r2,r2就是xPSR。这样图解分析简单吧!
        pxTopOfStack--;                                   /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */
        *pxTopOfStack = portINITIAL_XPSR;                 /* xPSR */
        pxTopOfStack--;
        *pxTopOfStack = ( StackType_t ) pxCode;           /* PC */
        pxTopOfStack--;
        *pxTopOfStack = ( StackType_t ) prvTaskExitError; /* LR */
        pxTopOfStack -= 5;                                /* R12, R3, R2 and R1. */
        *pxTopOfStack = ( StackType_t ) pvParameters;     /* R0 */
        pxTopOfStack -= 8;                                /* R11..R4. */
    
        return pxTopOfStack;
    

    四,小结

    这2天除了复习下FreeRTOS关于os跳入第一个执行任务的源码,还把FreeRTOS heap2.c内存动态分配的源码都复习了一遍。感觉很棒,又都想起来了,哈哈~

    相关文章

      网友评论

        本文标题:FreeRTOS的第一个任务跳转源码分析—Apple的学习笔记

        本文链接:https://www.haomeiwen.com/subject/dqwjlltx.html