WKern
Loading...
Searching...
No Matches
pit.c
Go to the documentation of this file.
1/*
2WKern - A Bare Metal OS / Kernel I am maKing (For Fun)
3Copyright (C) 2025 Wdboyes13
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18
19#include <io/kio.h>
20#include <types/nums.h>
21// PIT ports
22#define PIT_CHANNEL0 0x40
23#define PIT_COMMAND 0x43
24
25// Command byte bits for mode 3 (square wave generator)
26#define PIT_ACCESS_LOHI 0x30 // access low byte then high byte
27#define PIT_MODE3 0x06 // mode 3 (square wave)
28
29// PIT frequency and divisor
30#define PIT_FREQ 1193182 // base frequency in Hz
31// Setup PIT for a given frequency (e.g., 100 Hz)
32void PitInit(u32 freq) {
33 u16 divisor = (u16)(PIT_FREQ / freq);
34 Kprintf("Divisor Set\n");
35 // Send command byte: channel 0, access mode lo/hi, mode 3, binary mode
37 Kprintf("Command Byte Sent\n");
38 // Send divisor low byte
39 Outb(PIT_CHANNEL0, divisor & 0xFF);
40 Kprintf("Low byte sent\n");
41 // Send divisor high byte
42 Outb(PIT_CHANNEL0, (divisor >> 8) & 0xFF);
43 Kprintf("High byte sent\n");
44}
void Outb(u16 port, u8 val)
Write a byte to the specified I/O port.
Definition asm.c:30
#define PIT_FREQ
Definition idtirq.h:107
void Kprintf(const char *fmt,...)
Formatted output to the screen.
Definition printer.c:152
unsigned int u32
32-Bit Unsigned Int
Definition nums.h:30
unsigned short u16
16-Bit Unsigned Int
Definition nums.h:36
#define PIT_MODE3
Definition pit.c:27
#define PIT_COMMAND
Definition pit.c:23
#define PIT_ACCESS_LOHI
Definition pit.c:26
#define PIT_CHANNEL0
Definition pit.c:22
void PitInit(u32 freq)
Definition pit.c:32