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 committed Nov 22, 2024
1 parent 3d8204c commit 1e46f80
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 47 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;
}
}
74 changes: 49 additions & 25 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 @@ -103,38 +106,68 @@ 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:
}

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

fan_event();
}

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

peci_read_temp();
dgpu_read_temp();
}

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

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

if (main_cycle == 0) {
systick_t time = time_get();
if ((time - last_time_1sec) >= INTERVAL_1SEC) {
last_time_1sec = time;

if ((time - last_time_100ms) >= INTERVAL_100MS) {
last_time_100ms = time;
Expand All @@ -158,16 +191,7 @@ void main(void) {
}
}

// 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();
// Idle until next timer interrupt
//Disabled until interrupts used: PCON |= 1;
//PCON |= BIT(0);
}
}

0 comments on commit 1e46f80

Please sign in to comment.