rt_Timer.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*----------------------------------------------------------------------------
  2. * RL-ARM - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RT_TIMER.C
  5. * Purpose: User timer functions
  6. * Rev.: V4.70
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2013 ARM Germany GmbH
  10. * All rights reserved.
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * - Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * - Neither the name of ARM nor the names of its contributors may be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *---------------------------------------------------------------------------*/
  34. #include "rt_TypeDef.h"
  35. #include "RTX_Config.h"
  36. #include "rt_Timer.h"
  37. #include "rt_MemBox.h"
  38. /*----------------------------------------------------------------------------
  39. * Global Variables
  40. *---------------------------------------------------------------------------*/
  41. /* User Timer list pointer */
  42. struct OS_XTMR os_tmr;
  43. /*----------------------------------------------------------------------------
  44. * Functions
  45. *---------------------------------------------------------------------------*/
  46. /*--------------------------- rt_tmr_tick -----------------------------------*/
  47. void rt_tmr_tick (void) {
  48. /* Decrement delta count of timer list head. Timers having the value of */
  49. /* zero are removed from the list and the callback function is called. */
  50. P_TMR p;
  51. if (os_tmr.next == NULL) {
  52. return;
  53. }
  54. os_tmr.tcnt--;
  55. while (os_tmr.tcnt == 0 && (p = os_tmr.next) != NULL) {
  56. /* Call a user provided function to handle an elapsed timer */
  57. os_tmr_call (p->info);
  58. os_tmr.tcnt = p->tcnt;
  59. os_tmr.next = p->next;
  60. rt_free_box ((U32 *)m_tmr, p);
  61. }
  62. }
  63. /*--------------------------- rt_tmr_create ---------------------------------*/
  64. OS_ID rt_tmr_create (U16 tcnt, U16 info) {
  65. /* Create an user timer and put it into the chained timer list using */
  66. /* a timeout count value of "tcnt". User parameter "info" is used as a */
  67. /* parameter for the user provided callback function "os_tmr_call ()". */
  68. P_TMR p_tmr, p;
  69. U32 delta,itcnt = tcnt;
  70. if (tcnt == 0 || m_tmr == NULL) {
  71. return (NULL);
  72. }
  73. p_tmr = rt_alloc_box ((U32 *)m_tmr);
  74. if (!p_tmr) {
  75. return (NULL);
  76. }
  77. p_tmr->info = info;
  78. p = (P_TMR)&os_tmr;
  79. delta = p->tcnt;
  80. while (delta < itcnt && p->next != NULL) {
  81. p = p->next;
  82. delta += p->tcnt;
  83. }
  84. /* Right place found, insert timer into the list */
  85. p_tmr->next = p->next;
  86. p_tmr->tcnt = (U16)(delta - itcnt);
  87. p->next = p_tmr;
  88. p->tcnt -= p_tmr->tcnt;
  89. return (p_tmr);
  90. }
  91. /*--------------------------- rt_tmr_kill -----------------------------------*/
  92. OS_ID rt_tmr_kill (OS_ID timer) {
  93. /* Remove user timer from the chained timer list. */
  94. P_TMR p, p_tmr;
  95. p_tmr = (P_TMR)timer;
  96. p = (P_TMR)&os_tmr;
  97. /* Search timer list for requested timer */
  98. while (p->next != p_tmr) {
  99. if (p->next == NULL) {
  100. /* Failed, "timer" is not in the timer list */
  101. return (p_tmr);
  102. }
  103. p = p->next;
  104. }
  105. /* Timer was found, remove it from the list */
  106. p->next = p_tmr->next;
  107. p->tcnt += p_tmr->tcnt;
  108. rt_free_box ((U32 *)m_tmr, p_tmr);
  109. /* Timer killed */
  110. return (NULL);
  111. }
  112. /*----------------------------------------------------------------------------
  113. * end of file
  114. *---------------------------------------------------------------------------*/