Skip to content

Commit

Permalink
Simplify initialization of both ppp mode and ublox mode, by providing…
Browse files Browse the repository at this point in the history
… batteries included new functions that sets up ATAT and all related resources
  • Loading branch information
MathiasKoch committed Feb 21, 2024
1 parent 80c4c8d commit 942b93e
Show file tree
Hide file tree
Showing 7 changed files with 641 additions and 677 deletions.
27 changes: 21 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,32 @@ log = { version = "^0.4", default-features = false, optional = true }
defmt = { version = "^0.3", optional = true }

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"
embedded-nal = "0.8"
embedded-nal-async = { version = "0.7", optional = true }

embassy-at-cmux = { path = "../embassy/embassy-at-cmux", optional = true }
embassy-net-ppp = { version = "0.1", optional = true }
embassy-net = { version = "0.4", features = [
"proto-ipv4",
"medium-ip",
], optional = true }

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

[dev-dependencies]
atat = { version = "*", features = ["heapless"] }


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

ppp = ["dep:embassy-at-cmux", "dep:embassy-net-ppp", "dep:embassy-net"]

async = ["dep:embedded-nal-async", "dep:embassy-futures"]

defmt = [
Expand All @@ -57,6 +66,9 @@ defmt = [
"embassy-time/defmt",
"embassy-sync/defmt",
"embassy-futures?/defmt",
"embassy-net-ppp?/defmt",
"embassy-net?/defmt",
"embassy-at-cmux?/defmt",
]

log = ["dep:log", "ublox-sockets?/log", "atat/log"]
Expand All @@ -79,8 +91,8 @@ toby-l4 = []

upsd-context-activation = []

socket-tcp = ["ublox-sockets?/socket-tcp"]
socket-udp = ["ublox-sockets?/socket-udp"]
socket-tcp = ["ublox-sockets?/socket-tcp", "embassy-net?/tcp"]
socket-udp = ["ublox-sockets?/socket-udp", "embassy-net?/udp"]

[workspace]
members = []
Expand All @@ -91,11 +103,14 @@ exclude = ["examples"]
#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" }
# 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-net-ppp = { git = "https://github.com/embassy-rs/embassy", branch = "main" }
embassy-net = { 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" }
atat = { path = "../atat/atat" }
110 changes: 31 additions & 79 deletions examples/embassy-rp2040-example/src/bin/embassy-pdp-context.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
#![no_std]
#![no_main]
#![allow(stable_features)]
#![feature(type_alias_impl_trait)]

use atat::asynch::Client;

use atat::ResponseSlot;
use atat::UrcChannel;

use cortex_m_rt::entry;
use defmt::*;
use embassy_executor::{Executor, Spawner};
use embassy_executor::Spawner;
use embassy_rp::gpio;
use embassy_rp::gpio::Input;

Expand All @@ -20,24 +15,21 @@ use embassy_rp::uart::BufferedUartTx;
use embassy_rp::{bind_interrupts, peripherals::UART0, uart::BufferedInterruptHandler};
use embassy_time::{Duration, Timer};
use static_cell::StaticCell;
use ublox_cellular::asynch::Resources;
use ublox_cellular::asynch::UbloxRunner;
use {defmt_rtt as _, panic_probe as _};

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

use atat::{AtatIngress, DefaultDigester, Ingress};
use ublox_cellular::asynch::runner::Runner;
use ublox_cellular::asynch::state::OperationState;
use ublox_cellular::asynch::State;
use ublox_cellular::command;
use ublox_cellular::command::Urc;

bind_interrupts!(struct Irqs {
UART0_IRQ => BufferedInterruptHandler<UART0>;
});

const CMD_BUF_SIZE: usize = 128;
const INGRESS_BUF_SIZE: usize = 1024;
const URC_CAPACITY: usize = 2;
const URC_SUBSCRIBERS: usize = 2;

struct MyCelullarConfig {
reset_pin: Option<OutputOpenDrain<'static>>,
Expand Down Expand Up @@ -71,8 +63,8 @@ impl<'a> CellularConfig<'a> for MyCelullarConfig {
}
}

#[embassy_executor::task]
async fn main_task(spawner: Spawner) {
#[embassy_executor::main]
async fn main(spawner: Spawner) {
let p = {
let config =
embassy_rp::config::Config::new(embassy_rp::clocks::ClockConfig::crystal(12_000_000));
Expand All @@ -81,10 +73,6 @@ async fn main_task(spawner: Spawner) {

static TX_BUF: StaticCell<[u8; 16]> = StaticCell::new();
static RX_BUF: StaticCell<[u8; 16]> = StaticCell::new();
static INGRESS_BUF: StaticCell<[u8; INGRESS_BUF_SIZE]> = StaticCell::new();

let mut cell_uart_config = embassy_rp::uart::Config::default();
cell_uart_config.baudrate = 115200;

let cell_uart = BufferedUart::new_with_rtscts(
p.UART0,
Expand All @@ -95,45 +83,29 @@ async fn main_task(spawner: Spawner) {
p.PIN_2,
TX_BUF.init([0; 16]),
RX_BUF.init([0; 16]),
cell_uart_config,
embassy_rp::uart::Config::default(),
);

let (uart_rx, uart_tx) = cell_uart.split();
let cell_nrst = gpio::OutputOpenDrain::new(p.PIN_4, gpio::Level::High);
let cell_pwr = gpio::OutputOpenDrain::new(p.PIN_5, gpio::Level::High);
let cell_vint = gpio::Input::new(p.PIN_6, gpio::Pull::None);

let celullar_config = MyCelullarConfig {
reset_pin: Some(cell_nrst),
power_pin: Some(cell_pwr),
vint_pin: Some(cell_vint),
};
static RESOURCES: StaticCell<
Resources<BufferedUartTx<UART0>, CMD_BUF_SIZE, INGRESS_BUF_SIZE, URC_CAPACITY>,
> = StaticCell::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(),
INGRESS_BUF.init([0; INGRESS_BUF_SIZE]),
&RES_SLOT,
&URC_CHANNEL,
);
static BUF: StaticCell<[u8; INGRESS_BUF_SIZE]> = StaticCell::new();
let client = Client::new(
let (_net_device, mut control, runner) = ublox_cellular::asynch::new(
uart_rx,
uart_tx,
&RES_SLOT,
BUF.init([0; INGRESS_BUF_SIZE]),
atat::Config::default(),
RESOURCES.init(Resources::new()),
MyCelullarConfig {
reset_pin: Some(cell_nrst),
power_pin: Some(cell_pwr),
vint_pin: Some(cell_vint),
},
);

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

static STATE: StaticCell<State<Client<BufferedUartTx<UART0>, INGRESS_BUF_SIZE>>> =
StaticCell::new();
let (_device, mut control, runner) = ublox_cellular::asynch::new(
STATE.init(State::new(client)),
&URC_CHANNEL,
celullar_config,
);
// defmt::info!("{:?}", runner.init().await);
// control.set_desired_state(PowerState::Connected).await;
// control
Expand All @@ -143,7 +115,8 @@ async fn main_task(spawner: Spawner) {
// })
// .await;

defmt::unwrap!(spawner.spawn(cellular_task(runner)));
defmt::unwrap!(spawner.spawn(cell_task(runner)));

Timer::after(Duration::from_millis(1000)).await;
loop {
control
Expand Down Expand Up @@ -195,42 +168,21 @@ async fn main_task(spawner: Spawner) {
}
}

#[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 reader: BufferedUartRx<'static, UART0>,
) -> ! {
ingress.read_from(&mut reader).await
}
// #[embassy_executor::task]
// async fn net_task(stack: &'static Stack<embassy_net_ppp::Device<'static>>) -> ! {
// stack.run().await
// }

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

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

#[entry]
fn main() -> ! {
info!("Hello World!");

let executor = EXECUTOR.init(Executor::new());

executor.run(|spawner| {
unwrap!(spawner.spawn(main_task(spawner)));
})
}
Loading

0 comments on commit 942b93e

Please sign in to comment.