UCOS Learning (3) - Basics of Task Management


  • ⏩ Hello everyone! I'm Xiaoguang, an embedded enthusiast, a sophomore who wants to become a system architect.
  • ⏩I recently started to learn the UCOS operating system, and I will update some UCOS study notes later.
  • ⏩ Learned task management basics today.
  • ⏩Thank you for reading, please correct me if I am wrong.

一.delay

In delay_init(), we need to know SysTick->LOAD=reload; //每1/delay_ostickspersec秒中断一次that when we find the definition of delay_ostickspersec, we find that it is 1000u;
that is to say, the reload is 1/1000us=1ms; this is the period of OS system interruption; it has an impact
on the delay function in the OS system delay_ms(): that is Let us execute the ordinary delay function when the delay is less than the period of the system interrupt. It should be noted that this is also because we have adjusted the delay period of the system to 1ms, so there is no need to worry.

//延时nms
//nms:要延时的ms数
void delay_ms(u16 nms)
{
    
    	
	if(delay_osrunning&&delay_osintnesting==0)	//如果OS已经在跑了,并且不是在中断里面(中断里面不能任务调度)	    
	{
    
    		 
		if(nms>=fac_ms)							//延时的时间大于OS的最少时间周期 
		{
    
     
   			delay_ostimedly(nms/fac_ms);		//OS延时
		}
		nms%=fac_ms;							//OS已经无法提供这么小的延时了,采用普通方式延时    
	}
	delay_us((u32)(nms*1000));					//普通方式延时  
}
void delay_init()
{
    
    
#if SYSTEM_SUPPORT_OS  							//如果需要支持OS.
	u32 reload;
#endif
	SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);	//选择外部时钟  HCLK/8
	fac_us=SystemCoreClock/8000000;				//为系统时钟的1/8  
#if SYSTEM_SUPPORT_OS  							//如果需要支持OS.
	reload=SystemCoreClock/8000000;				//每秒钟的计数次数 单位为M  
	reload*=1000000/delay_ostickspersec;		//根据delay_ostickspersec设定溢出时间
												//reload为24位寄存器,最大值:16777216,在72M下,约合1.86s左右	
	fac_ms=1000/delay_ostickspersec;			//代表OS可以延时的最少单位	   

	SysTick->CTRL|=SysTick_CTRL_TICKINT_Msk;   	//开启SYSTICK中断
	SysTick->LOAD=reload; 						//每1/delay_ostickspersec秒中断一次	
	SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk;   	//开启SYSTICK    

#else
	fac_ms=(u16)fac_us*1000;					//非OS下,代表每个ms需要的systick时钟数   
#endif
}						

task management

UCOS system tasks:

  1. Idle task: the first task created by UCOSIII, the task that must be created by UCOSIII, this task is automatically created by UCOSIII.
  2. Clock beat task: This task is also a task that must be created.
  3. Statistical task: an optional task, used to count the CPU usage and the stack usage of each task. This task is an optional task, controlled by the macro OS_CFG_STAT_TASK_EN whether to use this task.
  4. Timed task: It is used to provide timers to users, and it is also an optional task. Whether to use this task is controlled by the macro OS_CFG_TMR_EN.
  5. Interrupt service management task: optional task, whether to use this task is controlled by OS_CFG_ISR_POST_DEFERRED_EN.

task status

  1. Dormant state: The task is already in the flash of the CPU, but it is not yet managed by UCOSIII.
  2. Ready state: The system assigns a task control block to the task, and the task has been registered in the ready list, and the task has the conditions to run, and the state of the task at this time is the ready state.
  3. Running state: The task has obtained the right to use the CPU and is running.
  4. Waiting state: The running task needs to wait for a period of time, or wait for an event, and the task enters the waiting state, and the system will transfer the CPU usage right to other tasks.
  5. Interrupt service state: When an interrupt is sent, the currently running task will be suspended, and the CPU will turn to execute the interrupt service function. The task state at this time is called the interrupt service state.

Task

1. Task stack

The task stack is used to save context when switching tasks and calling other functions.
The creation process is:

#define START_STK_SIZE 		512         //堆栈大小
CPU_STK START_TASK_STK[START_STK_SIZE]; //任务堆栈

2. Mission control block

The task control block is a data structure (structure) used to record task-related information, and its member variables cannot be directly accessed.

OS_TCB StartTaskTCB;//任务控制块

3. Priority

In the function os_cfg.h we can find:

#define OS_CFG_PRIO_MAX                64u

The task priority can be set up to 64-1, which can be changed by yourself, 数值越大,优先级越小.

4. Task Ready Form

UCOSIII puts ready tasks in the task ready table. The task ready table has two parts: priority bit mapping table OSPrioTbl[]: used to record which priority has task ready and ready task list OSRdyList[]: used to record all tasks under each priority ready task.
Bit-Priority Map:
insert image description here
We OS_PRIO OS_PrioGetHighest (void)can use this to find the highest priority task that is ready.

Summarize

We know what relevant information a task has, and then the use of task-related functions.

Guess you like

Origin blog.csdn.net/qq_52608074/article/details/122383195