目录

  • 1.延时函数-介绍
  • 2.相对延时函数-解析
    • 2.1函数`prvAddCurrentTaskToDelayedList`-解析
    • 2.3滴答定时器中断服务函数`xPortSysTickHandler()`-解析
    • 2.4函数`taskSWITCH_DELAYED_LISTS() `-解析
  • 3.延时函数-实验
  • 4.总结

1.延时函数-介绍

函数 描述
vTaskDelay() 相对延时
xTaskDelayUntil() 绝对延时

相对延时:指每次延时都是从执行函数vTaskDelay()开始,直到延时指定的时间结束(任务被阻塞的时间,到调用此函数开始的时间);
绝对延时:指将整个任务的运行周期看成一个整体,适用于需要按照一定频率运行的任务(整个任务执行的时间,从头到尾的时间)。


上图中的xTimeIncrement为绝对延时时间(假如绝对延时时间为100ms,那么以下三部分之和为100ms),包括以下三部分:
(1)为任务主体,也就是任务真正要做的工作;
(2)是任务函数中调用vTaskDelayUntil()对任务进行延时;
(3)为其他任务在运行(高优先级的任务进行抢占)。

2.相对延时函数-解析

首先入口参数必须大于0,延时时间有效。

void vTaskDelay( const TickType_t xTicksToDelay ){BaseType_t xAlreadyYielded = pdFALSE;/* A delay time of zero just forces a reschedule. */if( xTicksToDelay > ( TickType_t ) 0U )

vTaskSuspendAll()挂起任务调度器,traceTASK_DELAY()函数并没有被实现。 prvAddCurrentTaskToDelayedList(点击函数名可跳转至解析)
将当前正在执行的任务移到阻塞列表。

            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();

判断xAlreadyYielded 是否需要进行任务切换。

        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();}}#endif /* INCLUDE_vTaskDelay */

此函数是将任务挂载到阻塞列表,解除是在滴答定时器的中断服务函数 xPortSysTickHandler()(点击函数名可跳转至解析)中。

2.1函数prvAddCurrentTaskToDelayedList-解析

函数prvAddCurrentTaskToDelayedList()有两个入口参数一个是延时时间xTicksToWait,另一个是xCanBlockIndefinitely 等于pdFALSE。

static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,const BaseType_t xCanBlockIndefinitely )

xConstTickCount 存储时钟节拍,滴答定时器中断一次,变量xTickCount加1。宏INCLUDE_xTaskAbortDelay 判断是否是中断延时,这里并没有使用,所以不用管。

{TickType_t xTimeToWake;const TickType_t xConstTickCount = xTickCount;#if ( INCLUDE_xTaskAbortDelay == 1 ){/* About to enter a delayed list, so ensure the ucDelayAborted flag is* reset to pdFALSE so it can be detected as having been set to pdTRUE* when the task leaves the Blocked state. */pxCurrentTCB->ucDelayAborted = pdFALSE;}#endif

将当前正在执行的任务的状态列表项使用函数uxListRemove()从就绪列表中移除,移除完判断是否有同等优先级的任务,没有就代表只有这一个任务,被移除掉后就绪列表中剩余任务为0,那么将此优先级的任务优先级复位。

    /* Remove the task from the ready list before adding it to the blocked list* as the same list item is used for both lists. */if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 ){/* The current task must be in a ready list, so there is no need to* check, and the port reset macro can be called directly. */portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority ); /*lint !e931 pxCurrentTCB cannot change as it is the calling task.  pxCurrentTCB->uxPriority and uxTopReadyPriority cannot change as called with scheduler suspended or in a critical section. */}else{mtCOVERAGE_TEST_MARKER();}

宏INCLUDE_vTaskSuspend 判断是否使能挂起,判断延时时间xTicksToWait等于最大延时时间并且xCanBlockIndefinitely 不等于pdFALSE,此时将任务挂载到挂起列表中,由于传入参数为pdFALSE,所以不会挂载到挂起列表中,则执行else内容。

   #if ( INCLUDE_vTaskSuspend == 1 ){if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) ){/* Add the task to the suspended task list instead of a delayed task* list to ensure it is not woken by a timing event.  It will block* indefinitely. */listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );}

