Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce desired State for runner #94

Merged
merged 16 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,47 @@ doctest = false

[dependencies]
atat = { version = "0.20.0", features = ["derive", "bytes"] }
embedded-hal = "=1.0.0-rc.1"
embedded-nal = "0.7"
hash32 = "^0.2.1"
hash32-derive = "^0.1.0"
heapless = { version = "^0.7", features = ["serde"] }
heapless = { version = "^0.8", features = ["serde"] }
nb = "^1"
serde = { version = "^1", default-features = false, features = ["derive"] }
ublox-sockets = { version = "0.5", optional = true }
embassy-time = "0.1.5"
embassy-sync = "0.3"
#ublox-sockets = { version = "0.5", optional = true }
ublox-sockets = { git = "https://github.com/BlackbirdHQ/ublox-sockets", branch = "feature/async-borrowed-sockets", optional = true }
embassy-time = "0.2"
embassy-sync = "0.5"

no-std-net = { version = "^0.6", features = ["serde"] }

log = { version = "^0.4", default-features = false, optional = true }
defmt = { version = "^0.3", optional = true }

embedded-nal-async = { version = "0.6", optional = true }
futures = { version = "0.3.17", default-features = false, features = [
"async-await",
] }
futures-util = { version = "0.3.29", default-features = false }
#futures = { version = "0.3.17", default-features = false, features = [
# "async-await",
#] }

embassy-futures = { version = "0.1", optional = true }

embedded-hal = "=1.0.0-rc.3"
embedded-nal = "0.8"
embedded-nal-async = { version = "0.7", optional = true }


embedded-io = "0.6"
embedded-io-async = "0.6"

[features]
default = ["socket-udp", "socket-tcp", "async", "ublox-sockets"]

async = ["dep:embedded-nal-async", "atat/async", "ublox-sockets?/async"]
async = ["dep:embedded-nal-async", "dep:embassy-futures", "atat/async", "ublox-sockets?/async"]

defmt = [
"dep:defmt",
"ublox-sockets?/defmt",
"atat/defmt",
"heapless/defmt-impl",
"heapless/defmt-03",
"embassy-time/defmt",
"embassy-sync/defmt",
"embassy-futures?/defmt",
]

log = ["dep:log", "ublox-sockets?/log", "atat/log"]
Expand Down Expand Up @@ -80,5 +86,14 @@ default-members = ["."]
exclude = ["examples"]

[patch.crates-io]
ublox-sockets = { path = "../ublox-sockets" }
#ublox-sockets = { git = "https://github.com/BlackbirdHQ/ublox-sockets", branch = "feature/async-borrowed-sockets" }
no-std-net = { git = "https://github.com/rushmorem/no-std-net", branch = "issue-15" }
ublox-sockets = { path = "../ublox-sockets" }
atat = { git = "https://github.com/BlackbirdHQ/atat", branch = "master" }
embassy-time = { git = "https://github.com/embassy-rs/embassy", branch = "main" }
embassy-sync = { git = "https://github.com/embassy-rs/embassy", branch = "main" }
embassy-futures = { git = "https://github.com/embassy-rs/embassy", branch = "main" }

#embassy-time = { path = "../embassy/embassy-time" }
#embassy-sync = { path = "../embassy/embassy-sync" }
#embassy-futures = { path = "../embassy/embassy-futures" }
29 changes: 21 additions & 8 deletions examples/embassy-stm32-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ edition = "2021"

# Example for the STM32H747I-DISCO board with SARA-R5 modem attached to UART8
[dependencies]
embassy-stm32 = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy.git", branch = "main", features = ["nightly", "defmt", "stm32h747xi-cm7", "time-driver-any", "exti", "memory-x", "unstable-pac", "unstable-traits"] }
embassy-executor = { version = "0.3.0", features = ["nightly", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] }
embassy-time = { version = "0.1", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "nightly"] }
embassy-sync = { version = "0.3.0", features = ["defmt"] }
embassy-stm32 = { version = "0.1.0", features = ["defmt", "stm32h747xi-cm7", "time-driver-any", "exti", "memory-x", "unstable-pac"] }
embassy-executor = { version = "0.4.0", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] }
embassy-time = { version = "0.2", features = ["defmt", "defmt-timestamp-uptime"] }
embassy-sync = { version = "0.5", features = ["defmt"] }


