aboutsummaryrefslogtreecommitdiffstats
path: root/src/chdelta.c
blob: ef4493b7969b0599270b1dcbcf4bba399e8617b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
    ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @addtogroup VirtualTimers
 * @{
 */

#include <ch.h>

#ifdef CH_USE_VIRTUAL_TIMERS
DeltaList dlist;

/**
 * Virtual Timers initialization. Internal use only.
 */
void chVTInit(void) {

  dlist.dl_next = dlist.dl_prev = (VirtualTimer *)&dlist;
  dlist.dl_dtime = (systime_t)-1;
}

/**
 * Enables a virtual timer.
 * @param vtp the \p VirtualTimer structure pointer
 * @param time the number of time ticks, the value zero is allowed with
 *             meaning "infinite". In this case the structure is initialized
 *             but not inserted in the delta list, the timer will never be
 *             triggered.
 * @param vtfunc the timer callback function. After invoking the callback
 *               the timer is disabled and the structure can be disposed or
 *               reused.
 * @param par a parameter that will be passed to the callback function
 * @note Must be called with the interrupts disabled.
 * @note The associated function is invoked by an interrupt handler.
 */
void chVTSetI(VirtualTimer *vtp, systime_t time, vtfunc_t vtfunc, void *par) {

  vtp->vt_par = par;
  vtp->vt_func = vtfunc;
  if (time) {
    VirtualTimer *p = dlist.dl_next;
    while (p->vt_dtime < time) {
      time -= p->vt_dtime;
      p = p->vt_next;
    }

    vtp->vt_prev = (vtp->vt_next = p)->vt_prev;
    vtp->vt_prev->vt_next = p->vt_prev = vtp;
    vtp->vt_dtime = time;
    if (p != (VirtualTimer *)&dlist)
      p->vt_dtime -= time;
  }
  else
    vtp->vt_next = vtp->vt_prev = vtp; // Allows a chVTResetI() on the fake timer.
}

/**
 * Disables a Virtual Timer.
 * @param vtp the \p VirtualTimer structure pointer
 * @note It must be called with the interrupts disabled.
 * @note The timer MUST be active when this function is invoked.
 */
void chVTResetI(VirtualTimer *vtp) {

  if (vtp->vt_next != (VirtualTimer *)&dlist)
    vtp->vt_next->vt_dtime += vtp->vt_dtime;
  vtp->vt_prev->vt_next = vtp->vt_next;
  vtp->vt_next->vt_prev = vtp->vt_prev;
  vtp->vt_func = 0;
}
#endif /* CH_USE_VIRTUAL_TIMER */

/** @} */