Skip to content

Commit 08777b3

Browse files
committed
Add systick event flag
Signed-off-by: Tim Crawford <tcrawford@system76.com>
1 parent ba2a432 commit 08777b3

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

src/app/main/main.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ void timer_2(void) __interrupt(5) {}
4848

4949
uint8_t main_cycle = 0;
5050

51-
#define INTERVAL_1MS 1U
5251
#define INTERVAL_5MS 5U
5352
#define INTERVAL_100MS 100U
5453
#define INTERVAL_250MS 250U
@@ -107,7 +106,6 @@ void main(void) {
107106

108107
INFO("System76 EC board '%s', version '%s'\n", board(), version());
109108

110-
systick_t last_time_1ms = 0;
111109
systick_t last_time_5ms = 0;
112110
systick_t last_time_100ms = 0;
113111
systick_t last_time_250ms = 0;
@@ -122,32 +120,34 @@ void main(void) {
122120
__bit evt_1sec = 0;
123121

124122
for (main_cycle = 0;; main_cycle++) {
125-
systick_t time = time_get();
123+
// Calculate which intervals need to run at systick update.
124+
if (evt_systick) {
125+
evt_systick = 0;
126+
127+
systick_t time = time_get();
126128

127-
// FIXME: These only need to run on systick event instead of every loop.
128-
if ((time - last_time_1ms) >= INTERVAL_1MS) {
129-
last_time_1ms = time;
130129
evt_1ms = 1;
131-
}
132-
if ((time - last_time_5ms) >= INTERVAL_5MS) {
133-
last_time_5ms = time;
134-
evt_5ms = 1;
135-
}
136-
if ((time - last_time_100ms) >= INTERVAL_100MS) {
137-
last_time_100ms = time;
138-
evt_100ms = 1;
139-
}
140-
if ((time - last_time_250ms) >= INTERVAL_250MS) {
141-
last_time_250ms = time;
142-
evt_250ms = 1;
143-
}
144-
if ((time - last_time_500ms) >= INTERVAL_500MS) {
145-
last_time_500ms = time;
146-
evt_500ms = 1;
147-
}
148-
if ((time - last_time_1sec) >= INTERVAL_1SEC) {
149-
last_time_1sec = time;
150-
evt_1sec = 1;
130+
131+
if ((time - last_time_5ms) >= INTERVAL_5MS) {
132+
last_time_5ms = time;
133+
evt_5ms = 1;
134+
}
135+
if ((time - last_time_100ms) >= INTERVAL_100MS) {
136+
last_time_100ms = time;
137+
evt_100ms = 1;
138+
}
139+
if ((time - last_time_250ms) >= INTERVAL_250MS) {
140+
last_time_250ms = time;
141+
evt_250ms = 1;
142+
}
143+
if ((time - last_time_500ms) >= INTERVAL_500MS) {
144+
last_time_500ms = time;
145+
evt_500ms = 1;
146+
}
147+
if ((time - last_time_1sec) >= INTERVAL_1SEC) {
148+
last_time_1sec = time;
149+
evt_1sec = 1;
150+
}
151151
}
152152

153153
if (evt_1ms) {

src/arch/8051/include/arch/time.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
typedef uint16_t systick_t;
99

10+
// Event flag for app to hook systick update.
11+
extern volatile __bit evt_systick;
12+
1013
void time_init(void);
1114
systick_t time_get(void);
1215

src/arch/8051/time.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define TIMER_RELOAD (0xFFFF - (TICK_INTERVAL_MS * (CONFIG_CLOCK_FREQ_KHZ / OSC_DIVISOR)))
1212

1313
static volatile systick_t time_overflows = 0;
14+
volatile __bit evt_systick;
1415

1516
void timer_0(void) __interrupt(1) {
1617
// Hardware automatically clears the the interrupt
@@ -19,6 +20,7 @@ void timer_0(void) __interrupt(1) {
1920
TR0 = 0;
2021

2122
time_overflows++;
23+
evt_systick = 1;
2224

2325
// Reload the values
2426
TH0 = TIMER_RELOAD >> 8;
@@ -37,6 +39,7 @@ void time_init(void) __critical {
3739
TF0 = 0;
3840

3941
time_overflows = 0;
42+
evt_systick = 0;
4043

4144
// Enable the interrupt
4245
ET0 = 1;

0 commit comments

Comments
 (0)