cortex-m = { version = "0.7.7", features = ["inline-asm", "critical-section-single-core"] }
Expand All @@ -18,16 +18,29 @@ defmt = "0.3.5"
defmt-rtt = "0.4.0"
panic-probe = { version = "0.3.1", features = ["print-defmt"] }

static_cell = { version = "1.2.0", features = ["nightly"]}
static_cell = { version = "2.0", features = []}

atat = { version = "0.20.0", features = ["derive", "bytes", "defmt"] }
ublox-cellular-rs = {version = "0.4.0", path = "../..", features = ["sara-r5", "defmt", "async"]}

[patch.crates-io]
ublox-sockets = { path = "../../../ublox-sockets" }
ublox-sockets = { git = "https://github.com/BlackbirdHQ/ublox-sockets", branch = "feature/async-borrowed-sockets" }
atat = { git = "https://github.com/BlackbirdHQ/atat", branch = "master" }
no-std-net = { git = "https://github.com/rushmorem/no-std-net", branch = "issue-15" }
embassy-time = { package = "embassy-time", git = "https://github.com/embassy-rs/embassy", branch = "main" }

embassy-stm32 = { git = "https://github.com/embassy-rs/embassy", branch = "main" }
embassy-time = { git = "https://github.com/embassy-rs/embassy", branch = "main" }
embassy-sync = { git = "https://github.com/embassy-rs/embassy", branch = "main" }
embassy-futures = { git = "https://github.com/embassy-rs/embassy", branch = "main" }
embassy-executor = { git = "https://github.com/embassy-rs/embassy", branch = "main" }

#embassy-stm32 = { path = "../../../embassy/embassy-stm32" }
#embassy-time = { path = "../../../embassy/embassy-time" }
#embassy-sync = { path = "../../../embassy/embassy-sync" }
#embassy-futures = { path = "../../../embassy/embassy-futures" }
#embassy-executor = { path = "../../../embassy/embassy-executor" }

#ublox-sockets = { path = "../../../ublox-sockets" }
#atat = { path = "../../../atat/atat" }

[profile.dev]
opt-level = "s"
Expand Down
4 changes: 2 additions & 2 deletions examples/embassy-stm32-example/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Before upgrading check that everything is available on all tier1 targets here:
# https://rust-lang.github.io/rustup-components-history
[toolchain]
channel = "nightly-2023-10-02"
components = [ "rust-src", "rustfmt", "llvm-tools-preview" ]
channel = "1.75"
components = [ "rust-src", "rustfmt", "llvm-tools" ]
targets = [
"thumbv7em-none-eabi",
"thumbv7m-none-eabi",
Expand Down
127 changes: 86 additions & 41 deletions examples/embassy-stm32-example/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
#![no_std]
#![no_main]
#![allow(stable_features)]
#![feature(type_alias_impl_trait)]
// #![feature(type_alias_impl_trait)]

use atat::asynch::Client;
use atat::ResponseSlot;
use atat::UrcChannel;
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, OutputOpenDrain};
use embassy_stm32::gpio::{AnyPin, Input, Level, Output, OutputOpenDrain, Pin, Pull, Speed};
use embassy_stm32::peripherals::UART8;
use embassy_stm32::rcc::VoltageScale;
use embassy_stm32::time::{khz, mhz};
use embassy_stm32::{bind_interrupts, peripherals, Config, interrupt, usart};
use embassy_stm32::peripherals::UART8;
use embassy_stm32::usart::{BufferedUart, BufferedUartRx, BufferedUartTx};
use embassy_stm32::{bind_interrupts, interrupt, peripherals, usart, Config};
use embassy_time::{Duration, Timer};
use static_cell::StaticCell;
use static_cell::make_static;
use {defmt_rtt as _, panic_probe as _};


