timer.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:13k
- #include <string.h>
- #include "stlite.h"
- #include "stdevice.h"
- #include "stddefs.h"
- #include "sttbx.h"
- #include "stcommon.h"
- #include "errors.h"
- #include "timer.h"
- #include "osp.h"
- static T_TIMER_st *TIMAppTimerListStart;
- static T_TIMER_st *TIMAppTimerRepeat1TickStart;
- static T_TIMER_st *TIMAppTimerRepeat1TickEnd;
- static T_TIMER_st *TIMAppTimerRepeat1TickHISREnd;
- static T_TIMER_st *TIMAppTimerHISRProcess;
- static TIMR_FINISH_t TIMAppTimerHISRFinish;
- static UINT32 TIMAppHISRms;
- static UINT8 TIM1TickInHISR;
- static UINT32 TimeoutSemaphore;
- static void KD_TimerTask(void);
- static void KD_TimerHandleMsg(void);
- /*******************************************************************/
- INT32 KB_TimerInit(void)
- {
- static UINT8 isTimerInit = 0;
- UINT32 TaskId;
-
- if(isTimerInit)
- {
- STTBX_Print(("[J_TIMER]: Already Initialized!n"));
- return RETOK;
- }
-
- TIMAppTimerListStart = NULL;
- TIMAppTimerHISRProcess = NULL;
- TIMAppTimerRepeat1TickStart = NULL;
- TIMAppTimerRepeat1TickEnd = NULL;
- TIMAppTimerRepeat1TickHISREnd = NULL;
- TIM1TickInHISR = FALSE;
-
- TimeoutSemaphore = 0;
-
- KB_OSPSemInit("timersem1", 0, KB_Wait, &TimeoutSemaphore);
- KB_OSPTaskInit("KD_TimerTask", TIMER_STACK_SIZE,
- (void(*)(void *))KD_TimerTask, TIM_PRIORITY,
- NULL, &TaskId);
-
- isTimerInit = 1;
- return RETOK;
- }
- INT32 KB_TimerDel(UINT32 timerId)
- {
- INT32 ret;
- T_TIMER_st *timer = (T_TIMER_st *)timerId;
- ret = RETFIAL1;
-
- CHECK_TIMER_ID(timerId);
- if(timer->valid == TRUE)
- {
- if (TIMAppTimerHISRProcess == timer)
- {
- TIMAppTimerHISRFinish = TIMER_FINISH_DELETE;
- ret = RETOK;
- }
- else
- {
- ret = KB_TimerDisable(timerId);
- timer->valid = FALSE;
- timer->entry = NULL;
- timer->current_count = 0;
- timer->initial_ms = 0;
- }
- }
-
- KB_OSPFree((void*)timer);
- return(ret);
- }
- INT32 KB_TimerDisable(UINT32 timerId)
- {
- INT32 ret;
- T_TIMER_st *timer = (T_TIMER_st *)timerId;
- T_TIMER_st *prev_timer;
- T_TIMER_st *next_timer;
- CHECK_TIMER_ID(timerId);
-
- ret = RETFIAL1;
- if (TIMAppTimerHISRProcess == timer)
- {
- TIMAppTimerHISRFinish = TIMER_FINISH_DISABLE;
- ret = RETOK;
- }
- else
- {
- if(timer->valid == TRUE)
- {
- if (timer->enabled == TRUE)
- {
- if (timer->repeat1 == TRUE)
- {
- if (TIMAppTimerRepeat1TickStart == timer)
- {
- TIMAppTimerRepeat1TickStart = timer->next;
- if (timer->next != NULL)
- {
- TIMAppTimerRepeat1TickStart->prev = NULL;
- }
- }
- else
- {
- if (timer == TIMAppTimerRepeat1TickEnd)
- {
- if (timer == TIMAppTimerRepeat1TickHISREnd)
- {
- TIMAppTimerRepeat1TickHISREnd = timer->prev;
- }
-
- TIMAppTimerRepeat1TickEnd = timer->prev;
- TIMAppTimerRepeat1TickEnd->next = NULL;
- }
- else
- {
- prev_timer = timer->prev;
- next_timer = timer->next;
- prev_timer->next = next_timer;
- next_timer->prev = prev_timer;
- }
- }
- }
- else
- {
- if (TIMAppTimerListStart == timer)
- {
- next_timer = timer->next;
- if (next_timer != NULL)
- {
- next_timer->current_count += timer->current_count;
- next_timer->prev = NULL;
- }
- TIMAppTimerListStart = next_timer;
- }
- else
- {
- prev_timer = timer->prev;
- prev_timer->next = timer->next;
- next_timer = timer->next;
-
- if (next_timer != NULL)
- {
- next_timer->prev = prev_timer;
- next_timer->current_count += timer->current_count;
- }
- }
- }
- timer->next = NULL;
- timer->prev = NULL;
- timer->enabled = FALSE;
- timer->repeat1 = FALSE;
- }
- ret = RETOK;
- }
- }
- return(ret);
- }
- INT32 KB_TimerEnable(UINT32 timerId, UINT32 ms)
- {
- INT32 ret;
- UINT32 ticks;
- T_TIMER_st *timer = (T_TIMER_st *)timerId;
- T_TIMER_st *current_timer;
- T_TIMER_st *next_timer;
- CHECK_TIMER_ID(timerId);
-
- ret = RETFIAL1;
- if (ms > 0)
- {
- ticks = ms / TIMER_TICK_INT_PERIOD;
- if ((ms % TIMER_TICK_INT_PERIOD) != 0)
- {
- ticks++;
- }
- if (TIMAppTimerHISRProcess == timer)
- {
- if (timer->mode == KB_TIMER_ONCE)
- {
- TIMAppTimerHISRFinish = TIMER_FINISH_ENABLE;
- TIMAppHISRms = ms;
- }
- else
- {
- TIMAppTimerHISRFinish = TIMER_FINISH_NORMAL;
- timer->initial_ms = ms;
- }
-
- ret = RETOK;
- }
- else
- {
- if(timer->valid == TRUE)
- {
- if (timer->enabled == FALSE)
- {
- timer->current_count = ticks;
- timer->initial_ms = ms;
-
- timer->prev = NULL;
- timer->next = NULL;
- timer->repeat1 = FALSE;
- if ((ticks == 1)&&(timer->mode == KB_TIMER_REPEAT))
- {
- if (TIMAppTimerRepeat1TickStart == NULL)
- {
- TIMAppTimerRepeat1TickStart = timer;
- TIMAppTimerRepeat1TickEnd = timer;
- }
- else
- {
- TIMAppTimerRepeat1TickEnd->next = timer;
- timer->prev = TIMAppTimerRepeat1TickEnd;
- TIMAppTimerRepeat1TickEnd = timer;
- }
- timer->repeat1 = TRUE;
- }
- else
- {
- if (TIMAppTimerListStart == NULL)
- {
- TIMAppTimerListStart = timer;
- }
- else
- {
- if (TIM1TickInHISR == TRUE)
- {
- timer->current_count++;
- }
-
- if (TIMAppTimerListStart->current_count > timer->current_count)
- {
- TIMAppTimerListStart->prev = timer;
- timer->next = TIMAppTimerListStart;
- TIMAppTimerListStart->current_count -= timer->current_count;
- TIMAppTimerListStart = timer;
- }
- else
- {
- current_timer = TIMAppTimerListStart;
- while(current_timer != NULL)
- {
- next_timer = current_timer->next;
- timer->current_count -= current_timer->current_count;
- if (next_timer == NULL)
- {
- current_timer->next = timer;
- timer->prev = current_timer;
- }
- else
- {
- if (next_timer->current_count > timer->current_count)
- {
- current_timer->next = timer;
- timer->next = next_timer;
- timer->prev = current_timer;
- next_timer->prev = timer;
- next_timer->current_count -= timer->current_count;
- next_timer = NULL;
- }
- }
- current_timer = next_timer;
- }
- }
- }
- }
-
- timer->enabled = TRUE;
- }
-
- ret = RETOK;
- }
- }
- }
- return(ret);
- }
- static void KD_TimerHandleMsg(void)
- {
- T_TIMER_st *timer;
- T_TIMER_st *next;
- UINT32 expire;
- UINT32 last;
- expire = FALSE;
- last = FALSE;
- TIMAppTimerHISRFinish = TIMER_FINISH_NORMAL;
- /*
- ** Process the 1 tick repeat list. These timers always expire and always
- ** are rescheduled so we just go through the entire list every time. This
- ** saves us some overhead as we do not have to disable and re-enable the
- ** timers every time like in the main list.
- */
- timer = TIMAppTimerRepeat1TickStart;
- TIMAppTimerHISRProcess = timer;
- TIMAppTimerRepeat1TickHISREnd = TIMAppTimerRepeat1TickEnd;
- if (timer == TIMAppTimerRepeat1TickHISREnd)
- {
- last = TRUE;
- }
- TIM1TickInHISR = TRUE;
- while(timer != NULL)
- {
- next = timer->next;
-
- if(timer->entry != NULL)
- {
- timer->entry(timer->param);
- }
- TIMAppTimerHISRProcess = NULL;
-
- switch(TIMAppTimerHISRFinish)
- {
- default:
- break;
- case TIMER_FINISH_DISABLE:
- KB_TimerDisable((UINT32)timer);
- break;
- case TIMER_FINISH_DELETE:
- KB_TimerDel((UINT32)timer);
- break;
- }
- if (last == FALSE)
- {
- timer = next;
- if (timer == TIMAppTimerRepeat1TickHISREnd)
- {
- last = TRUE;
- }
- }
- else
- {
- timer = NULL;
- }
- TIMAppTimerHISRProcess = timer;
- TIMAppTimerHISRFinish = TIMER_FINISH_NORMAL;
- }
- TIM1TickInHISR = FALSE;
- TIMAppTimerRepeat1TickHISREnd = NULL;
- TIMAppTimerHISRProcess = NULL;
- TIMAppTimerHISRFinish = TIMER_FINISH_NORMAL;
-
- if (TIMAppTimerListStart != NULL)
- {
- TIMAppTimerListStart->current_count--;
- if (TIMAppTimerListStart->current_count == 0)
- {
- expire = TRUE;
- timer = TIMAppTimerListStart;
- TIMAppTimerHISRProcess = timer;
- }
- }
-
- while (expire == TRUE)
- {
- next = timer->next;
-
- if(timer->entry != NULL)
- {
- timer->entry(timer->param);
- }
- expire = FALSE;
- TIMAppTimerHISRProcess = NULL;
-
- KB_TimerDisable((UINT32)timer);
- switch(TIMAppTimerHISRFinish)
- {
- case TIMER_FINISH_ENABLE:
- KB_TimerEnable((UINT32)timer, TIMAppHISRms);
- break;
- default:
- case TIMER_FINISH_NORMAL:
- if (timer->mode == KB_TIMER_REPEAT)
- {
- KB_TimerEnable((UINT32)timer, timer->initial_ms);
- }
- break;
- case TIMER_FINISH_DISABLE:
- break;
- case TIMER_FINISH_DELETE:
- KB_TimerDel((UINT32)timer);
- break;
- }
-
- if (next != NULL)
- {
- if (next->current_count == 0)
- {
- expire = TRUE;
- timer = next;
- TIMAppTimerHISRProcess = timer;
- }
- }
- TIMAppTimerHISRFinish = TIMER_FINISH_NORMAL;
- }
- TIMAppTimerHISRProcess = NULL;
- }
- UINT32 KB_TimerCreate(KB_TimerMode mode, KB_TIMER_FUNC_POINTER entry, void *param)
- {
- T_TIMER_st *timer;
- timer = (T_TIMER_st *)KB_OSPMalloc(sizeof(T_TIMER_st));
-
- if(timer == NULL) return KB_TIMER_INVALID_ID;
-
- timer->entry = entry;
- timer->param = param;
-
- timer->next = NULL;
- timer->prev = NULL;
- timer->mode = mode;
- timer->enabled = FALSE;
- timer->repeat1 = FALSE;
- timer->current_count = 0;
- timer->initial_ms = 0;
-
- timer->valid = TRUE;
-
- return (UINT32)timer;
- }
- static void KD_TimerTask(void)
- {
- while(1)
- {
- #if 1
- KB_OSPSemGet(TimeoutSemaphore, KB_Wait, TIMER_TICK_INT_PERIOD);
- #else
- KB_OSPTaskDelay(TIMER_TICK_INT_PERIOD);
- #endif
- if((TIMAppTimerListStart != NULL) || (TIMAppTimerRepeat1TickStart != NULL))
- {
- KD_TimerHandleMsg();
- }
- }
- }
- /* EOF --------------------------------------------------------------------- */