Skip to content

Commit

Permalink
fix: Update the sd card driver of the vf2 board
Browse files Browse the repository at this point in the history
  • Loading branch information
Godones committed Jul 13, 2024
1 parent d4ec9cc commit a5b04fc
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 79 deletions.
40 changes: 20 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 2 additions & 36 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,8 @@ resolver = "2"
members = [
"kernel",
"user/userlib",
"subsystems/gmanager",
"user/apps/slint-helper",
"user/apps/cat",
"user/apps/sleep",
"user/apps/todo",
"user/apps/printdemo",
"user/apps/egui",
"user/apps/sysinfo",
"user/apps/init",
"user/apps/ls",
"user/apps/mkdir",
"user/apps/pwd",
"user/apps/shell",
"user/apps/touch",
"user/apps/run_test",
"user/apps/slint",
"user/apps/socket_test",
"user/apps/tests",
"user/apps/memory-game",
"user/apps/print",
"user/apps/final_test",
"subsystems/arch",
"subsystems/config",
"subsystems/ksync",
"subsystems/constants",
"subsystems/mem",
"subsystems/vfs",
"subsystems/devices",
"subsystems/drivers",
"subsystems/interrupt",
"subsystems/device_interface",
"subsystems/timer",
"kernel",
"subsystems/unwinder",
"subsystems/knet",
"subsystems/shim",
"subsystems/*",
"user/apps/*",
]


Expand Down
3 changes: 1 addition & 2 deletions kernel/src/fs/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub fn utimensat(
TimeSpec::now().into(),
)?;
dt.inode()?.update_time(
VfsTime::AccessTime(TimeSpec::now().into()),
VfsTime::ModifiedTime(TimeSpec::now().into()),
TimeSpec::now().into(),
)?;
} else {
Expand Down Expand Up @@ -184,7 +184,6 @@ pub fn utimensat(
.update_time(VfsTime::ModifiedTime(mtime.into()), TimeSpec::now().into())?;
};
};

Ok(0)
}

