Skip to content

Commit a5e34b5

Browse files
crawfxrdjackpot51
authored andcommitted
main: Run events at time intervals
Rewrite the main loop to run all its events at certain intervals of the systick instead of running most on every loop. Signed-off-by: Tim Crawford <[email protected]>
1 parent 85da2d2 commit a5e34b5

File tree

4 files changed

+61
-65
lines changed

4 files changed

+61
-65
lines changed

src/board/system76/addw1/board.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <board/kbc.h>
66
#include <common/debug.h>
77

8-
extern uint8_t main_cycle;
9-
108
void board_init(void) {
119
// Allow CPU to boot
1210
gpio_set(&SB_KBCRST_N, true);
@@ -23,14 +21,12 @@ void board_init(void) {
2321
}
2422

2523
void board_event(void) {
26-
if (main_cycle == 0) {
27-
// Set keyboard LEDs
28-
static uint8_t last_kbc_leds = 0;
29-
if (kbc_leds != last_kbc_leds) {
30-
gpio_set(&LED_SCROLL_N, (kbc_leds & 1) == 0);
31-
gpio_set(&LED_NUM_N, (kbc_leds & 2) == 0);
32-
gpio_set(&LED_CAP_N, (kbc_leds & 4) == 0);
33-
last_kbc_leds = kbc_leds;
34-
}
24+
// Set keyboard LEDs
25+
static uint8_t last_kbc_leds = 0;
26+
if (kbc_leds != last_kbc_leds) {
27+
gpio_set(&LED_SCROLL_N, (kbc_leds & 1) == 0);
28+
gpio_set(&LED_NUM_N, (kbc_leds & 2) == 0);
29+
gpio_set(&LED_CAP_N, (kbc_leds & 4) == 0);
30+
last_kbc_leds = kbc_leds;
3531
}
3632
}

src/board/system76/addw2/board.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#include <common/debug.h>
88
#include <ec/ec.h>
99

10-
extern uint8_t main_cycle;
11-
1210
void board_init(void) {
1311
// Allow backlight to be turned on
1412
gpio_set(&BKL_EN, true);
@@ -23,14 +21,12 @@ void board_init(void) {
2321
void board_event(void) {
2422
ec_read_post_codes();
2523

26-
if (main_cycle == 0) {
27-
// Set keyboard LEDs
28-
static uint8_t last_kbc_leds = 0;
29-
if (kbc_leds != last_kbc_leds) {
30-
gpio_set(&LED_SCROLL_N, (kbc_leds & 1) == 0);
31-
gpio_set(&LED_NUM_N, (kbc_leds & 2) == 0);
32-
gpio_set(&LED_CAP_N, (kbc_leds & 4) == 0);
33-
last_kbc_leds = kbc_leds;
34-
}
24+
// Set keyboard LEDs
25+
static uint8_t last_kbc_leds = 0;
26+
if (kbc_leds != last_kbc_leds) {
27+
gpio_set(&LED_SCROLL_N, (kbc_leds & 1) == 0);
28+
gpio_set(&LED_NUM_N, (kbc_leds & 2) == 0);
29+
gpio_set(&LED_CAP_N, (kbc_leds & 4) == 0);
30+
last_kbc_leds = kbc_leds;
3531
}
3632
}

src/board/system76/common/main.c

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ void timer_2(void) __interrupt(5) {}
4848

4949
uint8_t main_cycle = 0;
5050

51-
#define INTERVAL_100MS 100U
52-
#define INTERVAL_250MS 250U
53-
#define INTERVAL_1SEC 1000U
51+
#define INTERVAL_1MS 1U
52+
#define INTERVAL_5MS 5U
53+
#define INTERVAL_100MS 100U
54+
#define INTERVAL_250MS 250U
55+
#define INTERVAL_500MS 500U
56+
#define INTERVAL_1SEC 1000U
5457

5558
void init(void) {
5659
// Must happen first
@@ -104,71 +107,75 @@ void main(void) {
104107

105108
INFO("System76 EC board '%s', version '%s'\n", board(), version());
106109

110+
systick_t last_time_1ms = 0;
111+
systick_t last_time_5ms = 0;
107112
systick_t last_time_100ms = 0;
108113
systick_t last_time_250ms = 0;
114+
systick_t last_time_500ms = 0;
109115
systick_t last_time_1sec = 0;
110116

111117
for (main_cycle = 0;; main_cycle++) {
112-
// NOTE: Do note use modulo to avoid expensive call to SDCC library
113-
// call. (Modulo is optimized for powers of 2, however.)
114-
switch (main_cycle & 3U) {
115-
case 0:
118+
systick_t time = time_get();
119+
120+
if ((time - last_time_1ms) >= INTERVAL_1MS) {
121+
last_time_1ms = time;
122+
116123
// Handle USB-C events immediately before power states
117124
usbpd_event();
118-
119125
// Handle power states
120126
power_event();
121-
break;
122-
case 1:
127+
128+
// Board-specific events
129+
board_event();
130+
// Checks for keyboard/mouse packets from host
131+
kbc_event(&KBC);
132+
// Handles ACPI communication
133+
pmc_event(&PMC_1);
134+
// AP/EC communication over SMFI
135+
smfi_event();
136+
}
137+
138+
if ((time - last_time_5ms) >= INTERVAL_5MS) {
139+
last_time_5ms = time;
140+
123141
#if PARALLEL_DEBUG
124142
if (!parallel_debug)
125143
#endif // PARALLEL_DEBUG
126144
{
127145
// Scans keyboard and sends keyboard packets
128146
kbscan_event();
129147
}
130-
break;
131-
case 2:
132-
// Handle lid close/open
133-
lid_event();
134-
break;
135148
}
136149

137-
if (main_cycle == 0) {
138-
systick_t time = time_get();
139-
140-
if ((time - last_time_100ms) >= INTERVAL_100MS) {
141-
last_time_100ms = time;
150+
if ((time - last_time_100ms) >= INTERVAL_100MS) {
151+
last_time_100ms = time;
142152

143-
fan_event();
144-
}
153+
fan_event();
154+
}
145155

146-
if ((time - last_time_250ms) >= INTERVAL_250MS) {
147-
last_time_250ms = time;
156+
if ((time - last_time_250ms) >= INTERVAL_250MS) {
157+
last_time_250ms = time;
148158

149159
#if CONFIG_PLATFORM_INTEL
150-
peci_read_temp();
160+
peci_read_temp();
151161
#endif
152-
dgpu_read_temp();
153-
}
162+
dgpu_read_temp();
163+
}
154164

155-
if ((time - last_time_1sec) >= INTERVAL_1SEC) {
156-
last_time_1sec = time;
165+
if ((time - last_time_500ms) >= INTERVAL_500MS) {
166+
last_time_500ms = time;
157167

158-
battery_event();
159-
}
168+
// Handle lid close/open
169+
lid_event();
160170
}
161171

162-
// Board-specific events
163-
board_event();
172+
if ((time - last_time_1sec) >= INTERVAL_1SEC) {
173+
last_time_1sec = time;
174+
175+
battery_event();
176+
}
164177

165-
// Checks for keyboard/mouse packets from host
166-
kbc_event(&KBC);
167-
// Handles ACPI communication
168-
pmc_event(&PMC_1);
169-
// AP/EC communication over SMFI
170-
smfi_event();
171178
// Idle until next timer interrupt
172-
//Disabled until interrupts used: PCON |= 1;
179+
//PCON |= BIT(0);
173180
}
174181
}

src/board/system76/common/power/intel.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,6 @@ void power_event(void) {
388388
}
389389
battery_debug();
390390

391-
// Reset main loop cycle to force reading PECI and battery
392-
main_cycle = 0;
393-
394391
// Send SCI to update AC and battery information
395392
ac_send_sci = true;
396393
}

0 commit comments

Comments
 (0)