Nonblocking Schedule

Last Updated on : 2024-05-17 03:19:28download

This topic describes the nonblocking schedule to repeat tasks at specified intervals.

Overview

Concepts

Concepts Description
Nonblocking schedule Typically, it is used in a loop to schedule tasks in a nonblocking way. For example, the light blinks every second. The nonblocking schedule is easier to implement and consumes fewer resources than a timer, making it suitable for resource-constrained systems.

Features

Calculate the time elapsed between the recorded timestamp (system tick) and the current timestamp. If the elapsed time exceeds the specified duration, true will be returned to trigger the scheduled task.

Data structure

No external data structure.

API description

Function unsigned int hal_clock_get_system_tick(void) -
Purpose Get the system tick. -
Request parameter None -
Return The system tick. The conversion factor for changing ticks to microseconds varies by system.
Function unsigned int hal_clock_time_exceed_with_update
(unsigned int *ref, unsigned int span_us)
-
Purpose Nonblocking schedule -
Request parameter unsigned int *ref The reference tick, which is updated when this function returns 1.
-> unsigned int span_us The time span, in microseconds.
Return 0/1 Calculate the difference between the current tick and the reference tick. If the result exceeds span_us, 1 is returned. Otherwise, 0 is returned.

Example

Use the nonblocking schedule to repeat tasks at specified intervals.

static u32 count_1s_time = 0;
static u32 count_1s = 0;
if(hal_clock_time_exceed_with_update(&count_1s_time,1000000)){//1S
    count_1s++;
}

This method can create a nonblocking schedule accurate to microseconds, with a deviation of less than five seconds per day.

unsigned int hal_clock_get_system_tick(void) returns the system tick for measuring function execution time. Ensure proper conversion from ticks to microseconds.

Functional testing

Prerequisites

Write a nonblocking schedule to log every second.

Procedure

After the device has been running for over a day, check the logs to determine if the deviation is acceptable.

Support and help

If you have any problems with TuyaOS development, you can post your questions in the Tuya Developer Forum.