更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_swtmr.h
浏览该文件的文档.
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/**
33 * @defgroup los_swtmr Software timer
34 * @ingroup kernel
35 */
36
37#ifndef _LOS_SWTMR_H
38#define _LOS_SWTMR_H
39
40#include "los_base.h"
41#include "los_task.h"
42#include "los_sortlink_pri.h"
43
44#ifdef __cplusplus
45#if __cplusplus
46extern "C" {
47#endif /* __cplusplus */
48#endif /* __cplusplus */
49
50/**
51 * @ingroup los_swtmr
52 * Software timer error code: The timeout handling function is NULL.
53 *
54 * Value: 0x02000300
55 *
56 * Solution: Define the timeout handling function.
57 */ //软件定时器回调函数为空
58#define LOS_ERRNO_SWTMR_PTR_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x00)
59
60/**
61 * @ingroup los_swtmr
62 * Software timer error code: The expiration time is 0.
63 *
64 * Value: 0x02000301
65 *
66 * Solution: Re-define the expiration time.
67 */ //软件定时器的定时时长为0
68#define LOS_ERRNO_SWTMR_INTERVAL_NOT_SUITED LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x01)
69
70/**
71 * @ingroup los_swtmr
72 * Software timer error code: Invalid software timer mode.
73 *
74 * Value: 0x02000302
75 *
76 * Solution: Check the mode value. The value range is [0,3].
77 */ //不正确的软件定时器模式
78#define LOS_ERRNO_SWTMR_MODE_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x02)
79
80/**
81 * @ingroup los_swtmr
82 * Software timer error code: The passed-in software timer ID is NULL.
83 *
84 * Value: 0x02000303
85 *
86 * Solution: Define the software timer ID before passing it in.
87 */ //入参的软件定时器ID指针为NULL
88#define LOS_ERRNO_SWTMR_RET_PTR_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x03)
89
90/**
91 * @ingroup los_swtmr
92 * Software timer error code: The number of software timers exceeds the configured permitted maximum number.
93 *
94 * Value: 0x02000304
95 *
96 * Solution: Re-configure the permitted maximum number of software timers, or wait for a software timer to become
97 * available.
98 */ //软件定时器个数超过最大值
99#define LOS_ERRNO_SWTMR_MAXSIZE LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x04)
100
101/**
102 * @ingroup los_swtmr
103 * Software timer error code: Invalid software timer ID.
104 *
105 * Value: 0x02000305
106 *
107 * Solution: Pass in a valid software timer ID.
108 */ //入参的软件定时器ID不正确
109#define LOS_ERRNO_SWTMR_ID_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x05)
110
111/**
112 * @ingroup los_swtmr
113 * Software timer error code: The software timer is not created.
114 *
115 * Value: 0x02000306
116 *
117 * Solution: Create a software timer.
118 */ //软件定时器未创建
119#define LOS_ERRNO_SWTMR_NOT_CREATED LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x06)
120
121/**
122 * @ingroup los_swtmr
123 * Software timer error code: Insufficient memory for software timer linked list creation.
124 *
125 * Value: 0x02000307
126 *
127 * Solution: Allocate bigger memory partition to software timer linked list creation.
128 */ //初始化软件定时器模块时,内存不足
129#define LOS_ERRNO_SWTMR_NO_MEMORY LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x07)
130
131/**
132 * @ingroup los_swtmr
133 * Software timer error code: Invalid configured number of software timers.
134 *
135 * Value: 0x02000308
136 *
137 * Solution: Re-configure the number of software timers.
138 */ //配置的软件计时器数无效
139#define LOS_ERRNO_SWTMR_MAXSIZE_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x08)
140
141/**
142 * @ingroup los_swtmr
143 * Software timer error code: The software timer is being used during an interrupt.
144 *
145 * Value: 0x02000309
146 *
147 * Solution: Change the source code and do not use the software timer during an interrupt.
148 */ //在中断中使用定时器-修改源代码确保不在中断中使用
149#define LOS_ERRNO_SWTMR_HWI_ACTIVE LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x09)
150
151/**
152 * @ingroup los_swtmr
153 * Software timer error code: Insufficient memory allocated by membox.
154 *
155 * Value: 0x0200030a
156 *
157 * Solution: Expand the memory allocated by membox.
158 */ //membox分配的内存不足
159#define LOS_ERRNO_SWTMR_HANDLER_POOL_NO_MEM LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x0a)
160
161/**
162 * @ingroup los_swtmr
163 * Software timer error code: The software timer queue fails to be created.
164 *
165 * Value: 0x0200030b
166 *
167 * Solution: Check whether more memory can be allocated to the queue to be created.
168 */ //在软件定时器初始化时,创建定时器队列失败 - 调整OS_SYS_MEM_SIZE,以确保有足够的内存供软件定时器创建队列
169#define LOS_ERRNO_SWTMR_QUEUE_CREATE_FAILED LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x0b)
170
171/**
172 * @ingroup los_swtmr
173 * Software timer error code: The software timer task fails to be created.
174 *
175 * Value: 0x0200030c
176 *
177 * Solution: Check whether the memory is sufficient and re-create the task.
178 */ //在软件定时器初始化时,创建定时器任务失败 - 调整OS_SYS_MEM_SIZE,以确保有足够的内存供软件定时器创建任务
179#define LOS_ERRNO_SWTMR_TASK_CREATE_FAILED LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x0c)
180
181/**
182 * @ingroup los_swtmr
183 * Software timer error code: The software timer is not started.
184 *
185 * Value: 0x0200030d
186 *
187 * Solution: Start the software timer.
188 */ //未启动软件定时器 - 启动软件定时器
189#define LOS_ERRNO_SWTMR_NOT_STARTED LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x0d)
190
191/**
192 * @ingroup los_swtmr
193 * Software timer error code: Invalid software timer state.
194 *
195 * Value: 0x0200030e
196 *
197 * Solution: Check the software timer state.
198 */ //不正确的软件定时器状态 - 检查确认软件定时器状态
199#define LOS_ERRNO_SWTMR_STATUS_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x0e)
200
201/**
202 * @ingroup los_swtmr
203 * This error code is not in use temporarily.
204 */ //暂不使用该错误码
205#define LOS_ERRNO_SWTMR_SORTLIST_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x0f)
206
207/**
208 * @ingroup los_swtmr
209 * Software timer error code: The passed-in number of remaining Ticks configured on the software timer is NULL.
210 *
211 * Value: 0x02000310
212 *
213 * Solution: Define a variable of the number of remaining Ticks before passing in the number of remaining Ticks.
214 */ //用以获取软件定时器剩余Tick数的入参指针为NULL
215#define LOS_ERRNO_SWTMR_TICK_PTR_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x10)
216
217/**
218 * @ingroup los_swtmr
219 * Software timer error code: The software sortlink fails to be created.
220 *
221 * Value: 0x02000311
222 *
223 * Solution: Check whether the memory is sufficient and re-create the sortlink.
224 */ //在软件定时器初始化时,创建定时器链表失败
225#define LOS_ERRNO_SWTMR_SORTLINK_CREATE_FAILED LOS_ERRNO_OS_ERROR(LOS_MOD_SWTMR, 0x11)
226
227/**
228 * @ingroup los_swtmr
229 * Software timer mode
230 */
232 LOS_SWTMR_MODE_ONCE, /**< One-off software timer | 一次性的软件计时器*/
233 LOS_SWTMR_MODE_PERIOD, /**< Periodic software timer | 周期性的软件计时器*/
234 LOS_SWTMR_MODE_NO_SELFDELETE, /**< One-off software timer, but not self-delete | 一次性软件计时器,但不能自删除*/
235 LOS_SWTMR_MODE_OPP /**< After the one-off timer finishes timing,
236 the periodic software timer is enabled.
237 This mode is not supported temporarily. | 一次性完成后启用周期性软件计时器,鸿蒙目前暂时不支持这种方式*/
239
240/**
241* @ingroup los_swtmr
242* @brief Define the type of a callback function that handles software timer timeout.
243*
244* @par Description:
245* This API is used to define the type of a callback function that handles software timer timeout, so that it can be
246* called when software timer timeout.
247*
248* @attention
249* <ul>
250* <li>None.</li>
251* </ul>
252*
253* @param arg [IN] the parameter of the callback function that handles software timer timeout.
254*
255* @retval None.
256* @par Dependency:
257* <ul><li>los_swtmr.h: the header file that contains the API declaration.</li></ul>
258* @see None.
259*/
260typedef VOID (*SWTMR_PROC_FUNC)(UINTPTR arg); //函数指针, 赋值给 SWTMR_CTRL_S->pfnHandler,回调处理
261
262/**
263 * @ingroup los_swtmr
264 * Software timer control structure | 软件定时器控制块
265 * \n 变量前缀 uc:UINT8 us:UINT16 uw:UINT32 代表的意思
266 */
267typedef struct tagSwTmrCtrl {
268 SortLinkList stSortList; ///< 通过它挂到对应CPU核定时器链表上
269 UINT8 ucState; /**< Software timer state | 软件定时器的状态*/
270 UINT8 ucMode; /**< Software timer mode | 软件定时器的模式*/
271 UINT16 usTimerID; /**< Software timer ID | 软件定时器ID,唯一标识,由软件计时器池分配*/
272 UINT32 uwOverrun; /**< Times that a software timer repeats timing | 软件定时器重复执行的次数*/
273 UINT32 uwCount; /**< Times that a software timer works | 软件定时器工作的时间*/
274 UINT32 uwInterval; /**< Timeout interval of a periodic software timer | 周期性软件定时器的超时间隔*/
275 UINT32 uwExpiry; /**< Timeout interval of an one-off software timer | 一次性软件定时器的超时间隔*/
276 UINTPTR uwArg; /**< Parameter passed in when the callback function
277 that handles software timer timeout is called | 回调函数的参数*/
278 SWTMR_PROC_FUNC pfnHandler; /**< Callback function that handles software timer timeout | 处理软件计时器超时的回调函数*/
279 UINT32 uwOwnerPid; /**< Owner of this software timer | 软件定时器所属进程ID号*/
280 UINT64 startTime; /**< Software timer start time | 定时器开始时间 */
282
283/**
284 * @ingroup los_swtmr
285 * @brief Start a software timer.
286 *
287 * @par Description:
288 * This API is used to start a software timer that has a specified ID.
289 * @attention
290 * <ul>
291 * <li>The specific timer must be created first</li>
292 * </ul>
293 *
294 * @param swtmrID [IN] Software timer ID created by LOS_SwtmrCreate. The value of ID should be in
295 * [0, LOSCFG_BASE_CORE_SWTMR_LIMIT - 1].
296 *
297 * @retval #LOS_ERRNO_SWTMR_ID_INVALID Invalid software timer ID.
298 * @retval #LOS_ERRNO_SWTMR_NOT_CREATED The software timer is not created.
299 * @retval #LOS_ERRNO_SWTMR_STATUS_INVALID Invalid software timer state.
300 * @retval #LOS_OK The software timer is successfully started.
301 * @par Dependency:
302 * <ul><li>los_swtmr.h: the header file that contains the API declaration.</li></ul>
303 * @see LOS_SwtmrStop | LOS_SwtmrCreate
304 */
305extern UINT32 LOS_SwtmrStart(UINT16 swtmrID);
306
307/**
308 * @ingroup los_swtmr
309 * @brief Stop a software timer.
310 *
311 * @par Description:
312 * This API is used to stop a software timer that has a specified ID.
313 * @attention
314 * <ul>
315 * <li>The specific timer should be created and started firstly.</li>
316 * </ul>
317 *
318 * @param swtmrID [IN] Software timer ID created by LOS_SwtmrCreate. The value of ID should be in
319 * [0, LOSCFG_BASE_CORE_SWTMR_LIMIT - 1].
320 *
321 * @retval #LOS_ERRNO_SWTMR_ID_INVALID Invalid software timer ID.
322 * @retval #LOS_ERRNO_SWTMR_NOT_CREATED The software timer is not created.
323 * @retval #LOS_ERRNO_SWTMR_NOT_STARTED The software timer is not started.
324 * @retval #LOS_ERRNO_SWTMR_STATUS_INVALID Invalid software timer state.
325 * @retval #LOS_OK The software timer is successfully stopped.
326 * @par Dependency:
327 * <ul><li>los_swtmr.h: the header file that contains the API declaration.</li></ul>
328 * @see LOS_SwtmrStart | LOS_SwtmrCreate
329 */
330extern UINT32 LOS_SwtmrStop(UINT16 swtmrID);
331
332/**
333 * @ingroup los_swtmr
334 * @brief Obtain the number of remaining Ticks configured on a software timer.
335 *
336 * @par Description:
337 * This API is used to obtain the number of remaining Ticks configured on the software timer of which the ID is
338 * specified by usSwTmrID.
339 * @attention
340 * <ul>
341 * <li>The specific timer should be created and started successfully, error happends otherwise.</li>
342 * </ul>
343 *
344 * @param swtmrID [IN] Software timer ID created by LOS_SwtmrCreate. The value of ID should be in
345 * [0, LOSCFG_BASE_CORE_SWTMR_LIMIT - 1].
346 * @param tick [OUT] Number of remaining Ticks configured on the software timer.
347 *
348 * @retval #LOS_ERRNO_SWTMR_ID_INVALID Invalid software timer ID.
349 * @retval #LOS_ERRNO_SWTMR_NOT_CREATED The software timer is not created.
350 * @retval #LOS_ERRNO_SWTMR_NOT_STARTED The software timer is not started.
351 * @retval #LOS_ERRNO_SWTMR_STATUS_INVALID Invalid software timer state.
352 * @retval #LOS_OK The number of remaining Ticks is successfully obtained.
353 * @par Dependency:
354 * <ul><li>los_swtmr.h: the header file that contains the API declaration.</li></ul>
355 * @see LOS_SwtmrCreate
356 */
357extern UINT32 LOS_SwtmrTimeGet(UINT16 swtmrID, UINT32 *tick);
358
359/**
360 * @ingroup los_swtmr
361 * @brief Create a software timer.
362 *
363 * @par Description:
364 * This API is used to create a software timer that has specified timing duration, timeout handling function,
365 * and trigger mode, and to return a handle by which the software timer can be referenced.
366 * @attention
367 * <ul>
368 * <li>Do not use the delay interface in the callback function that handles software timer timeout.</li>
369 * <li>Threre are LOSCFG_BASE_CORE_SWTMR_LIMIT timers available, change it's value when necessory.</li>
370 * </ul>
371 *
372 * @param interval [IN] Timing duration of the software timer to be created (unit: tick).
373 * @param mode [IN] Software timer mode. Pass in one of the modes specified by enSwTmrType. There are three
374 * types of modes, one-off, periodic, and continuously periodic after one-off, of which the third mode is not
375 * supported temporarily.
376 * @param handler [IN] Callback function that handles software timer timeout.
377 * @param swtmrID [OUT] Software timer ID created by LOS_SwtmrCreate.
378 * @param arg [IN] Parameter passed in when the callback function that handles software timer timeout is
379 * called.
380 *
381 * @retval #LOS_ERRNO_SWTMR_INTERVAL_NOT_SUITED The software timer timeout interval is 0.
382 * @retval #LOS_ERRNO_SWTMR_MODE_INVALID Invalid software timer mode.
383 * @retval #LOS_ERRNO_SWTMR_PTR_NULL The callback function that handles software timer timeout is NULL.
384 * @retval #LOS_ERRNO_SWTMR_RET_PTR_NULL The passed-in software timer ID is NULL.
385 * @retval #LOS_ERRNO_SWTMR_MAXSIZE The number of software timers exceeds the configured permitted
386 * maximum number.
387 * @retval #LOS_OK The software timer is successfully created.
388 * @par Dependency:
389 * <ul><li>los_swtmr.h: the header file that contains the API declaration.</li></ul>
390 * @see LOS_SwtmrDelete
391 */
392extern UINT32 LOS_SwtmrCreate(UINT32 interval, UINT8 mode, SWTMR_PROC_FUNC handler, UINT16 *swtmrID, UINTPTR arg);
393
394/**
395 * @ingroup los_swtmr
396 * @brief Delete a software timer.
397 *
398 * @par Description:
399 * This API is used to delete a software timer.
400 * @attention
401 * <ul>
402 * <li>The specific timer should be created and then stopped firstly.</li>
403 * </ul>
404 *
405 * @param swtmrID [IN] Software timer ID created by LOS_SwtmrCreate. The value of ID should be in
406 * [0, LOSCFG_BASE_CORE_SWTMR_LIMIT - 1].
407 *
408 * @retval #LOS_ERRNO_SWTMR_ID_INVALID Invalid software timer ID.
409 * @retval #LOS_ERRNO_SWTMR_NOT_CREATED The software timer is not created.
410 * @retval #LOS_ERRNO_SWTMR_STATUS_INVALID Invalid software timer state.
411 * @retval #LOS_OK The software timer is successfully deleted.
412 * @par Dependency:
413 * <ul><li>los_swtmr.h: the header file that contains the API declaration.</li></ul>
414 * @see LOS_SwtmrCreate
415 */
416extern UINT32 LOS_SwtmrDelete(UINT16 swtmrID);
417
418#ifdef __cplusplus
419#if __cplusplus
420}
421#endif /* __cplusplus */
422#endif /* __cplusplus */
423
424#endif /* _LOS_SWTMR_H */
struct tagSwTmrCtrl SWTMR_CTRL_S
UINT32 LOS_SwtmrStop(UINT16 swtmrID)
Stop a software timer.
Definition: los_swtmr.c:808
UINT32 LOS_SwtmrStart(UINT16 swtmrID)
Start a software timer.
Definition: los_swtmr.c:764
UINT32 LOS_SwtmrTimeGet(UINT16 swtmrID, UINT32 *tick)
Obtain the number of remaining Ticks configured on a software timer.
Definition: los_swtmr.c:848
VOID(* SWTMR_PROC_FUNC)(UINTPTR arg)
Define the type of a callback function that handles software timer timeout.
Definition: los_swtmr.h:260
UINT32 LOS_SwtmrDelete(UINT16 swtmrID)
Delete a software timer.
Definition: los_swtmr.c:889
enSwTmrType
Definition: los_swtmr.h:231
UINT32 LOS_SwtmrCreate(UINT32 interval, UINT8 mode, SWTMR_PROC_FUNC handler, UINT16 *swtmrID, UINTPTR arg)
Create a software timer.
Definition: los_swtmr.c:712
@ LOS_SWTMR_MODE_PERIOD
Definition: los_swtmr.h:233
@ LOS_SWTMR_MODE_OPP
Definition: los_swtmr.h:235
@ LOS_SWTMR_MODE_ONCE
Definition: los_swtmr.h:232
@ LOS_SWTMR_MODE_NO_SELFDELETE
Definition: los_swtmr.h:234
unsigned short UINT16
Definition: los_typedef.h:56
long unsigned int UINT64
Definition: los_typedef.h:66
unsigned char UINT8
Definition: los_typedef.h:55
unsigned long UINTPTR
Definition: los_typedef.h:68
unsigned int UINT32
Definition: los_typedef.h:57
UINT8 ucState
Definition: los_swtmr.h:269
UINT16 usTimerID
Definition: los_swtmr.h:271
UINTPTR uwArg
Definition: los_swtmr.h:276
UINT32 uwOverrun
Definition: los_swtmr.h:272
UINT8 ucMode
Definition: los_swtmr.h:270
UINT32 uwOwnerPid
Definition: los_swtmr.h:279
UINT64 startTime
Definition: los_swtmr.h:280
UINT32 uwInterval
Definition: los_swtmr.h:274
UINT32 uwCount
Definition: los_swtmr.h:273
UINT32 uwExpiry
Definition: los_swtmr.h:275
SortLinkList stSortList
通过它挂到对应CPU核定时器链表上
Definition: los_swtmr.h:268
SWTMR_PROC_FUNC pfnHandler
Definition: los_swtmr.h:278