当前位置:网站首页>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 .
边栏推荐
- TAP 系列文章6 | TAP的应用模型
- Drools (1): introduction to drools
- D2admin framework is basically used
- USB to can device in nucleic acid extractor high performance USB interface can card
- Analysis of mobile semantics and perfect forwarding
- Light up the LED light of little bear patting learning
- Tap series article 9 | application development accelerator
- unity visual studio2019升级到2022版本(扔掉盗版红渣)
- Ways to improve the utilization of openeuler resources 01: Introduction
- H7-tool serial port offline burning operation instructions, support TTL serial port, RS232 and RS485 (2022-06-30)
猜你喜欢

Open source embedded sig in the openeuler community. Let's talk about its multi OS hybrid deployment framework

Upgrade unity visual studio 2019 to 2022 (throw away pirated red slag)

Grey prediction (matlab)

【音视频技术】视频质量评价 MSU VQMT & Netflix vmaf
![[audio and video technology] video quality evaluation MSU vqmt & Netflix vmaf](/img/1c/bc71ba1eb3723cdd80501f2b0ad5ce.png)
[audio and video technology] video quality evaluation MSU vqmt & Netflix vmaf

USB to can device in nucleic acid extractor high performance USB interface can card

Stm32f4 check the frequency of each part of the system

使用itextpdf提取PDF文件中的任意页码

Sword finger offer II 115. reconstruction sequence

一,数字逻辑的化简
随机推荐
Tap series article 4 | backstage based tap developer portal
Introduction to mysqlbinlog command (remote pull binlog)
ES6 use of arrow function
fl studio 20.9更新中文版宿主DAW数字音频工作站
Matlab Foundation
系列文章|云原生时代下微服务架构进阶之路 - 微服务拆分的最佳实践
2022-7-22 face review + simple topic sorting
Diabetes genetic risk testing challenge advanced
Basic operations of AutoCAD
Tap series article 6 | application model of tap
DHCP: prevent rogue DHCP server in the network
Lu Xia action | Source Kai Digital: Existing Mode or open source innovation?
Contemporary inspirational "women"
EasyNVR平台如何关闭匿名登录?
[in depth study of 4g/5g/6g topic -40]: urllc-11 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -5-5g QoS principle and Architecture: slicing, PDU s
狂神redis笔记10
AutoCAD advanced operation
Tiktok launches multilingual subtitles and translation tools
Mongodb database + graphical tools download, installation and use
D2admin framework is basically used