1# Software Timer 2 3 4## Basic Concepts<a name="section4118241563"></a> 5 6The 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. 7 8The software timer supports the following functions: 9 10- Disabling the software timer using a macro 11- Creating a software timer 12- Starting a software timer 13- Stopping a software timer 14- Deleting a software timer 15- Obtaining the number of remaining ticks of a software timer 16 17## Working Principles<a name="section31079397569"></a> 18 19The 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. 20 21When 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. 22 23When 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. 24 25Timer States 26 27- OS\_SWTMR\_STATUS\_UNUSED 28 29 The timer is not in use. When the timer module is initialized, all timer resources in the system are set to this state. 30 31- OS\_SWTMR\_STATUS\_CREATED 32 33 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. 34 35- OS\_SWTMR\_STATUS\_TICKING 36 37 The timer is running \(counting\). When **LOS\_SwtmrStart** is called for a newly created timer, the timer enters this state. 38 39 40Timer Modes 41 42The OpenHarmony provides three types of software timers: 43 44- One-shot timer: Once started, the timer is automatically deleted after triggering only one timer event. 45- Periodic timer: This type of timer periodically triggers timer events until it is manually stopped. 46- One-shot timer deleted by calling an API 47 48## Development Guidelines<a name="section18576131520577"></a> 49 50### Available APIs<a name="section3138019145719"></a> 51 52The following table describes APIs available for the OpenHarmony LiteOS-A software timer module. For more details about the APIs, see the API reference. 53 54**Table 1** Software timer APIs 55 56<a name="table107038227425"></a> 57<table><thead align="left"><tr id="row2704122217420"><th class="cellrowborder" valign="top" width="20.6020602060206%" id="mcps1.2.4.1.1"><p id="p57041622144212"><a name="p57041622144212"></a><a name="p57041622144212"></a>Function</p> 58</th> 59<th class="cellrowborder" valign="top" width="29.542954295429542%" id="mcps1.2.4.1.2"><p id="p19704142216424"><a name="p19704142216424"></a><a name="p19704142216424"></a>API</p> 60</th> 61<th class="cellrowborder" valign="top" width="49.85498549854985%" id="mcps1.2.4.1.3"><p id="p670412224217"><a name="p670412224217"></a><a name="p670412224217"></a>Description</p> 62</th> 63</tr> 64</thead> 65<tbody><tr id="row1470413222429"><td class="cellrowborder" rowspan="2" valign="top" width="20.6020602060206%" headers="mcps1.2.4.1.1 "><p id="p8284115918428"><a name="p8284115918428"></a><a name="p8284115918428"></a>Creating or deleting timers</p> 66</td> 67<td class="cellrowborder" valign="top" width="29.542954295429542%" headers="mcps1.2.4.1.2 "><p id="p117045225428"><a name="p117045225428"></a><a name="p117045225428"></a>LOS_SwtmrCreate</p> 68</td> 69<td class="cellrowborder" valign="top" width="49.85498549854985%" headers="mcps1.2.4.1.3 "><p id="p170422234213"><a name="p170422234213"></a><a name="p170422234213"></a>Creates a software timer.</p> 70</td> 71</tr> 72<tr id="row11704102217425"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1970492213426"><a name="p1970492213426"></a><a name="p1970492213426"></a>LOS_SwtmrDelete</p> 73</td> 74<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p870442211421"><a name="p870442211421"></a><a name="p870442211421"></a>Deletes a software timer.</p> 75</td> 76</tr> 77<tr id="row57041422184215"><td class="cellrowborder" rowspan="2" valign="top" width="20.6020602060206%" headers="mcps1.2.4.1.1 "><p id="p1476172124318"><a name="p1476172124318"></a><a name="p1476172124318"></a>Starting or stopping timers</p> 78</td> 79<td class="cellrowborder" valign="top" width="29.542954295429542%" headers="mcps1.2.4.1.2 "><p id="p167040225423"><a name="p167040225423"></a><a name="p167040225423"></a>LOS_SwtmrStart</p> 80</td> 81<td class="cellrowborder" valign="top" width="49.85498549854985%" headers="mcps1.2.4.1.3 "><p id="p1570412229421"><a name="p1570412229421"></a><a name="p1570412229421"></a>Starts a software timer.</p> 82</td> 83</tr> 84<tr id="row15704172224219"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1170422214216"><a name="p1170422214216"></a><a name="p1170422214216"></a>LOS_SwtmrStop</p> 85</td> 86<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p17705122211426"><a name="p17705122211426"></a><a name="p17705122211426"></a>Stops a software timer.</p> 87</td> 88</tr> 89<tr id="row12705222154214"><td class="cellrowborder" valign="top" width="20.6020602060206%" headers="mcps1.2.4.1.1 "><p id="p0705622134214"><a name="p0705622134214"></a><a name="p0705622134214"></a>Obtaining remaining ticks of a software timer</p> 90</td> 91<td class="cellrowborder" valign="top" width="29.542954295429542%" headers="mcps1.2.4.1.2 "><p id="p177052220424"><a name="p177052220424"></a><a name="p177052220424"></a>LOS_SwtmrTimeGet</p> 92</td> 93<td class="cellrowborder" valign="top" width="49.85498549854985%" headers="mcps1.2.4.1.3 "><p id="p3705122264210"><a name="p3705122264210"></a><a name="p3705122264210"></a>Obtains the number of remaining ticks of a software timer.</p> 94</td> 95</tr> 96</tbody> 97</table> 98 99### How to Develop<a name="section1344817403575"></a> 100 101The typical development process of software timers is as follows: 102 1031. Configure the software timer. 104 - Check that **LOSCFG\_BASE\_CORE\_SWTMR** and **LOSCFG\_BASE\_IPC\_QUEUE** are enabled. 105 - Configure **LOSCFG\_BASE\_CORE\_SWTMR\_LIMIT** \(maximum number of software timers supported by the system\). 106 - Configure **OS\_SWTMR\_HANDLE\_QUEUE\_SIZE** \(maximum length of the software timer queue\). 107 1082. Call **LOS\_SwtmrCreate** to create a software timer. 109 - Create a software timer with the specified timing duration, timeout handling function, and triggering mode. 110 - Return the function execution result \(success or failure\). 111 1123. Call **LOS\_SwtmrStart** to start the software timer. 1134. Call **LOS\_SwtmrTimeGet** to obtain the remaining number of ticks of the software timer. 1145. Call **LOS\_SwtmrStop** to stop the software timer. 1156. Call **LOS\_SwtmrDelete** to delete the software timer. 116 117> **NOTE:** 118>- 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. 119>- 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. 120>- 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. 121>- 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. 122>- 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. 123 124### Development Example<a name="section114416313585"></a> 125 126Prerequisites: 127 128- In **los\_config.h**, **LOSCFG\_BASE\_CORE\_SWTMR** is enabled. 129- The maximum number of software timers supported by the system \(**LOSCFG\_BASE\_CORE\_SWTMR\_LIMIT**\) is configured. 130- The maximum length of the software timer queue \(**OS\_SWTMR\_HANDLE\_QUEUE\_SIZE**\) is configured. 131 132**Sample Code** 133 134``` 135#include "los_swtmr.h" 136 137void Timer1_Callback(uint32_t arg); 138void Timer2_Callback(uint32_t arg); 139 140UINT32 g_timercount1 = 0; 141UINT32 g_timercount2 = 0; 142 143void Timer1_Callback(uint32_t arg) // Callback function 1 144{ 145 unsigned long tick_last1; 146 g_timercount1++; 147 tick_last1=(UINT32)LOS_TickCountGet(); // Obtain the current number of ticks. 148 PRINTK("g_timercount1=%d\n",g_timercount1); 149 PRINTK("tick_last1=%d\n",tick_last1); 150} 151 152void Timer2_Callback(uint32_t arg) // Callback function 2 153{ 154 unsigned long tick_last2; 155 tick_last2=(UINT32)LOS_TickCountGet(); 156 g_timercount2 ++; 157 PRINTK("g_timercount2=%d\n",g_timercount2); 158 PRINTK("tick_last2=%d\n",tick_last2); 159} 160 161void Timer_example(void) 162{ 163 UINT16 id1; 164 UINT16 id2; // timer id 165 UINT32 uwTick; 166 167 /* 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. */ 168 LOS_SwtmrCreate (1000, LOS_SWTMR_MODE_ONCE, Timer1_Callback, &id1, 1); 169 170 /* Create a periodic software timer and execute callback function 2 every 100 ticks. */ 171 LOS_SwtmrCreate(100, LOS_SWTMR_MODE_PERIOD, Timer2_Callback, &id2, 1); 172 PRINTK("create Timer1 success\n"); 173 174 LOS_SwtmrStart (id1); // Start the one-shot software timer. 175 dprintf("start Timer1 success\n"); 176 LOS_TaskDelay(200); // Delay 200 ticks. 177 LOS_SwtmrTimeGet(id1, &uwTick); // Obtain the number of remaining ticks of the one-short software timer. 178 PRINTK("uwTick =%d\n", uwTick); 179 180 LOS_SwtmrStop(id1); // Stop the software timer. 181 PRINTK("stop Timer1 success\n"); 182 183 LOS_SwtmrStart(id1); 184 LOS_TaskDelay(1000); 185 LOS_SwtmrDelete(id1); // Delete the software timer. 186 PRINTK("delete Timer1 success\n"); 187 188 LOS_SwtmrStart(id2); // Start the periodic software timer. 189 PRINTK("start Timer2\n"); 190 191 LOS_TaskDelay(1000); 192 LOS_SwtmrStop(id2); 193 LOS_SwtmrDelete(id2); 194} 195``` 196 197**Output** 198 199``` 200create Timer1 success 201start Timer1 success 202uwTick =800 203stop Timer1 success 204g_timercount1=1 205tick_last1=1201 206delete Timer1 success 207start Timer2 208g_timercount2 =1 209tick_last1=1301 210g_timercount2 =2 211tick_last1=1401 212g_timercount2 =3 213tick_last1=1501 214g_timercount2 =4 215tick_last1=1601 216g_timercount2 =5 217tick_last1=1701 218g_timercount2 =6 219tick_last1=1801 220g_timercount2 =7 221tick_last1=1901 222g_timercount2 =8 223tick_last1=2001 224g_timercount2 =9 225tick_last1=2101 226g_timercount2 =10 227tick_last1=2201 228``` 229 230