更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
Software timer
Software timer 的协作图:

结构体

struct  tagSwTmrCtrl
 

类型定义

typedef VOID(* SWTMR_PROC_FUNC) (UINTPTR arg)
 Define the type of a callback function that handles software timer timeout. 更多...
 
typedef struct tagSwTmrCtrl SWTMR_CTRL_S
 

枚举

enum  enSwTmrType { LOS_SWTMR_MODE_ONCE , LOS_SWTMR_MODE_PERIOD , LOS_SWTMR_MODE_NO_SELFDELETE , LOS_SWTMR_MODE_OPP }
 

函数

UINT32 LOS_SwtmrStart (UINT16 swtmrID)
 Start a software timer. 更多...
 
UINT32 LOS_SwtmrStop (UINT16 swtmrID)
 Stop a software timer. 更多...
 
UINT32 LOS_SwtmrTimeGet (UINT16 swtmrID, UINT32 *tick)
 Obtain the number of remaining Ticks configured on a software timer. 更多...
 
UINT32 LOS_SwtmrCreate (UINT32 interval, UINT8 mode, SWTMR_PROC_FUNC handler, UINT16 *swtmrID, UINTPTR arg)
 Create a software timer. 更多...
 
UINT32 LOS_SwtmrDelete (UINT16 swtmrID)
 Delete a software timer. 更多...
 

详细描述

类型定义说明

◆ SWTMR_CTRL_S

typedef struct tagSwTmrCtrl SWTMR_CTRL_S

Software timer control structure | 软件定时器控制块
变量前缀 uc:UINT8 us:UINT16 uw:UINT32 代表的意思

◆ SWTMR_PROC_FUNC

typedef VOID(* SWTMR_PROC_FUNC) (UINTPTR arg)

Define the type of a callback function that handles software timer timeout.

Description:
This API is used to define the type of a callback function that handles software timer timeout, so that it can be called when software timer timeout.
注意
  • None.
参数
arg[IN] the parameter of the callback function that handles software timer timeout.
返回值
None.
Dependency:
  • los_swtmr.h: the header file that contains the API declaration.
参见
None.

在文件 los_swtmr.h260 行定义.

枚举类型说明

◆ enSwTmrType

Software timer mode

枚举值
LOS_SWTMR_MODE_ONCE 

One-off software timer | 一次性的软件计时器

LOS_SWTMR_MODE_PERIOD 

Periodic software timer | 周期性的软件计时器

LOS_SWTMR_MODE_NO_SELFDELETE 

One-off software timer, but not self-delete | 一次性软件计时器,但不能自删除

LOS_SWTMR_MODE_OPP 

After the one-off timer finishes timing, the periodic software timer is enabled. This mode is not supported temporarily. | 一次性完成后启用周期性软件计时器,鸿蒙目前暂时不支持这种方式

在文件 los_swtmr.h231 行定义.

231 {
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. | 一次性完成后启用周期性软件计时器,鸿蒙目前暂时不支持这种方式*/
238};
@ 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

函数说明

◆ LOS_SwtmrCreate()

UINT32 LOS_SwtmrCreate ( UINT32  interval,
UINT8  mode,
SWTMR_PROC_FUNC  handler,
UINT16 swtmrID,
UINTPTR  arg 
)

Create a software timer.

Description:
This API is used to create a software timer that has specified timing duration, timeout handling function, and trigger mode, and to return a handle by which the software timer can be referenced.
注意
  • Do not use the delay interface in the callback function that handles software timer timeout.
  • Threre are LOSCFG_BASE_CORE_SWTMR_LIMIT timers available, change it's value when necessory.
