timer.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:13k
源码类别:

DVD

开发平台:

C/C++

  1. #include <string.h>
  2. #include "stlite.h"
  3. #include "stdevice.h"
  4. #include "stddefs.h"
  5. #include "sttbx.h"
  6. #include "stcommon.h"
  7. #include "errors.h"
  8. #include "timer.h"
  9. #include "osp.h"
  10. static T_TIMER_st *TIMAppTimerListStart;
  11. static T_TIMER_st *TIMAppTimerRepeat1TickStart;
  12. static T_TIMER_st *TIMAppTimerRepeat1TickEnd;
  13. static T_TIMER_st *TIMAppTimerRepeat1TickHISREnd;
  14. static T_TIMER_st *TIMAppTimerHISRProcess;
  15. static TIMR_FINISH_t TIMAppTimerHISRFinish;
  16. static UINT32 TIMAppHISRms;
  17. static UINT8 TIM1TickInHISR;
  18. static UINT32 TimeoutSemaphore;
  19. static void KD_TimerTask(void);
  20. static void KD_TimerHandleMsg(void);
  21. /*******************************************************************/
  22. INT32 KB_TimerInit(void)
  23. {
  24. static UINT8 isTimerInit = 0;
  25. UINT32 TaskId;
  26. if(isTimerInit)
  27. {
  28. STTBX_Print(("[J_TIMER]: Already Initialized!n"));
  29. return RETOK;
  30. }
  31. TIMAppTimerListStart          = NULL;
  32.     TIMAppTimerHISRProcess        = NULL;
  33.     TIMAppTimerRepeat1TickStart   = NULL;
  34.     TIMAppTimerRepeat1TickEnd     = NULL;
  35.     TIMAppTimerRepeat1TickHISREnd = NULL;
  36.     TIM1TickInHISR = FALSE;
  37. TimeoutSemaphore = 0;
  38. KB_OSPSemInit("timersem1", 0, KB_Wait, &TimeoutSemaphore);
  39. KB_OSPTaskInit("KD_TimerTask", TIMER_STACK_SIZE,
  40.  (void(*)(void *))KD_TimerTask, TIM_PRIORITY,
  41.  NULL, &TaskId);
  42.  
  43. isTimerInit = 1;
  44. return RETOK;
  45. }
  46. INT32 KB_TimerDel(UINT32 timerId)
  47. {
  48. INT32 ret;
  49. T_TIMER_st *timer = (T_TIMER_st *)timerId;
  50. ret = RETFIAL1;
  51. CHECK_TIMER_ID(timerId);
  52. if(timer->valid == TRUE)
  53. {
  54.         if (TIMAppTimerHISRProcess == timer)
  55.         {
  56.             TIMAppTimerHISRFinish = TIMER_FINISH_DELETE;
  57.             ret = RETOK;
  58.         }
  59.         else
  60.         {
  61.             ret = KB_TimerDisable(timerId);
  62.             timer->valid         = FALSE;
  63.             timer->entry         = NULL;
  64.             timer->current_count = 0;
  65.             timer->initial_ms    = 0;
  66.         }
  67. }
  68. KB_OSPFree((void*)timer);
  69.     return(ret);
  70. }
  71. INT32 KB_TimerDisable(UINT32 timerId)
  72. {
  73. INT32 ret;
  74. T_TIMER_st *timer = (T_TIMER_st *)timerId;
  75.     T_TIMER_st *prev_timer;
  76.     T_TIMER_st *next_timer;
  77. CHECK_TIMER_ID(timerId);
  78.     ret = RETFIAL1;
  79. if (TIMAppTimerHISRProcess == timer)
  80.     {
  81.         TIMAppTimerHISRFinish = TIMER_FINISH_DISABLE;
  82.         ret = RETOK;
  83.     }
  84.     else
  85.     {
  86.     if(timer->valid == TRUE)
  87.     {
  88.         if (timer->enabled == TRUE)
  89.         {
  90.             if (timer->repeat1 == TRUE)
  91.             {
  92.                 if (TIMAppTimerRepeat1TickStart == timer)
  93.                 {
  94.                     TIMAppTimerRepeat1TickStart = timer->next;
  95.                     if (timer->next != NULL)
  96.                     {
  97.                         TIMAppTimerRepeat1TickStart->prev = NULL;    
  98.                     }
  99.                 }
  100.                 else
  101.                 {
  102.                     if (timer == TIMAppTimerRepeat1TickEnd)
  103.                     {
  104.                             if (timer == TIMAppTimerRepeat1TickHISREnd)
  105.                             {
  106.                                 TIMAppTimerRepeat1TickHISREnd = timer->prev;
  107.                             }
  108.                         TIMAppTimerRepeat1TickEnd = timer->prev;
  109.                         TIMAppTimerRepeat1TickEnd->next = NULL;
  110.                     }
  111.                     else
  112.                     {
  113.                         prev_timer = timer->prev;
  114.                         next_timer = timer->next;
  115.                         prev_timer->next = next_timer;
  116.                         next_timer->prev = prev_timer;
  117.                     }
  118.                 }
  119.             }
  120.             else
  121.             {
  122.                 if (TIMAppTimerListStart == timer)
  123.                 {
  124.                     next_timer = timer->next;
  125.                     if (next_timer != NULL)
  126.                     {
  127.                         next_timer->current_count += timer->current_count;
  128.                         next_timer->prev = NULL;
  129.                     }
  130.                     TIMAppTimerListStart = next_timer;
  131.                 }
  132.                 else
  133.                 {
  134.                     prev_timer = timer->prev;
  135.                     prev_timer->next = timer->next;
  136.                     next_timer = timer->next;
  137.                                 
  138.                     if (next_timer != NULL)
  139.                     {
  140.                         next_timer->prev = prev_timer;
  141.                         next_timer->current_count += timer->current_count;
  142.                     }           
  143.                 }
  144.             }
  145.             timer->next    = NULL;
  146.             timer->prev    = NULL;
  147.                 timer->enabled = FALSE;
  148.             timer->repeat1 = FALSE;
  149.         }
  150.         ret = RETOK;
  151.     }
  152.     }
  153.     return(ret);
  154. }
  155. INT32 KB_TimerEnable(UINT32 timerId, UINT32 ms)
  156. {
  157. INT32 ret;
  158.     UINT32 ticks;
  159. T_TIMER_st *timer = (T_TIMER_st *)timerId;
  160.     T_TIMER_st *current_timer;
  161.     T_TIMER_st *next_timer;
  162. CHECK_TIMER_ID(timerId);
  163.     ret = RETFIAL1;
  164.     if (ms > 0)
  165.     {
  166.         ticks = ms / TIMER_TICK_INT_PERIOD;
  167.         if ((ms % TIMER_TICK_INT_PERIOD) != 0)
  168.         {
  169.             ticks++;
  170.         } 
  171. if (TIMAppTimerHISRProcess == timer)
  172.         {
  173.             if (timer->mode == KB_TIMER_ONCE)
  174.             {
  175.                 TIMAppTimerHISRFinish = TIMER_FINISH_ENABLE;
  176.                 TIMAppHISRms          = ms;
  177.             }
  178. else
  179. {
  180. TIMAppTimerHISRFinish = TIMER_FINISH_NORMAL;
  181.                 timer->initial_ms = ms;
  182. }
  183.             ret = RETOK;
  184.         }
  185.         else
  186.         {
  187.         if(timer->valid == TRUE)
  188.         {
  189. if (timer->enabled == FALSE)
  190.             {
  191.                 timer->current_count = ticks;
  192.                 timer->initial_ms    = ms;
  193.       
  194.                 timer->prev = NULL;
  195.                 timer->next = NULL;
  196.                 timer->repeat1 = FALSE;
  197.                 if ((ticks == 1)&&(timer->mode == KB_TIMER_REPEAT))
  198.                 {
  199.                     if (TIMAppTimerRepeat1TickStart == NULL)
  200.                     {
  201.                         TIMAppTimerRepeat1TickStart = timer;
  202.                         TIMAppTimerRepeat1TickEnd   = timer;
  203.                     }
  204.                     else
  205.                     {
  206.                         TIMAppTimerRepeat1TickEnd->next = timer;
  207.                         timer->prev = TIMAppTimerRepeat1TickEnd;
  208.                         TIMAppTimerRepeat1TickEnd = timer;
  209.                     }
  210.                     timer->repeat1 = TRUE;
  211.                 }
  212.                 else
  213.                 {
  214.                     if (TIMAppTimerListStart == NULL)
  215.                     {
  216.                         TIMAppTimerListStart = timer;
  217.                     }
  218.                     else
  219.                     {
  220.                             if (TIM1TickInHISR == TRUE)
  221.                             {
  222.                                 timer->current_count++;
  223.                             }
  224.                         if (TIMAppTimerListStart->current_count > timer->current_count)
  225.                         {
  226.                             TIMAppTimerListStart->prev = timer;
  227.                             timer->next = TIMAppTimerListStart; 
  228.                             TIMAppTimerListStart->current_count -= timer->current_count;
  229.                             TIMAppTimerListStart = timer;
  230.                         }
  231.                         else
  232.                         {
  233.                             current_timer = TIMAppTimerListStart;
  234.                             while(current_timer != NULL)
  235.                             {    
  236.                                 next_timer = current_timer->next;
  237.                                 timer->current_count -= current_timer->current_count;
  238.                                 if (next_timer == NULL)
  239.                                 {
  240.                                     current_timer->next = timer;
  241.                                     timer->prev = current_timer;    
  242.                                 }
  243.                                 else
  244.                                 {
  245.                                     if (next_timer->current_count > timer->current_count)
  246.                                     {
  247.                                         current_timer->next = timer;
  248.                                         timer->next = next_timer;
  249.                                         timer->prev = current_timer;
  250.                                         next_timer->prev = timer;
  251.                                         next_timer->current_count -= timer->current_count;
  252.                                         next_timer = NULL;
  253.                                     }
  254.                                 }
  255.                                 current_timer = next_timer;
  256.                             }
  257.                         }
  258.                     }
  259.                 }
  260.                 
  261.                 timer->enabled = TRUE;    
  262.             }
  263.             
  264.             ret = RETOK;
  265.         }
  266.         }
  267.     }
  268.     return(ret);
  269. }
  270. static void KD_TimerHandleMsg(void)
  271. {
  272.     T_TIMER_st *timer;
  273.     T_TIMER_st *next;
  274.     UINT32 expire;
  275.     UINT32 last;
  276.     expire = FALSE;
  277.     last   = FALSE;
  278.     TIMAppTimerHISRFinish  = TIMER_FINISH_NORMAL;
  279.     /* 
  280.     ** Process the 1 tick repeat list.  These timers always expire and always
  281.     ** are rescheduled so we just go through the entire list every time.  This
  282.     ** saves us some overhead as we do not have to disable and re-enable the
  283.     ** timers every time like in the main list.
  284.     */
  285.     timer = TIMAppTimerRepeat1TickStart;
  286.     TIMAppTimerHISRProcess = timer;
  287.     TIMAppTimerRepeat1TickHISREnd = TIMAppTimerRepeat1TickEnd;
  288.     if (timer == TIMAppTimerRepeat1TickHISREnd)
  289.     {
  290.         last = TRUE;
  291.     }
  292.     TIM1TickInHISR = TRUE;
  293.     while(timer != NULL)
  294.     {
  295. next = timer->next;
  296.         if(timer->entry != NULL)
  297.         {
  298. timer->entry(timer->param);
  299.         }
  300.         TIMAppTimerHISRProcess = NULL;
  301.         
  302.         switch(TIMAppTimerHISRFinish)
  303.         {
  304.         default:
  305.          break;
  306.         case  TIMER_FINISH_DISABLE:
  307.             KB_TimerDisable((UINT32)timer);
  308.          break;
  309.         case  TIMER_FINISH_DELETE:
  310.             KB_TimerDel((UINT32)timer);
  311.          break;
  312.         }
  313.         if (last == FALSE)
  314.         {
  315.             timer = next;
  316.             if (timer == TIMAppTimerRepeat1TickHISREnd)
  317.             {
  318.                 last = TRUE;
  319.             }
  320.         }
  321.         else
  322.         {
  323.             timer = NULL;
  324.         }
  325.         TIMAppTimerHISRProcess = timer;
  326.         TIMAppTimerHISRFinish  = TIMER_FINISH_NORMAL;
  327.     }
  328.     TIM1TickInHISR = FALSE;
  329.     TIMAppTimerRepeat1TickHISREnd = NULL;
  330.     TIMAppTimerHISRProcess = NULL;
  331.     TIMAppTimerHISRFinish  = TIMER_FINISH_NORMAL;
  332.     
  333.     if (TIMAppTimerListStart != NULL)
  334.     {
  335.         TIMAppTimerListStart->current_count--;
  336.         if (TIMAppTimerListStart->current_count == 0)
  337.         {
  338.             expire =  TRUE;
  339.             timer  =  TIMAppTimerListStart;
  340.             TIMAppTimerHISRProcess = timer;
  341.         }                
  342.     }
  343.     
  344.     while (expire == TRUE)
  345.     {    
  346. next = timer->next;
  347.         if(timer->entry != NULL)
  348.         {
  349. timer->entry(timer->param);
  350.         }
  351.         expire = FALSE;
  352.         TIMAppTimerHISRProcess = NULL;
  353.         
  354.         KB_TimerDisable((UINT32)timer);
  355.         switch(TIMAppTimerHISRFinish)
  356.         {
  357.         case TIMER_FINISH_ENABLE:
  358.             KB_TimerEnable((UINT32)timer, TIMAppHISRms);
  359.          break;
  360.         default:
  361.         case TIMER_FINISH_NORMAL:
  362.            if (timer->mode == KB_TIMER_REPEAT)
  363.            {
  364.                KB_TimerEnable((UINT32)timer, timer->initial_ms);
  365.            }
  366.          break;
  367.         case TIMER_FINISH_DISABLE:    
  368.          break;
  369.         case TIMER_FINISH_DELETE:
  370.             KB_TimerDel((UINT32)timer);
  371.          break;
  372.         }
  373.         
  374.         if (next != NULL)
  375.         {       
  376.             if (next->current_count == 0)
  377.             {
  378.                 expire = TRUE;
  379.                 timer  = next;
  380.                 TIMAppTimerHISRProcess = timer;
  381.             }
  382.         }
  383.         TIMAppTimerHISRFinish  = TIMER_FINISH_NORMAL;
  384.     }
  385.     TIMAppTimerHISRProcess = NULL;              
  386. }
  387. UINT32 KB_TimerCreate(KB_TimerMode mode, KB_TIMER_FUNC_POINTER entry, void *param)
  388. {
  389. T_TIMER_st *timer;
  390. timer = (T_TIMER_st *)KB_OSPMalloc(sizeof(T_TIMER_st));
  391. if(timer == NULL) return KB_TIMER_INVALID_ID;
  392.     timer->entry   = entry;
  393. timer->param   = param;
  394.     timer->next    = NULL;
  395.     timer->prev    = NULL;
  396.     timer->mode    = mode;
  397.     timer->enabled = FALSE;
  398.     timer->repeat1 = FALSE;
  399.     timer->current_count = 0;
  400.     timer->initial_ms    = 0;
  401.     
  402.     timer->valid   = TRUE;
  403. return (UINT32)timer;
  404. }
  405. static void KD_TimerTask(void)
  406. {
  407.     while(1)
  408.     {
  409. #if 1
  410. KB_OSPSemGet(TimeoutSemaphore, KB_Wait, TIMER_TICK_INT_PERIOD);
  411.         #else
  412.         KB_OSPTaskDelay(TIMER_TICK_INT_PERIOD);
  413. #endif
  414.         if((TIMAppTimerListStart != NULL) || (TIMAppTimerRepeat1TickStart != NULL))
  415.     {  
  416.         KD_TimerHandleMsg(); 
  417.     }
  418.     }
  419. }
  420. /* EOF --------------------------------------------------------------------- */