Skip to content

Commit

Permalink
refactor(pit): migrate to x86_64 crate
Browse files Browse the repository at this point in the history
  • Loading branch information
mkroening committed Dec 20, 2024
1 parent 4f08312 commit e9ebba2
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/arch/x86_64/kernel/pit.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
#![allow(dead_code)]

use x86::io::*;
use x86_64::instructions::port::Port;

use crate::arch::x86_64::kernel::pic;

const PIT_CLOCK: u64 = 1_193_182;
pub const PIT_INTERRUPT_NUMBER: u8 = pic::PIC1_INTERRUPT_OFFSET;

const PIT_CHANNEL0_DATA_PORT: u16 = 0x40;
const PIT_CHANNEL1_DATA_PORT: u16 = 0x41;
const PIT_CHANNEL2_DATA_PORT: u16 = 0x42;
const PIT_COMMAND_PORT: u16 = 0x43;
const fn pit_channel0_data() -> Port<u8> {
Port::new(0x40)
}

const fn pit_channel1_data() -> Port<u8> {
Port::new(0x41)
}

const fn pit_channel2_data() -> Port<u8> {
Port::new(0x42)
}

const fn pit_command() -> Port<u8> {
Port::new(0x43)
}

const PIT_BINARY_OUTPUT: u8 = 0b0000_0000;
const PIT_BCD_OUTPUT: u8 = 0b0000_0001;
Expand All @@ -34,8 +45,7 @@ pub fn init(frequency_in_hz: u64) {

unsafe {
// Reset the Programmable Interval Timer (PIT).
outb(
PIT_COMMAND_PORT,
pit_command().write(
PIT_BINARY_OUTPUT
| PIT_RATE_GENERATOR_MODE
| PIT_LOBYTE_ACCESS
Expand All @@ -46,8 +56,8 @@ pub fn init(frequency_in_hz: u64) {
// Calculate the reload value to count down (round it to the closest integer).
// Then transmit it as two individual bytes to the PIT.
let count = (PIT_CLOCK + frequency_in_hz / 2) / frequency_in_hz;
outb(PIT_CHANNEL0_DATA_PORT, count as u8);
outb(PIT_CHANNEL0_DATA_PORT, (count >> 8) as u8);
pit_channel0_data().write(count as u8);
pit_channel0_data().write((count >> 8) as u8);
}
}

Expand Down

0 comments on commit e9ebba2

Please sign in to comment.