Intitial commit
This commit is contained in:
commit
b20c5b4ca1
9
.cargo/config.toml
Normal file
9
.cargo/config.toml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
||||||
|
# runner = "probe-rs run --chip nRF9160_xxAA"
|
||||||
|
runner = [ "probe-rs", "run", "--chip=nRF9160_xxAA", "--always-print-stacktrace", "--log-format={t} {[{L}]%bold} {s} {{c} {ff}:{l:1}%dimmed}" ]
|
||||||
|
|
||||||
|
[build]
|
||||||
|
target = "thumbv8m.main-none-eabihf"
|
||||||
|
|
||||||
|
[env]
|
||||||
|
DEFMT_LOG = "trace"
|
||||||
21
.gitignore
vendored
Normal file
21
.gitignore
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# Generated by Cargo
|
||||||
|
# will have compiled files and executables
|
||||||
|
debug/
|
||||||
|
target/
|
||||||
|
|
||||||
|
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||||
|
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||||
|
Cargo.lock
|
||||||
|
|
||||||
|
# These are backup files generated by rustfmt
|
||||||
|
**/*.rs.bk
|
||||||
|
|
||||||
|
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||||
|
*.pdb
|
||||||
|
|
||||||
|
# RustRover
|
||||||
|
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||||
|
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||||
|
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||||
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
|
#.idea/
|
||||||
33
Cargo.toml
Normal file
33
Cargo.toml
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
[package]
|
||||||
|
edition = "2021"
|
||||||
|
name = "embassy-hhtp-test"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Sebastian H. Gabrielli <sebgab@sebamail.no>"]
|
||||||
|
resolver = "2"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
defmt = "0.3"
|
||||||
|
defmt-rtt = "0.4"
|
||||||
|
panic-probe = { version = "0.3", features = ["print-defmt"] }
|
||||||
|
|
||||||
|
embedded-hal = "1.0.0"
|
||||||
|
embedded-hal-async = "1.0.0"
|
||||||
|
embedded-io = "0.6.1"
|
||||||
|
embedded-io-async = { version = "0.6.1", features = ["defmt-03"] }
|
||||||
|
embedded-storage = "0.3.1"
|
||||||
|
|
||||||
|
cortex-m-rt = "0.7.3"
|
||||||
|
|
||||||
|
embassy-executor = { version = "0.6", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers", "executor-interrupt"] }
|
||||||
|
embassy-sync = { version = "0.6" }
|
||||||
|
embassy-time = { version = "0.3", features = ["defmt", "defmt-timestamp-uptime"] }
|
||||||
|
#embassy-net-nrf91 = { version = "0.1.0", features = ["defmt"] }
|
||||||
|
embassy-net = { version = "0.5.0", features = ["defmt", "tcp", "proto-ipv4", "medium-ip"] }
|
||||||
|
|
||||||
|
cortex-m = { version = "0.7.6", features = ["critical-section-single-core", "inline-asm"] }
|
||||||
|
embassy-nrf = { version = "0.2", features = ["defmt", "nrf9160-s", "time-driver-rtc1", "gpiote", "unstable-pac", "time"] }
|
||||||
|
|
||||||
|
static_cell = {version = "2"}
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
debug = 2
|
||||||
35
build.rs
Normal file
35
build.rs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
//! This build script copies the `memory.x` file from the crate root into
|
||||||
|
//! a directory where the linker can always find it at build time.
|
||||||
|
//! For many projects this is optional, as the linker always searches the
|
||||||
|
//! project root directory -- wherever `Cargo.toml` is. However, if you
|
||||||
|
//! are using a workspace or have a more complicated build setup, this
|
||||||
|
//! build script becomes required. Additionally, by requesting that
|
||||||
|
//! Cargo re-run the build script whenever `memory.x` is changed,
|
||||||
|
//! updating `memory.x` ensures a rebuild of the application with the
|
||||||
|
//! new memory settings.
|
||||||
|
|
||||||
|
use std::env;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// Put `memory.x` in our output directory and ensure it's
|
||||||
|
// on the linker search path.
|
||||||
|
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
||||||
|
File::create(out.join("memory.x"))
|
||||||
|
.unwrap()
|
||||||
|
.write_all(include_bytes!("memory.x"))
|
||||||
|
.unwrap();
|
||||||
|
println!("cargo:rustc-link-search={}", out.display());
|
||||||
|
|
||||||
|
// By default, Cargo will re-run a build script whenever
|
||||||
|
// any file in the project changes. By specifying `memory.x`
|
||||||
|
// here, we ensure the build script is only re-run when
|
||||||
|
// `memory.x` is changed.
|
||||||
|
println!("cargo:rerun-if-changed=memory.x");
|
||||||
|
|
||||||
|
println!("cargo:rustc-link-arg-bins=--nmagic");
|
||||||
|
println!("cargo:rustc-link-arg-bins=-Tlink.x");
|
||||||
|
println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
|
||||||
|
}
|
||||||
39
embassy-net-nrf91/Cargo.toml
Normal file
39
embassy-net-nrf91/Cargo.toml
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
[package]
|
||||||
|
name = "embassy-net-nrf91"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
description = "embassy-net driver for Nordic nRF91-series cellular modems"
|
||||||
|
keywords = ["embedded", "nrf91", "embassy-net", "cellular"]
|
||||||
|
categories = ["embedded", "hardware-support", "no-std", "network-programming", "asynchronous"]
|
||||||
|
license = "MIT OR Apache-2.0"
|
||||||
|
repository = "https://github.com/embassy-rs/embassy"
|
||||||
|
documentation = "https://docs.embassy.dev/embassy-net-nrf91"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
defmt = [ "dep:defmt", "heapless/defmt-03" ]
|
||||||
|
log = [ "dep:log" ]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
defmt = { version = "0.3", optional = true }
|
||||||
|
log = { version = "0.4.14", optional = true }
|
||||||
|
|
||||||
|
nrf-pac = { git = "https://github.com/embassy-rs/nrf-pac", rev = "52e3a757f06035c94291bfc42b0c03f71e4d677e", features = ["nrf9160"] }
|
||||||
|
cortex-m = "0.7.7"
|
||||||
|
|
||||||
|
embassy-time = { version = "0.3.1" }
|
||||||
|
embassy-sync = { version = "0.6.1" }
|
||||||
|
embassy-futures = { version = "0.1.0" }
|
||||||
|
embassy-net-driver-channel = { version = "0.3.0" }
|
||||||
|
|
||||||
|
heapless = "0.8"
|
||||||
|
embedded-io = "0.6.1"
|
||||||
|
at-commands = "0.5.4"
|
||||||
|
|
||||||
|
[package.metadata.embassy_docs]
|
||||||
|
src_base = "https://github.com/embassy-rs/embassy/blob/embassy-net-nrf91-v$VERSION/embassy-net-nrf91/src/"
|
||||||
|
src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-net-nrf91/src/"
|
||||||
|
target = "thumbv7em-none-eabi"
|
||||||
|
features = ["defmt"]
|
||||||
|
|
||||||
|
[package.metadata.docs.rs]
|
||||||
|
features = ["defmt"]
|
||||||
9
embassy-net-nrf91/README.md
Normal file
9
embassy-net-nrf91/README.md
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# nRF91 `embassy-net` integration
|
||||||
|
|
||||||
|
[`embassy-net`](https://crates.io/crates/embassy-net) driver for Nordic nRF91-series cellular modems.
|
||||||
|
|
||||||
|
See the [`examples`](https://github.com/embassy-rs/embassy/tree/main/examples/nrf9160) directory for usage examples with the nRF9160.
|
||||||
|
|
||||||
|
## Interoperability
|
||||||
|
|
||||||
|
This crate can run on any executor.
|
||||||
362
embassy-net-nrf91/src/context.rs
Normal file
362
embassy-net-nrf91/src/context.rs
Normal file
@ -0,0 +1,362 @@
|
|||||||
|
//! Helper utility to configure a specific modem context.
|
||||||
|
use core::net::IpAddr;
|
||||||
|
use core::str::FromStr;
|
||||||
|
|
||||||
|
use at_commands::builder::CommandBuilder;
|
||||||
|
use at_commands::parser::CommandParser;
|
||||||
|
use embassy_time::{Duration, Timer};
|
||||||
|
use heapless::Vec;
|
||||||
|
|
||||||
|
/// Provides a higher level API for controlling a given context.
|
||||||
|
pub struct Control<'a> {
|
||||||
|
control: crate::Control<'a>,
|
||||||
|
cid: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Configuration for a given context
|
||||||
|
pub struct Config<'a> {
|
||||||
|
/// Desired APN address.
|
||||||
|
pub apn: &'a [u8],
|
||||||
|
/// Desired authentication protocol.
|
||||||
|
pub auth_prot: AuthProt,
|
||||||
|
/// Credentials.
|
||||||
|
pub auth: Option<(&'a [u8], &'a [u8])>,
|
||||||
|
/// SIM pin
|
||||||
|
pub pin: Option<&'a [u8]>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Authentication protocol.
|
||||||
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||||
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||||
|
#[repr(u8)]
|
||||||
|
pub enum AuthProt {
|
||||||
|
/// No authentication.
|
||||||
|
None = 0,
|
||||||
|
/// PAP authentication.
|
||||||
|
Pap = 1,
|
||||||
|
/// CHAP authentication.
|
||||||
|
Chap = 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Error returned by control.
|
||||||
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||||
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||||
|
pub enum Error {
|
||||||
|
/// Not enough space for command.
|
||||||
|
BufferTooSmall,
|
||||||
|
/// Error parsing response from modem.
|
||||||
|
AtParseError,
|
||||||
|
/// Error parsing IP addresses.
|
||||||
|
AddrParseError,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<at_commands::parser::ParseError> for Error {
|
||||||
|
fn from(_: at_commands::parser::ParseError) -> Self {
|
||||||
|
Self::AtParseError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Status of a given context.
|
||||||
|
#[derive(PartialEq, Debug)]
|
||||||
|
pub struct Status {
|
||||||
|
/// Attached to APN or not.
|
||||||
|
pub attached: bool,
|
||||||
|
/// IP if assigned.
|
||||||
|
pub ip: Option<IpAddr>,
|
||||||
|
/// Gateway if assigned.
|
||||||
|
pub gateway: Option<IpAddr>,
|
||||||
|
/// DNS servers if assigned.
|
||||||
|
pub dns: Vec<IpAddr, 2>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "defmt")]
|
||||||
|
impl defmt::Format for Status {
|
||||||
|
fn format(&self, f: defmt::Formatter<'_>) {
|
||||||
|
defmt::write!(f, "attached: {}", self.attached);
|
||||||
|
if let Some(ip) = &self.ip {
|
||||||
|
defmt::write!(f, ", ip: {}", defmt::Debug2Format(&ip));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Control<'a> {
|
||||||
|
/// Create a new instance of a control handle for a given context.
|
||||||
|
///
|
||||||
|
/// Will wait for the modem to be initialized if not.
|
||||||
|
pub async fn new(control: crate::Control<'a>, cid: u8) -> Self {
|
||||||
|
control.wait_init().await;
|
||||||
|
Self { control, cid }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Perform a raw AT command
|
||||||
|
pub async fn at_command(&self, req: &[u8], resp: &mut [u8]) -> usize {
|
||||||
|
self.control.at_command(req, resp).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Configures the modem with the provided config.
|
||||||
|
///
|
||||||
|
/// NOTE: This will disconnect the modem from any current APN and should not
|
||||||
|
/// be called if the configuration has not been changed.
|
||||||
|
///
|
||||||
|
/// After configuring, invoke [`enable()`] to activate the configuration.
|
||||||
|
pub async fn configure(&self, config: &Config<'_>) -> Result<(), Error> {
|
||||||
|
let mut cmd: [u8; 256] = [0; 256];
|
||||||
|
let mut buf: [u8; 256] = [0; 256];
|
||||||
|
|
||||||
|
let op = CommandBuilder::create_set(&mut cmd, true)
|
||||||
|
.named("+CFUN")
|
||||||
|
.with_int_parameter(0)
|
||||||
|
.finish()
|
||||||
|
.map_err(|_| Error::BufferTooSmall)?;
|
||||||
|
let n = self.control.at_command(op, &mut buf).await;
|
||||||
|
CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?;
|
||||||
|
|
||||||
|
let op = CommandBuilder::create_set(&mut cmd, true)
|
||||||
|
.named("+CGDCONT")
|
||||||
|
.with_int_parameter(self.cid)
|
||||||
|
.with_string_parameter("IP")
|
||||||
|
.with_string_parameter(config.apn)
|
||||||
|
.finish()
|
||||||
|
.map_err(|_| Error::BufferTooSmall)?;
|
||||||
|
let n = self.control.at_command(op, &mut buf).await;
|
||||||
|
// info!("RES1: {}", unsafe { core::str::from_utf8_unchecked(&buf[..n]) });
|
||||||
|
CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?;
|
||||||
|
|
||||||
|
let mut op = CommandBuilder::create_set(&mut cmd, true)
|
||||||
|
.named("+CGAUTH")
|
||||||
|
.with_int_parameter(self.cid)
|
||||||
|
.with_int_parameter(config.auth_prot as u8);
|
||||||
|
if let Some((username, password)) = config.auth {
|
||||||
|
op = op.with_string_parameter(username).with_string_parameter(password);
|
||||||
|
}
|
||||||
|
let op = op.finish().map_err(|_| Error::BufferTooSmall)?;
|
||||||
|
|
||||||
|
let n = self.control.at_command(op, &mut buf).await;
|
||||||
|
// info!("RES2: {}", unsafe { core::str::from_utf8_unchecked(&buf[..n]) });
|
||||||
|
CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?;
|
||||||
|
|
||||||
|
if let Some(pin) = config.pin {
|
||||||
|
let op = CommandBuilder::create_set(&mut cmd, true)
|
||||||
|
.named("+CPIN")
|
||||||
|
.with_string_parameter(pin)
|
||||||
|
.finish()
|
||||||
|
.map_err(|_| Error::BufferTooSmall)?;
|
||||||
|
let _ = self.control.at_command(op, &mut buf).await;
|
||||||
|
// Ignore ERROR which means no pin required
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attach to the PDN
|
||||||
|
pub async fn attach(&self) -> Result<(), Error> {
|
||||||
|
let mut cmd: [u8; 256] = [0; 256];
|
||||||
|
let mut buf: [u8; 256] = [0; 256];
|
||||||
|
let op = CommandBuilder::create_set(&mut cmd, true)
|
||||||
|
.named("+CGATT")
|
||||||
|
.with_int_parameter(1)
|
||||||
|
.finish()
|
||||||
|
.map_err(|_| Error::BufferTooSmall)?;
|
||||||
|
let n = self.control.at_command(op, &mut buf).await;
|
||||||
|
CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read current connectivity status for modem.
|
||||||
|
pub async fn detach(&self) -> Result<(), Error> {
|
||||||
|
let mut cmd: [u8; 256] = [0; 256];
|
||||||
|
let mut buf: [u8; 256] = [0; 256];
|
||||||
|
let op = CommandBuilder::create_set(&mut cmd, true)
|
||||||
|
.named("+CGATT")
|
||||||
|
.with_int_parameter(0)
|
||||||
|
.finish()
|
||||||
|
.map_err(|_| Error::BufferTooSmall)?;
|
||||||
|
let n = self.control.at_command(op, &mut buf).await;
|
||||||
|
CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn attached(&self) -> Result<bool, Error> {
|
||||||
|
let mut cmd: [u8; 256] = [0; 256];
|
||||||
|
let mut buf: [u8; 256] = [0; 256];
|
||||||
|
|
||||||
|
let op = CommandBuilder::create_query(&mut cmd, true)
|
||||||
|
.named("+CGATT")
|
||||||
|
.finish()
|
||||||
|
.map_err(|_| Error::BufferTooSmall)?;
|
||||||
|
let n = self.control.at_command(op, &mut buf).await;
|
||||||
|
let (res,) = CommandParser::parse(&buf[..n])
|
||||||
|
.expect_identifier(b"+CGATT: ")
|
||||||
|
.expect_int_parameter()
|
||||||
|
.expect_identifier(b"\r\nOK")
|
||||||
|
.finish()?;
|
||||||
|
Ok(res == 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read current connectivity status for modem.
|
||||||
|
pub async fn status(&self) -> Result<Status, Error> {
|
||||||
|
let mut cmd: [u8; 256] = [0; 256];
|
||||||
|
let mut buf: [u8; 256] = [0; 256];
|
||||||
|
|
||||||
|
let op = CommandBuilder::create_query(&mut cmd, true)
|
||||||
|
.named("+CGATT")
|
||||||
|
.finish()
|
||||||
|
.map_err(|_| Error::BufferTooSmall)?;
|
||||||
|
let n = self.control.at_command(op, &mut buf).await;
|
||||||
|
let (res,) = CommandParser::parse(&buf[..n])
|
||||||
|
.expect_identifier(b"+CGATT: ")
|
||||||
|
.expect_int_parameter()
|
||||||
|
.expect_identifier(b"\r\nOK")
|
||||||
|
.finish()?;
|
||||||
|
let attached = res == 1;
|
||||||
|
if !attached {
|
||||||
|
return Ok(Status {
|
||||||
|
attached,
|
||||||
|
ip: None,
|
||||||
|
gateway: None,
|
||||||
|
dns: Vec::new(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let op = CommandBuilder::create_set(&mut cmd, true)
|
||||||
|
.named("+CGPADDR")
|
||||||
|
.with_int_parameter(self.cid)
|
||||||
|
.finish()
|
||||||
|
.map_err(|_| Error::BufferTooSmall)?;
|
||||||
|
let n = self.control.at_command(op, &mut buf).await;
|
||||||
|
let (_, ip1, _ip2) = CommandParser::parse(&buf[..n])
|
||||||
|
.expect_identifier(b"+CGPADDR: ")
|
||||||
|
.expect_int_parameter()
|
||||||
|
.expect_optional_string_parameter()
|
||||||
|
.expect_optional_string_parameter()
|
||||||
|
.expect_identifier(b"\r\nOK")
|
||||||
|
.finish()?;
|
||||||
|
|
||||||
|
let ip = if let Some(ip) = ip1 {
|
||||||
|
let ip = IpAddr::from_str(ip).map_err(|_| Error::AddrParseError)?;
|
||||||
|
Some(ip)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let op = CommandBuilder::create_set(&mut cmd, true)
|
||||||
|
.named("+CGCONTRDP")
|
||||||
|
.with_int_parameter(self.cid)
|
||||||
|
.finish()
|
||||||
|
.map_err(|_| Error::BufferTooSmall)?;
|
||||||
|
let n = self.control.at_command(op, &mut buf).await;
|
||||||
|
let (_cid, _bid, _apn, _mask, gateway, dns1, dns2, _, _, _, _, _mtu) = CommandParser::parse(&buf[..n])
|
||||||
|
.expect_identifier(b"+CGCONTRDP: ")
|
||||||
|
.expect_int_parameter()
|
||||||
|
.expect_optional_int_parameter()
|
||||||
|
.expect_optional_string_parameter()
|
||||||
|
.expect_optional_string_parameter()
|
||||||
|
.expect_optional_string_parameter()
|
||||||
|
.expect_optional_string_parameter()
|
||||||
|
.expect_optional_string_parameter()
|
||||||
|
.expect_optional_int_parameter()
|
||||||
|
.expect_optional_int_parameter()
|
||||||
|
.expect_optional_int_parameter()
|
||||||
|
.expect_optional_int_parameter()
|
||||||
|
.expect_optional_int_parameter()
|
||||||
|
.expect_identifier(b"\r\nOK")
|
||||||
|
.finish()?;
|
||||||
|
|
||||||
|
let gateway = if let Some(ip) = gateway {
|
||||||
|
if ip.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(IpAddr::from_str(ip).map_err(|_| Error::AddrParseError)?)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut dns = Vec::new();
|
||||||
|
if let Some(ip) = dns1 {
|
||||||
|
dns.push(IpAddr::from_str(ip).map_err(|_| Error::AddrParseError)?)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(ip) = dns2 {
|
||||||
|
dns.push(IpAddr::from_str(ip).map_err(|_| Error::AddrParseError)?)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Status {
|
||||||
|
attached,
|
||||||
|
ip,
|
||||||
|
gateway,
|
||||||
|
dns,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn wait_attached(&self) -> Result<Status, Error> {
|
||||||
|
while !self.attached().await? {
|
||||||
|
Timer::after(Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
|
let status = self.status().await?;
|
||||||
|
Ok(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Disable modem
|
||||||
|
pub async fn disable(&self) -> Result<(), Error> {
|
||||||
|
let mut cmd: [u8; 256] = [0; 256];
|
||||||
|
let mut buf: [u8; 256] = [0; 256];
|
||||||
|
|
||||||
|
let op = CommandBuilder::create_set(&mut cmd, true)
|
||||||
|
.named("+CFUN")
|
||||||
|
.with_int_parameter(0)
|
||||||
|
.finish()
|
||||||
|
.map_err(|_| Error::BufferTooSmall)?;
|
||||||
|
let n = self.control.at_command(op, &mut buf).await;
|
||||||
|
CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Enable modem
|
||||||
|
pub async fn enable(&self) -> Result<(), Error> {
|
||||||
|
let mut cmd: [u8; 256] = [0; 256];
|
||||||
|
let mut buf: [u8; 256] = [0; 256];
|
||||||
|
|
||||||
|
let op = CommandBuilder::create_set(&mut cmd, true)
|
||||||
|
.named("+CFUN")
|
||||||
|
.with_int_parameter(1)
|
||||||
|
.finish()
|
||||||
|
.map_err(|_| Error::BufferTooSmall)?;
|
||||||
|
let n = self.control.at_command(op, &mut buf).await;
|
||||||
|
CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?;
|
||||||
|
|
||||||
|
// Make modem survive PDN detaches
|
||||||
|
let op = CommandBuilder::create_set(&mut cmd, true)
|
||||||
|
.named("%XPDNCFG")
|
||||||
|
.with_int_parameter(1)
|
||||||
|
.finish()
|
||||||
|
.map_err(|_| Error::BufferTooSmall)?;
|
||||||
|
let n = self.control.at_command(op, &mut buf).await;
|
||||||
|
CommandParser::parse(&buf[..n]).expect_identifier(b"OK").finish()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Run a control loop for this context, ensuring that reaattach is handled.
|
||||||
|
pub async fn run<F: Fn(&Status)>(&self, reattach: F) -> Result<(), Error> {
|
||||||
|
self.enable().await?;
|
||||||
|
let status = self.wait_attached().await?;
|
||||||
|
let mut fd = self.control.open_raw_socket().await;
|
||||||
|
reattach(&status);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
if !self.attached().await? {
|
||||||
|
trace!("detached");
|
||||||
|
|
||||||
|
self.control.close_raw_socket(fd).await;
|
||||||
|
let status = self.wait_attached().await?;
|
||||||
|
trace!("attached");
|
||||||
|
fd = self.control.open_raw_socket().await;
|
||||||
|
reattach(&status);
|
||||||
|
}
|
||||||
|
Timer::after(Duration::from_secs(10)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
274
embassy-net-nrf91/src/fmt.rs
Normal file
274
embassy-net-nrf91/src/fmt.rs
Normal file
@ -0,0 +1,274 @@
|
|||||||
|
#![macro_use]
|
||||||
|
#![allow(unused)]
|
||||||
|
|
||||||
|
use core::fmt::{Debug, Display, LowerHex};
|
||||||
|
|
||||||
|
#[cfg(all(feature = "defmt", feature = "log"))]
|
||||||
|
compile_error!("You may not enable both `defmt` and `log` features.");
|
||||||
|
|
||||||
|
#[collapse_debuginfo(yes)]
|
||||||
|
macro_rules! assert {
|
||||||
|
($($x:tt)*) => {
|
||||||
|
{
|
||||||
|
#[cfg(not(feature = "defmt"))]
|
||||||
|
::core::assert!($($x)*);
|
||||||
|
#[cfg(feature = "defmt")]
|
||||||
|
::defmt::assert!($($x)*);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[collapse_debuginfo(yes)]
|
||||||
|
macro_rules! assert_eq {
|
||||||
|
($($x:tt)*) => {
|
||||||
|
{
|
||||||
|
#[cfg(not(feature = "defmt"))]
|
||||||
|
::core::assert_eq!($($x)*);
|
||||||
|
#[cfg(feature = "defmt")]
|
||||||
|
::defmt::assert_eq!($($x)*);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[collapse_debuginfo(yes)]
|
||||||
|
macro_rules! assert_ne {
|
||||||
|
($($x:tt)*) => {
|
||||||
|
{
|
||||||
|
#[cfg(not(feature = "defmt"))]
|
||||||
|
::core::assert_ne!($($x)*);
|
||||||
|
#[cfg(feature = "defmt")]
|
||||||
|
::defmt::assert_ne!($($x)*);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[collapse_debuginfo(yes)]
|
||||||
|
macro_rules! debug_assert {
|
||||||
|
($($x:tt)*) => {
|
||||||
|
{
|
||||||
|
#[cfg(not(feature = "defmt"))]
|
||||||
|
::core::debug_assert!($($x)*);
|
||||||
|
#[cfg(feature = "defmt")]
|
||||||
|
::defmt::debug_assert!($($x)*);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[collapse_debuginfo(yes)]
|
||||||
|
macro_rules! debug_assert_eq {
|
||||||
|
($($x:tt)*) => {
|
||||||
|
{
|
||||||
|
#[cfg(not(feature = "defmt"))]
|
||||||
|
::core::debug_assert_eq!($($x)*);
|
||||||
|
#[cfg(feature = "defmt")]
|
||||||
|
::defmt::debug_assert_eq!($($x)*);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[collapse_debuginfo(yes)]
|
||||||
|
macro_rules! debug_assert_ne {
|
||||||
|
($($x:tt)*) => {
|
||||||
|
{
|
||||||
|
#[cfg(not(feature = "defmt"))]
|
||||||
|
::core::debug_assert_ne!($($x)*);
|
||||||
|
#[cfg(feature = "defmt")]
|
||||||
|
::defmt::debug_assert_ne!($($x)*);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[collapse_debuginfo(yes)]
|
||||||
|
macro_rules! todo {
|
||||||
|
($($x:tt)*) => {
|
||||||
|
{
|
||||||
|
#[cfg(not(feature = "defmt"))]
|
||||||
|
::core::todo!($($x)*);
|
||||||
|
#[cfg(feature = "defmt")]
|
||||||
|
::defmt::todo!($($x)*);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "defmt"))]
|
||||||
|
#[collapse_debuginfo(yes)]
|
||||||
|
macro_rules! unreachable {
|
||||||
|
($($x:tt)*) => {
|
||||||
|
::core::unreachable!($($x)*)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "defmt")]
|
||||||
|
#[collapse_debuginfo(yes)]
|
||||||
|
macro_rules! unreachable {
|
||||||
|
($($x:tt)*) => {
|
||||||
|
::defmt::unreachable!($($x)*)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[collapse_debuginfo(yes)]
|
||||||
|
macro_rules! panic {
|
||||||
|
($($x:tt)*) => {
|
||||||
|
{
|
||||||
|
#[cfg(not(feature = "defmt"))]
|
||||||
|
::core::panic!($($x)*);
|
||||||
|
#[cfg(feature = "defmt")]
|
||||||
|
::defmt::panic!($($x)*);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[collapse_debuginfo(yes)]
|
||||||
|
macro_rules! trace {
|
||||||
|
($s:literal $(, $x:expr)* $(,)?) => {
|
||||||
|
{
|
||||||
|
#[cfg(feature = "log")]
|
||||||
|
::log::trace!($s $(, $x)*);
|
||||||
|
#[cfg(feature = "defmt")]
|
||||||
|
::defmt::trace!($s $(, $x)*);
|
||||||
|
#[cfg(not(any(feature = "log", feature="defmt")))]
|
||||||
|
let _ = ($( & $x ),*);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[collapse_debuginfo(yes)]
|
||||||
|
macro_rules! debug {
|
||||||
|
($s:literal $(, $x:expr)* $(,)?) => {
|
||||||
|
{
|
||||||
|
#[cfg(feature = "log")]
|
||||||
|
::log::debug!($s $(, $x)*);
|
||||||
|
#[cfg(feature = "defmt")]
|
||||||
|
::defmt::debug!($s $(, $x)*);
|
||||||
|
#[cfg(not(any(feature = "log", feature="defmt")))]
|
||||||
|
let _ = ($( & $x ),*);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[collapse_debuginfo(yes)]
|
||||||
|
macro_rules! info {
|
||||||
|
($s:literal $(, $x:expr)* $(,)?) => {
|
||||||
|
{
|
||||||
|
#[cfg(feature = "log")]
|
||||||
|
::log::info!($s $(, $x)*);
|
||||||
|
#[cfg(feature = "defmt")]
|
||||||
|
::defmt::info!($s $(, $x)*);
|
||||||
|
#[cfg(not(any(feature = "log", feature="defmt")))]
|
||||||
|
let _ = ($( & $x ),*);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[collapse_debuginfo(yes)]
|
||||||
|
macro_rules! warn {
|
||||||
|
($s:literal $(, $x:expr)* $(,)?) => {
|
||||||
|
{
|
||||||
|
#[cfg(feature = "log")]
|
||||||
|
::log::warn!($s $(, $x)*);
|
||||||
|
#[cfg(feature = "defmt")]
|
||||||
|
::defmt::warn!($s $(, $x)*);
|
||||||
|
#[cfg(not(any(feature = "log", feature="defmt")))]
|
||||||
|
let _ = ($( & $x ),*);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[collapse_debuginfo(yes)]
|
||||||
|
macro_rules! error {
|
||||||
|
($s:literal $(, $x:expr)* $(,)?) => {
|
||||||
|
{
|
||||||
|
#[cfg(feature = "log")]
|
||||||
|
::log::error!($s $(, $x)*);
|
||||||
|
#[cfg(feature = "defmt")]
|
||||||
|
::defmt::error!($s $(, $x)*);
|
||||||
|
#[cfg(not(any(feature = "log", feature="defmt")))]
|
||||||
|
let _ = ($( & $x ),*);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "defmt")]
|
||||||
|
#[collapse_debuginfo(yes)]
|
||||||
|
macro_rules! unwrap {
|
||||||
|
($($x:tt)*) => {
|
||||||
|
::defmt::unwrap!($($x)*)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "defmt"))]
|
||||||
|
#[collapse_debuginfo(yes)]
|
||||||
|
macro_rules! unwrap {
|
||||||
|
($arg:expr) => {
|
||||||
|
match $crate::fmt::Try::into_result($arg) {
|
||||||
|
::core::result::Result::Ok(t) => t,
|
||||||
|
::core::result::Result::Err(e) => {
|
||||||
|
::core::panic!("unwrap of `{}` failed: {:?}", ::core::stringify!($arg), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
($arg:expr, $($msg:expr),+ $(,)? ) => {
|
||||||
|
match $crate::fmt::Try::into_result($arg) {
|
||||||
|
::core::result::Result::Ok(t) => t,
|
||||||
|
::core::result::Result::Err(e) => {
|
||||||
|
::core::panic!("unwrap of `{}` failed: {}: {:?}", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
|
pub struct NoneError;
|
||||||
|
|
||||||
|
pub trait Try {
|
||||||
|
type Ok;
|
||||||
|
type Error;
|
||||||
|
fn into_result(self) -> Result<Self::Ok, Self::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Try for Option<T> {
|
||||||
|
type Ok = T;
|
||||||
|
type Error = NoneError;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn into_result(self) -> Result<T, NoneError> {
|
||||||
|
self.ok_or(NoneError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, E> Try for Result<T, E> {
|
||||||
|
type Ok = T;
|
||||||
|
type Error = E;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn into_result(self) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct Bytes<'a>(pub &'a [u8]);
|
||||||
|
|
||||||
|
impl<'a> Debug for Bytes<'a> {
|
||||||
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||||
|
write!(f, "{:#02x?}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Display for Bytes<'a> {
|
||||||
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||||
|
write!(f, "{:#02x?}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> LowerHex for Bytes<'a> {
|
||||||
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||||
|
write!(f, "{:#02x?}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "defmt")]
|
||||||
|
impl<'a> defmt::Format for Bytes<'a> {
|
||||||
|
fn format(&self, fmt: defmt::Formatter) {
|
||||||
|
defmt::write!(fmt, "{:02x}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
1042
embassy-net-nrf91/src/lib.rs
Normal file
1042
embassy-net-nrf91/src/lib.rs
Normal file
File diff suppressed because it is too large
Load Diff
9
memory.x
Normal file
9
memory.x
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
MEMORY
|
||||||
|
{
|
||||||
|
FLASH : ORIGIN = 0x00000000, LENGTH = 1024K
|
||||||
|
RAM : ORIGIN = 0x20010000, LENGTH = 192K
|
||||||
|
IPC : ORIGIN = 0x20000000, LENGTH = 64K
|
||||||
|
}
|
||||||
|
|
||||||
|
PROVIDE(__start_ipc = ORIGIN(IPC));
|
||||||
|
PROVIDE(__end_ipc = ORIGIN(IPC) + LENGTH(IPC));
|
||||||
10
rust-toolchain.toml
Normal file
10
rust-toolchain.toml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
# Before upgrading check that everything is available on all tier1 targets here:
|
||||||
|
# https://rust-lang.github.io/rustup-components-history
|
||||||
|
[toolchain]
|
||||||
|
channel = "stable"
|
||||||
|
components = [ "rustfmt" ]
|
||||||
|
targets = [
|
||||||
|
"thumbv6m-none-eabi",
|
||||||
|
"thumbv7em-none-eabihf",
|
||||||
|
"riscv32imac-unknown-none-elf",
|
||||||
|
]
|
||||||
23
src/main.rs
Normal file
23
src/main.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use embassy_executor::Spawner;
|
||||||
|
use embassy_nrf::gpio::{Level, Output, OutputDrive};
|
||||||
|
use embassy_time::Timer;
|
||||||
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
use defmt::info;
|
||||||
|
|
||||||
|
#[embassy_executor::main]
|
||||||
|
async fn main(_spawner: Spawner) {
|
||||||
|
let p = embassy_nrf::init(Default::default());
|
||||||
|
let mut led = Output::new(p.P0_02, Level::Low, OutputDrive::Standard);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
info!("Going high!");
|
||||||
|
led.set_high();
|
||||||
|
Timer::after_millis(300).await;
|
||||||
|
info!("Going low!");
|
||||||
|
led.set_low();
|
||||||
|
Timer::after_millis(300).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user