tape-kernel 1.0
a modular modern independent kernel
Loading...
Searching...
No Matches
pit.c
Go to the documentation of this file.
1#include "pit.h"
2#include "../io/io.h"
3
4#define PIT_PORT 0x40
5#define PIT_CMD_PORT 0x43
6#define PIT_FREQUENCY 1193182
7
8/**
9 * @brief pinit initializes the programmable interval timer
10 *
11 * pinit is a function that configures the pit to fire at 1000 hz
12 * providing 1ms resolution for timing purposes
13 *
14 * using pinit is done with
15 * @code{.c}
16 * #include "../lib/pit.h" //or just pit.h
17 * pinit();
18 * @endcode
19 *
20 * @see delay()
21 */
22void __pinit(void) {
23 //set pit to 1000 hz (1ms ticks)
24 uint32_t divisor = PIT_FREQUENCY / 1000;
25
26 outb(PIT_CMD_PORT, 0x34); //channel 0, lobyte/hibyte, rate gen
27 outb(PIT_PORT, divisor & 0xFF);
28 outb(PIT_PORT, (divisor >> 8) & 0xFF);
29}
30
31/**
32 * @brief delay pauses execution for a number of milliseconds
33 *
34 * delay is a function that busy-waits on the pit counter for the
35 * specified number of milliseconds
36 * @param ms, the number of milliseconds to wait
37 *
38 * using delay is done with
39 * @code{.c}
40 * #include "../lib/pit.h" //or just pit.h
41 * delay(500); //wait half a second
42 * @endcode
43 *
44 * @see pinit()
45 */
46void __delay(uint32_t ms) {
47 for (uint32_t i = 0; i < ms; i++) {
48 uint16_t count;
49
50 //wait for one tick (1ms)
51 do {
52 outb(PIT_CMD_PORT, 0x00); //latch current count
53 count = inb(PIT_PORT);
54 count |= inb(PIT_PORT) << 8;
55 } while (count == 0); //wait for counter to roll over
56 }
57}
#define inb
Definition io.h:6
#define outb
Definition io.h:7
#define PIT_FREQUENCY
Definition pit.c:6
#define PIT_PORT
Definition pit.c:4
void __pinit(void)
pinit initializes the programmable interval timer
Definition pit.c:22
#define PIT_CMD_PORT
Definition pit.c:5
void __delay(uint32_t ms)
delay pauses execution for a number of milliseconds
Definition pit.c:46
unsigned short uint16_t
Definition types.h:29
unsigned int uint32_t
Definition types.h:30