else中首先记录时间,xConstTickCount 为进入函数prvAddCurrentTaskToDelayedList()时记录的时间,加上延时时间xTicksToWait,就是任务到截止阻塞时间该被恢复的时间;通过函数listSET_LIST_ITEM_VALUE将延时时间写入到列表项值里,此值将用作挂载到阻塞列表时根据此值进行升序排列;

          else{/* Calculate the time at which the task should be woken if the event* does not occur.  This may overflow but this doesn't matter, the* kernel will manage it correctly. */xTimeToWake = xConstTickCount + xTicksToWait;/* The list item will be inserted in wake time order. */listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );

判断需要等待截止的时间是否小于进入函数prvAddCurrentTaskToDelayedList()时记录的时间,这里判断是否数值溢出,如果溢出就将任务挂载到溢出阻塞列表中,否则挂载到阻塞列表中。

if( xTimeToWake < xConstTickCount ){/* Wake time has overflowed.  Place this item in the overflow* list. */vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );}else{/* The wake time has not overflowed, so the current block list* is used. */vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );

判断下一个阻塞超时时间如果大于新的阻塞时间,那么将新的阻塞时间更新为下一个阻塞超时时间。例如下一个xNextTaskUnblockTime 超时时间为30ms,新的阻塞时间xTimeToWake为20ms,肯定是20ms的先来到,所以将下一个阻塞超时时间更新为20ms。

                    /* If the task entering the blocked state was placed at the* head of the list of blocked tasks then xNextTaskUnblockTime* needs to be updated too. */if( xTimeToWake < xNextTaskUnblockTime ){xNextTaskUnblockTime = xTimeToWake;}else{mtCOVERAGE_TEST_MARKER();}}}}

2.3滴答定时器中断服务函数xPortSysTickHandler()-解析

函数xTaskIncrementTick()的值如果不等于pdFALSE,则进行任务切换,触发PendSV中断。

void xPortSysTickHandler( void )
{/* The SysTick runs at the lowest interrupt priority, so when this interrupt* executes all interrupts must be unmasked.  There is therefore no need to* save and then restore the interrupt mask value as its value is already* known - therefore the slightly faster vPortRaiseBASEPRI() function is used* in place of portSET_INTERRUPT_MASK_FROM_ISR(). */vPortRaiseBASEPRI();{/* Increment the RTOS tick. */if( xTaskIncrementTick() != pdFALSE ){/* A context switch is required.  Context switching is performed in* the PendSV interrupt.  Pend the PendSV interrupt. */portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;}}vPortClearBASEPRIFromISR();
}

在函数xTaskIncrementTick()中判断任务是否需要被解除。首先判断任务调度器是否被挂起,如果等于pdFALSE 则没有被挂起,进入if内容。

BaseType_t xTaskIncrementTick( void )
{TCB_t * pxTCB;TickType_t xItemValue;BaseType_t xSwitchRequired = pdFALSE;/* Called by the portable layer each time a tick interrupt occurs.* Increments the tick then checks to see if the new tick value will cause any* tasks to be unblocked. */traceTASK_INCREMENT_TICK( xTickCount );if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )

将系统时钟节拍xTickCount加1,然后再将值赋给自己,没进来一次时钟节拍将自加1。

/* Minor optimisation.  The tick count cannot change in this* block. */const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;/* Increment the RTOS tick, switching the delayed and overflowed* delayed lists if it wraps to 0. */xTickCount = xConstTickCount;

判断xConstTickCount 是否为0,为0则值溢出,进入函数 taskSWITCH_DELAYED_LISTS()(点击函数名可跳转至解析)。

if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */{taskSWITCH_DELAYED_LISTS();}else{mtCOVERAGE_TEST_MARKER();}/* See if this tick has made a timeout expire.  Tasks are stored in* the  queue in the order of their wake time - meaning once one task* has been found whose block time has not expired there is no need to* look any further down the list. */

