Bläddra i källkod

Todo: compile in k210

luojia65 4 år sedan
förälder
incheckning
a8857d4a26
2 ändrade filer med 78 tillägg och 1 borttagningar
  1. 3 0
      platform/k210/Cargo.toml
  2. 75 1
      platform/k210/src/main.rs

+ 3 - 0
platform/k210/Cargo.toml

@@ -8,3 +8,6 @@ edition = "2018"
 
 [dependencies]
 rustsbi = { path = "../../rustsbi" }
+riscv = "0.6"
+linked_list_allocator = "0.8"
+k210-hal = { git = "https://github.com/riscv-rust/k210-hal" }

+ 75 - 1
platform/k210/src/main.rs

@@ -1,11 +1,85 @@
 #![no_std]
 #![no_main]
+#![feature(alloc_error_handler)]
+#![feature(global_asm)]
 
+use core::alloc::Layout;
 use core::panic::PanicInfo;
+use k210_hal::{clock::Clocks, fpioa, pac, prelude::*};
+use linked_list_allocator::LockedHeap;
+use rustsbi::{enter_privileged, println};
+use riscv::register::{
+    mepc, mhartid,
+    mstatus::{self, MPP},
+};
+
+#[global_allocator]
+static ALLOCATOR: LockedHeap = LockedHeap::empty();
 
 #[panic_handler]
 fn panic(_info: &PanicInfo) -> ! {
     loop {}
 }
 
-// todo
+#[alloc_error_handler]
+fn oom(_layout: Layout) -> ! {
+    loop {}
+}
+
+
+#[export_name = "start"]
+fn main() -> ! {
+    if mhartid::read() == 0 {
+        extern "C" {
+            fn _sheap();
+            fn _heap_size();
+        }
+        let sheap = &mut _sheap as *mut _ as usize;
+        let heap_size = &_heap_size as *const _ as usize;
+        unsafe {
+            ALLOCATOR.lock().init(sheap, heap_size);
+        }
+
+        let p = pac::Peripherals::take().unwrap();
+
+        let mut sysctl = p.SYSCTL.constrain();
+        let fpioa = p.FPIOA.split(&mut sysctl.apb0);
+        let clocks = Clocks::new();
+        let _uarths_tx = fpioa.io5.into_function(fpioa::UARTHS_TX);
+        let _uarths_rx = fpioa.io4.into_function(fpioa::UARTHS_RX);
+        // Configure UART
+        let serial = p.UARTHS.configure(115_200.bps(), &clocks);
+        let (tx, rx) = serial.split();
+        use rustsbi::legacy_stdio::init_legacy_stdio_embedded_hal_fuse;
+        init_legacy_stdio_embedded_hal_fuse(tx, rx);
+
+        println!("[rustsbi] Version 0.1.0");
+
+        println!("{}", rustsbi::LOGO);
+        println!("[rustsbi] Target device: K210");
+        println!("[rustsbi] Kernel entry: 0x80200000");
+    }
+    extern "C" {
+        fn _s_mode_start();
+    }
+    unsafe {
+        mepc::write(_s_mode_start as usize);
+        mstatus::set_mpp(MPP::Supervisor);
+        enter_privileged(mhartid::read(), 0x2333333366666666);
+    }
+}
+
+global_asm!(
+    "
+    .section .text
+    .globl _s_mode_start
+_s_mode_start:
+1:  auipc ra, %pcrel_hi(1f)
+    ld ra, %pcrel_lo(1b)(ra)
+    jr ra
+.align  3
+1:  .dword _start_payload /* defined by firmware-k210 */
+"
+);
+
+// todo: configurable target address