同步操作将从 OpenHarmony/docs 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
The software timer is a software-simulated timer based on system tick interrupts. When the preset tick counter value has elapsed, the user-defined callback will be invoked. The timing precision is related to the cycle of the system tick clock. Due to the limitation in hardware, the number of hardware timers cannot meet users' requirements. Therefore, the OpenHarmony LiteOS-A kernel provides the software timer function. The software timer allows more timing services to be created, increasing the number of timers.
The software timer supports the following functions:
The software timer is a system resource. When modules are initialized, a contiguous section of memory is allocated for software timers. The maximum number of timers supported by the system is configured by the LOSCFG_BASE_CORE_SWTMR_LIMIT macro in los_config.h. Software timers use a queue and a task resource of the system. The software timers are triggered based on the First In First Out (FIFO) rule. For the timers set at the same time, the timer with a shorter value is always closer to the queue head than the timer with a longer value, and is preferentially triggered. The software timer counts time in ticks. When a software timer is created and started, the OpenHarmony system determines the timer expiry time based on the current system time (in ticks) and the timing interval set by the user, and adds the timer control structure to the global timing list.
When a tick interrupt occurs, the tick interrupt handler scans the global timing list for expired timers. If such timers are found, the timers are recorded.
When the tick interrupt handling function is complete, the software timer task (with the highest priority) is woken up. In this task, the timeout callback function for the recorded timer is called.
Timer States
OS_SWTMR_STATUS_UNUSED
The timer is not in use. When the timer module is initialized, all timer resources in the system are set to this state.
OS_SWTMR_STATUS_CREATED
The timer is created but not started or the timer is stopped. When LOS_SwtmrCreate is called for a timer that is not in use or LOS_SwtmrStop is called for a newly started timer, the timer changes to this state.
OS_SWTMR_STATUS_TICKING
The timer is running (counting). When LOS_SwtmrStart is called for a newly created timer, the timer enters this state.
Timer Modes
The OpenHarmony provides three types of software timers:
The following table describes APIs available for the OpenHarmony LiteOS-A software timer module. For more details about the APIs, see the API reference.
Table 1 Software timer APIs
The typical development process of software timers is as follows:
Configure the software timer.
Call LOS_SwtmrCreate to create a software timer.
Call LOS_SwtmrStart to start the software timer.
Call LOS_SwtmrTimeGet to obtain the remaining number of ticks of the software timer.
Call LOS_SwtmrStop to stop the software timer.
Call LOS_SwtmrDelete to delete the software timer.
NOTE:
- Avoid too many operations in the callback function of the software timer. Do not use APIs or perform operations that may cause task suspension or blocking.
- The software timers use a queue and a task resource of the system. The priority of the software timer tasks is set to 0 and cannot be changed.
- The number of software timer resources that can be configured in the system is the total number of software timer resources available to the entire system, not the number of software timer resources available to users. For example, if the system software timer occupies one more resource, the number of software timer resources available to users decreases by one.
- If a one-shot software timer is created, the system automatically deletes the timer and reclaims resources after the timer times out and the callback function is executed.
- For a one-shot software timer that will not be automatically deleted after expiration, you need to call LOS_SwtmrDelete to delete it and reclaim the timer resource to prevent resource leakage.
Prerequisites:
Sample Code
#include "los_swtmr.h"
void Timer1_Callback(uint32_t arg);
void Timer2_Callback(uint32_t arg);
UINT32 g_timercount1 = 0;
UINT32 g_timercount2 = 0;
void Timer1_Callback(uint32_t arg) // Callback function 1
{
unsigned long tick_last1;
g_timercount1++;
tick_last1=(UINT32)LOS_TickCountGet(); // Obtain the current number of ticks.
PRINTK("g_timercount1=%d\n",g_timercount1);
PRINTK("tick_last1=%d\n",tick_last1);
}
void Timer2_Callback(uint32_t arg) // Callback function 2
{
unsigned long tick_last2;
tick_last2=(UINT32)LOS_TickCountGet();
g_timercount2 ++;
PRINTK("g_timercount2=%d\n",g_timercount2);
PRINTK("tick_last2=%d\n",tick_last2);
}
void Timer_example(void)
{
UINT16 id1;
UINT16 id2; // timer id
UINT32 uwTick;
/* Create a one-shot software timer, with the number of ticks set to 1000. When the number of ticks reaches 1000, callback function 1 is executed. */
LOS_SwtmrCreate (1000, LOS_SWTMR_MODE_ONCE, Timer1_Callback, &id1, 1);
/* Create a periodic software timer and execute callback function 2 every 100 ticks. */
LOS_SwtmrCreate(100, LOS_SWTMR_MODE_PERIOD, Timer2_Callback, &id2, 1);
PRINTK("create Timer1 success\n");
LOS_SwtmrStart (id1); // Start the one-shot software timer.
dprintf("start Timer1 success\n");
LOS_TaskDelay(200); // Delay 200 ticks.
LOS_SwtmrTimeGet(id1, &uwTick); // Obtain the number of remaining ticks of the one-short software timer.
PRINTK("uwTick =%d\n", uwTick);
LOS_SwtmrStop(id1); // Stop the software timer.
PRINTK("stop Timer1 success\n");
LOS_SwtmrStart(id1);
LOS_TaskDelay(1000);
LOS_SwtmrDelete(id1); // Delete the software timer.
PRINTK("delete Timer1 success\n");
LOS_SwtmrStart(id2); // Start the periodic software timer.
PRINTK("start Timer2\n");
LOS_TaskDelay(1000);
LOS_SwtmrStop(id2);
LOS_SwtmrDelete(id2);
}
Output
create Timer1 success
start Timer1 success
uwTick =800
stop Timer1 success
g_timercount1=1
tick_last1=1201
delete Timer1 success
start Timer2
g_timercount2 =1
tick_last1=1301
g_timercount2 =2
tick_last1=1401
g_timercount2 =3
tick_last1=1501
g_timercount2 =4
tick_last1=1601
g_timercount2 =5
tick_last1=1701
g_timercount2 =6
tick_last1=1801
g_timercount2 =7
tick_last1=1901
g_timercount2 =8
tick_last1=2001
g_timercount2 =9
tick_last1=2101
g_timercount2 =10
tick_last1=2201
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。