Add pit driver

Still no IRQ handler called
This commit is contained in:
Mathieu Maret 2018-07-16 14:20:12 +02:00
parent 3e3f4a1c4c
commit 24cab443f2
4 changed files with 39 additions and 7 deletions

View File

@ -8,3 +8,4 @@ void print_handler(struct interrupt_frame *frame, ulong error_code);
//IRQ
void keyboard_handler(struct interrupt_frame *frame);
void timer_handler(struct interrupt_frame *frame);

15
main.c
View File

@ -4,6 +4,7 @@
#include "interrupt.h"
#include "io.h"
#include "irq.h"
#include "pit.h"
#include "types.h"
#include "vga.h"
@ -24,7 +25,6 @@ void cpuid(int code, uint32_t *a, uint32_t *d)
asm volatile("cpuid" : "=a"(*a), "=d"(*d) : "0"(code) : "ebx", "ecx");
}
void kmain()
{
const short color = GREEN;
@ -34,17 +34,18 @@ void kmain()
idtSetup();
printString("Setting up IRQ", color, BLACK, 0, 1);
irqSetup();
initPit(100);
printString("Setting up IRQ_KEYBOARD", color, BLACK, 0, 1);
printString("Setting up IRQ handlers", color, BLACK, 0, 1);
irqSetRoutine(IRQ_KEYBOARD, keyboard_handler);
printString("Enabling HW interrupts", color, BLACK, 0, 1);
irqSetRoutine(IRQ_TIMER, timer_handler);
printString("Enabling HW interrupts", color, BLACK, 0, 2);
exceptionSetRoutine(EXCEPTION_DOUBLE_FAULT, print_handler);
// Enabling the HW interrupts
asm volatile("sti\n");
printString("Idling", color, BLACK, 0, 2);
int count = 0;
while (1) {
char c = getScancode();
printChar(c, color, BLACK, 0, 5);
printInt(count++, color, BLACK, 0, 6);
}
printString("exiting", color, BLACK, 0, 3);
printString("exiting", color, BLACK, 0, 4);
}

14
pit.c Normal file
View File

@ -0,0 +1,14 @@
#include "pit.h"
#include "io.h"
int initPit(unsigned int freq)
{
unsigned int divisor = PIT_FREQ / freq;
if (divisor > 65535)
divisor = 0; // Used to represent 35536
outb(PIT_CMD, 0x34); // chan 0; low then high; mode 2
outb(PIT_CHAN_0, divisor & 0xFF);
outb(PIT_CHAN_0, divisor >> 8u);
return 0;
}

16
pit.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
// C.f https://wiki.osdev.org/PIT
#define PIT_FREQ 1193182
#define PIT_CHAN_0 0x40 // IRQ0
#define PIT_CHAN_1 0x41 // Used for DRAM refresh. Not used anymore
#define PIT_CHAN_2 0x42 // PC Speaker
#define PIT_CMD 0x43
// Cmd are
// 7-6: select channel. 0 ->chan0, 1 -> chan1, 2 -> chan 2, 3 -> read back
// 5-4: access mode. 0 -> latch count; 1 -> low value only; 2 -> high value only;
// 3 -> low then high 3-1: mode. See https://wiki.osdev.org/PIT
int initPit(unsigned int freq);