当前位置:网站首页>FreeRTOS personal notes - delay function
FreeRTOS personal notes - delay function
2022-07-23 23:16:00 【Couvrir wild beast】
According to personal learning direction , Study FreeRTOS. Because brother wildfire FreeRTOS It's more implicit , I intend to be as detailed as possible in this column . As a personal note , For reference or reference only .
mixed material item :FreeRTOS Kernel Implementation and application development practice guide 、 Wildfire FreeRTOS Supporting video source code 、b Standing wildfire FreeRTOS video . It's better to match it !!!
The time delay function
Commonly used delay blocking function vTaskDelay()
#if ( INCLUDE_vTaskDelay == 1 )
void vTaskDelay( const TickType_t xTicksToDelay )
{
BaseType_t xAlreadyYielded = pdFALSE;
if( xTicksToDelay > ( TickType_t ) 0U )
{
configASSERT( uxSchedulerSuspended == 0 );
vTaskSuspendAll(); // Suspend task scheduler
{
traceTASK_DELAY();
// Add the current task to the delay list
prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
}
xAlreadyYielded = xTaskResumeAll(); // Restore task scheduler
}
else
{
mtCOVERAGE_TEST_MARKER();
}
if( xAlreadyYielded == pdFALSE )
{
portYIELD_WITHIN_API(); // Force switching tasks , take PendSV Of bit28 Set up 1
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
#endif /* INCLUDE_vTaskDelay */The above function called prvAddCurrentTaskToDelayedList() function , as follows
//xCanBlockIndefinitely: Whether it can block permanently pdFALSE: Permanent blocking is not allowed , That is, it is not allowed to suspend the current task pdTRUE: Allow permanent blocking , Support suspending the current task
static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait, const BaseType_t xCanBlockIndefinitely )
{
TickType_t xTimeToWake;
const TickType_t xConstTickCount = xTickCount; // Get the time point when the delay function is currently called
/* Omit code */
/* Before adding tasks to the block list , Remove the task from the ready list , Because both lists use the same list item . */
if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
{
portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
}
else
{
mtCOVERAGE_TEST_MARKER();
}
#if ( INCLUDE_vTaskSuspend == 1 )
{
if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) ) // Support pending
{
vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) ); // Insert the task into the pending list
}
else
{
// Calculate the time to wake up the task
xTimeToWake = xConstTickCount + xTicksToWait;
/* The list items will be inserted in the wake-up time order */
listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
if( xTimeToWake < xConstTickCount ) // If the wake-up time overflows , Will be added to the delay overflow list
{
vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
}
else
{
// Wake up time does not overflow , Add to the delay list
vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
// If the next task to wake up is the current delayed task , Then you need to reset the unblocking time of the next task xNextTaskUnblockTime Is the time to wake up the current delayed task xTimeToWake.
if( xTimeToWake < xNextTaskUnblockTime )
{
xNextTaskUnblockTime = xTimeToWake;
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
}
}
#else /* INCLUDE_vTaskSuspend */
{
xTimeToWake = xConstTickCount + xTicksToWait;
listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
if( xTimeToWake < xConstTickCount )
{
vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
}
else
{
vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
if( xTimeToWake < xNextTaskUnblockTime )
{
xNextTaskUnblockTime = xTimeToWake;
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
( void ) xCanBlockIndefinitely;
}
#endif /* INCLUDE_vTaskSuspend */
}vTaskDelay () The delay of is relative , It's uncertain , Its delay is waiting vTaskDelay () The calculation starts after the call . also vTaskDelay() When the delay time is up , If a high priority task or interrupt is being executed ,
Tasks blocked by delay will not be unblocked immediately , The period of each task execution is not completely determined .
vTaskDelayUntil() The delay is absolute , Suitable for tasks that are performed periodically . When (*pxPreviousWakeTime + xTimeIncrement) After time ,vTaskDelayUntil() The function immediately returns , If the task is the highest priority ,
Then the mission will immediately unblock , So vTaskDelayUntil() The delay of a function is absolute .
Absolute delay vTaskDelayUntil() It is often used for more accurate periodic running tasks , For example, I have a task , You want it to perform regularly at a fixed frequency , Without external influence , The time interval between the beginning of a task's last run and the beginning of its next run is absolute .
vTaskDelayUntil() as follows
#if ( INCLUDE_vTaskDelayUntil == 1 )
//pxPreviousWakeTime, Save the last time the task is unblocked . First use , This variable must be initialized to the current time , Then this variable will be in vTaskDelayUntil() Automatic update in function .
//xTimeIncrement, Cycle time . When when between etc. On (*pxPreviousWakeTime + xTimeIncrement) when , Task unblock . If you don't change the parameters xTimeIncrement Value , The tasks that call this function are executed at a fixed frequency .
void vTaskDelayUntil( TickType_t * const pxPreviousWakeTime, const TickType_t xTimeIncrement )
{
TickType_t xTimeToWake;
BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
configASSERT( pxPreviousWakeTime );
configASSERT( ( xTimeIncrement > 0U ) );
configASSERT( uxSchedulerSuspended == 0 );
vTaskSuspendAll();
{
// Get the time point when the delay starts
const TickType_t xConstTickCount = xTickCount;
// Calculate the time of delayed arrival , That is, the time to wake up the task
xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
/* pxPreviousWakeTime Is the last wake-up time , After wake-up, it takes a certain time to execute the task body code , If the last wake-up time is greater than the current time , Indicates that the beat counter overflowed */
if( xConstTickCount < *pxPreviousWakeTime )
{
/* If the wake-up time is less than the last wake-up time , And the wake-up time is greater than the start time , This is equivalent to no overflow , That is, it ensures that the periodic delay time is greater than the execution time of the task body code */
if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
{
xShouldDelay = pdTRUE;
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
else
{
// Just wake up time overflow or no overflow , It ensures that the delay time is greater than the execution time of the task body code
if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
{
xShouldDelay = pdTRUE;
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
// Update the last wake-up time
*pxPreviousWakeTime = xTimeToWake;
if( xShouldDelay != pdFALSE )
{
traceTASK_DELAY_UNTIL( xTimeToWake );
/* prvAddCurrentTaskToDelayedList() The function requires blocking time rather than wake-up time , So subtract the current tick count . */
prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
xAlreadyYielded = xTaskResumeAll();
// Force a context switch
if( xAlreadyYielded == pdFALSE )
{
portYIELD_WITHIN_API();
}
else
{
mtCOVERAGE_TEST_MARKER();
}
}
#endif /* INCLUDE_vTaskDelayUntil */xTimeIncrement : Task cycle time .
pxPreviousWakeTime : The time point of the last wake-up task .
xTimeToWake : The time point of this wake-up task .
xConstTickCount : Enter the time point of delay .
Absolute delay vTaskDelayUntil() example
void vTaskA( void * pvParameters )
{
/* Used to save the last time . The system will update automatically after calling */
static portTickType PreviousWakeTime;
/* Set delay time , Turn time into beats */
const portTickType TimeIncrement = pdMS_TO_TICKS(1000);
/* Get current system time */
PreviousWakeTime = xTaskGetTickCount();
while (1)
{
/* Call the absolute delay function , The task interval is 1000 individual tick */
vTaskDelayUntil( &PreviousWakeTime, TimeIncrement );
// ...
// Here is the task body code
// ...
}
}This section officially encapsulates two functions , You can use it directly .
vTaskDelay() Relative delay function .
vTaskDelayUntil() Absolute delay function .
边栏推荐
- [jailhouse article] a novel software architecture for mixed criticality systems (2020)
- Programming in the novel [serial 18] the moon bends in the yuan universe
- 汇编语言伪指令详解(附实例)
- Mongodb - Introduction to the usage of logical operators not, and, or, nor in query statements
- Lixia action 2022 Yuanqi digital round table forum will be launched soon
- [web vulnerability exploration] SQL injection vulnerability
- mysqlbinlog命令介绍(远程拉取binlog日志)
- 1000 okaleido tiger launched binance NFT, triggering a rush to buy
- 二,数字逻辑功能单元
- unity visual studio2019升级到2022版本(扔掉盗版红渣)
猜你喜欢
![[leetcode ladder] linked list · 203 remove linked list elements](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[leetcode ladder] linked list · 203 remove linked list elements

在openEuler社区开源的Embedded SIG,来聊聊它的多 OS 混合部署框架

Interviewer: if the order is not paid within 30 minutes after it is generated, it will be automatically cancelled. How to realize it?
![[laser principle and Application-8]: EMC design of laser circuit](/img/98/8b7a4fc3f9ef9b7e16c63a8c225b02.png)
[laser principle and Application-8]: EMC design of laser circuit

砺夏行动 2022|源启数字化圆桌论坛即将上线

A deserialized CTF question sharing

糖尿病遗传风险检测挑战赛进阶

The role of physical layer, link layer, network layer, transport layer and application layer of tcp/ip model of internet protocol stack

How to reasonably estimate the size of thread pool

Grey prediction (matlab)
随机推荐
The canfd/can interface offline burning operation instructions of h7-tool have been updated (2022-07-12)
[leetcode ladder] linked list · 203 remove linked list elements
2、 Digital logic functional unit
SecureCRT garbled
Tap series article 6 | application model of tap
Niuke C basic exercises
Programming in the novel [serial 20] the moon bends in the yuan universe
TAP 系列文章9 | 应用开发加速器
【音视频技术】视频质量评价 MSU VQMT & Netflix vmaf
ES6箭头函数的使用
Finding all paths between two points in a directed graph
Programming in the novel [serial 17] the moon bends in the yuan universe
Rosbag file recorded by LIDAR point cloud data is converted into CSV file
Grey correlation analysis (matlab)
48: Chapter 5: develop admin management service: 1: create sub project [imooc news dev Service Admin], management service module;
USB转CAN设备在核酸提取仪 高性能USB接口CAN卡
A deserialized CTF question sharing
Use of pairwise
Programming in the novel [serial 18] the moon bends in the yuan universe
[nuxt 3] (IX) server routing