-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys_time.c
85 lines (73 loc) · 2.44 KB
/
sys_time.c
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
/*
*
* Copyright (C) 2009-2011 The Paparazzi Team
*
* This file is part of paparazzi.
*
* paparazzi 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 2, or (at your option)
* any later version.
*
* paparazzi 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 paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
/**
* @file mcu_periph/sys_time.c
* @brief Architecture independent timing functions.
*
*/
#include "sys_time.h"
PRINT_CONFIG_VAR(SYS_TIME_FREQUENCY)
struct sys_time sys_time;
int sys_time_register_timer(float duration, sys_time_cb cb) {
uint32_t start_time = sys_time.nb_tick;
int i;
for (i = 0; i< SYS_TIME_NB_TIMER; i++) {
if (!sys_time.timer[i].in_use) {
sys_time.timer[i].cb = cb;
sys_time.timer[i].elapsed = FALSE;
sys_time.timer[i].end_time = start_time + sys_time_ticks_of_sec(duration);
sys_time.timer[i].duration = sys_time_ticks_of_sec(duration);
sys_time.timer[i].in_use = TRUE;
return i;
}
}
return -1;
}
void sys_time_cancel_timer(tid_t id) {
sys_time.timer[id].in_use = FALSE;
sys_time.timer[id].cb = NULL;
sys_time.timer[id].elapsed = FALSE;
sys_time.timer[id].end_time = 0;
sys_time.timer[id].duration = 0;
}
// FIXME: race condition ??
void sys_time_update_timer(tid_t id, float duration) {
sys_time.timer[id].end_time -= (sys_time.timer[id].duration - sys_time_ticks_of_sec(duration));
sys_time.timer[id].duration = sys_time_ticks_of_sec(duration);
}
void sys_time_init( void ) {
sys_time.nb_sec = 0;
sys_time.nb_sec_rem = 0;
sys_time.nb_tick = 0;
sys_time.ticks_per_sec = SYS_TIME_FREQUENCY;
sys_time.resolution = 1.0 / sys_time.ticks_per_sec;
unsigned int i;
for (i=0; i<SYS_TIME_NB_TIMER; i++) {
sys_time.timer[i].in_use = FALSE;
sys_time.timer[i].cb = NULL;
sys_time.timer[i].elapsed = FALSE;
sys_time.timer[i].end_time = 0;
sys_time.timer[i].duration = 0;
}
sys_time_arch_init();
}