-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from fgsoftware1/drivers
PIT
- Loading branch information
Showing
12 changed files
with
191 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,32 @@ | ||
#include "../include/libc/include/types.h" | ||
#include "../include/isr.h" | ||
#include "../include/idt.h" | ||
#include "../include/io.h" | ||
#include "../include/drivers/pic.h" | ||
|
||
void init_pic() { | ||
printf("Initiating PIC...\n"); | ||
u8 a1, a2; | ||
|
||
a1 = inportb(PIC1_DATA); | ||
a2 = inportb(PIC2_DATA); | ||
a1 = inportb(PIC_MASTER_DATA); | ||
a2 = inportb(PIC_SLAVE_DATA); | ||
|
||
outportb(PIC1_COMMAND, ICW1); | ||
outportb(PIC2_COMMAND, ICW1); | ||
outportb(PIC_MASTER_COMMAND, PIC_ICW1); | ||
outportb(PIC_SLAVE_COMMAND, PIC_ICW1); | ||
|
||
outportb(PIC1_DATA, 0x20); | ||
outportb(PIC2_DATA, 0x28); | ||
outportb(PIC_MASTER_DATA, 0x20); | ||
outportb(PIC_SLAVE_DATA, 0x28); | ||
|
||
outportb(PIC1_DATA, 4); | ||
outportb(PIC2_DATA, 2); | ||
outportb(PIC_MASTER_DATA, 4); | ||
outportb(PIC_SLAVE_DATA, 2); | ||
|
||
outportb(PIC1_DATA, ICW4_8086); | ||
outportb(PIC2_DATA, ICW4_8086); | ||
outportb(PIC_MASTER_DATA, PIC_ICW4_8086); | ||
outportb(PIC_SLAVE_DATA, PIC_ICW4_8086); | ||
|
||
outportb(PIC1_DATA, a1); | ||
outportb(PIC2_DATA, a2); | ||
outportb(PIC_MASTER_DATA, a1); | ||
outportb(PIC_SLAVE_DATA, a2); | ||
} | ||
|
||
void pic_eoi(u8 irq) { | ||
if(irq >= 0x28) | ||
outportb(PIC2, PIC_EOI); | ||
outportb(PIC1, PIC_EOI); | ||
outportb(PIC_SLAVE_COMMAND, PIC_EOI); | ||
outportb(PIC_MASTER_COMMAND, PIC_EOI); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#include "../include/drivers/pit.h" | ||
#include "../include/console.h" | ||
#include "../include/io.h" | ||
#include "../include/isr.h" | ||
|
||
u64 ticks; | ||
const u32 freq = 100; | ||
|
||
void pit_handler(struct registers_t *r) { ticks += 1; } | ||
|
||
void init_pit() { | ||
printf("Initiating PIT...\n"); | ||
isr_register_interrupt_handler(IRQ_BASE + IRQ_TIMER, pit_handler); | ||
start_pit_timer(); | ||
} | ||
|
||
void start_pit_timer() { | ||
if (freq == 0) { | ||
printf("Can't divide by zero!"); | ||
return; | ||
} | ||
|
||
u16 divisor = 1193180 / freq; | ||
|
||
outportb(PIT_COMMAND_PORT, PIT_COMMAND_REGISTER); | ||
outportb(PIT_CH0_DATA_PORT, (u8)(divisor & 0xFF)); // Send the low byte | ||
outportb(PIT_CH0_DATA_PORT, (u8)((divisor >> 8) & 0xFF)); // Send the high byte | ||
} | ||
|
||
void stop_pit_timer() { outportb(PIT_COMMAND_PORT, 0x30); } | ||
|
||
u8 read_pit(u8 channel) { | ||
u8 port; | ||
switch (channel) { | ||
case 0: | ||
port = PIT_CH0_DATA_PORT; | ||
break; | ||
case 1: | ||
port = PIT_CH1_DATA_PORT; | ||
break; | ||
case 2: | ||
port = PIT_CH2_DATA_PORT; | ||
break; | ||
default: | ||
// Handle invalid channel | ||
break; | ||
} | ||
|
||
// Read from the specified channel | ||
u8 value = inportb(port); | ||
return value; | ||
} | ||
|
||
void write_pit(u8 channel, u8 value) { | ||
u8 port; | ||
switch (channel) { | ||
case 0: | ||
port = PIT_CH0_DATA_PORT; | ||
break; | ||
case 1: | ||
port = PIT_CH1_DATA_PORT; | ||
break; | ||
case 2: | ||
port = PIT_CH2_DATA_PORT; | ||
break; | ||
default: | ||
// Handle invalid channel | ||
break; | ||
} | ||
|
||
// Write to the specified channel | ||
outportb(port, value); | ||
} | ||
|
||
void sleep(u32 milliseconds) { | ||
// Calculate the number of PIT ticks needed for the delay | ||
u32 ticks = milliseconds * 1000 / ticks; | ||
|
||
// Save the current PIT channel 0 count | ||
u8 initialCounter = inportb(PIT_CH0_DATA_PORT); | ||
|
||
start_pit_timer(); | ||
|
||
while (ticks > 0) { | ||
// Wait for PIT interrupt | ||
ticks--; | ||
} | ||
|
||
// Restore the initial PIT channel 0 count | ||
outportb(PIT_COMMAND_REGISTER, 0x36); // Set the operating mode to square wave generator | ||
outportb(PIT_CH0_DATA_PORT, initialCounter); // Set the initial counter value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#ifndef PIT_H | ||
#define PIT_H | ||
|
||
#define PIT_CH0_DATA_PORT 0x40 | ||
#define PIT_CH1_DATA_PORT 0x41 | ||
#define PIT_CH2_DATA_PORT 0x42 | ||
#define PIT_COMMAND_PORT 0x43 | ||
#define PIT_COMMAND_REGISTER 0x36 | ||
|
||
#include "../libc/include/types.h" | ||
|
||
void init_pit(); | ||
void start_pit_timer(); | ||
void stop_pit_timer(); | ||
void sleep(u32 milliseconds); | ||
u8 read_pit(u8 channel); | ||
void write_pit(u8 channel, u8 value); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,33 @@ | ||
#include "./include/libc/include/types.h" | ||
#include "./include/libc/include/defines.h" | ||
#include "include/io.h" | ||
#include "./include/libc/include/defines.h" | ||
#include "./include/libc/include/types.h" | ||
|
||
u8 inportb(u16 port) | ||
{ | ||
u8 ret; | ||
asmv("inb %1, %0" | ||
: "=a"(ret) | ||
: "Nd"(port)); | ||
return ret; | ||
} | ||
|
||
void outportb(u16 port, u8 val) | ||
{ | ||
asmv("outb %1, %0" ::"dN"(port), "a"(val)); | ||
u8 inportb(u16 port) { | ||
u8 ret; | ||
asmv("inb %1, %0" : "=a"(ret) : "dN"(port)); | ||
return ret; | ||
} | ||
|
||
u16 inports(u16 port) | ||
{ | ||
u16 rv; | ||
asmv("inw %1, %0" | ||
: "=a"(rv) | ||
: "dN"(port)); | ||
return rv; | ||
void outportb(u16 port, u8 data) { | ||
asmv("outb %1, %0" ::"dN"(port), "a"(data)); | ||
} | ||
|
||
void outports(u16 port, u16 data) | ||
{ | ||
asmv("outw %1, %0" | ||
: | ||
: "dN"(port), "a"(data)); | ||
u16 inportw(u16 port) { | ||
u16 rv; | ||
asmv("inw %1, %0" : "=a"(rv) : "dN"(port)); | ||
return rv; | ||
} | ||
|
||
u32 inportl(u16 port) | ||
{ | ||
u32 rv; | ||
asmv("inl %%dx, %%eax" | ||
: "=a"(rv) | ||
: "dN"(port)); | ||
return rv; | ||
void outportw(u16 port, u16 data) { | ||
asmv("outw %1, %0" : : "dN"(port), "a"(data)); | ||
} | ||
|
||
void outportl(u16 port, u32 data) | ||
{ | ||
asmv("outl %%eax, %%dx" | ||
: | ||
: "dN"(port), "a"(data)); | ||
u32 inportl(u16 port) { | ||
u32 rv; | ||
asmv("inl %%dx, %%eax" : "=a"(rv) : "dN"(port)); | ||
return rv; | ||
} | ||
|
||
void insw(u16 port, void *buffer, u32 count) | ||
{ | ||
asmv("cld; rep insw" | ||
: "+D"(buffer), "+c"(count) | ||
: "d"(port) | ||
: "memory"); | ||
void outportl(u16 port, u32 data) { | ||
asmv("outl %%eax, %%dx" : : "dN"(port), "a"(data)); | ||
} |
Oops, something went wrong.