keyboard use Pio

This commit is contained in:
wescande 2018-04-12 19:37:43 +02:00
parent ffcb85dd5b
commit cdf09f9869
3 changed files with 57 additions and 23 deletions

View file

@ -2,6 +2,7 @@ extern crate core;
// extern crate multiboot2; // extern crate multiboot2;
use acpi; use acpi;
use keyboard::PS2;
use cpuio; use cpuio;
use core::char; use core::char;
use vga::*; use vga::*;
@ -59,15 +60,10 @@ fn help() -> Result<(), &'static str> {
/// If reboot failed, will loop on a halt cmd /// If reboot failed, will loop on a halt cmd
/// ///
fn reboot() -> ! { fn reboot() -> ! {
unsafe { asm!("cli") }; //TODO volatile ????? // acpi::reboot()?;
// I will now clear the keyboard buffer // println!("Unable to perform ACPI reboot.");
let mut buffer: u8 = 0x02; unsafe {PS2.ps2_8042_reset()};// TODO unsafe
while buffer & 0x02 != 0 { println!("Unable to perform 8042 reboot. Kernel will be halted");
cpuio::inb(0x60);
buffer = cpuio::inb(0x64);
}
cpuio::outb(0x64, 0xFE); //Send reset value to CPU //TODO doesn't work in QEMU ==> it seems that qemu cannot reboot
println!("Unable to perform reboot. Kernel will be halted");
cpuio::halt(); cpuio::halt();
} }

View file

@ -38,9 +38,14 @@ pub fn outl(port: u16, value: u32) {
unsafe { asm!("outl %eax, %dx" :: "{dx}"(port), "{eax}"(value) :: "volatile") }; unsafe { asm!("outl %eax, %dx" :: "{dx}"(port), "{eax}"(value) :: "volatile") };
} }
/// Disable interruption
pub fn cli() {
unsafe { asm!("cli" : : : : "volatile") };
}
/// Halt system /// Halt system
pub fn halt() -> ! { pub fn halt() -> ! {
unsafe { asm!("cli" : : : : "volatile") }; cli();
loop { loop {
unsafe { asm!("hlt" : : : : "volatile") }; unsafe { asm!("hlt" : : : : "volatile") };
} }

View file

@ -2,6 +2,8 @@ extern crate core;
use cpuio; use cpuio;
use vga; use vga;
use io::Pio;
use io::Io;
const MAX_KEYS: usize = 59; const MAX_KEYS: usize = 59;
const KEYMAP_US: [[u8; 2]; MAX_KEYS] = [ const KEYMAP_US: [[u8; 2]; MAX_KEYS] = [
@ -66,6 +68,49 @@ const KEYMAP_US: [[u8; 2]; MAX_KEYS] = [
*b"\0\0",//capslock *b"\0\0",//capslock
]; ];
pub static mut PS2: Ps2 = Ps2::new();
pub struct Ps2 {
status: Pio<u8>,
data: Pio<u8>,
}
impl Ps2 {
pub const fn new() -> Ps2 {
Ps2 {
status: Pio::new(0x64),
data: Pio::new(0x60),
}
}
pub fn clear_buffer(&self) {
let mut buffer: u8 = 0x02;
while buffer & 0x02 != 0 {
self.data.read();
buffer = self.status.read();
}
}
pub fn ps2_8042_reset(&mut self) {
cpuio::cli();
self.clear_buffer();
self.status.write(0xFE);
}
pub fn get_scancode(&self) -> u8 {
let mut scancode = 0;
loop {
if self.data.read() != scancode {
scancode = self.data.read();
if scancode > 0 {
return scancode;
}
}
}
}
}
const TOUCH_RELEASE: u8 = 1 << 7; const TOUCH_RELEASE: u8 = 1 << 7;
fn check_key_state(key: u8) -> (bool, usize) { fn check_key_state(key: u8) -> (bool, usize) {
@ -76,23 +121,11 @@ fn check_key_state(key: u8) -> (bool, usize) {
} }
} }
fn get_scancode() -> u8 {
let mut scancode = 0;
loop {
if cpuio::inb(0x60) != scancode {
scancode = cpuio::inb(0x60);
if scancode > 0 {
return scancode;
}
}
}
}
pub fn kbd_callback() { pub fn kbd_callback() {
static mut SHIFT: bool = false; static mut SHIFT: bool = false;
static mut CTRL: bool = false; static mut CTRL: bool = false;
static mut ALT: bool = false; static mut ALT: bool = false;
let scancode = get_scancode(); let scancode = unsafe { PS2.get_scancode() };
let (is_release, scancode) = check_key_state(scancode); let (is_release, scancode) = check_key_state(scancode);
unsafe { unsafe {
//TODO remove unsafe //TODO remove unsafe