forked from system76/ec
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtime.c
More file actions
56 lines (42 loc) · 1.09 KB
/
Copy pathtime.c
File metadata and controls
56 lines (42 loc) · 1.09 KB
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
// SPDX-License-Identifier: GPL-3.0-only
// Uses timer 0 to keep track of global time
#include <8051.h>
#include <arch/time.h>
static volatile uint32_t time_overflows = 0;
// Set every 50 ms by timer_0; cleared by main loop periodic handler.
// 50 ms is the base for all periodic intervals:
// 2 ticks = 100 ms (power/usbpd poll)
// 5 ticks = 250 ms (smooth fan)
// 20 ticks = 1000 ms (fan normal + battery)
volatile bool timer_50ms_pending = false;
void timer_0(void) __interrupt(1) {
// Stop timer
TR0 = 0;
time_overflows++;
static uint8_t ticks = 0;
if (++ticks >= 50) {
ticks = 0;
timer_50ms_pending = true;
}
// Start timer
TH0 = 0xFD;
TL0 = 0x01;
TR0 = 1;
}
void time_init(void) __critical {
// Stop the timer
TR0 = 0;
TF0 = 0;
time_overflows = 0;
// Enable timer interrupts
ET0 = 1;
// Start timer in mode 1
// (65536 - 64769) / (9.2 MHz / 12) = ~1 ms interval
TMOD = (TMOD & 0xF0) | 0x01;
TH0 = 0xFD;
TL0 = 0x01;
TR0 = 1;
}
uint32_t time_get(void) __critical {
return time_overflows;
}