![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Quick and dirty CoOS + LWIP ( Webserver )
CoOS/kernel/timer.c@0:94897d537b31, 2011-09-10 (annotated)
- Committer:
- astroboy
- Date:
- Sat Sep 10 22:41:10 2011 +0000
- Revision:
- 0:94897d537b31
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
astroboy | 0:94897d537b31 | 1 | /** |
astroboy | 0:94897d537b31 | 2 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 3 | * @file timer.c |
astroboy | 0:94897d537b31 | 4 | * @version V1.1.4 |
astroboy | 0:94897d537b31 | 5 | * @date 2011.04.20 |
astroboy | 0:94897d537b31 | 6 | * @brief timer management implementation code of CooCox CoOS kernel. |
astroboy | 0:94897d537b31 | 7 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 8 | * @copy |
astroboy | 0:94897d537b31 | 9 | * |
astroboy | 0:94897d537b31 | 10 | * INTERNAL FILE,DON'T PUBLIC. |
astroboy | 0:94897d537b31 | 11 | * |
astroboy | 0:94897d537b31 | 12 | * <h2><center>© COPYRIGHT 2009 CooCox </center></h2> |
astroboy | 0:94897d537b31 | 13 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 14 | */ |
astroboy | 0:94897d537b31 | 15 | |
astroboy | 0:94897d537b31 | 16 | |
astroboy | 0:94897d537b31 | 17 | |
astroboy | 0:94897d537b31 | 18 | /*---------------------------- Include ---------------------------------------*/ |
astroboy | 0:94897d537b31 | 19 | #include <coocox.h> |
astroboy | 0:94897d537b31 | 20 | |
astroboy | 0:94897d537b31 | 21 | /*---------------------------- Variable Define -------------------------------*/ |
astroboy | 0:94897d537b31 | 22 | #if CFG_TMR_EN > 0 |
astroboy | 0:94897d537b31 | 23 | |
astroboy | 0:94897d537b31 | 24 | TmrCtrl TmrTbl[CFG_MAX_TMR]= {{0}};/*!< Table which save timer control block.*/ |
astroboy | 0:94897d537b31 | 25 | P_TmrCtrl TmrList = Co_NULL; /*!< The header of the TmrCtrl list. */ |
astroboy | 0:94897d537b31 | 26 | U32 TmrIDVessel = 0; /*!< Timer ID container. */ |
astroboy | 0:94897d537b31 | 27 | |
astroboy | 0:94897d537b31 | 28 | |
astroboy | 0:94897d537b31 | 29 | /** |
astroboy | 0:94897d537b31 | 30 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 31 | * @brief Insert a timer into the timer list |
astroboy | 0:94897d537b31 | 32 | * @param[in] tmrID Specify timer ID which insertted. |
astroboy | 0:94897d537b31 | 33 | * @param[out] None |
astroboy | 0:94897d537b31 | 34 | * @retval E_INVALID_ID Timer ID passed was invalid,insert fail. |
astroboy | 0:94897d537b31 | 35 | * @retval E_OK Insert successful. |
astroboy | 0:94897d537b31 | 36 | * |
astroboy | 0:94897d537b31 | 37 | * @par Description |
astroboy | 0:94897d537b31 | 38 | * @details This function is called to insert a timer into the timer list. |
astroboy | 0:94897d537b31 | 39 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 40 | */ |
astroboy | 0:94897d537b31 | 41 | static void InsertTmrList(OS_TCID tmrID) |
astroboy | 0:94897d537b31 | 42 | { |
astroboy | 0:94897d537b31 | 43 | P_TmrCtrl pTmr; |
astroboy | 0:94897d537b31 | 44 | S32 deltaTicks; |
astroboy | 0:94897d537b31 | 45 | U32 tmrCnt; |
astroboy | 0:94897d537b31 | 46 | tmrCnt = TmrTbl[tmrID].tmrCnt; /* Get timer time */ |
astroboy | 0:94897d537b31 | 47 | |
astroboy | 0:94897d537b31 | 48 | if(tmrCnt == 0) /* Is timer time==0? */ |
astroboy | 0:94897d537b31 | 49 | { |
astroboy | 0:94897d537b31 | 50 | return; /* Do nothing,return */ |
astroboy | 0:94897d537b31 | 51 | } |
astroboy | 0:94897d537b31 | 52 | |
astroboy | 0:94897d537b31 | 53 | OsSchedLock(); /* Lock schedule */ |
astroboy | 0:94897d537b31 | 54 | if(TmrList == Co_NULL) /* Is no item in timer list? */ |
astroboy | 0:94897d537b31 | 55 | { |
astroboy | 0:94897d537b31 | 56 | TmrList = &TmrTbl[tmrID]; /* Yes,set this as first item */ |
astroboy | 0:94897d537b31 | 57 | } |
astroboy | 0:94897d537b31 | 58 | else /* No,find correct place ,and insert inserted timer */ |
astroboy | 0:94897d537b31 | 59 | { |
astroboy | 0:94897d537b31 | 60 | pTmr = TmrList; |
astroboy | 0:94897d537b31 | 61 | deltaTicks = tmrCnt; /* Get timer tick */ |
astroboy | 0:94897d537b31 | 62 | |
astroboy | 0:94897d537b31 | 63 | /* find correct place */ |
astroboy | 0:94897d537b31 | 64 | while(pTmr != Co_NULL) |
astroboy | 0:94897d537b31 | 65 | { |
astroboy | 0:94897d537b31 | 66 | deltaTicks -= pTmr->tmrCnt; /* Get ticks with previous item */ |
astroboy | 0:94897d537b31 | 67 | if(deltaTicks < 0) /* Is delta ticks<0? */ |
astroboy | 0:94897d537b31 | 68 | { |
astroboy | 0:94897d537b31 | 69 | /* Yes,get correct place */ |
astroboy | 0:94897d537b31 | 70 | if(pTmr->tmrPrev!= Co_NULL)/* Is head item of timer list? */ |
astroboy | 0:94897d537b31 | 71 | { |
astroboy | 0:94897d537b31 | 72 | /* No,insert into */ |
astroboy | 0:94897d537b31 | 73 | pTmr->tmrPrev->tmrNext = &TmrTbl[tmrID]; |
astroboy | 0:94897d537b31 | 74 | TmrTbl[tmrID].tmrPrev = pTmr->tmrPrev; |
astroboy | 0:94897d537b31 | 75 | TmrTbl[tmrID].tmrNext = pTmr; |
astroboy | 0:94897d537b31 | 76 | pTmr->tmrPrev = &TmrTbl[tmrID]; |
astroboy | 0:94897d537b31 | 77 | } |
astroboy | 0:94897d537b31 | 78 | else /* Yes,set task as first item */ |
astroboy | 0:94897d537b31 | 79 | { |
astroboy | 0:94897d537b31 | 80 | TmrTbl[tmrID].tmrNext = TmrList; |
astroboy | 0:94897d537b31 | 81 | TmrList->tmrPrev = &TmrTbl[tmrID]; |
astroboy | 0:94897d537b31 | 82 | TmrList = &TmrTbl[tmrID]; |
astroboy | 0:94897d537b31 | 83 | } |
astroboy | 0:94897d537b31 | 84 | TmrTbl[tmrID].tmrCnt = TmrTbl[tmrID].tmrNext->tmrCnt+deltaTicks; |
astroboy | 0:94897d537b31 | 85 | TmrTbl[tmrID].tmrNext->tmrCnt -= TmrTbl[tmrID].tmrCnt; |
astroboy | 0:94897d537b31 | 86 | break; |
astroboy | 0:94897d537b31 | 87 | } |
astroboy | 0:94897d537b31 | 88 | /* Is last item in list? */ |
astroboy | 0:94897d537b31 | 89 | else if((deltaTicks >= 0) && (pTmr->tmrNext == Co_NULL)) |
astroboy | 0:94897d537b31 | 90 | { |
astroboy | 0:94897d537b31 | 91 | /* Yes,insert into */ |
astroboy | 0:94897d537b31 | 92 | TmrTbl[tmrID].tmrPrev = pTmr; |
astroboy | 0:94897d537b31 | 93 | pTmr->tmrNext = &TmrTbl[tmrID]; |
astroboy | 0:94897d537b31 | 94 | TmrTbl[tmrID].tmrCnt = deltaTicks; |
astroboy | 0:94897d537b31 | 95 | break; |
astroboy | 0:94897d537b31 | 96 | } |
astroboy | 0:94897d537b31 | 97 | pTmr = pTmr->tmrNext; /* Get the next item in timer list */ |
astroboy | 0:94897d537b31 | 98 | } |
astroboy | 0:94897d537b31 | 99 | } |
astroboy | 0:94897d537b31 | 100 | OsSchedUnlock(); /* Unlock schedule */ |
astroboy | 0:94897d537b31 | 101 | } |
astroboy | 0:94897d537b31 | 102 | |
astroboy | 0:94897d537b31 | 103 | |
astroboy | 0:94897d537b31 | 104 | /** |
astroboy | 0:94897d537b31 | 105 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 106 | * @brief Remove a timer from the timer list |
astroboy | 0:94897d537b31 | 107 | * @param[in] tmrID Specify ID for a timer which removed form timer list. |
astroboy | 0:94897d537b31 | 108 | * @param[out] None |
astroboy | 0:94897d537b31 | 109 | * @retval None |
astroboy | 0:94897d537b31 | 110 | * |
astroboy | 0:94897d537b31 | 111 | * @par Description |
astroboy | 0:94897d537b31 | 112 | * @details This function is called to remove a timer from the timer list. |
astroboy | 0:94897d537b31 | 113 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 114 | */ |
astroboy | 0:94897d537b31 | 115 | static void RemoveTmrList(OS_TCID tmrID) |
astroboy | 0:94897d537b31 | 116 | { |
astroboy | 0:94897d537b31 | 117 | P_TmrCtrl pTmr; |
astroboy | 0:94897d537b31 | 118 | |
astroboy | 0:94897d537b31 | 119 | pTmr = &TmrTbl[tmrID]; |
astroboy | 0:94897d537b31 | 120 | |
astroboy | 0:94897d537b31 | 121 | OsSchedLock(); /* Lock schedule */ |
astroboy | 0:94897d537b31 | 122 | |
astroboy | 0:94897d537b31 | 123 | /* Is there only one item in timer list? */ |
astroboy | 0:94897d537b31 | 124 | if((pTmr->tmrPrev == Co_NULL) && (pTmr->tmrNext == Co_NULL)) |
astroboy | 0:94897d537b31 | 125 | { |
astroboy | 0:94897d537b31 | 126 | TmrList = Co_NULL; /* Yes,set timer list as Co_NULL */ |
astroboy | 0:94897d537b31 | 127 | } |
astroboy | 0:94897d537b31 | 128 | else if(pTmr->tmrPrev == Co_NULL) /* Is the first item in timer list? */ |
astroboy | 0:94897d537b31 | 129 | { /* Yes,remove timer from list,and reset timer list */ |
astroboy | 0:94897d537b31 | 130 | TmrList = pTmr->tmrNext; |
astroboy | 0:94897d537b31 | 131 | TmrList->tmrPrev = Co_NULL; |
astroboy | 0:94897d537b31 | 132 | pTmr->tmrNext->tmrCnt += pTmr->tmrCnt; |
astroboy | 0:94897d537b31 | 133 | pTmr->tmrNext = Co_NULL; |
astroboy | 0:94897d537b31 | 134 | } |
astroboy | 0:94897d537b31 | 135 | else if(pTmr->tmrNext == Co_NULL) /* Is the last item in timer list? */ |
astroboy | 0:94897d537b31 | 136 | { |
astroboy | 0:94897d537b31 | 137 | /* Yes,remove timer form list */ |
astroboy | 0:94897d537b31 | 138 | pTmr->tmrPrev->tmrNext = Co_NULL; |
astroboy | 0:94897d537b31 | 139 | pTmr->tmrPrev = Co_NULL; |
astroboy | 0:94897d537b31 | 140 | } |
astroboy | 0:94897d537b31 | 141 | else /* No, remove timer from list */ |
astroboy | 0:94897d537b31 | 142 | { |
astroboy | 0:94897d537b31 | 143 | pTmr->tmrPrev->tmrNext = pTmr->tmrNext; |
astroboy | 0:94897d537b31 | 144 | pTmr->tmrNext->tmrPrev = pTmr->tmrPrev; |
astroboy | 0:94897d537b31 | 145 | pTmr->tmrNext->tmrCnt += pTmr->tmrCnt; |
astroboy | 0:94897d537b31 | 146 | pTmr->tmrNext = Co_NULL; |
astroboy | 0:94897d537b31 | 147 | pTmr->tmrPrev = Co_NULL; |
astroboy | 0:94897d537b31 | 148 | } |
astroboy | 0:94897d537b31 | 149 | OsSchedUnlock(); /* Unlock schedule */ |
astroboy | 0:94897d537b31 | 150 | } |
astroboy | 0:94897d537b31 | 151 | |
astroboy | 0:94897d537b31 | 152 | |
astroboy | 0:94897d537b31 | 153 | /** |
astroboy | 0:94897d537b31 | 154 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 155 | * @brief Create a timer |
astroboy | 0:94897d537b31 | 156 | * @param[in] tmrType Specify timer's type. |
astroboy | 0:94897d537b31 | 157 | * @param[in] tmrCnt Specify timer initial counter value. |
astroboy | 0:94897d537b31 | 158 | * @param[in] tmrReload Specify timer reload value. |
astroboy | 0:94897d537b31 | 159 | * @param[in] func Specify timer callback function entry. |
astroboy | 0:94897d537b31 | 160 | * @param[out] None |
astroboy | 0:94897d537b31 | 161 | * @retval E_CREATE_FAIL Create timer fail. |
astroboy | 0:94897d537b31 | 162 | * @retval others Create timer successful. |
astroboy | 0:94897d537b31 | 163 | * |
astroboy | 0:94897d537b31 | 164 | * @par Description |
astroboy | 0:94897d537b31 | 165 | * @details This function is called to create a timer. |
astroboy | 0:94897d537b31 | 166 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 167 | */ |
astroboy | 0:94897d537b31 | 168 | OS_TCID CoCreateTmr(U8 tmrType, U32 tmrCnt, U32 tmrReload, vFUNCPtr func) |
astroboy | 0:94897d537b31 | 169 | { |
astroboy | 0:94897d537b31 | 170 | U8 i; |
astroboy | 0:94897d537b31 | 171 | #if CFG_PAR_CHECKOUT_EN >0 /* Check validity of parameter */ |
astroboy | 0:94897d537b31 | 172 | if((tmrType != TMR_TYPE_ONE_SHOT) && (tmrType != TMR_TYPE_PERIODIC)) |
astroboy | 0:94897d537b31 | 173 | { |
astroboy | 0:94897d537b31 | 174 | return E_CREATE_FAIL; |
astroboy | 0:94897d537b31 | 175 | } |
astroboy | 0:94897d537b31 | 176 | if(func == Co_NULL) |
astroboy | 0:94897d537b31 | 177 | { |
astroboy | 0:94897d537b31 | 178 | return E_CREATE_FAIL; |
astroboy | 0:94897d537b31 | 179 | } |
astroboy | 0:94897d537b31 | 180 | #endif |
astroboy | 0:94897d537b31 | 181 | OsSchedLock(); /* Lock schedule */ |
astroboy | 0:94897d537b31 | 182 | for(i = 0; i < CFG_MAX_TMR; i++) |
astroboy | 0:94897d537b31 | 183 | { |
astroboy | 0:94897d537b31 | 184 | if((TmrIDVessel & (1 << i)) == 0) /* Is free timer ID? */ |
astroboy | 0:94897d537b31 | 185 | { |
astroboy | 0:94897d537b31 | 186 | TmrIDVessel |= (1<<i); /* Yes,assign ID to this timer */ |
astroboy | 0:94897d537b31 | 187 | OsSchedUnlock(); /* Unlock schedule */ |
astroboy | 0:94897d537b31 | 188 | TmrTbl[i].tmrID = i; /* Initialize timer as user set */ |
astroboy | 0:94897d537b31 | 189 | TmrTbl[i].tmrType = tmrType; |
astroboy | 0:94897d537b31 | 190 | TmrTbl[i].tmrState = TMR_STATE_STOPPED; |
astroboy | 0:94897d537b31 | 191 | TmrTbl[i].tmrCnt = tmrCnt; |
astroboy | 0:94897d537b31 | 192 | TmrTbl[i].tmrReload = tmrReload; |
astroboy | 0:94897d537b31 | 193 | TmrTbl[i].tmrCallBack = func; |
astroboy | 0:94897d537b31 | 194 | TmrTbl[i].tmrPrev = Co_NULL; |
astroboy | 0:94897d537b31 | 195 | TmrTbl[i].tmrNext = Co_NULL; |
astroboy | 0:94897d537b31 | 196 | return i; /* Return timer ID */ |
astroboy | 0:94897d537b31 | 197 | } |
astroboy | 0:94897d537b31 | 198 | } |
astroboy | 0:94897d537b31 | 199 | OsSchedUnlock(); /* Unlock schedule */ |
astroboy | 0:94897d537b31 | 200 | return E_CREATE_FAIL; /* Error return */ |
astroboy | 0:94897d537b31 | 201 | } |
astroboy | 0:94897d537b31 | 202 | |
astroboy | 0:94897d537b31 | 203 | |
astroboy | 0:94897d537b31 | 204 | /** |
astroboy | 0:94897d537b31 | 205 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 206 | * @brief Start counter |
astroboy | 0:94897d537b31 | 207 | * @param[in] tmrID Specify a timer which startted. |
astroboy | 0:94897d537b31 | 208 | * @param[out] None |
astroboy | 0:94897d537b31 | 209 | * @retval E_INVALID_ID The timer id passed was invalid,can't start timer |
astroboy | 0:94897d537b31 | 210 | * @retval E_OK Insert a timer to timer list and start it successful. |
astroboy | 0:94897d537b31 | 211 | * |
astroboy | 0:94897d537b31 | 212 | * @par Description |
astroboy | 0:94897d537b31 | 213 | * @details This function is called to make a timer start countering. |
astroboy | 0:94897d537b31 | 214 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 215 | */ |
astroboy | 0:94897d537b31 | 216 | StatusType CoStartTmr(OS_TCID tmrID) |
astroboy | 0:94897d537b31 | 217 | { |
astroboy | 0:94897d537b31 | 218 | #if CFG_PAR_CHECKOUT_EN >0 /* Check validity of parameter */ |
astroboy | 0:94897d537b31 | 219 | if(tmrID >= CFG_MAX_TMR) |
astroboy | 0:94897d537b31 | 220 | { |
astroboy | 0:94897d537b31 | 221 | return E_INVALID_ID; |
astroboy | 0:94897d537b31 | 222 | } |
astroboy | 0:94897d537b31 | 223 | if( (TmrIDVessel & (1<<tmrID)) == 0) |
astroboy | 0:94897d537b31 | 224 | { |
astroboy | 0:94897d537b31 | 225 | return E_INVALID_ID; |
astroboy | 0:94897d537b31 | 226 | } |
astroboy | 0:94897d537b31 | 227 | #endif |
astroboy | 0:94897d537b31 | 228 | |
astroboy | 0:94897d537b31 | 229 | if(TmrTbl[tmrID].tmrState == TMR_STATE_RUNNING) /* Is timer running? */ |
astroboy | 0:94897d537b31 | 230 | { |
astroboy | 0:94897d537b31 | 231 | return E_OK; /* Yes,do nothing,return OK */ |
astroboy | 0:94897d537b31 | 232 | } |
astroboy | 0:94897d537b31 | 233 | |
astroboy | 0:94897d537b31 | 234 | /* No,set timer status as TMR_STATE_RUNNING */ |
astroboy | 0:94897d537b31 | 235 | TmrTbl[tmrID].tmrState = TMR_STATE_RUNNING; |
astroboy | 0:94897d537b31 | 236 | InsertTmrList(tmrID); /* Insert this timer into timer list */ |
astroboy | 0:94897d537b31 | 237 | return E_OK; /* Return OK */ |
astroboy | 0:94897d537b31 | 238 | } |
astroboy | 0:94897d537b31 | 239 | |
astroboy | 0:94897d537b31 | 240 | |
astroboy | 0:94897d537b31 | 241 | |
astroboy | 0:94897d537b31 | 242 | /** |
astroboy | 0:94897d537b31 | 243 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 244 | * @brief Stop countering for a spcify timer |
astroboy | 0:94897d537b31 | 245 | * @param[in] tmrID Specify a timer which stopped. |
astroboy | 0:94897d537b31 | 246 | * @param[out] None |
astroboy | 0:94897d537b31 | 247 | * @retval E_INVALID_ID The timer id passed was invalid, stop failure. |
astroboy | 0:94897d537b31 | 248 | * @retval E_OK Stop a timer countering successful. |
astroboy | 0:94897d537b31 | 249 | * |
astroboy | 0:94897d537b31 | 250 | * @par Description |
astroboy | 0:94897d537b31 | 251 | * @details This function is called to stop a timer from counting. |
astroboy | 0:94897d537b31 | 252 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 253 | */ |
astroboy | 0:94897d537b31 | 254 | StatusType CoStopTmr(OS_TCID tmrID) |
astroboy | 0:94897d537b31 | 255 | { |
astroboy | 0:94897d537b31 | 256 | #if CFG_PAR_CHECKOUT_EN >0 /* Check validity of parameter */ |
astroboy | 0:94897d537b31 | 257 | if(tmrID >= CFG_MAX_TMR) |
astroboy | 0:94897d537b31 | 258 | { |
astroboy | 0:94897d537b31 | 259 | return E_INVALID_ID; |
astroboy | 0:94897d537b31 | 260 | } |
astroboy | 0:94897d537b31 | 261 | if((TmrIDVessel & (1<<tmrID)) == 0) |
astroboy | 0:94897d537b31 | 262 | { |
astroboy | 0:94897d537b31 | 263 | return E_INVALID_ID; |
astroboy | 0:94897d537b31 | 264 | } |
astroboy | 0:94897d537b31 | 265 | #endif |
astroboy | 0:94897d537b31 | 266 | |
astroboy | 0:94897d537b31 | 267 | |
astroboy | 0:94897d537b31 | 268 | if(TmrTbl[tmrID].tmrState == TMR_STATE_STOPPED)/* Does timer stop running?*/ |
astroboy | 0:94897d537b31 | 269 | { |
astroboy | 0:94897d537b31 | 270 | return E_OK; /* Yes,do nothing,return OK */ |
astroboy | 0:94897d537b31 | 271 | } |
astroboy | 0:94897d537b31 | 272 | RemoveTmrList(tmrID); /* No,remove this timer from timer list */ |
astroboy | 0:94897d537b31 | 273 | |
astroboy | 0:94897d537b31 | 274 | /* Set timer status as TMR_STATE_STOPPED */ |
astroboy | 0:94897d537b31 | 275 | TmrTbl[tmrID].tmrState = TMR_STATE_STOPPED; |
astroboy | 0:94897d537b31 | 276 | return E_OK; /* Return OK */ |
astroboy | 0:94897d537b31 | 277 | } |
astroboy | 0:94897d537b31 | 278 | |
astroboy | 0:94897d537b31 | 279 | |
astroboy | 0:94897d537b31 | 280 | /** |
astroboy | 0:94897d537b31 | 281 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 282 | * @brief Delete a timer |
astroboy | 0:94897d537b31 | 283 | * @param[in] tmrID Specify a timer which deleted. |
astroboy | 0:94897d537b31 | 284 | * @param[out] None |
astroboy | 0:94897d537b31 | 285 | * @retval E_INVALID_ID The timer id passed was invalid,deleted failure. |
astroboy | 0:94897d537b31 | 286 | * @retval E_OK Delete a timer successful. |
astroboy | 0:94897d537b31 | 287 | * |
astroboy | 0:94897d537b31 | 288 | * @par Description |
astroboy | 0:94897d537b31 | 289 | * @details This function is called to delete a timer which created before. |
astroboy | 0:94897d537b31 | 290 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 291 | */ |
astroboy | 0:94897d537b31 | 292 | StatusType CoDelTmr(OS_TCID tmrID) |
astroboy | 0:94897d537b31 | 293 | { |
astroboy | 0:94897d537b31 | 294 | #if CFG_PAR_CHECKOUT_EN >0 /* Check validity of parameter */ |
astroboy | 0:94897d537b31 | 295 | if(tmrID >= CFG_MAX_TMR) |
astroboy | 0:94897d537b31 | 296 | { |
astroboy | 0:94897d537b31 | 297 | return E_INVALID_ID; |
astroboy | 0:94897d537b31 | 298 | } |
astroboy | 0:94897d537b31 | 299 | if( (TmrIDVessel & (1<<tmrID)) == 0) |
astroboy | 0:94897d537b31 | 300 | { |
astroboy | 0:94897d537b31 | 301 | return E_INVALID_ID; |
astroboy | 0:94897d537b31 | 302 | } |
astroboy | 0:94897d537b31 | 303 | #endif |
astroboy | 0:94897d537b31 | 304 | |
astroboy | 0:94897d537b31 | 305 | if(TmrTbl[tmrID].tmrState == TMR_STATE_RUNNING) /* Is timer running? */ |
astroboy | 0:94897d537b31 | 306 | { |
astroboy | 0:94897d537b31 | 307 | RemoveTmrList(tmrID); /* Yes,remove this timer from timer list*/ |
astroboy | 0:94897d537b31 | 308 | } |
astroboy | 0:94897d537b31 | 309 | TmrIDVessel &=~(1<<tmrID); /* Release resource that this timer hold*/ |
astroboy | 0:94897d537b31 | 310 | return E_OK; /* Return OK */ |
astroboy | 0:94897d537b31 | 311 | } |
astroboy | 0:94897d537b31 | 312 | |
astroboy | 0:94897d537b31 | 313 | |
astroboy | 0:94897d537b31 | 314 | /** |
astroboy | 0:94897d537b31 | 315 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 316 | * @brief Get current counter of specify timer |
astroboy | 0:94897d537b31 | 317 | * @param[in] tmrID Specify timer by ID. |
astroboy | 0:94897d537b31 | 318 | * @param[out] E_INVALID_ID Invalid ID was passed and get counter failure. |
astroboy | 0:94897d537b31 | 319 | * @param[out] E_OK Get current counter successful. |
astroboy | 0:94897d537b31 | 320 | * @retval Current counter of a timer which specify by id. |
astroboy | 0:94897d537b31 | 321 | * |
astroboy | 0:94897d537b31 | 322 | * @par Description |
astroboy | 0:94897d537b31 | 323 | * @details This function is called to obtain current counter of specify timer. |
astroboy | 0:94897d537b31 | 324 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 325 | */ |
astroboy | 0:94897d537b31 | 326 | U32 CoGetCurTmrCnt(OS_TCID tmrID,StatusType* perr) |
astroboy | 0:94897d537b31 | 327 | { |
astroboy | 0:94897d537b31 | 328 | #if CFG_PAR_CHECKOUT_EN >0 /* Check validity of parameter */ |
astroboy | 0:94897d537b31 | 329 | if(tmrID >= CFG_MAX_TMR) |
astroboy | 0:94897d537b31 | 330 | { |
astroboy | 0:94897d537b31 | 331 | *perr = E_INVALID_ID; |
astroboy | 0:94897d537b31 | 332 | return 0; |
astroboy | 0:94897d537b31 | 333 | } |
astroboy | 0:94897d537b31 | 334 | if((TmrIDVessel & (1<<tmrID)) == 0) |
astroboy | 0:94897d537b31 | 335 | { |
astroboy | 0:94897d537b31 | 336 | *perr = E_INVALID_ID; |
astroboy | 0:94897d537b31 | 337 | return 0; |
astroboy | 0:94897d537b31 | 338 | } |
astroboy | 0:94897d537b31 | 339 | #endif |
astroboy | 0:94897d537b31 | 340 | *perr = E_OK; |
astroboy | 0:94897d537b31 | 341 | return TmrTbl[tmrID].tmrCnt; /* Return timer counter */ |
astroboy | 0:94897d537b31 | 342 | } |
astroboy | 0:94897d537b31 | 343 | |
astroboy | 0:94897d537b31 | 344 | |
astroboy | 0:94897d537b31 | 345 | /** |
astroboy | 0:94897d537b31 | 346 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 347 | * @brief Setting for a specify timer |
astroboy | 0:94897d537b31 | 348 | * @param[in] tmrID Specify timer by ID. |
astroboy | 0:94897d537b31 | 349 | * @param[in] tmrCnt Specify timer counter which need to be set. |
astroboy | 0:94897d537b31 | 350 | * @param[in] tmrReload Specify timer reload value which need to be set. |
astroboy | 0:94897d537b31 | 351 | * @param[out] None |
astroboy | 0:94897d537b31 | 352 | * @retval E_INVALID_ID The ID passed was invalid,set fail. |
astroboy | 0:94897d537b31 | 353 | * @retval E_OK Set timer counter successful. |
astroboy | 0:94897d537b31 | 354 | * |
astroboy | 0:94897d537b31 | 355 | * @par Description |
astroboy | 0:94897d537b31 | 356 | * @details This function is called to set timer counter and reload value. |
astroboy | 0:94897d537b31 | 357 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 358 | */ |
astroboy | 0:94897d537b31 | 359 | StatusType CoSetTmrCnt(OS_TCID tmrID,U32 tmrCnt,U32 tmrReload) |
astroboy | 0:94897d537b31 | 360 | { |
astroboy | 0:94897d537b31 | 361 | #if CFG_PAR_CHECKOUT_EN >0 /* Check validity of parameter */ |
astroboy | 0:94897d537b31 | 362 | if(tmrID >= CFG_MAX_TMR) |
astroboy | 0:94897d537b31 | 363 | { |
astroboy | 0:94897d537b31 | 364 | return E_INVALID_ID; |
astroboy | 0:94897d537b31 | 365 | } |
astroboy | 0:94897d537b31 | 366 | if( (TmrIDVessel & (1<<tmrID)) == 0) |
astroboy | 0:94897d537b31 | 367 | { |
astroboy | 0:94897d537b31 | 368 | return E_INVALID_ID; |
astroboy | 0:94897d537b31 | 369 | } |
astroboy | 0:94897d537b31 | 370 | #endif |
astroboy | 0:94897d537b31 | 371 | TmrTbl[tmrID].tmrCnt = tmrCnt; /* Reset timer counter and reload value */ |
astroboy | 0:94897d537b31 | 372 | TmrTbl[tmrID].tmrReload = tmrReload; |
astroboy | 0:94897d537b31 | 373 | |
astroboy | 0:94897d537b31 | 374 | if(TmrTbl[tmrID].tmrState == TMR_STATE_RUNNING) /* Is timer running? */ |
astroboy | 0:94897d537b31 | 375 | { |
astroboy | 0:94897d537b31 | 376 | RemoveTmrList(tmrID); /* Yes,reorder timer in timer list */ |
astroboy | 0:94897d537b31 | 377 | InsertTmrList(tmrID); |
astroboy | 0:94897d537b31 | 378 | } |
astroboy | 0:94897d537b31 | 379 | return E_OK; /* Return OK */ |
astroboy | 0:94897d537b31 | 380 | } |
astroboy | 0:94897d537b31 | 381 | |
astroboy | 0:94897d537b31 | 382 | |
astroboy | 0:94897d537b31 | 383 | /** |
astroboy | 0:94897d537b31 | 384 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 385 | * @brief Timer counter dispose |
astroboy | 0:94897d537b31 | 386 | * @param[in] None |
astroboy | 0:94897d537b31 | 387 | * @param[out] None |
astroboy | 0:94897d537b31 | 388 | * @retval None |
astroboy | 0:94897d537b31 | 389 | * |
astroboy | 0:94897d537b31 | 390 | * @par Description |
astroboy | 0:94897d537b31 | 391 | * @details This function is called to dispose timer counter. |
astroboy | 0:94897d537b31 | 392 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 393 | */ |
astroboy | 0:94897d537b31 | 394 | void TmrDispose(void) |
astroboy | 0:94897d537b31 | 395 | { |
astroboy | 0:94897d537b31 | 396 | P_TmrCtrl pTmr; |
astroboy | 0:94897d537b31 | 397 | |
astroboy | 0:94897d537b31 | 398 | pTmr = TmrList; /* Get first item of timer list */ |
astroboy | 0:94897d537b31 | 399 | while((pTmr != Co_NULL) && (pTmr->tmrCnt == 0) ) |
astroboy | 0:94897d537b31 | 400 | { |
astroboy | 0:94897d537b31 | 401 | if(pTmr->tmrType == TMR_TYPE_ONE_SHOT) /* Is a One-shot timer? */ |
astroboy | 0:94897d537b31 | 402 | { |
astroboy | 0:94897d537b31 | 403 | /* Yes,remove this timer from timer list */ |
astroboy | 0:94897d537b31 | 404 | RemoveTmrList(pTmr->tmrID); |
astroboy | 0:94897d537b31 | 405 | |
astroboy | 0:94897d537b31 | 406 | /* Set timer status as TMR_STATE_STOPPED */ |
astroboy | 0:94897d537b31 | 407 | pTmr->tmrState = TMR_STATE_STOPPED; |
astroboy | 0:94897d537b31 | 408 | (pTmr->tmrCallBack)(); /* Call timer callback function */ |
astroboy | 0:94897d537b31 | 409 | } |
astroboy | 0:94897d537b31 | 410 | else if(pTmr->tmrType == TMR_TYPE_PERIODIC) /* Is a periodic timer? */ |
astroboy | 0:94897d537b31 | 411 | { |
astroboy | 0:94897d537b31 | 412 | /* Yes,remove this timer from timer list */ |
astroboy | 0:94897d537b31 | 413 | RemoveTmrList(pTmr->tmrID); |
astroboy | 0:94897d537b31 | 414 | pTmr->tmrCnt = pTmr->tmrReload; /* Reset timer tick */ |
astroboy | 0:94897d537b31 | 415 | InsertTmrList(pTmr->tmrID); /* Insert timer into timer list */ |
astroboy | 0:94897d537b31 | 416 | (pTmr->tmrCallBack)(); /* Call timer callback function */ |
astroboy | 0:94897d537b31 | 417 | } |
astroboy | 0:94897d537b31 | 418 | pTmr = TmrList; /* Get first item of timer list */ |
astroboy | 0:94897d537b31 | 419 | } |
astroboy | 0:94897d537b31 | 420 | } |
astroboy | 0:94897d537b31 | 421 | |
astroboy | 0:94897d537b31 | 422 | |
astroboy | 0:94897d537b31 | 423 | /** |
astroboy | 0:94897d537b31 | 424 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 425 | * @brief Timer counter dispose in ISR |
astroboy | 0:94897d537b31 | 426 | * @param[in] None |
astroboy | 0:94897d537b31 | 427 | * @param[out] None |
astroboy | 0:94897d537b31 | 428 | * @retval None |
astroboy | 0:94897d537b31 | 429 | * |
astroboy | 0:94897d537b31 | 430 | * @par Description |
astroboy | 0:94897d537b31 | 431 | * @details This function is called to dispose timer counter. |
astroboy | 0:94897d537b31 | 432 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 433 | */ |
astroboy | 0:94897d537b31 | 434 | void isr_TmrDispose(void) |
astroboy | 0:94897d537b31 | 435 | { |
astroboy | 0:94897d537b31 | 436 | if(OSSchedLock > 1) /* Is schedule lock? */ |
astroboy | 0:94897d537b31 | 437 | { |
astroboy | 0:94897d537b31 | 438 | IsrReq = Co_TRUE; |
astroboy | 0:94897d537b31 | 439 | TimerReq = Co_TRUE; /* Yes,set timer request true */ |
astroboy | 0:94897d537b31 | 440 | } |
astroboy | 0:94897d537b31 | 441 | else |
astroboy | 0:94897d537b31 | 442 | { |
astroboy | 0:94897d537b31 | 443 | TmrDispose(); /* No,call handler */ |
astroboy | 0:94897d537b31 | 444 | } |
astroboy | 0:94897d537b31 | 445 | } |
astroboy | 0:94897d537b31 | 446 | |
astroboy | 0:94897d537b31 | 447 | #endif |