rt_Timer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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-2017 ARM Germany GmbH. All rights reserved.
  10. *
  11. * SPDX-License-Identifier: Apache-2.0
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the License); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *---------------------------------------------------------------------------*/
  25. #include "rt_TypeDef.h"
  26. #include "RTX_Config.h"
  27. #include "rt_Timer.h"
  28. #include "rt_MemBox.h"
  29. #ifndef __CMSIS_RTOS
  30. /*----------------------------------------------------------------------------
  31. * Global Variables
  32. *---------------------------------------------------------------------------*/
  33. /* User Timer list pointer */
  34. struct OS_XTMR os_tmr;
  35. /*----------------------------------------------------------------------------
  36. * Functions
  37. *---------------------------------------------------------------------------*/
  38. /*--------------------------- rt_tmr_tick -----------------------------------*/
  39. void rt_tmr_tick (void) {
  40. /* Decrement delta count of timer list head. Timers having the value of */
  41. /* zero are removed from the list and the callback function is called. */
  42. P_TMR p;
  43. if (os_tmr.next == NULL) {
  44. return;
  45. }
  46. os_tmr.tcnt--;
  47. while ((os_tmr.tcnt == 0U) && ((p = os_tmr.next) != NULL)) {
  48. /* Call a user provided function to handle an elapsed timer */
  49. os_tmr_call (p->info);
  50. os_tmr.tcnt = p->tcnt;
  51. os_tmr.next = p->next;
  52. rt_free_box ((U32 *)m_tmr, p);
  53. }
  54. }
  55. /*--------------------------- rt_tmr_create ---------------------------------*/
  56. OS_ID rt_tmr_create (U16 tcnt, U16 info) {
  57. /* Create an user timer and put it into the chained timer list using */
  58. /* a timeout count value of "tcnt". User parameter "info" is used as a */
  59. /* parameter for the user provided callback function "os_tmr_call ()". */
  60. P_TMR p_tmr, p;
  61. U32 delta,itcnt = tcnt;
  62. if ((tcnt == 0U) || (m_tmr == NULL)) {
  63. return (NULL);
  64. }
  65. p_tmr = rt_alloc_box ((U32 *)m_tmr);
  66. if (!p_tmr) {
  67. return (NULL);
  68. }
  69. p_tmr->info = info;
  70. p = (P_TMR)&os_tmr;
  71. delta = p->tcnt;
  72. while ((delta < itcnt) && (p->next != NULL)) {
  73. p = p->next;
  74. delta += p->tcnt;
  75. }
  76. /* Right place found, insert timer into the list */
  77. p_tmr->next = p->next;
  78. p_tmr->tcnt = (U16)(delta - itcnt);
  79. p->next = p_tmr;
  80. p->tcnt -= p_tmr->tcnt;
  81. return (p_tmr);
  82. }
  83. /*--------------------------- rt_tmr_kill -----------------------------------*/
  84. OS_ID rt_tmr_kill (OS_ID timer) {
  85. /* Remove user timer from the chained timer list. */
  86. P_TMR p, p_tmr;
  87. p_tmr = (P_TMR)timer;
  88. p = (P_TMR)&os_tmr;
  89. /* Search timer list for requested timer */
  90. while (p->next != p_tmr) {
  91. if (p->next == NULL) {
  92. /* Failed, "timer" is not in the timer list */
  93. return (p_tmr);
  94. }
  95. p = p->next;
  96. }
  97. /* Timer was found, remove it from the list */
  98. p->next = p_tmr->next;
  99. p->tcnt += p_tmr->tcnt;
  100. rt_free_box ((U32 *)m_tmr, p_tmr);
  101. /* Timer killed */
  102. return (NULL);
  103. }
  104. #endif
  105. /*----------------------------------------------------------------------------
  106. * end of file
  107. *---------------------------------------------------------------------------*/