From 6438d0190e837df7841a1b07ed9f3c703922b127 Mon Sep 17 00:00:00 2001 From: Tim Crawford Date: Fri, 12 Jul 2024 16:48:56 -0600 Subject: [PATCH] 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 --- src/board/system76/addw1/board.c | 18 ++--- src/board/system76/addw2/board.c | 18 ++--- src/board/system76/common/main.c | 87 +++++++++++++------------ src/board/system76/common/power/intel.c | 3 - 4 files changed, 61 insertions(+), 65 deletions(-) diff --git a/src/board/system76/addw1/board.c b/src/board/system76/addw1/board.c index dce4f9ba2..169445806 100644 --- a/src/board/system76/addw1/board.c +++ b/src/board/system76/addw1/board.c @@ -5,8 +5,6 @@ #include #include -extern uint8_t main_cycle; - void board_init(void) { // Allow CPU to boot gpio_set(&SB_KBCRST_N, true); @@ -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; } } diff --git a/src/board/system76/addw2/board.c b/src/board/system76/addw2/board.c index a0a35ae2c..02d6eacf6 100644 --- a/src/board/system76/addw2/board.c +++ b/src/board/system76/addw2/board.c @@ -7,8 +7,6 @@ #include #include -extern uint8_t main_cycle; - void board_init(void) { // Allow backlight to be turned on gpio_set(&BKL_EN, true); @@ -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; } } diff --git a/src/board/system76/common/main.c b/src/board/system76/common/main.c index 9f14c5f47..b0de2a363 100644 --- a/src/board/system76/common/main.c +++ b/src/board/system76/common/main.c @@ -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 @@ -104,22 +107,37 @@ 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 @@ -127,48 +145,37 @@ void main(void) { // 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); } } diff --git a/src/board/system76/common/power/intel.c b/src/board/system76/common/power/intel.c index b64a76021..9e3dd4846 100644 --- a/src/board/system76/common/power/intel.c +++ b/src/board/system76/common/power/intel.c @@ -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; }