Skip to content

Commit

Permalink
time: Replace hard-coded values with macro
Browse files Browse the repository at this point in the history
This is more descriptive than the comment and allows for changing
values, such as using a chip with a different clock frequency.

The resulting binary is identical.

Signed-off-by: Tim Crawford <[email protected]>
  • Loading branch information
crawfxrd committed Feb 26, 2024
1 parent 449bafd commit 82b9e19
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/arch/8051/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,52 @@
// Uses timer 0 to keep track of global time

#include <8051.h>

#include <arch/time.h>

#define OSC_DIVISOR 12
#define TICK_INTERVAL_MS 1
// 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;

void timer_0(void) __interrupt(1) {
// Hardware automatically clears the the interrupt

// Stop timer
TR0 = 0;

time_overflows++;

// Start timer
TH0 = 0xFD;
TL0 = 0x01;
// Reload the values
TH0 = TIMER_RELOAD >> 8;
TL0 = TIMER_RELOAD & 0xFF;

// Restart the timer
TR0 = 1;
}

/**
* Set up Timer 0 as the system tick.
*/
void time_init(void) __critical {
// Stop the timer
TR0 = 0;
TF0 = 0;

time_overflows = 0;

// Enable timer interrupts
// Enable the interrupt
ET0 = 1;

// Start timer in mode 1
// (65536 - 64769) / (9.2 MHz / 12) = ~1 ms interval
// Set the timer to mode 1 (16-bit timer)
TMOD = (TMOD & 0xF0) | 0x01;
TH0 = 0xFD;
TL0 = 0x01;

// Set the initial values
TH0 = TIMER_RELOAD >> 8;
TL0 = TIMER_RELOAD & 0xFF;

// Start the timer
TR0 = 1;
}

Expand Down
3 changes: 3 additions & 0 deletions src/ec/ite/ec.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ ec-y += ps2.c
ec-y += signature.c
ec-y += wuc.c

# Chip clock frequency: 9.2 MHz
CFLAGS += -DCONFIG_CLOCK_FREQ_KHZ=9200

ifeq ($(CONFIG_EC_ITE_IT8587E), y)
CFLAGS+=-DCONFIG_EC_ITE_IT8587E=1
# SRAM is 4096 bytes, but SRAM at address 2048 is used for scratch ROM
Expand Down

0 comments on commit 82b9e19

Please sign in to comment.