Skip to content

Commit

Permalink
main: Run events at time intervals
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
crawfxrd authored and jackpot51 committed Dec 3, 2024
1 parent 85da2d2 commit a5e34b5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 65 deletions.
18 changes: 7 additions & 11 deletions src/board/system76/addw1/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <board/kbc.h>
#include <common/debug.h>

extern uint8_t main_cycle;

void board_init(void) {
// Allow CPU to boot
gpio_set(&SB_KBCRST_N, true);
Expand All @@ -23,14 +21,12 @@ void board_init(void) {
}

void board_event(void) {
if (main_cycle == 0) {
// Set keyboard LEDs
static uint8_t last_kbc_leds = 0;
if (kbc_leds != last_kbc_leds) {
gpio_set(&LED_SCROLL_N, (kbc_leds & 1) == 0);
gpio_set(&LED_NUM_N, (kbc_leds & 2) == 0);
gpio_set(&LED_CAP_N, (kbc_leds & 4) == 0);
last_kbc_leds = kbc_leds;
}
// Set keyboard LEDs
static uint8_t last_kbc_leds = 0;
if (kbc_leds != last_kbc_leds) {
gpio_set(&LED_SCROLL_N, (kbc_leds & 1) == 0);
gpio_set(&LED_NUM_N, (kbc_leds & 2) == 0);
gpio_set(&LED_CAP_N, (kbc_leds & 4) == 0);
last_kbc_leds = kbc_leds;
}
}
18 changes: 7 additions & 11 deletions src/board/system76/addw2/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include <common/debug.h>
#include <ec/ec.h>

extern uint8_t main_cycle;

void board_init(void) {
// Allow backlight to be turned on
gpio_set(&BKL_EN, true);
Expand All @@ -23,14 +21,12 @@ void board_init(void) {
void board_event(void) {
ec_read_post_codes();

if (main_cycle == 0) {
// Set keyboard LEDs
static uint8_t last_kbc_leds = 0;
if (kbc_leds != last_kbc_leds) {
gpio_set(&LED_SCROLL_N, (kbc_leds & 1) == 0);
gpio_set(&LED_NUM_N, (kbc_leds & 2) == 0);
gpio_set(&LED_CAP_N, (kbc_leds & 4) == 0);
last_kbc_leds = kbc_leds;
}
// Set keyboard LEDs
static uint8_t last_kbc_leds = 0;
if (kbc_leds != last_kbc_leds) {
gpio_set(&LED_SCROLL_N, (kbc_leds & 1) == 0);
gpio_set(&LED_NUM_N, (kbc_leds & 2) == 0);
gpio_set(&LED_CAP_N, (kbc_leds & 4) == 0);
last_kbc_leds = kbc_leds;
}
}
87 changes: 47 additions & 40 deletions src/board/system76/common/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ void timer_2(void) __interrupt(5) {}

uint8_t main_cycle = 0;

#define INTERVAL_100MS 100U
#define INTERVAL_250MS 250U
#define INTERVAL_1SEC 1000U
#define INTERVAL_1MS 1U
#define INTERVAL_5MS 5U
#define INTERVAL_100MS 100U
#define INTERVAL_250MS 250U
#define INTERVAL_500MS 500U
#define INTERVAL_1SEC 1000U

void init(void) {
// Must happen first
Expand Down Expand Up @@ -104,71 +107,75 @@ void main(void) {

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

systick_t last_time_1ms = 0;
systick_t last_time_5ms = 0;
systick_t last_time_100ms = 0;
systick_t last_time_250ms = 0;
systick_t last_time_500ms = 0;
systick_t last_time_1sec = 0;

for (main_cycle = 0;; main_cycle++) {
// NOTE: Do note use modulo to avoid expensive call to SDCC library
// call. (Modulo is optimized for powers of 2, however.)
switch (main_cycle & 3U) {
case 0:
systick_t time = time_get();

if ((time - last_time_1ms) >= INTERVAL_1MS) {
last_time_1ms = time;

// Handle USB-C events immediately before power states
usbpd_event();

// Handle power states
power_event();
break;
case 1:

// Board-specific events
board_event();
// Checks for keyboard/mouse packets from host
kbc_event(&KBC);
// Handles ACPI communication
pmc_event(&PMC_1);
// AP/EC communication over SMFI
smfi_event();
}

if ((time - last_time_5ms) >= INTERVAL_5MS) {
last_time_5ms = time;

#if PARALLEL_DEBUG
if (!parallel_debug)
#endif // PARALLEL_DEBUG
{
// Scans keyboard and sends keyboard packets
kbscan_event();
}
break;
case 2:
// Handle lid close/open
lid_event();
break;
}

if (main_cycle == 0) {
systick_t time = time_get();

if ((time - last_time_100ms) >= INTERVAL_100MS) {
last_time_100ms = time;
if ((time - last_time_100ms) >= INTERVAL_100MS) {
last_time_100ms = time;

fan_event();
}
fan_event();
}

if ((time - last_time_250ms) >= INTERVAL_250MS) {
last_time_250ms = time;
if ((time - last_time_250ms) >= INTERVAL_250MS) {
last_time_250ms = time;

#if CONFIG_PLATFORM_INTEL
peci_read_temp();
peci_read_temp();
#endif
dgpu_read_temp();
}
dgpu_read_temp();
}

if ((time - last_time_1sec) >= INTERVAL_1SEC) {
last_time_1sec = time;
if ((time - last_time_500ms) >= INTERVAL_500MS) {
last_time_500ms = time;

battery_event();
}
// Handle lid close/open
lid_event();
}

// Board-specific events
board_event();
if ((time - last_time_1sec) >= INTERVAL_1SEC) {
last_time_1sec = time;

battery_event();
}

// Checks for keyboard/mouse packets from host
kbc_event(&KBC);
// Handles ACPI communication
pmc_event(&PMC_1);
// AP/EC communication over SMFI
smfi_event();
// Idle until next timer interrupt
//Disabled until interrupts used: PCON |= 1;
//PCON |= BIT(0);
}
}
3 changes: 0 additions & 3 deletions src/board/system76/common/power/intel.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,6 @@ void power_event(void) {
}
battery_debug();

// Reset main loop cycle to force reading PECI and battery
main_cycle = 0;

// Send SCI to update AC and battery information
ac_send_sci = true;
}
Expand Down

0 comments on commit a5e34b5

Please sign in to comment.