diff --git a/examples/embassy-stm32-example/src/main.rs b/examples/embassy-stm32-example/src/main.rs index 4e6cd77..548073e 100644 --- a/examples/embassy-stm32-example/src/main.rs +++ b/examples/embassy-stm32-example/src/main.rs @@ -7,7 +7,7 @@ use core::cell::RefCell; use cortex_m_rt::entry; use defmt::*; use embassy_executor::{Executor, Spawner}; -use embassy_stm32::gpio::{AnyPin, Input, Level, Output, Pin, Pull, Speed}; +use embassy_stm32::gpio::{AnyPin, Input, Level, Output, Pin, Pull, Speed, OutputOpenDrain}; use embassy_stm32::rcc::VoltageScale; use embassy_stm32::time::{khz, mhz}; use embassy_stm32::{bind_interrupts, peripherals, Config, interrupt, usart}; @@ -22,7 +22,7 @@ use {defmt_rtt as _, panic_probe as _}; // use embedded_hal::digital::{ErrorType, InputPin, OutputPin}; use ublox_cellular; -use ublox_cellular::config::CellularConfig; +use ublox_cellular::config::{CellularConfig, ReverseOutputPin}; use atat::{Buffers, DefaultDigester, Ingress, AtatIngress, AtDigester, Parser}; use atat::asynch::AtatClient; @@ -42,7 +42,7 @@ const URC_SUBSCRIBERS: usize = 2; struct MyCelullarConfig { reset_pin: Option>, // reset_pin: Option, - power_pin: Option>, + power_pin: Option>>, // power_pin: Option, vint_pin: Option>, // vint_pin: Option @@ -51,7 +51,7 @@ struct MyCelullarConfig { impl CellularConfig for MyCelullarConfig { type ResetPin = Output<'static, AnyPin>; // type ResetPin = NoPin; - type PowerPin = Output<'static, AnyPin>; + type PowerPin = ReverseOutputPin>; // type PowerPin = NoPin; type VintPin = Input<'static, AnyPin>; // type VintPin = NoPin; @@ -129,9 +129,11 @@ async fn main_task(spawner: Spawner) { // let power = Output::new(p.PJ4, Level::High, Speed::VeryHigh).degrade(); // let reset = Output::new(p.PF8, Level::High, Speed::VeryHigh).degrade(); let celullar_config = MyCelullarConfig { - reset_pin: Some(Output::new(p.PF8, Level::High, Speed::VeryHigh).degrade()), - // power_pin: Some(Output::new(p.PJ4, Level::High, Speed::Low).degrade()), - power_pin: None, + reset_pin: Some(Output::new(p.PF8, Level::High, Speed::Low).degrade()), + power_pin: Some(ReverseOutputPin(Output::new(p.PJ4, Level::High, Speed::Low).degrade())), + // reset_pin: Some(OutputOpenDrain::new(p.PF8, Level::High, Speed::Low, Pull::None).degrade()), + // power_pin: Some(OutputOpenDrain::new(p.PJ4, Level::High, Speed::Low, Pull::None).degrade()), + // power_pin: None, vint_pin: Some(Input::new(p.PJ3, Pull::Down).degrade()) }; @@ -151,6 +153,13 @@ async fn main_task(spawner: Spawner) { runner.init().await.unwrap(); } Timer::after(Duration::from_millis(1000)).await; + if runner.has_power().await == Ok(true){ + runner.power_down().await.unwrap(); + } + Timer::after(Duration::from_millis(5000)).await; + if runner.has_power().await == Ok(false){ + runner.power_up().await.unwrap(); + } } defmt::unwrap!(spawner.spawn(cellular_task(runner))); diff --git a/src/config.rs b/src/config.rs index 98a25f4..9ffb488 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,5 @@ -use embedded_hal::digital::{ErrorType, InputPin, OutputPin}; +use core::convert::Infallible; +use embedded_hal::digital::{ErrorType, InputPin, OutputPin, PinState}; pub struct NoPin; @@ -26,6 +27,41 @@ impl OutputPin for NoPin { } } +pub struct ReverseOutputPin> (pub P); + +impl> ErrorType for ReverseOutputPin

{ type Error = Infallible; } + +impl> OutputPin for ReverseOutputPin

{ + fn set_low(&mut self) -> Result<(), Self::Error> { + self.0.set_high() + } + + fn set_high(&mut self) -> Result<(), Self::Error> { + self.0.set_low() + } + + fn set_state(&mut self, state: PinState) -> Result<(), Self::Error> { + match state { + PinState::Low => self.0.set_state(PinState::High), + PinState::High => self.0.set_state(PinState::Low), + } + } +} + +pub struct ReverseInputPin> (pub P); + +impl > ErrorType for ReverseInputPin

{ type Error = Infallible; } + +impl > InputPin for ReverseInputPin

{ + fn is_high(&self) -> Result { + self.0.is_low() + } + + fn is_low(&self) -> Result { + self.0.is_high() + } +} + pub trait CellularConfig { type ResetPin: OutputPin; type PowerPin: OutputPin; diff --git a/src/module_timing.rs b/src/module_timing.rs index 9950b9a..58b16d4 100644 --- a/src/module_timing.rs +++ b/src/module_timing.rs @@ -6,6 +6,8 @@ use embassy_time::Duration; pub fn pwr_on_time() -> Duration { if cfg!(feature = "lara-r6") { Duration::from_millis(150) + } else if cfg!(feature = "sara-r5") { + Duration::from_millis(150) } else if cfg!(feature = "toby-r2") { Duration::from_micros(50) } else { @@ -17,9 +19,11 @@ pub fn pwr_on_time() -> Duration { pub fn pwr_off_time() -> Duration { if cfg!(feature = "lara-r6") { Duration::from_millis(1500) + } else if cfg!(feature = "sara-r5") { + Duration::from_millis(5000) } else if cfg!(feature = "toby-r2") { Duration::from_secs(1) - } else { + } else { Duration::from_secs(1) } }