rt_Timer.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*----------------------------------------------------------------------------
  2. * CMSIS-RTOS - 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. #ifndef __CMSIS_RTOS
  39. /*----------------------------------------------------------------------------
  40. * Global Variables
  41. *---------------------------------------------------------------------------*/
  42. /* User Timer list pointer */
  43. struct OS_XTMR os_tmr;
  44. /*----------------------------------------------------------------------------
  45. * Functions
  46. *---------------------------------------------------------------------------*/
  47. /*--------------------------- rt_tmr_tick -----------------------------------*/
  48. void rt_tmr_tick (void) {
  49. /* Decrement delta count of timer list head. Timers having the value of */
  50. /* zero are removed from the list and the callback function is called. */
  51. P_TMR p;
  52. if (os_tmr.next == NULL) {
  53. return;
  54. }
  55. os_tmr.tcnt--;
  56. while ((os_tmr.tcnt == 0U) && ((p = os_tmr.next) != NULL)) {
  57. /* Call a user provided function to handle an elapsed timer */
  58. os_tmr_call (p->info);
  59. os_tmr.tcnt = p->tcnt;
  60. os_tmr.next = p->next;
  61. rt_free_box ((U32 *)m_tmr, p);
  62. }
  63. }
  64. /*--------------------------- rt_tmr_create ---------------------------------*/
  65. OS_ID rt_tmr_create (U16 tcnt, U16 info) {
  66. /* Create an user timer and put it into the chained timer list using */
  67. /* a timeout count value of "tcnt". User parameter "info" is used as a */
  68. /* parameter for the user provided callback function "os_tmr_call ()". */
  69. P_TMR p_tmr, p;
  70. U32 delta,itcnt = tcnt;
  71. if ((tcnt == 0U) || (m_tmr == NULL)) {
  72. return (NULL);
  73. }
  74. p_tmr = rt_alloc_box ((U32 *)m_tmr);
  75. if (!p_tmr) {
  76. return (NULL);
  77. }
  78. p_tmr->info = info;
  79. p = (P_TMR)&os_tmr;
  80. delta = p->tcnt;
  81. while ((delta < itcnt) && (p->next != NULL)) {
  82. p = p->next;
  83. delta += p->tcnt;
  84. }
  85. /* Right place found, insert timer into the list */
  86. p_tmr->next = p->next;
  87. p_tmr->tcnt = (U16)(delta - itcnt);
  88. p->next = p_tmr;
  89. p->tcnt -= p_tmr->tcnt;
  90. return (p_tmr);
  91. }
  92. /*--------------------------- rt_tmr_kill -----------------------------------*/
  93. OS_ID rt_tmr_kill (OS_ID timer) {
  94. /* Remove user timer from the chained timer list. */
  95. P_TMR p, p_tmr;
  96. p_tmr = (P_TMR)timer;
  97. p = (P_TMR)&os_tmr;
  98. /* Search timer list for requested timer */
  99. while (p->next != p_tmr) {
  100. if (p->next == NULL) {
  101. /* Failed, "timer" is not in the timer list */
  102. return (p_tmr);
  103. }
  104. p = p->next;
  105. }
  106. /* Timer was found, remove it from the list */
  107. p->next = p_tmr->next;
  108. p->tcnt += p_tmr->tcnt;
  109. rt_free_box ((U32 *)m_tmr, p_tmr);
  110. /* Timer killed */
  111. return (NULL);
  112. }
  113. #endif
  114. /*----------------------------------------------------------------------------
  115. * end of file
  116. *---------------------------------------------------------------------------*/