7 #error 8254 timer requires TIMER_FREQ >= 19
10 /* Number of timer ticks since OS booted. */
11 static volatile int64_t ticks;
13 static intr_handler_func timer_interrupt;
15 /* Sets up the 8254 Programmable Interrupt Timer (PIT) to
16 interrupt PIT_FREQ times per second, and registers the
17 corresponding interrupt. */
21 /* 8254 input frequency divided by TIMER_FREQ, rounded to
23 uint16_t count = (1193180 + TIMER_FREQ / 2) / TIMER_FREQ;
25 outb (0x43, 0x34); /* CW: counter 0, LSB then MSB, mode 2, binary. */
26 outb (0x40, count & 0xff);
27 outb (0x40, count >> 8);
29 intr_register (0x20, 0, INTR_OFF, timer_interrupt, "8254 Timer");
32 /* Returns the number of timer ticks since the OS booted. */
36 enum intr_level old_level = intr_disable ();
38 intr_set_level (old_level);
42 /* Returns the number of timer ticks elapsed since THEN, which
43 should be a value once returned by timer_ticks(). */
45 timer_elapsed (int64_t then)
47 return timer_ticks () - then;
50 /* Suspends execution for approximately MS milliseconds. */
52 timer_msleep (int64_t ms)
54 int64_t ticks = (int64_t) ms * TIMER_FREQ / 1000;
55 int64_t start = timer_ticks ();
57 while (timer_elapsed (start) < ticks)
61 /* Suspends execution for approximately US microseconds.
62 Note: this is ridiculously inaccurate. */
64 timer_usleep (int64_t us)
66 timer_msleep (us / 1000 + 1);
69 /* Suspends execution for approximately NS nanoseconds.
70 Note: this is ridiculously inaccurate. */
72 timer_nsleep (int64_t ns)
74 timer_msleep (ns / 1000000 + 1);
77 /* Timer interrupt handler. */
79 timer_interrupt (struct intr_frame *args UNUSED)
82 intr_yield_on_return ();