Merge pull request #51 from stakwork/hold-until-sdcard

Hold execution until the sd card is mounted
This commit is contained in:
decentclock
2022-07-26 16:59:59 -06:00
committed by GitHub
4 changed files with 18 additions and 21 deletions

View File

@@ -23,6 +23,7 @@ pub enum Event {
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
pub enum Status {
Starting,
MountingSDCard,
WifiAccessPoint,
Configuring,
ConnectingToWifi,

View File

@@ -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 =

View File

@@ -20,6 +20,7 @@ pub struct Led {
fn states() -> BTreeMap<Status, (Color, Time)> {
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));

View File

@@ -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() {