判断当前时钟节拍ConstTickCount 是否大于等于下一个阻塞超时时间。

       /* See if this tick has made a timeout expire.  Tasks are stored in* the  queue in the order of their wake time - meaning once one task* has been found whose block time has not expired there is no need to* look any further down the list. */if( xConstTickCount >= xNextTaskUnblockTime ){for( ; ; ){

判断阻塞列表中是否有任务,如果没有任务则没有需要被解除的任务,则将下一个阻塞超时时间xNextTaskUnblockTime设置为最大值。

              if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE ){/* The delayed list is empty.  Set xNextTaskUnblockTime* to the maximum possible value so it is extremely* unlikely that the* if( xTickCount >= xNextTaskUnblockTime ) test will pass* next time through. */xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */break;}

else则阻塞列表中有任务,通过函数listGET_OWNER_OF_HEAD_ENTRY()获取阻塞列表的第一个成员的任务控制块;通过函数listGET_LIST_ITEM_VALUE()获取列表项的数值,列表项中一般存放的是阻塞时间,则xItemValue被赋值阻塞时间。

               else{/* The delayed list is not empty, get the value of the* item at the head of the delayed list.  This is the time* at which the task at the head of the delayed list must* be removed from the Blocked state. */pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );

判断系统时间节拍的数值是否小于阻塞时间,代表此时发生异常。因为在首先进入if时判断了当前的系统时钟节拍比下一个阻塞超时时间大。此时将列表项的值赋值给下一个阻塞超时时间,退出。

if( xConstTickCount < xItemValue ){/* It is not time to unblock this item yet, but the* item value is the time at which the task at the head* of the blocked list must be removed from the Blocked* state -  so record the item value in* xNextTaskUnblockTime. */xNextTaskUnblockTime = xItemValue;break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */}else{mtCOVERAGE_TEST_MARKER();}

下面的情况为正常执行的情况。使用函数listREMOVE_ITEM()将任务从阻塞列表中移除,同时也从使用函数listREMOVE_ITEM从事件列表中移除。

                    /* It is time to remove the item from the Blocked state. */listREMOVE_ITEM( &( pxTCB->xStateListItem ) );/* Is the task waiting on an event also?  If so remove* it from the event list. */if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL ){listREMOVE_ITEM( &( pxTCB->xEventListItem ) );}else{mtCOVERAGE_TEST_MARKER();}

使用函数prvAddTaskToReadyList()将任务添加到就绪列表中,

                    /* Place the unblocked task into the appropriate ready* list. */prvAddTaskToReadyList( pxTCB );

判断宏configUSE_PREEMPTION是否使能抢占式任务调度,是则判断恢复的任务的任务优先级是否比当前正在执行的任务优先级高,是则将任务切换xSwitchRequired变量赋值pdTRUE。

                    /* A task being unblocked cannot cause an immediate* context switch if preemption is turned off. */#if ( configUSE_PREEMPTION == 1 ){/* Preemption is on, but a context switch should* only be performed if the unblocked task has a* priority that is equal to or higher than the* currently executing task. */if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority ){xSwitchRequired = pdTRUE;}else{mtCOVERAGE_TEST_MARKER();}}#endif /* configUSE_PREEMPTION */}}}

以下程序是时间片调度:

        /* Tasks of equal priority to the currently running task will share* processing time (time slice) if preemption is on, and the application* writer has not explicitly turned time slicing off. */#if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ){if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > ( UBaseType_t ) 1 ){xSwitchRequired = pdTRUE;}else{mtCOVERAGE_TEST_MARKER();}}#endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */#if ( configUSE_TICK_HOOK == 1 ){/* Guard against the tick hook being called when the pended tick* count is being unwound (when the scheduler is being unlocked). */if( xPendedTicks == ( TickType_t ) 0 ){vApplicationTickHook();}else{mtCOVERAGE_TEST_MARKER();}}#endif /* configUSE_TICK_HOOK */#if ( configUSE_PREEMPTION == 1 ){if( xYieldPending != pdFALSE ){xSwitchRequired = pdTRUE;}else{mtCOVERAGE_TEST_MARKER();}}#endif /* configUSE_PREEMPTION */}else{++xPendedTicks;/* The tick hook gets called at regular intervals, even if the* scheduler is locked. */#if ( configUSE_TICK_HOOK == 1 ){vApplicationTickHook();}#endif}return xSwitchRequired;
}

