Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the task delay function in the ucos kernel

Shulou Source: shulou.com Published: 2022-06-03 05:41:04 09月14日 Update

This article introduces what is the task delay function in the ucos kernel, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

Ysmz4: the following is mainly to solve the problem of why I set OSTimeDly (2) and delay 2ms.

As long as you delay more than 1 system clock beat (1ms), UCOSII will perform task switching, there is no need to calculate the delay of each task, as long as you ensure that the delay is greater than one system clock beat.

Anyone who has written a single-chip microcomputer program knows that the delay function is often used.

In ucos II, the system also provides such a delay function. One is the popular version of OSTimeDly (), and the other is the refined version of OSTimeDlyHMSM (). Radish and green vegetables, each has his own love, whatever you choose.

The difference between the two is that the delay time of OSTimeDly () is measured by the number of beats, while the delay time of OSTimeDlyHMSM () is measured by the specific time size.

These two functions play a very important role, that is, when you call these two functions, ucos II will schedule a task. So cpu can switch between tasks, thanks a lot to these two functions.

Calling both of these delay functions suspends the task, and cpu performs the highest priority task in the ready table.

It is important to note that after a task calls OSTimeDly (), as soon as the specified time expires or another task cancels the delay by calling OSTimeDlyResume (), it will immediately enter the ready state rather than the execution state, that is, the task will not necessarily be executed immediately after the delay ends.

Suppose you delay 0.001 seconds by calling the delay function during the execution of the task, and the delay time is likely to be greater than this value. It depends on the arrangement of your task.

Let's take a look at the implementation of these two functions.

Program listing L 5.1 OSTimeDly ().

Void OSTimeDly (INT16U ticks)

{

If (ticks > 0) {(1)

OS_ENTER_CRITICAL ()

If ((OSRdyTblr [OSTCBCur-> OSTCBY] & = ~ OSTCBCur- > OSTCBBitX) = = 0) {(2)

OSRdyGrp & = ~ OSTCBCur- > OSTCBBitY

}

OSTCBCur- > OSTCBDly = ticks; (3)

OS_EXIT_CRITICAL ()

OSSched (); (4)

}

}

In fact, the program does the following things

1. Remove this task from the task ready list

two。 Keep the delay value in the task control block and reduce the number of delay beats every other clock beat through OSTimeTick ()

3. Call the task scheduling function to switch tasks.

INT8U OSTimeDlyHMSM (INT8U hours, INT8U minutes, INT8U seconds, INT16U milli)

{

INT32U ticks

INT16U loops

If (hours > 0 | | minutes > 0 | | seconds > 0 | | milli > 0) {(1)

If (minutes > 59) {

Return (OS_TIME_INVALID_MINUTES)

}

If (seconds > 59) {

Return (OS_TIME_INVALID_SECONDS)

}

If (milli > 999) {

Return (OS_TIME_INVALID_MILLI)

}

Ticks = (INT32U) hours * 3600L * OS_TICKS_PER_SEC (2)

+ (INT32U) minutes * 60L * OS_TICKS_PER_SEC

+ (INT32U) seconds * OS_TICKS_PER_SEC

+ OS_TICKS_PER_SEC * ((INT32U) milli)

+ 500L/OS_TICKS_PER_SEC) / 1000L; (3)

Loops = ticks / 65536L; (4)

Ticks = ticks% 65536L; (5)

OSTimeDly (ticks); (6)

While (loops > 0) {(7)

OSTimeDly (32768); (8)

OSTimeDly (32768)

Loops--

}

Return (OS_NO_ERR)

} else {

Return (OS_TIME_ZERO_DLY); (9)

}

}

So much for sharing what the task delay function in the ucos kernel is. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Task function time two beat clock program system switch kernel important content more state help scheduling good big highest each has its own preference Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux NVidia Microsoft Shulou Tech Info OPPO Reno