Skip to content

Commit

Permalink
Add spi flash storage
Browse files Browse the repository at this point in the history
  • Loading branch information
fangpenlin committed Jan 7, 2025
1 parent 8b3a177 commit cefa760
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use anyhow::Debug;
use serde::Deserialize;
use std::fmt::{Debug, Formatter};

Expand Down Expand Up @@ -40,10 +39,15 @@ struct Api {

#[derive(Debug, Default, Deserialize)]
struct Usb {
#[derivative(Default(value = "true"))]
high_speed: bool,
}

impl Default for Usb {
fn default() -> Self {
Self { high_speed: true }
}
}

#[derive(Debug, Deserialize)]
struct Config {
wifi: Wifi,
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod api;
mod usb;
mod wifi;
mod config;
mod storage;

use crate::api::processor::{process_events, DeviceInfo, DeviceInfoProducer};
use crate::api::websocket::{ConnectionState, SessionEvent, WebSocketSession};
Expand Down
2 changes: 2 additions & 0 deletions src/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod spiflash;
pub mod traits;
48 changes: 48 additions & 0 deletions src/storage/spiflash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use anyhow::{bail, Context};
use esp_idf_svc::partition::{EspPartition, EspWlPartition};

#[derive(Debug, Clone)]
pub struct SPIFlashConfig {
pub partition_label: String,
pub mount_path: String,
}

pub struct SPIFlashStorage {
config: SPIFlashConfig,
wl_partition: Option<EspWlPartition<EspPartition>>,
}

impl SPIFlashStorage {
pub fn new(config: &SPIFlashConfig) -> Self {
Self {
config: config.clone(),
wl_partition: None,
}
}

pub fn install(&mut self) -> anyhow::Result<()> {
if self.wl_partition.is_some() {
bail!("Already installed");
}
let partition = Some(
unsafe { EspPartition::new(&self.config.partition_label) }?.ok_or_else(|| {
anyhow::anyhow!(
"Failed to find partition with label {:#?}",
self.config.partition_label
)
})?,
);
self.wl_partition = Some(EspWlPartition::new(partition.unwrap()).with_context(|| {
format!(
"Failed to mount partition {} at {}",
self.config.partition_label, self.config.mount_path
)
})?);
log::info!(
"Mount SPI Flash storage with label {} at {}",
self.config.partition_label,
self.config.mount_path
);
Ok(())
}
}
1 change: 1 addition & 0 deletions src/storage/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub trait Storage {}
2 changes: 1 addition & 1 deletion src/usb.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod msc_device;
pub mod msc_device;
5 changes: 0 additions & 5 deletions src/usb/msc_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ impl MSCDevice {
esp!(unsafe { tinyusb_msc_storage_init_spiflash(&config_spi) })
.with_context(|| "Failed to initialize spiflash")?;

let base_path_c_str = CString::new(self.mount_path.as_bytes()).unwrap();
esp!(unsafe { tinyusb_msc_storage_mount(base_path_c_str.as_ptr()) })
.with_context(|| format!("Failed to mount storage at {}", self.mount_path))?;

let mut tusb_cfg = tinyusb_config_t::default();
if self.high_speed {
// TODO:
Expand All @@ -93,7 +89,6 @@ impl MSCDevice {

log::info!("TinyUSB driver installed.");

self.mount_path_c_str = base_path_c_str;
self.wl_partition = wl_partition;
Ok(())
}
Expand Down

0 comments on commit cefa760

Please sign in to comment.