Time in UC/OS-II

Source: Internet
Author: User

Time is a very important concept. It takes us and friends a period of time to get out and do things. In short, we cannot live without time. The same is true for the operating system. It also takes time to standardize the execution of its tasks.

In our daily life, the minimum unit of time is second. In the hardware circuit, the minimum generation time is the frequency of the crystal oscillator. in the operating system, the smallest unit of time is called the time tick, which is a period of time in milliseconds (MS) generated by a hardware timer, that is, when the timer reaches the specified time, generates an interrupt (ostickisr (). The clock cycle is the interval between two interrupts.

When the clock cycle interrupt function (ostickisr () is enabled, the CPU register value is saved first (where is the value stored? The storage space for ISR execution and task execution should be different. During task execution, tasks should be stored in the task stack, But where should ISR be stored during execution ?) Add 1 to the osintnesting layer and determine whether the nested layer is interrupted. If not, assign the stack pointer (SP) to the TCB member ostcbstkptr of the current task. Call the clock cycle function (ostimetick (), clear the interrupt, and exit the interrupt function (osintexit (). A task scheduling is triggered when the interrupt function is released.

In the clock cycle function (ostimetick (), there are two main tasks: one is to record the time variable ostime + 1, and the other is to traverse all the task control block linked lists, to the OSTCBDly-1 in the task block that is used to save the task latency (that is, to tell the task that time has passed a beat), if the time limit for a non-pending task has reached, then, it enters the task readiness state (Note: a non-suspended task (ostasksuspend () must call the restore task function (ostaskresume () to restore it ).

Sometimes, we want to do some work in every clock cycle. For example, in real life, I want the cell phone signal to flash every second. We can call the hook function (ostimetickhook () of the clock cycle service function to complete this task. There are many hook functions, such as the work to be done (ostaskswhook () During task switching ()). There are 10 such hook functions in total. Of course, if it is not necessary, it is best to use the hook function, because execution also takes time, which may cause inaccurate timing. (Is the hardware timer working when you execute the hook function? When it reaches the scheduled time, there will be interruptions. Will this cause inaccurate timing ?)

In each task (except for idle tasks), you must use the delay function (ostimedly () to pause the running of the current task for a period of time and execute a task scheduling, enable the system to execute other ready tasks with a higher priority. Otherwise, the task will continue to occupy the CPU usage, resulting in the task's exclusive CPU.

The role of ostimedly () is to store the delayed time limit to the ostcbdly member who controls TCB, cancel the readiness status of the current task, and then schedule the task, enables the system to execute ready tasks with the highest priority. Ostimedly (int16u ticks) is a function scheduled by the number of clock beats. The system also defines another delay function int8u ostimedlyhmsm (int8u hours, int8u minutes, int8u seconds, int8u Milli ), that is, the specified time, minute, second, and millisecond.

If there is a function that can delay a task, you also need to cancel the function. ostimediy (), no matter whether you call the delay function above, you call ostimedlyresume () the delay of canceling the task enters the ready state. The prototype of this function is int8u ostimedlyresume (int8u PRIO );

Finally, you can call ostimeget () and ostimeset () to obtain or set the system time, that is, the number of clock beats (ostime value ). The function prototype asks int32u ostimeget (void) and void ostimeset (int32u ticks) respectively ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.