2.4函数taskSWITCH_DELAYED_LISTS() -解析

只溢出之后,将就绪列表pxDelayedTaskList和溢出就绪列表pxOverflowDelayedTaskList进行互换。

/* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick* count overflows. */
#define taskSWITCH_DELAYED_LISTS()                                                \{                                                                             \List_t * pxTemp;                                                          \\/* The delayed tasks list should be empty when the lists are switched. */ \configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) );               \\pxTemp = pxDelayedTaskList;                                               \pxDelayedTaskList = pxOverflowDelayedTaskList;                            \pxOverflowDelayedTaskList = pxTemp;                                       \xNumOfOverflows++;                                                        \prvResetNextTaskUnblockTime();                                            \}

3.延时函数-实验

1、实验目的:学习 FreeRTOS 相对延时和绝对延时API 函数的使用,并了解其区别
2、实验设计:将设计三个任务:start_task、task1,task2 三个任务的功能如下:
start_task:用来创建task1和task2任务 task1用于展示相对延时函数vTaskDelay ( )的使用;
task1:用于展示相对延时函数vTaskDelay ( )的使用;
task2:用于展示绝对延时函数vTaskDelayUntil( )的使用 。
为了直观显示两个延时函数的区别,将使用LED0(PB1) 和LED1(PB0) 的翻转波形来表示

1.首先删除无关的程序内容,task1和task2程序如下,其他程序保持不变。

/* 任务1,用于展示相对延时函数vTaskDelay ( )的使用 */
void task1( void * pvParameters )
{while(1){LED0=~LED0;vTaskDelay(500);}
}
/* 任务2,用于展示绝对延时函数vTaskDelayUntil( )的使用 */
void task2( void * pvParameters )
{while(1){vTaskDelay(10);}
}

2.task1中的本身就是使用的相对于延时,相对于调用的时候起,到延时时间结束,所以不用改变;task2在使用绝对延时函数vTaskDelayUntil()时,可以查看FreeRTOS官网对该函数的使用介绍,根据示例来编写函数使用,绝对延时是整个task2运行的时间。

由于LED翻转语句执行较快基本看不到差距,所以这里加了死延时。

/* 任务1,用于展示相对延时函数vTaskDelay ( )的使用 */
void task1( void * pvParameters )
{while(1){LED0=~LED0;delay_ms(20);vTaskDelay(500);}
}
/* 任务2,用于展示绝对延时函数vTaskDelayUntil( )的使用 */
void task2( void * pvParameters )
{TickType_t xLastWakeTime;xLastWakeTime = xTaskGetTickCount(); /* 获取当前的系统时钟节拍 */while(1)   {LED1=~LED1;delay_ms(20);vTaskDelayUntil(&xLastWakeTime,500);}
}

(这里由于手头没有示波器,我并没有做出实验结果,理论结果如下。)
理论实验结果
由于task1和task2都加了死延时20ms,LED0翻转周期为520ms左右,而LED1的翻转周期仍为500ms。由于task2优先级比task1要高,task2会抢占task1,所以在执行时,会出现task1处于阻塞延时时结束时,task2处于死延时,此时并不能进行任务切换,task1会等到task2死延时结束进入阻塞延时再运行,LED0的亮灭时间会有所变化,LED0亮灭时间会变长,LED1的亮灭周期为500ms保持不变。将task1的优先级变高,将会影响task2的绝对延时时间。

4.总结