// use embedded_hal::digital::{ErrorType, InputPin, OutputPin};

use ublox_cellular;
use ublox_cellular::config::{CellularConfig, ReverseOutputPin};

use atat::{Buffers, DefaultDigester, Ingress, AtatIngress, AtDigester, Parser};
use atat::asynch::AtatClient;
use atat::{AtDigester, AtatIngress, DefaultDigester, Ingress, Parser};
use ublox_cellular::asynch::runner::Runner;
use ublox_cellular::asynch::state::{LinkState, OperationState};
use ublox_cellular::asynch::State;
use ublox_cellular::command::{AT, Urc};

use ublox_cellular::command;
use ublox_cellular::command::{Urc, AT};

bind_interrupts!(struct Irqs {
UART8 => embassy_stm32::usart::BufferedInterruptHandler<peripherals::UART8>;
Expand Down Expand Up @@ -60,15 +62,15 @@ impl CellularConfig for MyCelullarConfig {
const HEX_MODE: bool = true;
fn reset_pin(&mut self) -> Option<&mut Self::ResetPin> {
info!("reset_pin");
return self.reset_pin.as_mut()
return self.reset_pin.as_mut();
}
fn power_pin(&mut self) -> Option<&mut Self::PowerPin> {
info!("power_pin");
return self.power_pin.as_mut()
return self.power_pin.as_mut();
}
fn vint_pin(&mut self) -> Option<&mut Self::VintPin> {
info!("vint_pin = {}", self.vint_pin.as_mut()?.is_high());
return self.vint_pin.as_mut()
return self.vint_pin.as_mut();
}
}

Expand All @@ -77,14 +79,15 @@ async fn main_task(spawner: Spawner) {
let mut config = Config::default();
{
use embassy_stm32::rcc::*;
config.rcc.supply_config = SupplyConfig::DirectSMPS;
config.rcc.hsi = Some(HSIPrescaler::DIV1);
config.rcc.csi = true;
config.rcc.pll1 = Some(Pll {
source: PllSource::HSI,
prediv: PllPreDiv::DIV4,
mul: PllMul::MUL50,
divp: Some(PllDiv::DIV2),
divq: Some(PllDiv::DIV8), // used by SPI3. 100Mhz.
divq: Some(PllDiv::DIV8), // 100mhz
divr: None,
});
config.rcc.sys = Sysclk::PLL1_P; // 400 Mhz
Expand All @@ -101,10 +104,13 @@ async fn main_task(spawner: Spawner) {
let led2_pin = p.PI13.degrade();
let led3_pin = p.PI14.degrade();

spawner.spawn(blinky(led1_pin)).unwrap();
spawner.spawn(blinky(led2_pin)).unwrap();
spawner.spawn(blinky(led3_pin)).unwrap();

static tx_buf: StaticCell<[u8; 16]> = StaticCell::new();
static rx_buf: StaticCell<[u8; 16]> = StaticCell::new();

let tx_buf = &mut make_static!([0u8; 16])[..];
let rx_buf = &mut make_static!([0u8; 16])[..];
let (tx_pin, rx_pin, uart) = (p.PJ8, p.PJ9, p.UART8);
let mut uart_config = embassy_stm32::usart::Config::default();
{
Expand All @@ -115,63 +121,86 @@ async fn main_task(spawner: Spawner) {
uart_config.data_bits = embassy_stm32::usart::DataBits::DataBits8;
}


let uart = BufferedUart::new(
uart,
Irqs,
rx_pin,
tx_pin,
tx_buf,
rx_buf,
tx_buf.init([0u8; 16]),
rx_buf.init([0u8; 16]),
uart_config,
);
let (writer, reader) = uart.unwrap().split();
// 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::Low).degrade()),
power_pin: Some(ReverseOutputPin(Output::new(p.PJ4, Level::High, Speed::Low).degrade())),
power_pin: Some(ReverseOutputPin(
Output::new(p.PJ4, Level::Low, 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())
vint_pin: Some(Input::new(p.PJ3, Pull::Down).degrade()),
};

let buffers = &*make_static!(atat::Buffers::new());
let (ingress, client) = buffers.split(writer, AtDigester::default(), atat::Config::new());
static RES_SLOT: ResponseSlot<INGRESS_BUF_SIZE> = ResponseSlot::new();
static URC_CHANNEL: UrcChannel<command::Urc, URC_CAPACITY, URC_SUBSCRIBERS> = UrcChannel::new();
let ingress = Ingress::new(
DefaultDigester::<command::Urc>::default(),
&RES_SLOT,
&URC_CHANNEL,
);
static buf: StaticCell<[u8; INGRESS_BUF_SIZE]> = StaticCell::new();
let mut client = Client::new(
writer,
&RES_SLOT,
buf.init([0; INGRESS_BUF_SIZE]),
atat::Config::default(),
);

spawner.spawn(ingress_task(ingress, reader)).unwrap();

let state = make_static!(State::new(client));
let (device, mut control, mut runner) = ublox_cellular::asynch::new(state, &buffers.urc_channel, celullar_config).await;

static state: StaticCell<State<Client<BufferedUartTx<UART8>, INGRESS_BUF_SIZE>>> =
StaticCell::new();
let (device, mut control, mut runner) = ublox_cellular::asynch::new(
state.init(State::new(client)),
&URC_CHANNEL,
celullar_config,
)
.await;
// defmt::info!("{:?}", runner.init().await);
// control.set_desired_state(PowerState::Connected).await;

defmt::unwrap!(spawner.spawn(cellular_task(runner)));
Timer::after(Duration::from_millis(1000)).await;
loop {
// runner.init().await.unwrap();
defmt::info!("{:?}", runner.is_alive().await);
if runner.is_alive().await != Ok(true){
runner.init().await.unwrap();
control.set_desired_state(OperationState::Initialized).await;
info!("set_desired_state(PowerState::Alive)");
while control.power_state() != OperationState::Initialized {
Timer::after(Duration::from_millis(1000)).await;
}
Timer::after(Duration::from_millis(1000)).await;
if runner.has_power().await == Ok(true){
runner.power_down().await.unwrap();
control.set_desired_state(OperationState::PowerDown).await;
info!("set_desired_state(PowerState::PowerDown)");
while control.power_state() != OperationState::PowerDown {
Timer::after(Duration::from_millis(1000)).await;
}
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)));

loop {
Timer::after(Duration::from_millis(1000)).await;
}
}


#[embassy_executor::task]
async fn ingress_task(
mut ingress: Ingress<'static, DefaultDigester<Urc>, ublox_cellular::command::Urc, {INGRESS_BUF_SIZE}, {URC_CAPACITY}, {URC_SUBSCRIBERS}>,
mut ingress: Ingress<
'static,
DefaultDigester<Urc>,
ublox_cellular::command::Urc,
{ INGRESS_BUF_SIZE },
{ URC_CAPACITY },
{ URC_SUBSCRIBERS },
>,
mut reader: BufferedUartRx<'static, UART8>,
) -> ! {
ingress.read_from(&mut reader).await;
Expand All @@ -180,11 +209,27 @@ async fn ingress_task(

#[embassy_executor::task]
async fn cellular_task(
runner: Runner<'static, atat::asynch::Client<'_, BufferedUartTx<'static, UART8>, {INGRESS_BUF_SIZE}>, MyCelullarConfig, {URC_CAPACITY}>,
runner: Runner<
'static,
atat::asynch::Client<'_, BufferedUartTx<'static, UART8>, { INGRESS_BUF_SIZE }>,
MyCelullarConfig,
{ URC_CAPACITY },
>,
) -> ! {
runner.run().await
}

#[embassy_executor::task(pool_size = 3)]
async fn blinky(mut led: AnyPin){
let mut output = Output::new(led, Level::High, Speed::Low).degrade();
loop {
output.set_high();
Timer::after(Duration::from_millis(1000)).await;
output.set_low();
Timer::after(Duration::from_millis(1000)).await;
}
}

static EXECUTOR: StaticCell<Executor> = StaticCell::new();

#[entry]
Expand Down
Loading
Loading