Expand Down
12 changes: 2 additions & 10 deletions subsystems/devices/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,8 @@ fn init_block_device(blk: prob::DeviceInfo, mmio_transport: Option<MmioTransport
// starfive2
#[cfg(not(feature = "ramdisk"))]
{
use arch::read_timer;
use platform::config::CLOCK_FREQ;
pub fn sleep(ms: usize) {
let start = read_timer();
while read_timer() - start < ms * (CLOCK_FREQ / 1000) {
core::hint::spin_loop();
}
}
use drivers::block_device::{VF2SDDriver, Vf2SdDriver};
let block_device = VF2SDDriver::new(Vf2SdDriver::new(sleep));
use drivers::block_device::VF2SDDriver;
let block_device = VF2SDDriver::new();
let size = block_device.capacity();
println!("Block device size is {}MB", size * 512 / 1024 / 1024);
let block_device = Arc::new(GenericBlockDevice::new(Box::new(block_device)));
Expand Down
68 changes: 60 additions & 8 deletions subsystems/drivers/src/block_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ use device_interface::{BlockDevice, DeviceBase, LowBlockDevice};
use ksync::{Mutex, RwLock};
use lru::LruCache;
use mem::{alloc_frames, free_frames};
use platform::config::BLOCK_CACHE_FRAMES;
use platform::config::{BLOCK_CACHE_FRAMES, CLOCK_FREQ};
use shim::KTask;
use timer::read_timer;
use virtio_drivers::{
device::blk::VirtIOBlk,
transport::mmio::{MmioTransport, VirtIOHeader},
};
pub use visionfive2_sd::Vf2SdDriver;
use visionfive2_sd::{SDIo, SleepOps};

use crate::hal::HalImpl;
const PAGE_CACHE_SIZE: usize = FRAME_SIZE;
Expand Down Expand Up @@ -367,28 +369,28 @@ impl MemoryFat32Img {
}

pub struct VF2SDDriver {
driver: Vf2SdDriver,
driver: Mutex<Vf2SdDriver<SdIoImpl, SleepOpsImpl>>,
}

impl VF2SDDriver {
pub fn new(vf2sd_driver: Vf2SdDriver) -> Self {
pub fn new() -> Self {
Self {
driver: vf2sd_driver,
driver: Mutex::new(Vf2SdDriver::new(SdIoImpl)),
}
}
pub fn init(&self) {
self.driver.init();
pub fn init(&mut self) {
self.driver.lock().init();
}
}

impl LowBlockDevice for VF2SDDriver {
fn read_block(&self, block_id: usize, buf: &mut [u8]) -> AlienResult<()> {
self.driver.read_block(block_id, buf);
self.driver.lock().read_block(block_id, buf);
Ok(())
}

fn write_block(&self, block_id: usize, buf: &[u8]) -> AlienResult<()> {
self.driver.write_block(block_id, buf);
self.driver.lock().write_block(block_id, buf);
Ok(())
}
fn capacity(&self) -> usize {
Expand All @@ -407,3 +409,53 @@ impl LowBlockDevice for VF2SDDriver {
unimplemented!()
}
}

pub struct SdIoImpl;
pub const SDIO_BASE: usize = 0x16020000;

impl SDIo for SdIoImpl {
fn read_reg_at(&self, offset: usize) -> u32 {
let addr = (SDIO_BASE + offset) as *mut u32;
unsafe { addr.read_volatile() }
}
fn write_reg_at(&mut self, offset: usize, val: u32) {
let addr = (SDIO_BASE + offset) as *mut u32;
unsafe { addr.write_volatile(val) }
}
fn read_data_at(&self, offset: usize) -> u64 {
let addr = (SDIO_BASE + offset) as *mut u64;
unsafe { addr.read_volatile() }
}
fn write_data_at(&mut self, offset: usize, val: u64) {
let addr = (SDIO_BASE + offset) as *mut u64;
unsafe { addr.write_volatile(val) }
}
}

pub struct SleepOpsImpl;

impl SleepOps for SleepOpsImpl {
fn sleep_ms(ms: usize) {
sleep_ms(ms)
}
fn sleep_ms_until(ms: usize, f: impl FnMut() -> bool) {
sleep_ms_until(ms, f)
}
}

fn sleep_ms(ms: usize) {
let start = read_timer();
while read_timer() - start < ms * CLOCK_FREQ / 1000 {
core::hint::spin_loop();
}
}

fn sleep_ms_until(ms: usize, mut f: impl FnMut() -> bool) {
let start = read_timer();
while read_timer() - start < ms * CLOCK_FREQ / 1000 {
if f() {
return;
}
core::hint::spin_loop();
}
}
File renamed without changes.
4 changes: 1 addition & 3 deletions user/apps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ BUILD_CRATES := \
sleep \
socket_test \
final_test \
ls \
cat \
mkdir \
ftest \
# shell \
print \

Expand Down
7 changes: 7 additions & 0 deletions user/apps/ftest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "ftest"
version = "0.1.0"
edition = "2021"

[dependencies]
Mstd = {path = "../../userlib" }
42 changes: 42 additions & 0 deletions user/apps/ftest/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#![no_std]
#![no_main]
use Mstd::{
fs::{close, open, read, OpenFlags},
println,
time::get_time_ms,
};

#[no_mangle]
fn main() -> isize {
read_bash_test();
// in cache
read_bash_test();
0
}

fn read_bash_test() -> isize {
let bash_file_test = open("/tests/bash\0", OpenFlags::O_RDONLY);
if bash_file_test < 0 {
println!("Failed to open /tests/bash");
return -1;
}
let now = get_time_ms();
let mut buf = [0u8; 100];
let mut bytes = 0;
loop {
let res = read(bash_file_test as usize, &mut buf);
if res == 0 {
break;
}
bytes += res;
}
let new = get_time_ms();
let time = new - now;
let speed = bytes as f64 / time as f64 * 1000.0 / 1024.0;
println!(
"Read {} bytes in {}ms, speed: {} KB/s",
bytes, time, speed as isize
);
close(bash_file_test as _);
0
}

0 comments on commit a5b04fc

Please sign in to comment.