Skip to content

Commit cefa760

Browse files
committed
Add spi flash storage
1 parent 8b3a177 commit cefa760

File tree

7 files changed

+59
-8
lines changed

7 files changed

+59
-8
lines changed

src/config.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use anyhow::Debug;
21
use serde::Deserialize;
32
use std::fmt::{Debug, Formatter};
43

@@ -40,10 +39,15 @@ struct Api {
4039

4140
#[derive(Debug, Default, Deserialize)]
4241
struct Usb {
43-
#[derivative(Default(value = "true"))]
4442
high_speed: bool,
4543
}
4644

45+
impl Default for Usb {
46+
fn default() -> Self {
47+
Self { high_speed: true }
48+
}
49+
}
50+
4751
#[derive(Debug, Deserialize)]
4852
struct Config {
4953
wifi: Wifi,

src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod api;
22
mod usb;
33
mod wifi;
44
mod config;
5+
mod storage;
56

67
use crate::api::processor::{process_events, DeviceInfo, DeviceInfoProducer};
78
use crate::api::websocket::{ConnectionState, SessionEvent, WebSocketSession};

src/storage.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod spiflash;
2+
pub mod traits;

src/storage/spiflash.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use anyhow::{bail, Context};
2+
use esp_idf_svc::partition::{EspPartition, EspWlPartition};
3+
4+
#[derive(Debug, Clone)]
5+
pub struct SPIFlashConfig {
6+
pub partition_label: String,
7+
pub mount_path: String,
8+
}
9+
10+
pub struct SPIFlashStorage {
11+
config: SPIFlashConfig,
12+
wl_partition: Option<EspWlPartition<EspPartition>>,
13+
}
14+
15+
impl SPIFlashStorage {
16+
pub fn new(config: &SPIFlashConfig) -> Self {
17+
Self {
18+
config: config.clone(),
19+
wl_partition: None,
20+
}
21+
}
22+
23+
pub fn install(&mut self) -> anyhow::Result<()> {
24+
if self.wl_partition.is_some() {
25+
bail!("Already installed");
26+
}
27+
let partition = Some(
28+
unsafe { EspPartition::new(&self.config.partition_label) }?.ok_or_else(|| {
29+
anyhow::anyhow!(
30+
"Failed to find partition with label {:#?}",
31+
self.config.partition_label
32+
)
33+
})?,
34+
);
35+
self.wl_partition = Some(EspWlPartition::new(partition.unwrap()).with_context(|| {
36+
format!(
37+
"Failed to mount partition {} at {}",
38+
self.config.partition_label, self.config.mount_path
39+
)
40+
})?);
41+
log::info!(
42+
"Mount SPI Flash storage with label {} at {}",
43+
self.config.partition_label,
44+
self.config.mount_path
45+
);
46+
Ok(())
47+
}
48+
}

src/storage/traits.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub trait Storage {}

src/usb.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pub mod msc_device;
1+
pub mod msc_device;

src/usb/msc_device.rs

-5
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ impl MSCDevice {
8080
esp!(unsafe { tinyusb_msc_storage_init_spiflash(&config_spi) })
8181
.with_context(|| "Failed to initialize spiflash")?;
8282

83-
let base_path_c_str = CString::new(self.mount_path.as_bytes()).unwrap();
84-
esp!(unsafe { tinyusb_msc_storage_mount(base_path_c_str.as_ptr()) })
85-
.with_context(|| format!("Failed to mount storage at {}", self.mount_path))?;
86-
8783
let mut tusb_cfg = tinyusb_config_t::default();
8884
if self.high_speed {
8985
// TODO:
@@ -93,7 +89,6 @@ impl MSCDevice {
9389

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

96-
self.mount_path_c_str = base_path_c_str;
9792
self.wl_partition = wl_partition;
9893
Ok(())
9994
}

0 commit comments

Comments
 (0)