From da6bda9a06f366bda1d4f5dd25d52ad8031ea278 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sat, 13 Jun 2020 16:44:54 +0800 Subject: [PATCH] add config skeleton --- src/runtime/src/config.rs | 65 +++++++++++++++++++++++++++++++++++++++ src/runtime/src/main.rs | 6 ++++ 2 files changed, 71 insertions(+) create mode 100644 src/runtime/src/config.rs diff --git a/src/runtime/src/config.rs b/src/runtime/src/config.rs new file mode 100644 index 00000000..5ae22519 --- /dev/null +++ b/src/runtime/src/config.rs @@ -0,0 +1,65 @@ +use log::info; +use core::fmt; +use alloc::{vec::Vec, string::String, string::FromUtf8Error}; +use core_io as io; + +use libboard_zynq::sdio; + +use crate::sd_reader; + +#[derive(Debug)] +pub enum Error { + SdError(sdio::sd_card::CardInitializationError), + IoError(io::Error), + Utf8Error(FromUtf8Error), +} + +pub type Result = core::result::Result; + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Error::SdError(error) => write!(f, "SD error: {:?}", error), // TODO: Display for CardInitializationError? + Error::IoError(error) => write!(f, "I/O error: {}", error), + Error::Utf8Error(error) => write!(f, "UTF-8 error: {}", error), + } + } +} + +impl From for Error { + fn from(error: sdio::sd_card::CardInitializationError) -> Self { + Error::SdError(error) + } +} + +impl From for Error { + fn from(error: io::Error) -> Self { + Error::IoError(error) + } +} + +impl From for Error { + fn from(error: FromUtf8Error) -> Self { + Error::Utf8Error(error) + } +} + +pub fn read(key: &str) -> Result> { + let sdio = sdio::SDIO::sdio0(true); + let mut sd = sdio::sd_card::SdCard::from_sdio(sdio)?; + let reader = sd_reader::SdReader::new(&mut sd); + + let fs = reader.mount_fatfs(sd_reader::PartitionEntry::Entry1)?; + let root_dir = fs.root_dir(); + for entry in root_dir.iter() { + if let Ok(entry) = entry { + let bytes = entry.short_file_name_as_bytes(); + info!("{}", core::str::from_utf8(bytes).unwrap()); + } + } + Err(sdio::sd_card::CardInitializationError::NoCardInserted)? // TODO +} + +pub fn read_str(key: &str) -> Result { + Ok(String::from_utf8(read(key)?)?) +} diff --git a/src/runtime/src/main.rs b/src/runtime/src/main.rs index a202780c..3f7f064e 100644 --- a/src/runtime/src/main.rs +++ b/src/runtime/src/main.rs @@ -12,6 +12,7 @@ use libboard_zynq::{timer::GlobalTimer, logger, devc}; use libsupport_zynq::ram; mod sd_reader; +mod config; mod proto_core_io; mod proto_async; mod comms; @@ -45,6 +46,11 @@ pub fn main_core0() { ram::init_alloc_linker(); + match config::read_str("foo") { + Ok(val) => info!("read: {}", val), + Err(error) => info!("failed to read config: {}", error), + } + let devc = devc::DevC::new(); if devc.is_done() { info!("gateware already loaded");