参数
interval[IN] Timing duration of the software timer to be created (unit: tick).
mode[IN] Software timer mode. Pass in one of the modes specified by enSwTmrType. There are three types of modes, one-off, periodic, and continuously periodic after one-off, of which the third mode is not supported temporarily.
handler[IN] Callback function that handles software timer timeout.
swtmrID[OUT] Software timer ID created by LOS_SwtmrCreate.
arg[IN] Parameter passed in when the callback function that handles software timer timeout is called.
返回值
#LOS_ERRNO_SWTMR_INTERVAL_NOT_SUITEDThe software timer timeout interval is 0.
#LOS_ERRNO_SWTMR_MODE_INVALIDInvalid software timer mode.
#LOS_ERRNO_SWTMR_PTR_NULLThe callback function that handles software timer timeout is NULL.
#LOS_ERRNO_SWTMR_RET_PTR_NULLThe passed-in software timer ID is NULL.
#LOS_ERRNO_SWTMR_MAXSIZEThe number of software timers exceeds the configured permitted maximum number.
#LOS_OKThe software timer is successfully created.
Dependency:
  • los_swtmr.h: the header file that contains the API declaration.
参见
LOS_SwtmrDelete

Create a software timer.

在文件 los_swtmr.c712 行定义.

717{
718 SWTMR_CTRL_S *swtmr = NULL;
719 UINT32 intSave;
720 SortLinkList *sortList = NULL;
721
722 if (interval == 0) {
723 return LOS_ERRNO_SWTMR_INTERVAL_NOT_SUITED;
724 }
725
726 if ((mode != LOS_SWTMR_MODE_ONCE) && (mode != LOS_SWTMR_MODE_PERIOD) &&
728 return LOS_ERRNO_SWTMR_MODE_INVALID;
729 }
730
731 if (handler == NULL) {
732 return LOS_ERRNO_SWTMR_PTR_NULL;
733 }
734
735 if (swtmrID == NULL) {
736 return LOS_ERRNO_SWTMR_RET_PTR_NULL;
737 }
738
739 SWTMR_LOCK(intSave);
740 if (LOS_ListEmpty(&g_swtmrFreeList)) {//空闲链表不能为空
741 SWTMR_UNLOCK(intSave);
742 return LOS_ERRNO_SWTMR_MAXSIZE;
743 }
744
745 sortList = LOS_DL_LIST_ENTRY(g_swtmrFreeList.pstNext, SortLinkList, sortLinkNode);
746 swtmr = LOS_DL_LIST_ENTRY(sortList, SWTMR_CTRL_S, stSortList);
747 LOS_ListDelete(LOS_DL_LIST_FIRST(&g_swtmrFreeList));//
748 SWTMR_UNLOCK(intSave);
749
750 swtmr->uwOwnerPid = OsCurrProcessGet()->processID;//定时器进程归属设定
751 swtmr->pfnHandler = handler;//时间到了的回调函数
752 swtmr->ucMode = mode; //定时器模式
753 swtmr->uwOverrun = 0;
754 swtmr->uwInterval = interval; //周期性超时间隔
755 swtmr->uwExpiry = interval; //一次性超时间隔
756 swtmr->uwArg = arg; //回调函数的参数
757 swtmr->ucState = OS_SWTMR_STATUS_CREATED; //已创建状态
758 SET_SORTLIST_VALUE(&swtmr->stSortList, OS_SORT_LINK_INVALID_TIME);
759 *swtmrID = swtmr->usTimerID;
760 OsHookCall(LOS_HOOK_TYPE_SWTMR_CREATE, swtmr);
761 return LOS_OK;
762}
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListDelete(LOS_DL_LIST *node)
Definition: los_list.h:292
LITE_OS_SEC_ALW_INLINE STATIC INLINE BOOL LOS_ListEmpty(LOS_DL_LIST *list)
Identify whether a specified doubly linked list is empty. | 判断链表是否为空
Definition: los_list.h:321
STATIC INLINE LosProcessCB * OsCurrProcessGet(VOID)
LITE_OS_SEC_BSS LOS_DL_LIST g_swtmrFreeList
Definition: los_swtmr.c:114
@ OS_SWTMR_STATUS_CREATED
Definition: los_swtmr_pri.h:57
unsigned int UINT32
Definition: los_typedef.h:57
struct LOS_DL_LIST * pstNext
Definition: los_list.h:84
UINT32 processID
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
UINT32 uwInterval
Definition: los_swtmr.h:274
UINT32 uwExpiry
Definition: los_swtmr.h:275
SortLinkList stSortList
通过它挂到对应CPU核定时器链表上
Definition: los_swtmr.h:268
SWTMR_PROC_FUNC pfnHandler
Definition: los_swtmr.h:278
函数调用图:
这是这个函数的调用关系图:

