-
Notifications
You must be signed in to change notification settings - Fork 1
Implement PIT driver #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chbaker0
wants to merge
5
commits into
master
Choose a base branch
from
pit
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
541898a
Initialize PIT and install interrupt handler.
chbaker0 71cdd27
Merge branch 'master' into pit
chbaker0 82275d1
Move PIT into new module and define TickSource interface.
chbaker0 7cb3721
Merge branch 'master' into pit
chbaker0 1667094
Apply rustfmt
chbaker0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,15 @@ | ||
| mod pit; | ||
|
|
||
| pub fn init() { | ||
| pit::init(); | ||
| } | ||
|
|
||
| /// Represents a periodic tick generator. | ||
| trait TickSource { | ||
| /// Get an estimation of the ticks emitted per second. | ||
| fn approx_ticks_per_second(&self) -> u64; | ||
| // Set a function to be called for each tick. | ||
| fn set_tick_handler(&mut self, fn(u64)); | ||
| // Get the current number of elapsed ticks. | ||
| fn get_ticks(&self) -> u64; | ||
| } |
This file contains hidden or 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,109 @@ | ||
| use spin::Mutex; | ||
|
|
||
| use interrupts::set_irq_handler; | ||
| use x86_util::{inb, outb}; | ||
|
|
||
| use super::TickSource; | ||
|
|
||
| /// A `TickSource` adapter for the programmable interval timer. | ||
| pub struct Pit { | ||
| data: spin::Mutex<PitData>, | ||
| } | ||
|
|
||
| struct PitData { | ||
| ticks: u64, | ||
| handler: Option<fn(u64)>, | ||
| } | ||
|
|
||
| impl TickSource for Pit { | ||
| fn approx_ticks_per_second(&self) -> u64 { | ||
| 1193182 / PIT_RELOAD_VALUE as u64 | ||
| } | ||
|
|
||
| fn set_tick_handler(&mut self, tick_handler: fn(u64)) { | ||
| unsafe { | ||
| asm!("cli"); | ||
| } | ||
|
|
||
| { | ||
| let mut data = self.data.lock(); | ||
| data.handler = Some(tick_handler); | ||
| } | ||
|
|
||
| unsafe { | ||
| asm!("sti"); | ||
| } | ||
| } | ||
|
|
||
| fn get_ticks(&self) -> u64 { | ||
| let ticks; | ||
|
|
||
| unsafe { | ||
| asm!("cli"); | ||
| } | ||
|
|
||
| { | ||
| let mut data = self.data.lock(); | ||
| ticks = data.ticks; | ||
| } | ||
|
|
||
| unsafe { | ||
| asm!("sti"); | ||
| } | ||
|
|
||
| ticks | ||
| } | ||
| } | ||
|
|
||
| static PIT: Pit = Pit { | ||
| data: spin::Mutex::new(PitData { | ||
| ticks: 0, | ||
| handler: None, | ||
| }), | ||
| }; | ||
|
|
||
| pub fn init() { | ||
| // The PIT IRQ is 0. Set our handler for that IRQ. | ||
| set_irq_handler(0, Some(timer_handler)); | ||
|
|
||
| unsafe { | ||
| // Set channel 0 to lobyte/hibyte access mode and rate | ||
| // generator operating mode. | ||
| outb(0x43, 0b00_11_010_0); | ||
| // Output our desired reload value. | ||
| outb(0x40, PIT_RELOAD_VALUE as u8); | ||
| outb(0x40, (PIT_RELOAD_VALUE >> 8) as u8); | ||
| } | ||
| } | ||
|
|
||
| fn timer_handler() { | ||
| let now_ticks; | ||
| let maybe_handler; | ||
|
|
||
| { | ||
| unsafe { | ||
| asm!("cli"); | ||
| } | ||
|
|
||
| let mut pit_data = PIT.data.lock(); | ||
| pit_data.ticks += 1; | ||
|
|
||
| now_ticks = pit_data.ticks; | ||
| maybe_handler = pit_data.handler; | ||
|
|
||
| unsafe { | ||
| asm!("sti"); | ||
| } | ||
| } | ||
|
|
||
| match maybe_handler { | ||
| Some(handler) => handler(now_ticks), | ||
| None => (), | ||
| }; | ||
| } | ||
|
|
||
| // The counter value at which we want the PIT to generate an | ||
| // interrupt. The interrupt frequency is | ||
| // 1193182 / `PIT_RELOAD_VALUE`. For an interrupt about every | ||
| // 10 microseconds, we use a value of 20. | ||
| const PIT_RELOAD_VALUE: u16 = 1193; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should probably document this number some how, either a constant or a comment