【11】FreeRTOS的延时函数相关推荐

  1. FreeRTOS任务延时函数

    系统提供了两个任务延时函数:相对延时函数vTaskDelay()和绝对延时函数vTaskDelayUntil() 相对延时是指:vTaskDelay()开始执行到退出执行的时间固定 /* 相对延时函数 ...

  2. FreeRTOS延时函数

    vTaskDelay() 相对延时函数 vTaskDelay()延时固定数量的tick中断,将调用任务置于阻塞状态.(vTaskDelay() 函数只有在宏 INCLUDE_vTaskDelay 置1 ...

  3. FreeRTOS一天一个小知识之任务延时函数vTaskDelay

    想必各位嵌入式工程师对于Delay延时函数再也熟悉不过了~ 但对于各位刚入RTOS的小白来说,有操作系统的延时函数,真的和裸机中的延时函数一样吗?FreeRTOS的任务调度是怎么调度的?如何分配系统的 ...

  4. 嵌入式开发(7)系统定时器(SysTick)之延时函数运用

    目录 一.系统定时器 1. 简介 2.工作原理 3.频率的概念 二.库函数SysTick定时器操作 系统定时器配置 三.寄存器SysTick定时器操作 1.系统定时器的用途 2.寄存器 3.官方示例 ...

  5. stm32延时us寄存器_STM32延时函数的四种方法

    关注.星标公众号,不错过精彩内容 单片机编程过程中经常用到延时函数,最常用的莫过于微秒级延时delay_us()和毫秒级delay_ms().本文基于STM32F207介绍4种不同方式实现的延时函数. ...

  6. iar stm32_STM32延时函数的四种方法

    关注.星标公众号,不错过精彩内容 单片机编程过程中经常用到延时函数,最常用的莫过于微秒级延时delay_us()和毫秒级delay_ms().本文基于STM32F207介绍4种不同方式实现的延时函数. ...

  7. c语言延时函数delay延时一秒_IMX6UL裸机实现C语言LED实验

    C 语言部分有两个文件 main.c 和 main.h,main.h 里面主要是定义的寄存器地址,在 main.h里面输入代码: #ifndef MAIN_H #define MAIN_H1 /*2 ...

  8. 用C51编写单片机延时函数

    参考了51单片机 Keil C 延时程序的简单研究,自己也亲身测试和计算了一些已有的延时函数. 这里假定单片机是时钟频率为12MHz,则一个机器周期为:1us. 参考了51单片机 Keil C 延时程 ...

  9. STC51几种简单的延时函数

    STC51几种简单的延时函数 1,* 延时子程序 * * * ********************************************************************* ...

最新文章

  1. matlab安装教程补充
  2. MacOS下保护浏览器主页和默认搜索
  3. android 键盘的隐藏与显示
  4. 四则运算2的单元测试
  5. ssh备份linux系统,远程自动备份 rsync+ssh+sshpass
  6. 小学生机器人挑战赛_厉害了我的小学生!德阳中小学生机器人大赛
  7. 密码编码学与网络安全-------原理与实践(第七版)
  8. 免费图片验证码识别方法,支持旋转图片识别
  9. Filter为什么会在一次请求执行多次doFilter?
  10. 安格最近推出的AG6202来设计一款HDMI 1.4转VGA的产品|AG6202设计应用
  11. 主成分分析、因子分析及其有关的数学基础
  12. IT外企那点儿事(18): 当“we are a team”成为口头禅
  13. Python+Vue计算机毕业设计龙一公益网站的设计与实现ngash(源码+程序+LW+部署)
  14. 仿QQ弹出窗口[转]
  15. 登录用友显示java已被阻止_解决Spring Security 用户帐号已被锁定问题
  16. android仿音乐播放器,Android仿音乐播放器功能
  17. 甲骨文的转型路,道阻且长
  18. 手板是什么?产品设计为什么要做手板?干货满满!!!
  19. 启英泰伦推出离在线语音方案,断网、联网都能实现语音交互
  20. Unicode和UCS

热门文章

  1. DataV 3D 平面地图 2.0 焕新上线
  2. SudaMod-81.0 / crDroidAndroid-8.1(android-8.1.0_r20)红米3 2018年5月3日更新
  3. 刘津:职业生涯的前一半是靠努力,后一半是靠创新
  4. 1-系统C盘迁移到新买的固态硬盘SSD中有影响但不主要,但有必要将系统盘C盘迁移到新安装的 固态硬盘SSD中吗?
  5. 企业邮局和邮件服务器
  6. 74ls163是同步清零吗_74LS163是具有同步清零功能的十六进制计数器,要
  7. 前端需要懂的 APP 容器原理
  8. 浙江大学计算机学院 金小刚,金小刚(浙江大学CADCG国家重点实验室教授)_百度百科...
  9. 基于Thinkphp5+EasyWeChat+fastadmin微信小程序授权登录获取手机号微信公众号网页---联合授权登录
  10. 2018 年物联网发展五大趋势预测