◆ LOS_SwtmrDelete()

UINT32 LOS_SwtmrDelete ( UINT16  swtmrID)

Delete a software timer.

Description:
This API is used to delete a software timer.
注意
  • The specific timer should be created and then stopped firstly.
参数
swtmrID[IN] Software timer ID created by LOS_SwtmrCreate. The value of ID should be in [0, LOSCFG_BASE_CORE_SWTMR_LIMIT - 1].
返回值
#LOS_ERRNO_SWTMR_ID_INVALIDInvalid software timer ID.
#LOS_ERRNO_SWTMR_NOT_CREATEDThe software timer is not created.
#LOS_ERRNO_SWTMR_STATUS_INVALIDInvalid software timer state.
#LOS_OKThe software timer is successfully deleted.
Dependency:
  • los_swtmr.h: the header file that contains the API declaration.
参见
LOS_SwtmrCreate

Delete a software timer.

在文件 los_swtmr.c889 行定义.

890{
891 SWTMR_CTRL_S *swtmr = NULL;
892 UINT32 intSave;
893 UINT32 ret = LOS_OK;
894 UINT16 swtmrCBID;
895
896 if (swtmrID >= OS_SWTMR_MAX_TIMERID) {
897 return LOS_ERRNO_SWTMR_ID_INVALID;
898 }
899
900 swtmrCBID = swtmrID % LOSCFG_BASE_CORE_SWTMR_LIMIT;//取模
901 swtmr = g_swtmrCBArray + swtmrCBID;//获取定时器控制结构体
902 SWTMR_LOCK(intSave);
903
904 if (swtmr->usTimerID != swtmrID) {//ID必须一样
905 SWTMR_UNLOCK(intSave);
906 return LOS_ERRNO_SWTMR_ID_INVALID;
907 }
908
909 switch (swtmr->ucState) {
911 ret = LOS_ERRNO_SWTMR_NOT_CREATED;
912 break;
913 case OS_SWTMR_STATUS_TICKING://正在计数就先停止再删除,这里没有break;
914 SwtmrStop(swtmr);
915 /* fall-through */
916 case OS_SWTMR_STATUS_CREATED://再删除定时器
917 SwtmrDelete(swtmr);
918 break;
919 default:
920 ret = LOS_ERRNO_SWTMR_STATUS_INVALID;
921 break;
922 }
923
924 SWTMR_UNLOCK(intSave);
925 OsHookCall(LOS_HOOK_TYPE_SWTMR_DELETE, swtmr);
926 return ret;
927}
LITE_OS_SEC_BSS SWTMR_CTRL_S * g_swtmrCBArray
Definition: los_swtmr.c:112
STATIC INLINE VOID SwtmrDelete(SWTMR_CTRL_S *swtmr)
Definition: los_swtmr.c:590
STATIC VOID SwtmrStop(SWTMR_CTRL_S *swtmr)
Definition: los_swtmr.c:690
@ OS_SWTMR_STATUS_UNUSED
Definition: los_swtmr_pri.h:56
@ OS_SWTMR_STATUS_TICKING
Definition: los_swtmr_pri.h:58
unsigned short UINT16
Definition: los_typedef.h:56
函数调用图:
这是这个函数的调用关系图:

◆ LOS_SwtmrStart()

UINT32 LOS_SwtmrStart ( UINT16  swtmrID)

Start a software timer.

Description:
This API is used to start a software timer that has a specified ID.
注意
  • The specific timer must be created first
参数
swtmrID[IN] Software timer ID created by LOS_SwtmrCreate. The value of ID should be in [0, LOSCFG_BASE_CORE_SWTMR_LIMIT - 1].
返回值
#LOS_ERRNO_SWTMR_ID_INVALIDInvalid software timer ID.
#LOS_ERRNO_SWTMR_NOT_CREATEDThe software timer is not created.
#LOS_ERRNO_SWTMR_STATUS_INVALIDInvalid software timer state.
#LOS_OKThe software timer is successfully started.
Dependency:
  • los_swtmr.h: the header file that contains the API declaration.
