-
-
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.
- adding date to time display Signed-off-by: ᛒᛅᚱᚾᚢᛚᚠ <[email protected]>
- Loading branch information
root
authored and
root
committed
Aug 24, 2024
1 parent
01fab8a
commit 713bb3d
Showing
5 changed files
with
79 additions
and
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,92 @@ | ||
#include "../include/drivers/pit.h" | ||
#include "../include/drivers/pic.h" | ||
#include "../include/isr.h" | ||
#include "../include/io.h" | ||
#include "../include/console.h" | ||
#include "../include/io.h" | ||
#include "../include/isr.h" | ||
|
||
void pit_handler(struct registers_t *r){ | ||
u8 dummy = inportb(PIT_CH0_DATA_PORT); | ||
u64 ticks; | ||
const u32 freq = 100; | ||
|
||
pic_eoi(IRQ_BASE); | ||
} | ||
void pit_handler(struct registers_t *r) { ticks += 1; } | ||
|
||
void init_pit(){ | ||
printf("Initiating PIT...\n"); | ||
isr_register_interrupt_handler(IRQ_TIMER, pit_handler); | ||
|
||
//setup PIT into PIC | ||
outportb(PIC_MASTER_COMMAND, PIC_ICW1 | PIC_ICW4_8086); | ||
outportb(PIC_SLAVE_COMMAND, PIC_ICW1 | PIC_ICW4_8086); | ||
outportb(PIC_MASTER_DATA, 1 << IRQ_TIMER); | ||
outportb(PIC_SLAVE_DATA, IRQ_TIMER); | ||
u8 current_mask = inportb(PIC_MASTER_DATA); | ||
current_mask &= ~(1 << IRQ_TIMER); // Clear the bit to enable the PIT interrupt | ||
outportb(PIC_MASTER_DATA, current_mask); | ||
void init_pit() { | ||
printf("Initiating PIT...\n"); | ||
isr_register_interrupt_handler(IRQ_BASE + IRQ_TIMER, pit_handler); | ||
start_pit_timer(); | ||
} | ||
|
||
void start_pit_timer(u16 frequency){ | ||
if (frequency == 0) { | ||
printf("Can't divide by zero!"); | ||
return; | ||
} | ||
void start_pit_timer() { | ||
if (freq == 0) { | ||
printf("Can't divide by zero!"); | ||
return; | ||
} | ||
|
||
u16 divisor = 1193180 / freq; | ||
|
||
u16 divisor = 1193180 / frequency; // Calculate the divisor for the desired frequency | ||
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 | ||
} | ||
|
||
// Set the PIT to the desired frequency | ||
outportb(PIT_COMMAND_PORT, PIT_COMMAND_REGISTER); | ||
outportb(PIT_CH0_DATA_PORT, divisor & 0xFF); // Send the low byte | ||
outportb(PIT_CH0_DATA_PORT, divisor >> 8); // 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 stop_pit_timer(){ | ||
outportb(PIT_COMMAND_PORT, 0x30); | ||
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){ | ||
u32 ticks = milliseconds * 1000 / PIT_TICKS_PER_SECOND; // Calculate the number of ticks | ||
void sleep(u32 milliseconds) { | ||
// Calculate the number of PIT ticks needed for the delay | ||
u32 ticks = milliseconds * 1000 / ticks; | ||
|
||
// Save the current PIT counter value | ||
// Save the current PIT channel 0 count | ||
u8 initialCounter = inportb(PIT_CH0_DATA_PORT); | ||
|
||
// Start the PIT timer | ||
start_pit_timer((u16)PIT_TICKS_PER_SECOND); | ||
start_pit_timer(); | ||
|
||
// Wait for the specified number of ticks | ||
while (ticks > 0) { | ||
// Wait for the PIT interrupt | ||
|
||
// Decrement the remaining ticks | ||
// Wait for PIT interrupt | ||
ticks--; | ||
} | ||
|
||
// Stop the PIT timer | ||
stop_pit_timer(); | ||
|
||
// Restore the initial PIT counter value | ||
outportb(PIT_COMMAND_PORT, PIT_COMMAND_REGISTER); // Set the PIT back to the initial state | ||
outportb(PIT_CH0_DATA_PORT, initialCounter & 0xFF); // Restore the low byte | ||
outportb(PIT_CH0_DATA_PORT, initialCounter >> 8); // Restore the high byte | ||
} | ||
|
||
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); | ||
// 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
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