原创 七、FreeRTOS时间管理

2020-8-23 16:45 1928 20 2 分类: 汽车电子 文集: FreeRTOS

FreeRTOS时间管理

FreeRTOS的两个延时函数:

vTaskDelay()          相对延时

vTaskDelayUntil()   绝对延时

1 相对延时

主要过程:挂起任务调度器,将任务以参数延时值添加到演示列表,然后恢复任务调度器。

  1. void vTaskDelay(const TickType_t xTicksToDelay)
  2. {
  3. BaseType_t xAlreadyYielded = pdFALSE;
  4. /* A delay time of zero just forces a reschedule. */
  5. if (xTicksToDelay > (TickType_t)0U)
  6. {
  7. configASSERT(uxSchedulerSuspended == 0);
  8. vTaskSuspendAll();
  9. {
  10. traceTASK_DELAY();
  11. /* A task that is removed from the event list while the
  12. scheduler is suspended will not get placed in the ready
  13. list or removed from the blocked list until the scheduler
  14. is resumed.
  15. This task cannot be in an event list as it is the currently
  16. executing task. */
  17. prvAddCurrentTaskToDelayedList(xTicksToDelay, pdFALSE);
  18. }
  19. xAlreadyYielded = xTaskResumeAll();
  20. }
  21. else
  22. {
  23. mtCOVERAGE_TEST_MARKER();
  24. }
  25. /* Force a reschedule if xTaskResumeAll has not already done so, we may
  26. have put ourselves to sleep. */
  27. if (xAlreadyYielded == pdFALSE)
  28. {
  29. portYIELD_WITHIN_API();
  30. }
  31. else
  32. {
  33. mtCOVERAGE_TEST_MARKER();
  34. }
  35. }

函数实现过程分析:

添加任务到演示列表的过程:

先将任务从当前的状态列表中移除,然后计算当前tick值+延时值,以计算的值添加到延时列表,然后重新计算下一次任务切换的阻塞延时变量值

  1. static void prvAddCurrentTaskToDelayedList(TickType_t xTicksToWait, const BaseType_t xCanBlockIndefinitely)
  2. {
  3. TickType_t xTimeToWake;
  4. const TickType_t xConstTickCount = xTickCount;
  5. #if (INCLUDE_xTaskAbortDelay == 1)
  6. {
  7. /* About to enter a delayed list, so ensure the ucDelayAborted flag is
  8. reset to pdFALSE so it can be detected as having been set to pdTRUE
  9. when the task leaves the Blocked state. */
  10. pxCurrentTCB->ucDelayAborted = pdFALSE;
  11. }
  12. #endif
  13. /* Remove the task from the ready list before adding it to the blocked list
  14. as the same list item is used for both lists. */
  15. if (uxListRemove(&(pxCurrentTCB->xStateListItem)) == (UBaseType_t)0)
  16. {
  17. /* The current task must be in a ready list, so there is no need to
  18. check, and the port reset macro can be called directly. */
  19. portRESET_READY_PRIORITY(pxCurrentTCB->uxPriority, uxTopReadyPriority);
  20. }
  21. else
  22. {
  23. mtCOVERAGE_TEST_MARKER();
  24. }
  25. #if (INCLUDE_vTaskSuspend == 1)
  26. {
  27. if ((xTicksToWait == portMAX_DELAY) && (xCanBlockIndefinitely != pdFALSE))
  28. {
  29. /* Add the task to the suspended task list instead of a delayed task
  30. list to ensure it is not woken by a timing event. It will block
  31. indefinitely. */
  32. vListInsertEnd(&xSuspendedTaskList, &(pxCurrentTCB->xStateListItem));
  33. }
  34. else
  35. {
  36. /* Calculate the time at which the task should be woken if the event
  37. does not occur. This may overflow but this doesn't matter, the
  38. kernel will manage it correctly. */
  39. xTimeToWake = xConstTickCount + xTicksToWait;
  40. /* The list item will be inserted in wake time order. */
  41. listSET_LIST_ITEM_VALUE(&(pxCurrentTCB->xStateListItem), xTimeToWake);
  42. if (xTimeToWake < xConstTickCount)
  43. {
  44. /* Wake time has overflowed. Place this item in the overflow
  45. list. */
  46. vListInsert(pxOverflowDelayedTaskList, &(pxCurrentTCB->xStateListItem));
  47. }
  48. else
  49. {
  50. /* The wake time has not overflowed, so the current block list
  51. is used. */
  52. vListInsert(pxDelayedTaskList, &(pxCurrentTCB->xStateListItem));
  53. /* If the task entering the blocked state was placed at the
  54. head of the list of blocked tasks then xNextTaskUnblockTime
  55. needs to be updated too. */
  56. if (xTimeToWake < xNextTaskUnblockTime)
  57. {
  58. xNextTaskUnblockTime = xTimeToWake;
  59. }
  60. else
  61. {
  62. mtCOVERAGE_TEST_MARKER();
  63. }
  64. }
  65. }
  66. }
  67. #else /* INCLUDE_vTaskSuspend */
  68. {
  69. /* Calculate the time at which the task should be woken if the event
  70. does not occur. This may overflow but this doesn't matter, the kernel
  71. will manage it correctly. */
  72. xTimeToWake = xConstTickCount + xTicksToWait;
  73. /* The list item will be inserted in wake time order. */
  74. listSET_LIST_ITEM_VALUE(&(pxCurrentTCB->xStateListItem), xTimeToWake);
  75. if (xTimeToWake < xConstTickCount)
  76. {
  77. /* Wake time has overflowed. Place this item in the overflow list. */
  78. vListInsert(pxOverflowDelayedTaskList, &(pxCurrentTCB->xStateListItem));
  79. }
  80. else
  81. {
  82. /* The wake time has not overflowed, so the current block list is used. */
  83. vListInsert(pxDelayedTaskList, &(pxCurrentTCB->xStateListItem));
  84. /* If the task entering the blocked state was placed at the head of the
  85. list of blocked tasks then xNextTaskUnblockTime needs to be updated
  86. too. */
  87. if (xTimeToWake < xNextTaskUnblockTime)
  88. {
  89. xNextTaskUnblockTime = xTimeToWake;
  90. }
  91. else
  92. {
  93. mtCOVERAGE_TEST_MARKER();
  94. }
  95. }
  96. /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
  97. (void)xCanBlockIndefinitely;
  98. }
  99. #endif /* INCLUDE_vTaskSuspend */
  100. }

