之前的文章学习了ARM函数调用和返回时的操作,但是对于操作系统下的任务堆栈以及任务切换时堆栈的切换还不太了解,因此,首先分析了一下任务的源码,包括创建任务时,创建堆栈的过程,以及任务调度过程。后来,发现这个分析清楚了,就可以把程序堆栈和任务堆栈也梳理清楚,于是,就继续梳理一下程序堆栈和任务堆栈的关系。
以STM32F4x7_ETH_LwIP_V1.1.1工程为例,使用的版本是FreeRTOSV7.3.0。
STM32F4x7_ETH_LwIP_V1.1.1\Project\FreeRTOS\udptcp_echo_server_netconn\src\main.c中启动任务如下
1 int main(void) 2 { 3 /* Configures the priority grouping: 4 bits pre-emption priority */ 4 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); 5 6 /* Init task */ 7 xTaskCreate(Main_task, (int8_t *)"Main", configMINIMAL_STACK_SIZE * 2, NULL,MAIN_TASK_PRIO, NULL); 8 9 /* Start scheduler */ 10 vTaskStartScheduler(); 11 12 /* We should never get here as control is now taken by the scheduler */ 13 for( ;; ); 14 }
在main中一般都会启动一个主任务或者叫启动任务,然后,开始任务调度,在主任务中,完成其它任务的创建。(为什么要这种模式呢?直接在main中创建所有任务,然后,开始任务调度不可以吗?)
任务控制块TCB,首个成员是任务堆栈顶部地址,第17行表示任务堆栈起始(堆栈像一个桶,桶底是高地址,桶上面是低地址,桶底部为“任务堆栈起始”pxStack,桶里的最后一个数据位置为“任务堆栈顶部地址”pxTopOfStack)。
xGenericListItem是用于将任务串成列表的列表成员,后续该任务加入就绪任务列表还是其他任务列表,都是将该列表成员插入进任务列表。
xEventListItem用于记录该任务是否在等待事件,比如是否向队列发送数据但队列已满、是否从队列读取数据但队列是空的,且设置了等待时间或无限等待。例如,若是向队列发送数据但队列已满,则该任务的xEventListItem会插入该队列的xTasksWaitingToSend列表中;同时将xGenericListItem从就绪任务列表删除,插入到挂起任务队列(若等待时间是无限)或延时任务队列(若等待时间是有限)(该过程由vTaskPlaceOnEventList完成)。若是队列非满了,则会将任务的xEventListItem从xTasksWaitingToSend中移除;同时,将任务的xGenericListItem从挂起任务队列或延时任务队列中移除,并添加到就绪队列中(该过程由xTaskRemoveFromEventList完成)。
1 /* 2 * Task control block. A task control block (TCB) is allocated for each task, 3 * and stores task state information, including a pointer to the task's context 4 * (the task's run time environment, including register values) 5 */ 6 typedef struct tskTaskControlBlock 7 { 8 volatile portSTACK_TYPE *pxTopOfStack; /*< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */ 9 10 #if ( portUSING_MPU_WRAPPERS == 1 ) 11 xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */ 12 #endif 13 14 xListItem xGenericListItem; /*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */ 15 xListItem xEventListItem; /*< Used to reference a task from an event list. */ 16 unsigned portBASE_TYPE uxPriority; /*< The priority of the task. 0 is the lowest priority. */ 17 portSTACK_TYPE *pxStack; /*< Points to the start of the stack. */ 18 signed char pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created. Facilitates debugging only. */ 19 20 #if ( portSTACK_GROWTH > 0 ) 21 portSTACK_TYPE *pxEndOfStack; /*< Points to the end of the stack on architectures where the stack grows up from low memory. */ 22 #endif 23 24 #if ( portCRITICAL_NESTING_IN_TCB == 1 ) 25 unsigned portBASE_TYPE uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */ 26 #endif 27 28 #if ( configUSE_TRACE_FACILITY == 1 ) 29 unsigned portBASE_TYPE 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. */ 30 unsigned portBASE_TYPE uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */ 31 #endif 32 33 #if ( configUSE_MUTEXES == 1 ) 34 unsigned portBASE_TYPE uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */ 35 #endif 36 37 #if ( configUSE_APPLICATION_TASK_TAG == 1 ) 38 pdTASK_HOOK_CODE pxTaskTag; 39 #endif 40 41 #if ( configGENERATE_RUN_TIME_STATS == 1 ) 42 unsigned long ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */ 43 #endif 44 45 } tskTCB;
下面看任务创建函数,xTaskCreate实际调用的是xTaskGenericCreate
E:\project\rtos\STM32F4x7_ETH_LwIP_V1.1.1\Utilities\Third_Party\FreeRTOSV7.3.0\include\task.h
1 #define xTaskCreate( pvTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask ) xTaskGenericCreate( ( pvTaskCode ), ( pcName ), ( usStackDepth ), ( pvParameters ), ( uxPriority ), ( pxCreatedTask ), ( NULL ), ( NULL ) )
E:\project\rtos\STM32F4x7_ETH_LwIP_V1.1.1\Utilities\Third_Party\FreeRTOSV7.3.0\tasks.c,486
1 signed portBASE_TYPE xTaskGenericCreate( pdTASK_CODE pxTaskCode, const signed char * const pcName, unsigned short usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pxCreatedTask, portSTACK_TYPE *puxStackBuffer, const xMemoryRegion * const xRegions ) 2 { 3 signed portBASE_TYPE xReturn; 4 tskTCB * pxNewTCB; 5 6 configASSERT( pxTaskCode ); 7 configASSERT( ( ( uxPriority & ( ~portPRIVILEGE_BIT ) ) < configMAX_PRIORITIES ) ); 8 9 /* Allocate the memory required by the TCB and stack for the new task, 10 checking that the allocation was successful. */ 11 pxNewTCB = prvAllocateTCBAndStack( usStackDepth, puxStackBuffer ); 12 13 if( pxNewTCB != NULL ) 14 { 15 portSTACK_TYPE *pxTopOfStack; 16 17 #if( portUSING_MPU_WRAPPERS == 1 ) 18 /* Should the task be created in privileged mode? */ 19 portBASE_TYPE xRunPrivileged; 20 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U ) 21 { 22 xRunPrivileged = pdTRUE; 23 } 24 else 25 { 26 xRunPrivileged = pdFALSE; 27 } 28 uxPriority &= ~portPRIVILEGE_BIT; 29 #endif /* portUSING_MPU_WRAPPERS == 1 */ 30 31 /* Calculate the top of stack address. This depends on whether the 32 stack grows from high memory to low (as per the 80x86) or visa versa. 33 portSTACK_GROWTH is used to make the result positive or negative as 34 required by the port. */ 35 #if( portSTACK_GROWTH < 0 ) 36 { 37 pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth - ( unsigned short ) 1 ); 38 pxTopOfStack = ( portSTACK_TYPE * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ( portPOINTER_SIZE_TYPE ) ~portBYTE_ALIGNMENT_MASK ) ); 39 40 /* Check the alignment of the calculated top of stack is correct. */ 41 configASSERT( ( ( ( unsigned long ) pxTopOfStack & ( unsigned long ) portBYTE_ALIGNMENT_MASK ) == 0UL ) ); 42 } 43 #else 44 { 45 pxTopOfStack = pxNewTCB->pxStack; 46 47 /* Check the alignment of the stack buffer is correct. */ 48 configASSERT( ( ( ( unsigned long ) pxNewTCB->pxStack & ( unsigned long ) portBYTE_ALIGNMENT_MASK ) == 0UL ) ); 49 50 /* If we want to use stack checking on architectures that use 51 a positive stack growth direction then we also need to store the 52 other extreme of the stack space. */ 53 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( usStackDepth - 1 ); 54 } 55 #endif 56 57 /* Setup the newly allocated TCB with the initial state of the task. */ 58 prvInitialiseTCBVariables( pxNewTCB, pcName, uxPriority, xRegions, usStackDepth ); 59 60 /* Initialize the TCB stack to look as if the task was already running, 61 but had been interrupted by the scheduler. The return address is set 62 to the start of the task function. Once the stack has been initialised 63 the top of stack variable is updated. */ 64 #if( portUSING_MPU_WRAPPERS == 1 ) 65 { 66 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged ); 67 } 68 #else 69 { 70 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters ); 71 } 72 #endif 73 74 /* Check the alignment of the initialised stack. */ 75 portALIGNMENT_ASSERT_pxCurrentTCB( ( ( ( unsigned long ) pxNewTCB->pxTopOfStack & ( unsigned long ) portBYTE_ALIGNMENT_MASK ) == 0UL ) ); 76 77 if( ( void * ) pxCreatedTask != NULL ) 78 { 79 /* Pass the TCB out - in an anonymous way. The calling function/ 80 task can use this as a handle to delete the task later if 81 required.*/ 82 *pxCreatedTask = ( xTaskHandle ) pxNewTCB; 83 } 84 85 /* We are going to manipulate the task queues to add this task to a 86 ready list, so must make sure no interrupts occur. */ 87 taskENTER_CRITICAL(); 88 { 89 uxCurrentNumberOfTasks++; 90 if( pxCurrentTCB == NULL ) 91 { 92 /* There are no other tasks, or all the other tasks are in 93 the suspended state - make this the current task. */ 94 pxCurrentTCB = pxNewTCB; 95 96 if( uxCurrentNumberOfTasks == ( unsigned portBASE_TYPE ) 1 ) 97 { 98 /* This is the first task to be created so do the preliminary 99 initialisation required. We will not recover if this call 100 fails, but we will report the failure. */ 101 prvInitialiseTaskLists(); 102 } 103 } 104 else 105 { 106 /* If the scheduler is not already running, make this task the 107 current task if it is the highest priority task to be created 108 so far. */ 109 if( xSchedulerRunning == pdFALSE ) 110 { 111 if( pxCurrentTCB->uxPriority <= uxPriority ) 112 { 113 pxCurrentTCB = pxNewTCB; 114 } 115 } 116 } 117 118 /* Remember the top priority to make context switching faster. Use 119 the priority in pxNewTCB as this has been capped to a valid value. */ 120 if( pxNewTCB->uxPriority > uxTopUsedPriority ) 121 { 122 uxTopUsedPriority = pxNewTCB->uxPriority; 123 } 124 125 #if ( configUSE_TRACE_FACILITY == 1 ) 126 { 127 /* Add a counter into the TCB for tracing only. */ 128 pxNewTCB->uxTCBNumber = uxTaskNumber; 129 } 130 #endif 131 uxTaskNumber++; 132 133 prvAddTaskToReadyQueue( pxNewTCB ); 134 135 xReturn = pdPASS; 136 portSETUP_TCB( pxNewTCB ); 137 traceTASK_CREATE( pxNewTCB ); 138 } 139 taskEXIT_CRITICAL(); 140 } 141 else 142 { 143 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY; 144 traceTASK_CREATE_FAILED(); 145 } 146 147 if( xReturn == pdPASS ) 148 { 149 if( xSchedulerRunning != pdFALSE ) 150 { 151 /* If the created task is of a higher priority than the current task 152 then it should run now. */ 153 if( pxCurrentTCB->uxPriority < uxPriority ) 154 { 155 portYIELD_WITHIN_API(); 156 } 157 } 158 } 159 160 return xReturn; 161 }