1/**
2 * \file
3 * \brief Support of one-shot timers
4 *
5 * To simplify things we maintain one timer for the scheduler, and one for the
6 * wakeup infrastructure. Each of these subsystems is responsible for updating
7 * their timer value. When an update happens, we update the hardware timer if
8 * the previous (global) timer has changed.
9 */
10
11/*
12 * Copyright (c) 2011, ETH Zurich.
13 * All rights reserved.
14 *
15 * This file is distributed under the terms in the attached LICENSE file.
16 * If you do not find this file, copies can be found by writing to:
17 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
18 */
19
20#include <timer.h>
21#include <kernel.h>
22
23/* these are systime_t i.e., absolute time values in ms */
24static systime_t next_sched_timer = TIMER_INF;   //< timer for scheduler
25static systime_t next_wakeup_timer = TIMER_INF;  //< timer for wakeups
26static systime_t last_timer;                     //< last set timer
27
28#define MIN(x,y) ((x<y) ? (x) : (y))
29
30static void update_timer(void)
31{
32    systime_t timer = MIN(next_sched_timer, next_wakeup_timer);
33    assert(timer != 0);
34    if (last_timer != timer) {
35        #if 0 /* does not seem to create a problem */
36        if (timer == TIMER_INF) {
37            printk(LOG_WARN, "********* %s:%s() timer == TIMER_INF\n", __FILE__, __FUNCTION__);
38            //timer = kernel_now + kernel_timeslice;
39        }
40        #endif
41        arch_set_timer(last_timer = timer);
42    }
43}
44
45/**
46 * \brief update the wakeup timer
47 * \param t absolute time in ms for the next interrupt
48 */
49void update_wakeup_timer(systime_t t)
50{
51    next_wakeup_timer = t;
52    update_timer();
53}
54
55/**
56 * \brief update the sched timer
57 * \param t absolute time in ms for the next interrupt
58 */
59void update_sched_timer(systime_t t)
60{
61    next_sched_timer = t;
62    update_timer();
63}
64
65