具体分析:

什么是相对延时?

  1. void test_task(void *pvParameters)
  2. {
  3. // 整个任务的执行时间 = 主体函数执行时间(不定)+ 延时值(800)
  4. for (; ;)
  5. {
  6. // 主体函数,如读取寄存器值,则这函数执行时间是不确定的
  7. task_main_function();
  8. vTaskDelay(800);
  9. }
  10. }

在上面的任务函数中,如主体函数是在某条件下读取寄存器值(状态寄存器可能需要等待),那么这个主体函数的执行时间是不确定的,整个循环的执行时间等于主体函数执行时间 + 延时值(800)。这里的相对,只是 vTaskDelay() 相对于主体函数延时800,而整个 for (; ;)的执行时间是浮动的

2 绝对延时void vTaskDelayUntil(TickType_t *const pxPreviousWakeTime, const TickType_t xTimeIncrement)

参数说明:

绝对延时的原理:

  1. void test_task(void *pvParameters)
  2. {
  3. uint32_t origin_tick = xTickCount; // 获取初始 Tick 值
  4. for (; ;)
  5. {
  6. task_main_function(); // 这个主体函数的执行时间浮动不定
  7. vTaskDelayUntil(origin_tick, 800); // 绝对延时函数
  8. }
  9. }

如下所示,绝对延时函数会获取进入循环时的系统 Tick 值,然后每隔固定的延时节拍数(800)都会唤醒任务,不管任务的主体函数执行时间如何浮动,内核都会在 初始Tick值 + 延时Tick值(×1 ×2 ×3...)唤醒任务,因此是绝对延时函数。

函数源码:

实现要点:获取下次唤醒时间值 = 上次唤醒时间值 + 绝对延时值,自动更新上次唤醒时间值(设定为当前唤醒值,以实现绝对时间间隔),根据下次唤醒时间值,以及当前的 Tick 值,来计算延时值并插入到延时列表中。

  1. void vTaskDelayUntil(TickType_t *const pxPreviousWakeTime, const TickType_t xTimeIncrement)
  2. {
  3. TickType_t xTimeToWake;
  4. BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
  5. configASSERT(pxPreviousWakeTime);
  6. configASSERT((xTimeIncrement > 0U));
  7. configASSERT(uxSchedulerSuspended == 0);
  8. vTaskSuspendAll();
  9. {
  10. /* Minor optimisation. The tick count cannot change in this
  11. block. */
  12. const TickType_t xConstTickCount = xTickCount;
  13. /* Generate the tick time at which the task wants to wake. */
  14. // 获取下次唤醒时间值 = 上次唤醒时间值 + 绝对延时值
  15. xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
  16. if (xConstTickCount < *pxPreviousWakeTime)
  17. {
  18. /* The tick count has overflowed since this function was
  19. lasted called. In this case the only time we should ever
  20. actually delay is if the wake time has also overflowed,
  21. and the wake time is greater than the tick time. When this
  22. is the case it is as if neither time had overflowed. */
  23. if ((xTimeToWake < *pxPreviousWakeTime) && (xTimeToWake > xConstTickCount))
  24. {
  25. xShouldDelay = pdTRUE;
  26. }
  27. else
  28. {
  29. mtCOVERAGE_TEST_MARKER();
  30. }
  31. }
  32. else
  33. {
  34. /* The tick time has not overflowed. In this case we will
  35. delay if either the wake time has overflowed, and/or the
  36. tick time is less than the wake time. */
  37. if ((xTimeToWake < *pxPreviousWakeTime) || (xTimeToWake > xConstTickCount))
  38. {
  39. xShouldDelay = pdTRUE;
  40. }
  41. else
  42. {
  43. mtCOVERAGE_TEST_MARKER();
  44. }
  45. }
  46. /* Update the wake time ready for the next call. */
  47. // 自动更新上次唤醒时间值(设定为当前唤醒值,以实现绝对时间间隔)
  48. *pxPreviousWakeTime = xTimeToWake;
  49. if (xShouldDelay != pdFALSE)
  50. {
  51. traceTASK_DELAY_UNTIL(xTimeToWake);
  52. /* prvAddCurrentTaskToDelayedList() needs the block time, not
  53. the time to wake, so subtract the current tick count. */
  54. // 根据下次唤醒时间值,以及当前的 Tick 值,来计算延时值并插入到延时列表中
  55. prvAddCurrentTaskToDelayedList(xTimeToWake - xConstTickCount, pdFALSE);
  56. }
  57. else
  58. {
  59. mtCOVERAGE_TEST_MARKER();
  60. }
  61. }
  62. xAlreadyYielded = xTaskResumeAll();
  63. /* Force a reschedule if xTaskResumeAll has not already done so, we may
  64. have put ourselves to sleep. */
  65. if (xAlreadyYielded == pdFALSE)
  66. {
  67. portYIELD_WITHIN_API();
  68. }
  69. else
  70. {
  71. mtCOVERAGE_TEST_MARKER();
  72. }
  73. }

