之前的文章学习了ARM函数调用和返回时的操作,但是对于操作系统下的任务堆栈以及任务切换时堆栈的切换还不太了解,因此,首先分析了一下任务的源码,包括创建任务时,创建堆栈的过程,以及任务调度过程。后来,发现这个分析清楚了,就可以把程序堆栈和任务堆栈也梳理清楚,于是,就继续梳理一下程序堆栈和任务堆栈的关系。
以STM32F4x7_ETH_LwIP_V1.1.1工程为例,使用的版本是FreeRTOSV7.3.0。
STM32F4x7_ETH_LwIP_V1.1.1ProjectFreeRTOSudptcp_echo_server_netconnsrcmain.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:projectrtosSTM32F4x7_ETH_LwIP_V1.1.1UtilitiesThird_PartyFreeRTOSV7.3.0includetask.h
1 #define xTaskCreate( pvTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask ) xTaskGenericCreate( ( pvTaskCode ), ( pcName ), ( usStackDepth ), ( pvParameters ), ( uxPriority ), ( pxCreatedTask ), ( NULL ), ( NULL ) )
E:projectrtosSTM32F4x7_ETH_LwIP_V1.1.1UtilitiesThird_PartyFreeRTOSV7.3.0tasks.c,486
View Code
分配TCB和stack空间
1 /* Allocate the memory required by the TCB and stack for the new task,
2 checking that the allocation was successful. */
3 pxNewTCB = prvAllocateTCBAndStack( usStackDepth, puxStackBuffer );
第9行先分配TCB的空间,11行,TCB分配成功,再分配堆栈空间。第16行,分配输入参数*4个字节的堆栈,赋值给“任务堆栈起始”pxStack。如果分配成功,那么27行,会初始化堆栈为填充图案,这里为0xa5。
1 /*-----------------------------------------------------------*/
2
3 static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TYPE *puxStackBuffer )
4 {
5 tskTCB *pxNewTCB;
6
7 /* Allocate space for the TCB. Where the memory comes from depends on
8 the implementation of the port malloc function. */
9 pxNewTCB = ( tskTCB * ) pvPortMalloc( sizeof( tskTCB ) );
10
11 if( pxNewTCB != NULL )
12 {
13 /* Allocate space for the stack used by the task being created.
14 The base of the stack memory stored in the TCB so the task can
15 be deleted later if required. */
16 pxNewTCB->pxStack = ( portSTACK_TYPE * ) pvPortMallocAligned( ( ( ( size_t )usStackDepth ) * sizeof( portSTACK_TYPE ) ), puxStackBuffer );
17
18 if( pxNewTCB->pxStack == NULL )
19 {
20 /* Could not allocate the stack. Delete the allocated TCB. */
21 vPortFree( pxNewTCB );
22 pxNewTCB = NULL;
23 }
24 else
25 {
26 /* Just to help debugging. */
27 memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) usStackDepth * sizeof( portSTACK_TYPE ) );
28 }
29 }
30
31 return pxNewTCB;
32 }
task.c, 204,定义的堆栈填充图案为a5,仅用于检测任务的高地址水印。
1 /*
2 * The value used to fill the stack of a task when the task is created. This
3 * is used purely for checking the high water mark for tasks.
4 */
5 #define tskSTACK_FILL_BYTE ( 0xa5U )
计算任务堆栈顶部指针
第5行,会根据堆栈生成方向来分别计算,对于arm堆栈是向下生长的,分配的pxStack是低地址,因此,第7行,栈顶就是pxStack+深度-1(-1是因为是full stack,堆栈指针指向最后一个数据)。第8行会进行一下对齐,因为ARM是4字节对齐,因此,该句不会改变地址。
1 /* Calculate the top of stack address. This depends on whether the
2 stack grows from high memory to low (as per the 80x86) or visa versa.
3 portSTACK_GROWTH is used to make the result positive or negative as
4 required by the port. */
5 #if( portSTACK_GROWTH < 0 )
6 {
7 pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth - ( unsigned short ) 1 );
8 pxTopOfStack = ( portSTACK_TYPE * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ( portPOINTER_SIZE_TYPE ) ~portBYTE_ALIGNMENT_MASK ) );
9
10 /* Check the alignment of the calculated top of stack is correct. */
11 configASSERT( ( ( ( unsigned long ) pxTopOfStack & ( unsigned long ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
12 }
初始化TCB变量
prvInitialiseTCBVariables主要给TCB的变量赋值。重点关注以下几个地方,第3、4行,初始化两个链表的成员,第8、12行设置两个链表的拥有者为TCB(拥有者Owner一般为包含该链表成员的结构体对象),第11行设置xEventListItem的链表成员数值为优先级补数,事件链表永远按优先级排序。
1 static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned short usStackDepth )
2 {
3 vListInitialiseItem( &( pxTCB->xGenericListItem ) );
上一篇:摄像头驱动学习
下一篇:ARM处理器基础Cortex-M4
推荐阅读最新更新时间:2024-11-13 20:27
设计资源 培训 开发板 精华推荐
- SG3524 稳压脉宽调制器的典型推挽变压器耦合电路
- AD7262-5、500 Ksps、12 位、同步采样 SAR ADC 的典型应用,采用 PGA 和四个比较器的引脚驱动模式
- NCV33074DR2G高速低压比较器典型应用
- 使用 L5970D/L5973D 6 至 12 VDC 输入 1W LED 驱动器的高强度应用电路
- 双输出低压差稳压器的典型应用
- labview汽车仪表程序
- 使用 Analog Devices 的 LTC1596-1CCSW 的参考设计
- 从USB micro-B到Type-C的快速轻松迁移
- LTC3631EMS8E 小尺寸、有限峰值电流、20mA 稳压器的典型应用电路
- 1.3寸Arduboy竖版