diff --git a/sphinx-key/src/core/events.rs b/sphinx-key/src/core/events.rs index cbc9dcf..624eabc 100644 --- a/sphinx-key/src/core/events.rs +++ b/sphinx-key/src/core/events.rs @@ -23,6 +23,7 @@ pub enum Event { #[derive(Debug, Ord, PartialOrd, Eq, PartialEq)] pub enum Status { Starting, + MountingSDCard, WifiAccessPoint, Configuring, ConnectingToWifi, diff --git a/sphinx-key/src/main.rs b/sphinx-key/src/main.rs index c2338f5..f168153 100644 --- a/sphinx-key/src/main.rs +++ b/sphinx-key/src/main.rs @@ -5,7 +5,7 @@ mod periph; use crate::core::{config::*, events::*}; use crate::periph::led::led_control_loop; -use crate::periph::sd::{sd_card, simple_fs_test}; +use crate::periph::sd::{mount_sd_card, simple_fs_test}; use anyhow::Result; use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported @@ -42,9 +42,13 @@ fn main() -> Result<()> { // LED control thread led_control_loop(pins.gpio8, peripherals.rmt.channel0, led_rx); - // sd card - sd_card(); - // simple_fs_test(); + led_tx.send(Status::MountingSDCard).unwrap(); + println!("About to mount the sdcard..."); + while let Err(_e) = mount_sd_card() { + println!("Failed to mount sd card. Make sure it is connected, trying again..."); + thread::sleep(Duration::from_secs(5)); + } + println!("SD card mounted!"); let default_nvs = Arc::new(EspDefaultNvs::new()?); let mut store = diff --git a/sphinx-key/src/periph/led.rs b/sphinx-key/src/periph/led.rs index 8b25c00..1d0a2eb 100644 --- a/sphinx-key/src/periph/led.rs +++ b/sphinx-key/src/periph/led.rs @@ -20,6 +20,7 @@ pub struct Led { fn states() -> BTreeMap { let mut s = BTreeMap::new(); s.insert(Status::Starting, (0x000001, 100)); + s.insert(Status::MountingSDCard, (0x000102, 100)); s.insert(Status::WifiAccessPoint, (0x000100, 100)); s.insert(Status::Configuring, (0x010000, 20)); s.insert(Status::ConnectingToWifi, (0x010100, 350)); diff --git a/sphinx-key/src/periph/sd.rs b/sphinx-key/src/periph/sd.rs index f87df42..91b0aad 100644 --- a/sphinx-key/src/periph/sd.rs +++ b/sphinx-key/src/periph/sd.rs @@ -52,7 +52,7 @@ enum SDMMCFreq { _26M = 26000, } -pub fn sd_card() { +pub fn mount_sd_card() -> anyhow::Result<()> { let mount_config = esp_vfs_fat_sdmmc_mount_config_t { format_if_mount_failed: false, max_files: 5, @@ -80,18 +80,15 @@ pub fn sd_card() { intr_flags: 0, }; - let res = esp!(unsafe { + if let Err(error) = esp!(unsafe { spi_bus_initialize( SPI_HOST_SLOT as u32, &bus_cfg, esp_idf_sys::spi_common_dma_t_SPI_DMA_CH_AUTO, ) - }); - - match res { - Ok(_) => (), - Err(e) => { - println!("Failed to initialize SPI Bus: {}", e); + }) { + if error.code() != 259 { + return Err(anyhow::Error::new(error)); } } @@ -124,7 +121,7 @@ pub fn sd_card() { command_timeout_ms: 0, }; - let res = esp!(unsafe { + esp!(unsafe { esp_vfs_fat_sdspi_mount( C_MOUNT_POINT.as_ptr() as *const c_char, &host, @@ -132,14 +129,8 @@ pub fn sd_card() { &mount_config, &mut card as *mut *mut sdmmc_card_t, ) - }); - - match res { - Ok(_) => (), - Err(e) => { - println!("Failed to mount filesystem: {}", e); - } - } + })?; + Ok(()) } pub fn simple_fs_test() {