函数实现过程分析:

绝对函数执行图示:

绝对延时函数的调用方式:

其实使用函数 vTaskDelayUntil()延时的任务也不一定就能周期性的运行,使用函数vTaskDelayUntil()只能保证你按照一定的周期取消阻塞,进入就绪态。如果有更高优先级或者中断的话你还是得等待其他的高优先级任务或者中断服务函数运行完成才能轮到你。这个绝对延时只是相对于 vTaskDelay()这个简单的延时函数而言的。

 

滴答定时器

SysTick直属于Cortex-M内核,他不是stm32专属的,只要是Cortex-M内核的MCU就都有SysTick。SysTick计数器是个24位的向下计数器,这个计数器的使命就是为系统提供服务的。操作系统都需要一个系统时钟,每个系统时钟周期都会触发OS内核执行一些系统调用,比如进行任务管理任务切换等。SysTick就可以完成此功能,使能SysTick中断,设定好定时周期,SysTick就会周期性的触发中断,跟系统有关的操作就可以在SysTick的中断服务函数中完成。如果不使用系统的话,SysTick也可以当成普通的定时器来使用。

Cortex-M编程手册中关于SysTick和寄存器的描述:

在库函数中关于 SysTick 的描述:

  1. #define SCS_BASE (0xE000E000UL) /*!< System Control Space Base Address */
  2. #define SysTick_BASE (SCS_BASE + 0x0010UL) /*!< SysTick Base Address */
  3. #define SysTick ((SysTick_Type *)SysTick_BASE) /*!< SysTick configuration struct */
  4. /**
  5. \brief Structure type to access the System Timer (SysTick).
  6. */
  7. typedef struct
  8. {
  9. __IOM uint32_t CTRL; /*!< Offset: 0x000 (R/W) SysTick Control and Status Register */
  10. __IOM uint32_t LOAD; /*!< Offset: 0x004 (R/W) SysTick Reload Value Register */
  11. __IOM uint32_t VAL; /*!< Offset: 0x008 (R/W) SysTick Current Value Register */
  12. __IM uint32_t CALIB; /*!< Offset: 0x00C (R/ ) SysTick Calibration Register */
  13. } SysTick_Type;

FreeRTOS中,有关SysTick的中断服务函数:

其实就是在中断服务函数中,先禁用系统中断,然后调用 xTaskIncrementTick() 来递增系统计数值,根据函数返回值来判断是否要进行任务切换,通过将PENDSV寄存器中断位置1以启用中断,并在PENDSV中断服务函数中进行任务切换。

  1. /**
  2. * @brief This function handles System tick timer.
  3. */
  4. void SysTick_Handler(void)
  5. {
  6. #if (INCLUDE_xTaskGetSchedulerState == 1)
  7. if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
  8. {
  9. xPortSysTickHandler();
  10. }
  11. #endif
  12. }
  13. void xPortSysTickHandler(void)
  14. {
  15. vPortRaiseBASEPRI();
  16. {
  17. /* Increment the RTOS tick. */
  18. if (xTaskIncrementTick() != pdFALSE)
  19. {
  20. /* A context switch is required. Context switching is performed in
  21. the PendSV interrupt. Pend the PendSV interrupt. */
  22. portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
  23. }
  24. }
  25. vPortClearBASEPRIFromISR();
  26. }

不管是什么系统,运行都需要有个系统时钟节拍,xTickCount 就是FreeRTOS 的系统时钟节拍计数器。每个滴答定时器中断中 xTickCount 就会加一,xTickCount 的具体操作过程是在函数 xTaskIncrementTick()中进行的,这个函数相当复杂,不用纠结。

转载于:https://blog.csdn.net/dingyc_ee/article/details/104092532

文章评论0条评论)

登录后参与讨论
我要评论
0
20
关闭 站长推荐上一条 /2 下一条