Skip to content

Commit

Permalink
Add ReverseOutputPin and ReverseInputPin to cope with the switched ac…
Browse files Browse the repository at this point in the history
…tive state of some boards for pwr_on pin
  • Loading branch information
tarfu committed Oct 22, 2023
1 parent 912bae7 commit 4f8fc75
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
23 changes: 16 additions & 7 deletions examples/embassy-stm32-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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;
Expand All @@ -42,7 +42,7 @@ const URC_SUBSCRIBERS: usize = 2;
struct MyCelullarConfig {
reset_pin: Option<Output<'static, AnyPin>>,
// reset_pin: Option<NoPin>,
power_pin: Option<Output<'static, AnyPin>>,
power_pin: Option<ReverseOutputPin<Output<'static, AnyPin>>>,
// power_pin: Option<NoPin>,
vint_pin: Option<Input<'static, AnyPin>>,
// vint_pin: Option<NoPin>
Expand All @@ -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<Output<'static, AnyPin>>;
// type PowerPin = NoPin;
type VintPin = Input<'static, AnyPin>;
// type VintPin = NoPin;
Expand Down Expand Up @@ -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())
};

Expand All @@ -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)));

Expand Down
38 changes: 37 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -26,6 +27,41 @@ impl OutputPin for NoPin {
}
}

pub struct ReverseOutputPin<P: OutputPin<Error = Infallible>> (pub P);

impl<P: OutputPin<Error = Infallible>> ErrorType for ReverseOutputPin<P> { type Error = Infallible; }

impl<P: OutputPin<Error = Infallible>> OutputPin for ReverseOutputPin<P> {
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<P: InputPin<Error = Infallible>> (pub P);

impl <P: InputPin<Error = Infallible>> ErrorType for ReverseInputPin<P> { type Error = Infallible; }

impl <P: InputPin<Error = Infallible>> InputPin for ReverseInputPin<P> {
fn is_high(&self) -> Result<bool, Self::Error> {
self.0.is_low()
}

fn is_low(&self) -> Result<bool, Self::Error> {
self.0.is_high()
}
}

pub trait CellularConfig {
type ResetPin: OutputPin;
type PowerPin: OutputPin;
Expand Down
6 changes: 5 additions & 1 deletion src/module_timing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
Expand Down

0 comments on commit 4f8fc75

Please sign in to comment.