Browse Source

fixed mmio thanks wes <3

master
Isabelle L. 5 years ago
parent
commit
c5046e6c13
3 changed files with 22 additions and 11 deletions
  1. +1
    -0
      justfile
  2. +7
    -5
      src/lib.rs
  3. +14
    -6
      src/mem.rs

+ 1
- 0
justfile View File

@@ -1,3 +1,4 @@

# build the OS # build the OS
build-os: build-os:
rm build/* rm build/*


+ 7
- 5
src/lib.rs View File

@@ -17,7 +17,7 @@ pub enum BoardType {


impl BoardType { impl BoardType {
/// detect the type of Raspberry Pi board /// detect the type of Raspberry Pi board
pub fn detect() -> Self {
fn detect() -> Self {
let mut reg_data: u32; let mut reg_data: u32;


unsafe { unsafe {
@@ -34,15 +34,17 @@ impl BoardType {
} }


/// return the MMIO base address /// return the MMIO base address
pub fn mmio_base_addr(self) -> *const u32 {
fn mmio_base_addr(self) -> *const u8 {
match self { match self {
BoardType::PiZeroOne | BoardType::Unknown => 0x2000_0000 as *const u32,
BoardType::PiTwo | BoardType::PiThree => 0x3F00_0000 as *const u32,
BoardType::PiFour => 0xFE00_0000 as *const u32,
BoardType::PiZeroOne | BoardType::Unknown => 0x2000_0000 as *const u8,
BoardType::PiTwo | BoardType::PiThree => 0x3F00_0000 as *const u8,
BoardType::PiFour => 0xFE00_0000 as *const u8,
} }
} }
} }


/// loops

#[no_mangle] #[no_mangle]
pub extern "C" fn kernel_main() { pub extern "C" fn kernel_main() {
let board_type = BoardType::detect(); let board_type = BoardType::detect();


+ 14
- 6
src/mem.rs View File

@@ -1,7 +1,8 @@
pub struct MemoryMappedIo { pub struct MemoryMappedIo {
base_addr: *const u32,
base_addr: *const u8,
} }


#[allow(dead_code)]
impl MemoryMappedIo { impl MemoryMappedIo {
pub fn init(board_type: crate::BoardType) -> Self { pub fn init(board_type: crate::BoardType) -> Self {
let mem = MemoryMappedIo { base_addr: board_type.mmio_base_addr() }; let mem = MemoryMappedIo { base_addr: board_type.mmio_base_addr() };
@@ -9,16 +10,19 @@ impl MemoryMappedIo {
mem mem
} }


fn uart_init(&self) {}
fn uart_init(&self) {
self.write_word(PeripheralAddress::UartCr, 0x0000_0000);
self.write_word(PeripheralAddress::Gppud, 0x0000_0000);
}


// receive fifo empty // receive fifo empty
fn recv_fifo_empty(&self) -> bool { fn recv_fifo_empty(&self) -> bool {
self.read_word(PeripheralAddress::UartFr) & (1 << 5) > 0
self.read_word(PeripheralAddress::UartFr) & (1 << 4) > 0
} }


// transmit fifo full // transmit fifo full
fn tran_fifo_full(&self) -> bool { fn tran_fifo_full(&self) -> bool {
self.read_word(PeripheralAddress::UartFr) & (1 << 4) > 0
self.read_word(PeripheralAddress::UartFr) & (1 << 5) > 0
} }


pub fn write_str(&self, s: &str) { pub fn write_str(&self, s: &str) {
@@ -26,12 +30,16 @@ impl MemoryMappedIo {
} }


pub fn write_byte(&self, byte: u8) { pub fn write_byte(&self, byte: u8) {
while self.tran_fifo_full() {}
while self.tran_fifo_full() {
unsafe { asm!("nop") }
}
self.write_word(PeripheralAddress::UartDr, byte as u32); self.write_word(PeripheralAddress::UartDr, byte as u32);
} }


pub fn get_byte(&self) -> u8 { pub fn get_byte(&self) -> u8 {
while self.recv_fifo_empty() {}
while self.recv_fifo_empty() {
unsafe { asm!("nop") }
}
self.read_word(PeripheralAddress::UartDr) as u8 self.read_word(PeripheralAddress::UartDr) as u8
} }




Loading…
Cancel
Save