Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/pic/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ union {

void init_timer();

double get_uptime();

void sleep(double seconds);

#endif
53 changes: 34 additions & 19 deletions src/pic/timer.c
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@

#include <arch/io.h>
#include <pic/timer.h>
#include <pic/interrupts.h>

int i = 0;
int j = 0;
void timer(struct interrupt_frame *frame) {
return;
unsigned long ticks; // The amount of ticks that have passed
int hz; // Self-Explanitory
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const int = 20;

// Self-explanatory isn't a valuable comment.


// Timer callback
void timer(struct interrupt_frame * frame) {
ticks++;
return;
}

void sleep(double seconds) {
double to_wait = seconds + get_uptime();
while(to_wait != get_uptime());
}

void set_timer_phase(int hz) {
PIT_8254 cmd;
cmd.BCD = BCD_16_BIT;
cmd.mode = MODE_SQR_WAVE;
cmd.RW = RW_READ | RW_WRITE;
cmd.CNTR = 0;

outb(PIT_CMD, cmd.cmd);
int divisor = 1193180 / hz;
outb(PIT_CH0, divisor & 0xff);
outb(PIT_CH0, divisor >> 8);
Comment on lines -12 to -22
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

likewise, do not change file indentation.

// Returns how many seconds the computer was awake
double get_uptime() {
return ticks * (1 / hz);
}

void set_timer_phase() {
PIT_8254 cmd;
cmd.BCD = BCD_16_BIT;
cmd.mode = MODE_SQR_WAVE;
cmd.RW = RW_READ | RW_WRITE;
cmd.CNTR = 0;

outb(PIT_CMD, cmd.cmd);
int divisor = 1193180 / hz;
outb(PIT_CH0, divisor & 0xff);
outb(PIT_CH0, divisor >> 8);
}

void init_timer() {
set_timer_phase(20);
hz = 20;
secs = 0;
ticks = 0;
set_timer_phase();

set_interrupt_handler(0x20, &timer);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not change file indentation.

outb(PIC1_DATA, inb(PIC1_DATA)&~0x01);
set_interrupt_handler(0x20, & timer);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a take-reference, not a bitwise and. please remove the space.

outb(PIC1_DATA, inb(PIC1_DATA) & ~0x01);
}