参见
LOS_SwtmrStop | LOS_SwtmrCreate

Start a software timer.

在文件 los_swtmr.c764 行定义.

765{
766 SWTMR_CTRL_S *swtmr = NULL;
767 UINT32 intSave;
768 UINT32 ret = LOS_OK;
769 UINT16 swtmrCBID;
770
771 if (swtmrID >= OS_SWTMR_MAX_TIMERID) {
772 return LOS_ERRNO_SWTMR_ID_INVALID;
773 }
774
775 swtmrCBID = swtmrID % LOSCFG_BASE_CORE_SWTMR_LIMIT;//取模
776 swtmr = g_swtmrCBArray + swtmrCBID;//获取定时器控制结构体
777
778 SWTMR_LOCK(intSave);
779 if (swtmr->usTimerID != swtmrID) {//ID必须一样
780 SWTMR_UNLOCK(intSave);
781 return LOS_ERRNO_SWTMR_ID_INVALID;
782 }
783
784 switch (swtmr->ucState) {//判断定时器状态
786 ret = LOS_ERRNO_SWTMR_NOT_CREATED;
787 break;
788 /* 如果定时器的状态为启动中,应先停止定时器再重新启动
789 * If the status of swtmr is timing, it should stop the swtmr first,
790 * then start the swtmr again.
791 */
792 case OS_SWTMR_STATUS_TICKING://正在计数的定时器
793 SwtmrStop(swtmr);//先停止定时器,注意这里没有break;,在OsSwtmrStop中状态将会回到了OS_SWTMR_STATUS_CREATED 接下来就是执行启动了
794 /* fall-through */
795 case OS_SWTMR_STATUS_CREATED://已经创建好了
796 SwtmrStart(swtmr);
797 break;
798 default:
799 ret = LOS_ERRNO_SWTMR_STATUS_INVALID;
800 break;
801 }
802
803 SWTMR_UNLOCK(intSave);
804 OsHookCall(LOS_HOOK_TYPE_SWTMR_START, swtmr);
805 return ret;
806}
STATIC INLINE VOID SwtmrStart(SWTMR_CTRL_S *swtmr)
Definition: los_swtmr.c:571
函数调用图:
这是这个函数的调用关系图:

◆ LOS_SwtmrStop()

UINT32 LOS_SwtmrStop ( UINT16  swtmrID)

Stop a software timer.

Description:
This API is used to stop a software timer that has a specified ID.
注意
  • The specific timer should be created and started firstly.
参数
swtmrID[IN] Software timer ID created by LOS_SwtmrCreate. The value of ID should be in [0, LOSCFG_BASE_CORE_SWTMR_LIMIT - 1].
返回值
#LOS_ERRNO_SWTMR_ID_INVALIDInvalid software timer ID.
#LOS_ERRNO_SWTMR_NOT_CREATEDThe software timer is not created.
#LOS_ERRNO_SWTMR_NOT_STARTEDThe software timer is not started.
#LOS_ERRNO_SWTMR_STATUS_INVALIDInvalid software timer state.
#LOS_OKThe software timer is successfully stopped.
Dependency:
  • los_swtmr.h: the header file that contains the API declaration.
参见
LOS_SwtmrStart | LOS_SwtmrCreate

Stop a software timer.

在文件 los_swtmr.c808 行定义.

