2 * Copyright Droids Corporation, Microb Technology, Eirbot (2005)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Revision : $Id: time.c,v 1.4.4.5 2009-04-24 19:26:37 zer0 Exp $
22 /* Droids-corp, Eirbot, Microb Technology 2005 - Zer0
23 * Implementation of the time module
27 * \brief Implementation of the TIME module.
30 * This module can be used to get a human readable time. It uses the
31 * scheduler module. Its goal is not to be very precise, but just
36 /**********************************************************/
39 #include <scheduler.h>
42 #include <time_config.h>
44 /**********************************************************/
46 #define NB_SCHEDULER_UNIT ( ((float)(TIME_PRECISION)) / SCHEDULER_UNIT_FLOAT )
47 #define NB_SCHEDULER_UNIT_NOT_NULL (NB_SCHEDULER_UNIT == 0 ? 1.0 : NB_SCHEDULER_UNIT)
49 static volatile time_h t;
51 static volatile microseconds us2; // this one overflows naturally
54 void time_increment(void * dummy);
56 /**********************************************************/
58 void time_init(uint8_t priority)
61 scheduler_add_periodical_event_priority(time_increment,NULL,
62 (int)NB_SCHEDULER_UNIT_NOT_NULL, priority);
65 /**********************************************************/
67 seconds time_get_s(void)
77 /**********************************************************/
79 microseconds time_get_us(void)
89 /**********************************************************/
91 microseconds time_get_us2(void)
101 /**********************************************************/
103 time_h time_get_time(void)
113 /**********************************************************/
115 void time_reset(void)
124 /**********************************************************/
126 void time_set(seconds s, microseconds us)
135 /**********************************************************/
137 void time_wait_ms(uint16_t ms)
139 microseconds old = time_get_us2();
140 while (time_get_us2() - old < ms*1000L);
143 /**********************************************************/
145 /**********************************************************/
147 void time_increment(__attribute__((unused)) void * dummy)
150 /* XXX we should lock only when writing */
151 IRQ_LOCK(flags); // for reading correct time inside an interrupt
153 us2 += ((int)NB_SCHEDULER_UNIT_NOT_NULL * SCHEDULER_UNIT);
154 t.us += ((int)NB_SCHEDULER_UNIT_NOT_NULL * SCHEDULER_UNIT);
155 while (t.us > 1000000) {