diff --git a/sphinx-key/src/btn.rs b/sphinx-key/src/btn.rs index 317db2a..c50e121 100644 --- a/sphinx-key/src/btn.rs +++ b/sphinx-key/src/btn.rs @@ -5,7 +5,9 @@ mod periph; mod status; pub use crate::core::control::FlashPersister; +use esp_idf_hal::gpio::Gpio0; use esp_idf_hal::gpio::Gpio9; +use esp_idf_hal::peripheral::Peripheral; use esp_idf_hal::peripherals::Peripherals; use esp_idf_svc::nvs::EspDefaultNvsPartition; use esp_idf_svc::nvs::EspNvs; @@ -22,17 +24,21 @@ const ID_LEN: usize = 12; fn main() -> anyhow::Result<()> { esp_idf_sys::link_patches(); - esp_idf_svc::log::EspLogger::initialize_default(); - thread::sleep(Duration::from_secs(1)); + let mut peripherals = Peripherals::take().unwrap(); - let peripherals = Peripherals::take().unwrap(); - let pins = peripherals.pins; - - let (led_tx, led_rx) = mpsc::channel::(); // LED control thread - periph::led::led_control_loop(pins.gpio0, peripherals.rmt.channel0, led_rx); + let (mut led_tx, mut led_rx) = mpsc::channel::(); + while let Err(e) = periph::led::led_control_loop( + unsafe { Gpio0::new() }, + unsafe { peripherals.rmt.channel0.clone_unchecked() }, + led_rx, + ) { + log::error!("unable to spawn led thread: {:?}", e); + thread::sleep(Duration::from_millis(1000)); + (led_tx, led_rx) = mpsc::channel::(); + } // BUTTON thread let default_nvs = EspDefaultNvsPartition::take()?; diff --git a/sphinx-key/src/main.rs b/sphinx-key/src/main.rs index bd7f3c0..1152222 100644 --- a/sphinx-key/src/main.rs +++ b/sphinx-key/src/main.rs @@ -14,7 +14,8 @@ use crate::periph::sd::{mount_sd_card, simple_fs_test}; use crate::status::Status; use anyhow::Result; -use esp_idf_hal::gpio::Gpio9; +use esp_idf_hal::gpio::{Gpio0, Gpio9}; +use esp_idf_hal::peripheral::Peripheral; use esp_idf_hal::peripherals::Peripherals; use esp_idf_svc::nvs::EspDefaultNvsPartition; use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported @@ -31,17 +32,21 @@ fn main() -> Result<()> { // Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once, // or else some patches to the runtime implemented by esp-idf-sys might not link properly. esp_idf_sys::link_patches(); - esp_idf_svc::log::EspLogger::initialize_default(); - thread::sleep(Duration::from_secs(1)); + let mut peripherals = Peripherals::take().unwrap(); - let peripherals = Peripherals::take().unwrap(); - let pins = peripherals.pins; - - let (led_tx, led_rx) = mpsc::channel(); // LED control thread - led_control_loop(pins.gpio0, peripherals.rmt.channel0, led_rx); + let (mut led_tx, mut led_rx) = mpsc::channel::(); + while let Err(e) = led_control_loop( + unsafe { Gpio0::new() }, + unsafe { peripherals.rmt.channel0.clone_unchecked() }, + led_rx, + ) { + log::error!("unable to spawn led thread: {:?}", e); + thread::sleep(Duration::from_millis(1000)); + (led_tx, led_rx) = mpsc::channel::(); + } led_tx.send(Status::MountingSDCard).unwrap(); println!("About to mount the sdcard..."); @@ -57,9 +62,7 @@ fn main() -> Result<()> { let flash_per = FlashPersister::new(default_nvs.clone()); let flash_arc = Arc::new(Mutex::new(flash_per)); // BUTTON thread - while let Err(e) = - button_loop(unsafe { Gpio9::new() }, led_tx.clone(), flash_arc.clone()) - { + while let Err(e) = button_loop(unsafe { Gpio9::new() }, led_tx.clone(), flash_arc.clone()) { log::error!("unable to spawn button thread: {:?}", e); thread::sleep(Duration::from_millis(1000)); } diff --git a/sphinx-key/src/periph/led.rs b/sphinx-key/src/periph/led.rs index bc76d2f..c0b3fe1 100644 --- a/sphinx-key/src/periph/led.rs +++ b/sphinx-key/src/periph/led.rs @@ -1,4 +1,5 @@ use crate::status::Status; +use anyhow::Result; use esp_idf_hal::rmt::config::TransmitConfig; use esp_idf_hal::rmt::{FixedLengthSignal, PinState, Pulse, TxRmtDriver}; use esp_idf_hal::{gpio, rmt}; @@ -37,12 +38,17 @@ fn states() -> BTreeMap { s } -pub fn led_control_loop(gpio0: gpio::Gpio0, channel0: rmt::CHANNEL0, rx: mpsc::Receiver) { +pub fn led_control_loop( + gpio0: gpio::Gpio0, + channel0: rmt::CHANNEL0, + rx: mpsc::Receiver, +) -> Result<()> { let config = TransmitConfig::new().clock_divider(1); let transmit = Arc::new(Mutex::new( TxRmtDriver::new(channel0, gpio0, &config).unwrap(), )); - thread::spawn(move || { + let builder = thread::Builder::new().stack_size(1500); + builder.spawn(move || { let mut led = Led::new(0x000001, 100); let states = states(); loop { @@ -55,7 +61,8 @@ pub fn led_control_loop(gpio0: gpio::Gpio0, channel0: rmt::CHANNEL0, rx: mpsc::R led.blink(transmit.clone()); thread::sleep(Duration::from_millis(400)); } - }); + })?; + Ok(()) } impl Led {