lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /** |
| 2 | * @file soft_timer.h |
| 3 | * @brief Èí¶¨Ê±Æ÷Í·Îļþ |
| 4 | * |
| 5 | * Copyright (C) 2017 Sanechips Technology Co., Ltd. |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #ifndef _SOFT_TIMER_H_ |
| 10 | #define _SOFT_TIMER_H_ |
| 11 | |
| 12 | /******************************************************************************* |
| 13 | * Include header files * |
| 14 | ******************************************************************************/ |
| 15 | #ifdef __cplusplus |
| 16 | extern "C" { |
| 17 | #endif |
| 18 | |
| 19 | #include "message.h" |
| 20 | #include "os_type_def.h" |
| 21 | |
| 22 | |
| 23 | /******************************************************************************* |
| 24 | * Macro definitions * |
| 25 | ******************************************************************************/ |
| 26 | /** |
| 27 | * @brief Èí¼þ¶¨Ê±Æ÷µÄ×î´ó¸öÊý |
| 28 | */ |
| 29 | #define MAX_SOFT_TIMER_NUM 32 |
| 30 | |
| 31 | /** |
| 32 | * @brief ¶¨Ê±Æ÷ÀàÐͱê־λucFlagµÄºê,´Ë¶¨Ê±Æ÷Ö»´¥·¢Ò»´Î |
| 33 | */ |
| 34 | #define TIMER_FLAG_ONCE 0 |
| 35 | |
| 36 | /** |
| 37 | * @brief ¶¨Ê±Æ÷ÀàÐͱê־λucFlagµÄºê,´Ë¶¨Ê±Æ÷³ÖÐøÓÐЧ |
| 38 | */ |
| 39 | #define TIMER_FLAG_RESTART 1 |
| 40 | |
| 41 | /** |
| 42 | * @brief Èí¼þ¶¨Ê±Æ÷´íÎóÂ붨Ò壬»Øµ÷º¯ÊýΪ¿Õ |
| 43 | */ |
| 44 | #define SOFTTIMER_ERRCODE_CALLBACK_NULL 1 |
| 45 | |
| 46 | /** |
| 47 | * @brief Èí¼þ¶¨Ê±Æ÷´íÎóÂ붨Ò壬³õʼ»¯Ê§°Ü |
| 48 | */ |
| 49 | #define SOFTTIMER_ERRCODE_INIT_FAIL 2 |
| 50 | |
| 51 | /** |
| 52 | * @brief Èí¼þ¶¨Ê±Æ÷´íÎóÂ붨Ò壬´´½¨µÄÈí¶¨Ê±Æ÷³¬¹ý×î´ó¸öÊý |
| 53 | */ |
| 54 | #define SOFTTIMER_ERRCODE_QUEUE_FULL 3 |
| 55 | |
| 56 | /** |
| 57 | * @brief Èí¼þ¶¨Ê±Æ÷´íÎóÂ붨Ò壬´´½¨µÄÈí¶¨Ê±Æ÷idÒѾ´æÔÚ |
| 58 | */ |
| 59 | #define SOFTTIMER_ERRCODE_TIMERID_EXIST 4 |
| 60 | |
| 61 | |
| 62 | #ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 63 | |
| 64 | /** |
| 65 | * @brief Ó²¼þ¶¨Ê±Æ÷µÄ²½³¤£¬10ms |
| 66 | */ |
| 67 | #define TIMER_STEP 10 |
| 68 | |
| 69 | /*if the thread of soft timer is exist, 0 means not exist*/ |
| 70 | #define SOFT_TIMER_THREAD_NULL 0 |
| 71 | #define SOFT_TIMER_THREAD_EXIST 1 |
| 72 | |
| 73 | #endif//#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 74 | |
| 75 | |
| 76 | /******************************************************************************* |
| 77 | * Type definitions * |
| 78 | ******************************************************************************/ |
| 79 | #ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 80 | |
| 81 | /*¶¨ÒåÈí¼þ¶¨Ê±Æ÷µÄÊý¾Ý½á¹¹*/ |
| 82 | typedef struct tagSOFT_TIMER { |
| 83 | struct tagSOFT_TIMER *prev; |
| 84 | struct tagSOFT_TIMER *next; |
| 85 | int used; |
| 86 | int done; |
| 87 | int timerValue; |
| 88 | |
| 89 | USHORT usTimerID; /*¶¨Ê±Æ÷ID,ÓÃÀ´Çø·Ö²»Í¬µÄÈí¼þ¶¨Ê±Æ÷, 0±êʶ*/ |
| 90 | UCHAR ucFlag; /*¶¨Ê±Æ÷ÀàÐͱê־λ*/ |
| 91 | UCHAR ucReserved; /*±£Áô×Ö¶Î*/ |
| 92 | ULONG ulCurInterval; /*µ±Ç°¶¨Ê±Æ÷³¬Ê±Ê£Óàʱ¼ä£¬µ¥Î» ºÁÃë(×¢Òâ²»ÊÇ΢Ãë)£¬±ØÐ붨ÒåΪӲ¼þ¶¨Ê±Æ÷²½³¤µÄ±¶Êý*/ |
| 93 | ULONG ulNextInterval; /*³¬Ê±ºó¶¨Ê±Æ÷ÖØÖõļä¸ô£¬µ¥Î» ºÁÃë(×¢Òâ²»ÊÇ΢Ãë)*/ |
| 94 | void * (*procCallBack)(void *); /*¶¨Ò嶨ʱÆ÷³¬Ê±ºóµÄ»Øµ÷´¦Àíº¯ÊýÖ¸Õë*/ |
| 95 | void *args; /*¶¨Ò嶨ʱÆ÷³¬Ê±ºóµÄ»Øµ÷´¦Àíº¯ÊýµÄ²ÎÊý*/ |
| 96 | } SOFT_TIMER; |
| 97 | |
| 98 | typedef struct tagMSG_TIMERBUF { |
| 99 | LONG lMsgType; /*ÏûÏ¢ÀàÐÍ£¬Õâ¸öÊÇÓÃÀ´¿ØÖÆÏûÏ¢ÊÕÈ¡µÄÓÅÏÈ˳ÐòµÄ*/ |
| 100 | USHORT usMsgCmd; /*ÏûÏ¢Â룬Õâ¸öÊÇÓÃÀ´Ö¸Ê¾ÏûÏ¢µÄÄÚÈÝÊÇʲôµÄ*/ |
| 101 | USHORT usDataLen; /*Я´øµÄÓÐЧÊý¾Ý³¤¶È*/ |
| 102 | UCHAR aucDataBuf[4]; /*Я´øµÄÓÐЧÊý¾Ýbuffer,´æ·Å¶¨Ê±Æ÷index*/ |
| 103 | ULONG ulMagic; /*ħÊõ×Ö£¬Õý³£Çé¿öÏÂֵΪMSG_MAGIC_WORD*/ |
| 104 | } T_TIMER_MSG_BUF; |
| 105 | |
| 106 | #endif//#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 107 | |
| 108 | /******************************************************************************* |
| 109 | * Global variable declarations * |
| 110 | ******************************************************************************/ |
| 111 | |
| 112 | |
| 113 | /******************************************************************************* |
| 114 | * Global function declarations * |
| 115 | ******************************************************************************/ |
| 116 | /** |
| 117 | * @brief ´´½¨Èí¶¨Ê±Æ÷½Ó¿Ú |
| 118 | * @param usTimerID Èë²Î£¬Èí¶¨Ê±Æ÷id£¬ÓÃÀ´Çø·Ö²»Í¬µÄÈí¼þ¶¨Ê±Æ÷ |
| 119 | * @param ucFlag Èë²Î£¬¶¨Ê±Æ÷ÀàÐͱê־λ |
| 120 | * @param ulInterval Èë²Î£¬Èí¶¨Ê±Æ÷³¬Ê±Ê±¼ä¼ä¸ô£¬µ¥Î»£ºms |
| 121 | * @param procCallBack Èë²Î£¬¶¨Ê±Æ÷³¬Ê±ºóµÄ»Øµ÷º¯ÊýÖ¸Õë |
| 122 | * @param args Èë²Î£¬¶¨Ê±Æ÷³¬Ê±ºóµÄ»Øµ÷º¯Êý²ÎÊý |
| 123 | * @return OK ³É¹¦£¬ÆäËûֵʧ°Ü |
| 124 | * @retval OK ³É¹¦ |
| 125 | * @retval SOFTTIMER_ERRCODE_CALLBACK_NULL Èí¶¨Ê±Æ÷µÄ»Øµ÷º¯ÊýµØÖ·Îª¿Õ |
| 126 | * @retval SOFTTIMER_ERRCODE_INIT_FAIL Èí¶¨Ê±Æ÷³õʼ»¯Ê§°Ü |
| 127 | * @retval SOFTTIMER_ERRCODE_QUEUE_FULL ´´½¨µÄÈí¶¨Ê±Æ÷³¬¹ý×î´ó¸öÊýÏÞÖÆ |
| 128 | * @retval SOFTTIMER_ERRCODE_TIMERID_EXIST ¸ÃÈí¶¨Ê±Æ÷idÒѾ´æÔÚ |
| 129 | * @note |
| 130 | * @warning |
| 131 | */ |
| 132 | LONG sc_timer_create(USHORT usTimerID, |
| 133 | UCHAR ucFlag, |
| 134 | ULONG ulInterval, |
| 135 | void * (*procCallBack)(void *), |
| 136 | void *args); |
| 137 | |
| 138 | /** |
| 139 | * @brief ɾ³ýÈí¶¨Ê±Æ÷½Ó¿Ú |
| 140 | * @param usTimerID Èë²Î£¬ÒªÉ¾³ýµÄÈí¶¨Ê±Æ÷id |
| 141 | * @return OK£º³É¹¦£¬ERROR£ºÊ§°Ü |
| 142 | * @retval OK ³É¹¦ |
| 143 | * @retval ERROR ʧ°Ü |
| 144 | * @note |
| 145 | * @warning |
| 146 | */ |
| 147 | LONG sc_timer_delete(USHORT usTimerID); |
| 148 | |
| 149 | #ifndef LIBSOFT_TIMER_API_NEW |
| 150 | LONG CreateSoftTimer(USHORT usTimerID, |
| 151 | UCHAR ucFlag, |
| 152 | ULONG ulInterval, |
| 153 | void * (*procCallBack)(void *), |
| 154 | void *args); |
| 155 | LONG DeleteSoftTimer(USHORT usTimerID); |
| 156 | #endif |
| 157 | |
| 158 | /******************************************************************************* |
| 159 | * Inline function implementations * |
| 160 | ******************************************************************************/ |
| 161 | |
| 162 | |
| 163 | #ifdef __cplusplus |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | |
| 168 | #endif // #ifndef _SOFT_TIMER_H_ |
| 169 | |