update ldscript
[protos/xbee-avr.git] / time.c
1 /*  
2  *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
3  * 
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.
8  *
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.
13  *
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
17  *
18  *  Revision : $Id: time.c,v 1.4.4.5 2009-04-24 19:26:37 zer0 Exp $
19  *
20  */
21
22 /* Droids-corp, Eirbot, Microb Technology 2005 - Zer0
23  * Implementation of the time module
24  */
25
26 /** \file time.c
27  *  \brief Implementation of the TIME module.
28  *
29  *
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
32  *  simple to use.
33  */
34
35
36 /**********************************************************/
37
38 #include <stdlib.h>
39 #include <scheduler.h>
40
41 #include <clock_time.h>
42 #include <time_config.h>
43
44 /**********************************************************/
45
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)
48
49 static volatile time_h t;
50
51 static volatile microseconds us2; // this one overflows naturally
52
53
54 void time_increment(void * dummy);
55
56 /**********************************************************/
57
58 void time_init(uint8_t priority)
59 {
60   time_reset();
61   scheduler_add_periodical_event_priority(time_increment,NULL,
62                                           (int)NB_SCHEDULER_UNIT_NOT_NULL, priority);
63 }
64
65 /**********************************************************/
66
67 seconds time_get_s(void)
68 {
69   uint16_t tmp;
70   uint8_t flags;
71   IRQ_LOCK(flags);
72   tmp = t.s;
73   IRQ_UNLOCK(flags);
74   return tmp;
75 }
76
77 /**********************************************************/
78
79 microseconds time_get_us(void)
80 {
81   microseconds tmp;
82   uint8_t flags;
83   IRQ_LOCK(flags);
84   tmp = t.us;
85   IRQ_UNLOCK(flags);
86   return tmp;
87 }
88
89 /**********************************************************/
90
91 microseconds time_get_us2(void)
92 {
93   microseconds tmp;
94   uint8_t flags;
95   IRQ_LOCK(flags);
96   tmp = us2;
97   IRQ_UNLOCK(flags);
98   return tmp;
99 }
100
101 /**********************************************************/
102
103 time_h time_get_time(void)
104 {
105   time_h tmp;
106   uint8_t flags;
107   IRQ_LOCK(flags);
108   tmp = t;
109   IRQ_UNLOCK(flags);
110   return tmp;
111 }
112
113 /**********************************************************/
114
115 void time_reset(void)
116 {
117   uint8_t flags;
118   IRQ_LOCK(flags);
119   t.us = 0;
120   t.s = 0;
121   IRQ_UNLOCK(flags);
122 }
123
124 /**********************************************************/
125
126 void time_set(seconds s, microseconds us)
127 {
128   uint8_t flags;
129   IRQ_LOCK(flags);
130   t.us = us;
131   t.s = s;
132   IRQ_UNLOCK(flags);
133 }
134
135 /**********************************************************/
136
137 void time_wait_ms(uint16_t ms)
138 {
139         microseconds old = time_get_us2();
140         while (time_get_us2() - old < ms*1000L);
141 }
142
143 /**********************************************************/
144 /* private */
145 /**********************************************************/
146
147 void time_increment(__attribute__((unused)) void * dummy)
148 {
149   uint8_t flags;
150   /* XXX we should lock only when writing */
151   IRQ_LOCK(flags); // for reading correct time inside an interrupt
152
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) {
156       t.s ++;
157       t.us -= 1000000;
158   }
159
160   IRQ_UNLOCK(flags);
161 }