Skip to content

Commit

Permalink
Use a 16-bit system tick
Browse files Browse the repository at this point in the history
The maximum interval when configured for a 1ms tick:

- 16-bit: ~65 seconds
- 32-bit: ~49.7 days

The value is used for scheduling and timeouts, and not to track the
uptime of the system, so the 32-bit value is excessive.

Signed-off-by: Tim Crawford <tcrawford@system76.com>
  • Loading branch information
crawfxrd committed Jul 25, 2024
1 parent 9e7f195 commit 0f7642d
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/arch/8051/include/arch/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

#include <stdint.h>

typedef uint16_t systick_t;

void time_init(void);
uint32_t time_get(void);
systick_t time_get(void);

#endif // _ARCH_TIME_H
4 changes: 2 additions & 2 deletions src/arch/8051/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// Value to reload into the timer when the overflow interrupt is triggered.
#define TIMER_RELOAD (0xFFFF - (TICK_INTERVAL_MS * (CONFIG_CLOCK_FREQ_KHZ / OSC_DIVISOR)))

static volatile uint32_t time_overflows = 0;
static volatile systick_t time_overflows = 0;

void timer_0(void) __interrupt(1) {
// Hardware automatically clears the the interrupt
Expand Down Expand Up @@ -52,6 +52,6 @@ void time_init(void) __critical {
TR0 = 1;
}

uint32_t time_get(void) __critical {
systick_t time_get(void) __critical {
return time_overflows;
}
10 changes: 5 additions & 5 deletions src/board/system76/common/kbscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,15 @@ void kbscan_event(void) {
uint8_t matrix_curr[KM_OUT];

static bool debounce = false;
static uint32_t debounce_time = 0;
static systick_t debounce_time = 0;

static bool repeat = false;
static uint16_t repeat_key = 0;
static uint32_t repeat_key_time = 0;
static systick_t repeat_key_time = 0;

// If debounce complete
if (debounce) {
uint32_t time = time_get();
systick_t time = time_get();
if ((time - debounce_time) >= DEBOUNCE_DELAY) {
// Debounce time elapsed: Read new state
debounce = false;
Expand Down Expand Up @@ -384,8 +384,8 @@ void kbscan_event(void) {
kbscan_matrix[i] = new;
} else if (new && repeat_key != 0 && key_should_repeat(repeat_key)) {
// A key is being pressed
uint32_t time = time_get();
static uint32_t repeat_start = 0;
systick_t time = time_get();
static systick_t repeat_start = 0;

if (!repeat) {
if (time < repeat_key_time) {
Expand Down
6 changes: 3 additions & 3 deletions src/board/system76/common/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ void main(void) {

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

uint32_t last_time_battery = 0;
uint32_t last_time_fan = 0;
systick_t last_time_battery = 0;
systick_t last_time_fan = 0;

for (main_cycle = 0;; main_cycle++) {
// NOTE: Do note use modulo to avoid expensive call to SDCC library
Expand Down Expand Up @@ -128,7 +128,7 @@ void main(void) {
}

if (main_cycle == 0) {
uint32_t time = time_get();
systick_t time = time_get();
// Only run the following once per interval
if ((time - last_time_fan) >= fan_interval) {
last_time_fan = time;
Expand Down
2 changes: 1 addition & 1 deletion src/board/system76/common/parallel.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
bool parallel_debug = false;

static bool parallel_wait_peripheral(uint8_t mask, uint8_t value) {
uint32_t start = time_get();
systick_t start = time_get();

while ((time_get() - start) < PARALLEL_TIMEOUT) {
if ((KSOHGDMRR & mask) == value) {
Expand Down
8 changes: 4 additions & 4 deletions src/board/system76/common/peci.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ bool peci_get_temp(int16_t *const data) {
ESUCTRL0 |= ESUCTRL0_GO;

// Wait until upstream done
uint32_t start = time_get();
systick_t start = time_get();
while (!(ESUCTRL0 & ESUCTRL0_DONE)) {
if ((time_get() - start) >= PECI_ESPI_TIMEOUT) {
DEBUG("peci_get_temp: upstream timeout\n");
Expand Down Expand Up @@ -189,7 +189,7 @@ int16_t peci_wr_pkg_config(uint8_t index, uint16_t param, uint32_t data) {
ESUCTRL0 |= ESUCTRL0_GO;

// Wait until upstream done
uint32_t start = time_get();
systick_t start = time_get();
while (!(ESUCTRL0 & ESUCTRL0_DONE)) {
DEBUG("peci_wr_pkg_config: wait upstream\n");
if ((time_get() - start) >= PECI_ESPI_TIMEOUT) {
Expand Down Expand Up @@ -254,7 +254,7 @@ void peci_init(void) {
// Returns true on success, false on error
bool peci_get_temp(int16_t *const data) {
// Wait for any in-progress transaction to complete
uint32_t start = time_get();
systick_t start = time_get();
while (HOSTAR & BIT(0)) {
if ((time_get() - start) >= PECI_ESPI_TIMEOUT) {
DEBUG("%s: host timeout\n", __func__);
Expand Down Expand Up @@ -310,7 +310,7 @@ bool peci_get_temp(int16_t *const data) {
// negative (0x1000 | status register) on PECI hardware error
int16_t peci_wr_pkg_config(uint8_t index, uint16_t param, uint32_t data) {
// Wait for any in-progress transaction to complete
uint32_t start = time_get();
systick_t start = time_get();
while (HOSTAR & BIT(0)) {
if ((time_get() - start) >= PECI_ESPI_TIMEOUT) {
DEBUG("%s: host timeout\n", __func__);
Expand Down
4 changes: 2 additions & 2 deletions src/board/system76/common/power.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,8 @@ void power_event(void) {
wake_last = wake_new;
#endif // HAVE_LAN_WAKEUP_N

static uint32_t last_time = 0;
uint32_t time = time_get();
static systick_t last_time = 0;
systick_t time = time_get();
if (power_state == POWER_STATE_S0) {
#if CONFIG_BUS_ESPI
// HOST_C10 virtual wire is high when CPU is in C10 sleep state
Expand Down

0 comments on commit 0f7642d

Please sign in to comment.