FreeRTOS之vTaskDelay(const TickType_t xTicksToDelay)函数

vTaskDelay(const TickType_t xTickToDelay) 

相对延时函数:考虑下列任务函数

void TaskA(void)
{
    const portTickType delay_c =  pdMS_TO_TICKS(500) /*将ms转化成节拍数*/
    for(::)
    {

        //函数主体.....
        .............
        ......
        /*调用相对延时函数,阻塞500ms*/
        
        /*当任务A被执行,调用延时函数时,开始延时计数,任务A进入阻塞状态
        cpu执行其他任务,当系统检查到任务A阻塞结束后,又重新执行任务A(假
        设任务A有最高优先级),任务A每次阻塞的时间是从调用vTaskDelay()函
        数时算起,是一个相对延时,所以叫相对延时函数。
        任务A被循环执行,但这个周期不是固定周期,如果执行任务A的过程中有
        产生中断,那么任务A的执行周期就会被延长,所以调用相对延时函数,无
        法周期性执行任务*/

        vTaskDelay(delay_c);

    }

}


vTaskDelay(const TickType_t xTicksToDelay)函数原型:

void vTaskDelay( const TickType_t xTicksToDelay )
	{
	BaseType_t xAlreadyYielded = pdFALSE;

		/* A delay time of zero just forces a reschedule. */
                /*延时时间为0时,不会将当前任务加入延时列表*/
		if( xTicksToDelay > ( TickType_t ) 0U )
		{

                        /*断言,为0时进入断言,程序会先关闭中断,然后进入死循环*/
			configASSERT( uxSchedulerSuspended == 0 );
                        /*禁止调度*/
			vTaskSuspendAll();
			{
				traceTASK_DELAY();

				/* A task that is removed from the event list while the
				scheduler is suspended will not get placed in the ready
				list or removed from the blocked list until the scheduler
				is resumed.

				This task cannot be in an event list as it is the 
                                currently executing task. */
                
                
				prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
			}
                        /*恢复调度*/
			xAlreadyYielded = xTaskResumeAll();
		}
		else
		{
			mtCOVERAGE_TEST_MARKER();
		}

		/* Force a reschedule if xTaskResumeAll has not already done so, we may
		have put ourselves to sleep. */
		if( xAlreadyYielded == pdFALSE )
		{
			portYIELD_WITHIN_API();
		}
		else
		{
			mtCOVERAGE_TEST_MARKER();
		}
	}

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(FreeRTOS)