809{
810 SWTMR_CTRL_S *swtmr = NULL;
811 UINT32 intSave;
812 UINT32 ret = LOS_OK;
813 UINT16 swtmrCBID;
814
815 if (swtmrID >= OS_SWTMR_MAX_TIMERID) {
816 return LOS_ERRNO_SWTMR_ID_INVALID;
817 }
818
819 swtmrCBID = swtmrID % LOSCFG_BASE_CORE_SWTMR_LIMIT;//取模
820 swtmr = g_swtmrCBArray + swtmrCBID;//获取定时器控制结构体
821 SWTMR_LOCK(intSave);
822
823 if (swtmr->usTimerID != swtmrID) {//ID必须一样
824 SWTMR_UNLOCK(intSave);
825 return LOS_ERRNO_SWTMR_ID_INVALID;
826 }
827
828 switch (swtmr->ucState) {//判断定时器状态
830 ret = LOS_ERRNO_SWTMR_NOT_CREATED;//返回没有创建
831 break;
833 ret = LOS_ERRNO_SWTMR_NOT_STARTED;//返回没有开始
834 break;
835 case OS_SWTMR_STATUS_TICKING://正在计数
836 SwtmrStop(swtmr);//执行正在停止定时器操作
837 break;
838 default:
839 ret = LOS_ERRNO_SWTMR_STATUS_INVALID;
840 break;
841 }
842
843 SWTMR_UNLOCK(intSave);
844 OsHookCall(LOS_HOOK_TYPE_SWTMR_STOP, swtmr);
845 return ret;
846}
函数调用图:
这是这个函数的调用关系图:

◆ LOS_SwtmrTimeGet()

UINT32 LOS_SwtmrTimeGet ( UINT16  swtmrID,
UINT32 tick 
)

Obtain the number of remaining Ticks configured on a software timer.

Description:
This API is used to obtain the number of remaining Ticks configured on the software timer of which the ID is specified by usSwTmrID.
注意
  • The specific timer should be created and started successfully, error happends otherwise.
参数
swtmrID[IN] Software timer ID created by LOS_SwtmrCreate. The value of ID should be in [0, LOSCFG_BASE_CORE_SWTMR_LIMIT - 1].
tick[OUT] Number of remaining Ticks configured on the software timer.
返回值
#LOS_ERRNO_SWTMR_ID_INVALIDInvalid software timer ID.
#LOS_ERRNO_SWTMR_NOT_CREATEDThe software timer is not created.
#LOS_ERRNO_SWTMR_NOT_STARTEDThe software timer is not started.
#LOS_ERRNO_SWTMR_STATUS_INVALIDInvalid software timer state.
#LOS_OKThe number of remaining Ticks is successfully obtained.
Dependency:
  • los_swtmr.h: the header file that contains the API declaration.
参见
LOS_SwtmrCreate

Obtain the number of remaining Ticks configured on a software timer.

在文件 los_swtmr.c848 行定义.

849{
850 SWTMR_CTRL_S *swtmr = NULL;
851 UINT32 intSave;
852 UINT32 ret = LOS_OK;
853 UINT16 swtmrCBID;
854
855 if (swtmrID >= OS_SWTMR_MAX_TIMERID) {
856 return LOS_ERRNO_SWTMR_ID_INVALID;
857 }
858
859 if (tick == NULL) {
860 return LOS_ERRNO_SWTMR_TICK_PTR_NULL;
861 }
862
863 swtmrCBID = swtmrID % LOSCFG_BASE_CORE_SWTMR_LIMIT;//取模
864 swtmr = g_swtmrCBArray + swtmrCBID;//获取定时器控制结构体
865 SWTMR_LOCK(intSave);
866
867 if (swtmr->usTimerID != swtmrID) {//ID必须一样
868 SWTMR_UNLOCK(intSave);
869 return LOS_ERRNO_SWTMR_ID_INVALID;
870 }
871 switch (swtmr->ucState) {
873 ret = LOS_ERRNO_SWTMR_NOT_CREATED;
874 break;
876 ret = LOS_ERRNO_SWTMR_NOT_STARTED;
877 break;
878 case OS_SWTMR_STATUS_TICKING://正在计数的定时器
879 *tick = OsSwtmrTimeGet(swtmr);//获取
880 break;
881 default:
882 ret = LOS_ERRNO_SWTMR_STATUS_INVALID;
883 break;
884 }
885 SWTMR_UNLOCK(intSave);
886 return ret;
887}
LITE_OS_SEC_TEXT STATIC UINT32 OsSwtmrTimeGet(const SWTMR_CTRL_S *swtmr)
Definition: los_swtmr.c:702
函数调用图:
这是这个函数的调用关系图: