-
Notifications
You must be signed in to change notification settings - Fork 2
/
timer.c
236 lines (181 loc) · 6.46 KB
/
timer.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*-------------------------------------------------------------------------
timer.c - timer routines for the Embedded Controler of the OLPC project
Copyright (C) 2007 Frieder Ferlemann <Frieder.Ferlemann AT web.de>
This program 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.
This program 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, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
In other words, you are welcome to use, share and improve this program.
You are forbidden to forbid anyone else to use, share and improve
what you give them. Help stamp out software-hoarding!
As a special exception, you may use this file as part of a free software
library for the XO of the One Laptop per Child project without restriction.
Specifically, if other files instantiate
templates or use macros or inline functions from this file, or you compile
this file and link it with other files to produce an executable, this
file does not by itself cause the resulting executable to be covered by
the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
-------------------------------------------------------------------------*/
#include <stdbool.h>
#include "chip.h"
#include "adc.h"
#include "idle.h"
#include "power.h"
#include "watchdog.h"
#include "timer.h"
//! incremented by IRQ
/*! It's a 16 bit variable and the 8051 is a 8 bit CPU,
so when reading this variable be sure that hi and lo
byte are consistent ("atomic access").
\see get_tick()
*/
volatile unsigned int __pdata tick;
static volatile unsigned char __pdata tick_next_s;
//! might as well count seconds since 01.01.1970
/*! please no translation from/to YYYY MM DD on the EC!
(If subsecond resolution should be needed this
could be achieved by a clever mix of
tick_next_s, tick, HZ, + and - operators)
*/
volatile unsigned long __pdata second;
//! There is no embedded device without a timer, is there?
/*! different speed if powered down?
Currently using the 8-bit timer with lowermost priority
for the timer tick IRQ
\bug HZ==100 and GPTCLOCK==32768u result in an
\bug unexpected intervall of 10.70 ms here.
*/
void timer_gpt3_init(void)
{
GPT3H = GPTCLOCK / HZ >> 8;
GPT3L = GPTCLOCK / HZ & 0xff;
/* IRQ enable & enable. Is it true that one bit is
used with dual purpose? */
GPTCFG |= 0x08; /**< enable GPTn counting */
/* start */
GPTPF |= 0x80;
/* set IRQ mask */
P1IE |= 0x80;
}
//! every xx ms (HZ times per second)
/*! It is very tempting to insert stuff here:)
Yet the price is high: Higher interrupt latency
and interrupt jitter for other IRQ routines.
And we cannot do "serious work" within IRQ as
the code the compiler generates is not
reentrant (unless you tell the compiler).
On 8051 architecture reentrancy usually means
high overhead.
Also 16 bit data within a struct handled by
the state machines might be in an intermediate
state (or the variables are not consistent within
themselves (f.e. temperature was read but
flag for overtemperature not yet updated)).
See section "Common interrupt pitfall" in
http://sdcc.sf.net/doc/sdccman.pdf for a(n incomplete:)
list of what do avoid within IRQ.
Please do not call subroutines here (also
make sure that f.e. no 16 bit multiply
silently slips in).
In general: off load work to the state-machines
whereever possible and rely on the main loop
spinning around quickly enough.
*/
void timer_gpt3_interrupt(void) __interrupt(0x17)
{
/* reset IRQ pending flag
is this the way to reset it?
or rather GPTPF = 0x08; */
GPTPF |= 0x08;
tick++;
#if (HZ > 255)
# warning code expects HZ to fit in a byte
#endif
if( (unsigned char)tick == tick_next_s )
{
tick_next_s += HZ;
second++;
// WDTCFG |= 0x01;
// WDTPF = 0x03;
/* handle watchdog. Here? */
if( watchdog_all_up_and_well == (WATCHDOG_MAIN_LOOP_IS_FINE |
WATCHDOG_ONE_WIRE_IS_FINE) )
{
/* reset pending flags */
// WDTPF = 0x03;
WDTCFG |= 0x01;
/* reset this so subsystems have to report again */
watchdog_all_up_and_well = 0x00;
}
ADC_START_CONVERSION;
}
busy = 1;
}
//! safely gets the timer tick
int get_tick(void)
{
unsigned int t;
/* mask the IRQ that changes tick */
P1IE &= ~0x80;
t = tick;
/* reenable the IRQ. It was enabled was it? */
P1IE |= 0x80;
return t;
}
//! safely gets the time
unsigned long get_time(void)
{
unsigned long t;
/* mask the IRQ that changes tick */
P1IE &= ~0x80;
t = second;
/* need subsecond resolution? */
//subsecond = tick_next_s - tick;
/* reenable the IRQ. It was enabled wasn't it? */
P1IE |= 0x80;
return t;
}
//! safely gets the time
unsigned long get_time_ms(void)
{
unsigned long t;
unsigned int subsecond;
/* mask the IRQ that changes tick */
P1IE &= ~0x80;
t = second;
/* need subsecond resolution? */
subsecond = tick_next_s - tick;
/* reenable the IRQ. It was enabled wasn't it? */
P1IE |= 0x80;
t *= 1000;
t += subsecond * (1000/HZ);
return t;
}
//! sets the time
/*! calling this might upset functions using the variable second
\warning Listed in order of my personal preference:
\warning a) should this clear the wake-up timer? or
\warning b) should the wake-up timer be corrected accordingly? or
\warning c) should we rely on the host doing it himself?
*/
void set_time(unsigned long s)
{
/* mask the IRQ that changes tick */
P1IE &= ~0x80;
second = s;
/* sync */
tick_next_s = tick + HZ;
/* reset timer and an eventually pending IRQ flag */
GPTPF = 0x88;
/* reenable the IRQ. It was enabled wasn't it? */
P1IE |= 0x80;
}