mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-25 00:02:28 +00:00
Merge pull request #3105 from embassy-rs/net-nrf91
embassy-net driver for nrf91
This commit is contained in:
commit
2286e5da13
38
embassy-net-nrf91/Cargo.toml
Normal file
38
embassy-net-nrf91/Cargo.toml
Normal file
@ -0,0 +1,38 @@
|
||||
[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 }
|
||||
|
||||
nrf9160-pac = { version = "0.12.0" }
|
||||
|
||||
embassy-time = { version = "0.3.1", path = "../embassy-time" }
|
||||
embassy-sync = { version = "0.6.0", path = "../embassy-sync"}
|
||||
embassy-futures = { version = "0.1.0", path = "../embassy-futures"}
|
||||
embassy-net-driver-channel = { version = "0.3.0", path = "../embassy-net-driver-channel"}
|
||||
|
||||
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.
|
350
embassy-net-nrf91/src/context.rs
Normal file
350
embassy-net-nrf91/src/context.rs
Normal file
@ -0,0 +1,350 @@
|
||||
//! 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])>,
|
||||
}
|
||||
|
||||
/// 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()?;
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
1046
embassy-net-nrf91/src/lib.rs
Normal file
1046
embassy-net-nrf91/src/lib.rs
Normal file
File diff suppressed because it is too large
Load Diff
@ -358,6 +358,11 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> {
|
||||
self.tx.write(buf).await
|
||||
}
|
||||
|
||||
/// Try writing a buffer without waiting, returning how many bytes were written.
|
||||
pub fn try_write(&mut self, buf: &[u8]) -> Result<usize, Error> {
|
||||
self.tx.try_write(buf)
|
||||
}
|
||||
|
||||
/// Flush this output stream, ensuring that all intermediately buffered contents reach their destination.
|
||||
pub async fn flush(&mut self) -> Result<(), Error> {
|
||||
self.tx.flush().await
|
||||
@ -482,6 +487,29 @@ impl<'d, U: UarteInstance> BufferedUarteTx<'d, U> {
|
||||
.await
|
||||
}
|
||||
|
||||
/// Try writing a buffer without waiting, returning how many bytes were written.
|
||||
pub fn try_write(&mut self, buf: &[u8]) -> Result<usize, Error> {
|
||||
//trace!("poll_write: {:?}", buf.len());
|
||||
let s = U::buffered_state();
|
||||
let mut tx = unsafe { s.tx_buf.writer() };
|
||||
|
||||
let tx_buf = tx.push_slice();
|
||||
if tx_buf.is_empty() {
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
let n = min(tx_buf.len(), buf.len());
|
||||
tx_buf[..n].copy_from_slice(&buf[..n]);
|
||||
tx.push_done(n);
|
||||
|
||||
//trace!("poll_write: queued {:?}", n);
|
||||
|
||||
compiler_fence(Ordering::SeqCst);
|
||||
U::Interrupt::pend();
|
||||
|
||||
Ok(n)
|
||||
}
|
||||
|
||||
/// Flush this output stream, ensuring that all intermediately buffered contents reach their destination.
|
||||
pub async fn flush(&mut self) -> Result<(), Error> {
|
||||
poll_fn(move |cx| {
|
||||
|
@ -1,5 +1,6 @@
|
||||
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
||||
runner = "probe-rs run --chip nRF9160_xxAA"
|
||||
# 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"
|
||||
|
@ -8,13 +8,19 @@ license = "MIT OR Apache-2.0"
|
||||
embassy-executor = { version = "0.6.0", path = "../../embassy-executor", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] }
|
||||
embassy-time = { version = "0.3.2", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] }
|
||||
embassy-nrf = { version = "0.2.0", path = "../../embassy-nrf", features = ["defmt", "nrf9160-s", "time-driver-rtc1", "gpiote", "unstable-pac", "time"] }
|
||||
embassy-net-nrf91 = { version = "0.1.0", path = "../../embassy-net-nrf91", features = ["defmt"] }
|
||||
embassy-net = { version = "0.4.0", path = "../../embassy-net", features = ["defmt", "tcp", "proto-ipv4", "medium-ip"] }
|
||||
|
||||
defmt = "0.3"
|
||||
defmt-rtt = "0.4"
|
||||
|
||||
heapless = "0.8"
|
||||
cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] }
|
||||
cortex-m-rt = "0.7.0"
|
||||
panic-probe = { version = "0.3", features = ["print-defmt"] }
|
||||
static_cell = { version = "2" }
|
||||
embedded-io = "0.6.1"
|
||||
embedded-io-async = { version = "0.6.1", features = ["defmt-03"] }
|
||||
|
||||
[profile.release]
|
||||
debug = 2
|
||||
|
@ -1,5 +1,9 @@
|
||||
MEMORY
|
||||
{
|
||||
FLASH : ORIGIN = 0x00000000, LENGTH = 1024K
|
||||
RAM : ORIGIN = 0x20018000, LENGTH = 160K
|
||||
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));
|
||||
|
204
examples/nrf9160/src/bin/modem_tcp_client.rs
Normal file
204
examples/nrf9160/src/bin/modem_tcp_client.rs
Normal file
@ -0,0 +1,204 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use core::mem::MaybeUninit;
|
||||
use core::net::IpAddr;
|
||||
use core::ptr::addr_of_mut;
|
||||
use core::slice;
|
||||
use core::str::FromStr;
|
||||
|
||||
use defmt::{info, unwrap, warn};
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources};
|
||||
use embassy_net_nrf91::context::Status;
|
||||
use embassy_net_nrf91::{context, Runner, State, TraceBuffer, TraceReader};
|
||||
use embassy_nrf::buffered_uarte::{self, BufferedUarteTx};
|
||||
use embassy_nrf::gpio::{AnyPin, Level, Output, OutputDrive, Pin};
|
||||
use embassy_nrf::uarte::Baudrate;
|
||||
use embassy_nrf::{bind_interrupts, interrupt, peripherals, uarte};
|
||||
use embassy_time::{Duration, Timer};
|
||||
use embedded_io_async::Write;
|
||||
use heapless::Vec;
|
||||
use static_cell::StaticCell;
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
#[interrupt]
|
||||
fn IPC() {
|
||||
embassy_net_nrf91::on_ipc_irq();
|
||||
}
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
UARTE0_SPIM0_SPIS0_TWIM0_TWIS0 => buffered_uarte::InterruptHandler<peripherals::SERIAL0>;
|
||||
});
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn trace_task(mut uart: BufferedUarteTx<'static, peripherals::SERIAL0>, reader: TraceReader<'static>) -> ! {
|
||||
let mut rx = [0u8; 1024];
|
||||
loop {
|
||||
let n = reader.read(&mut rx[..]).await;
|
||||
unwrap!(uart.write_all(&rx[..n]).await);
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn modem_task(runner: Runner<'static>) -> ! {
|
||||
runner.run().await
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn net_task(stack: &'static Stack<embassy_net_nrf91::NetDriver<'static>>) -> ! {
|
||||
stack.run().await
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn control_task(
|
||||
control: &'static context::Control<'static>,
|
||||
config: context::Config<'static>,
|
||||
stack: &'static Stack<embassy_net_nrf91::NetDriver<'static>>,
|
||||
) {
|
||||
unwrap!(control.configure(&config).await);
|
||||
unwrap!(
|
||||
control
|
||||
.run(|status| {
|
||||
stack.set_config_v4(status_to_config(status));
|
||||
})
|
||||
.await
|
||||
);
|
||||
}
|
||||
|
||||
fn status_to_config(status: &Status) -> embassy_net::ConfigV4 {
|
||||
let Some(IpAddr::V4(addr)) = status.ip else {
|
||||
panic!("Unexpected IP address");
|
||||
};
|
||||
let addr = Ipv4Address(addr.octets());
|
||||
|
||||
let gateway = if let Some(IpAddr::V4(addr)) = status.gateway {
|
||||
Some(Ipv4Address(addr.octets()))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut dns_servers = Vec::new();
|
||||
for dns in status.dns.iter() {
|
||||
if let IpAddr::V4(ip) = dns {
|
||||
unwrap!(dns_servers.push(Ipv4Address(ip.octets())));
|
||||
}
|
||||
}
|
||||
|
||||
embassy_net::ConfigV4::Static(embassy_net::StaticConfigV4 {
|
||||
address: Ipv4Cidr::new(addr, 32),
|
||||
gateway,
|
||||
dns_servers,
|
||||
})
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn blink_task(pin: AnyPin) {
|
||||
let mut led = Output::new(pin, Level::Low, OutputDrive::Standard);
|
||||
loop {
|
||||
led.set_high();
|
||||
Timer::after_millis(1000).await;
|
||||
led.set_low();
|
||||
Timer::after_millis(1000).await;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
static __start_ipc: u8;
|
||||
static __end_ipc: u8;
|
||||
}
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(spawner: Spawner) {
|
||||
let p = embassy_nrf::init(Default::default());
|
||||
|
||||
info!("Hello World!");
|
||||
|
||||
unwrap!(spawner.spawn(blink_task(p.P0_02.degrade())));
|
||||
|
||||
let ipc_mem = unsafe {
|
||||
let ipc_start = &__start_ipc as *const u8 as *mut MaybeUninit<u8>;
|
||||
let ipc_end = &__end_ipc as *const u8 as *mut MaybeUninit<u8>;
|
||||
let ipc_len = ipc_end.offset_from(ipc_start) as usize;
|
||||
slice::from_raw_parts_mut(ipc_start, ipc_len)
|
||||
};
|
||||
|
||||
static mut TRACE_BUF: [u8; 4096] = [0u8; 4096];
|
||||
let mut config = uarte::Config::default();
|
||||
config.baudrate = Baudrate::BAUD1M;
|
||||
let uart = BufferedUarteTx::new(
|
||||
//let trace_uart = BufferedUarteTx::new(
|
||||
unsafe { peripherals::SERIAL0::steal() },
|
||||
Irqs,
|
||||
unsafe { peripherals::P0_01::steal() },
|
||||
//unsafe { peripherals::P0_14::steal() },
|
||||
config,
|
||||
unsafe { &mut *addr_of_mut!(TRACE_BUF) },
|
||||
);
|
||||
|
||||
static STATE: StaticCell<State> = StaticCell::new();
|
||||
static TRACE: StaticCell<TraceBuffer> = StaticCell::new();
|
||||
let (device, control, runner, tracer) =
|
||||
embassy_net_nrf91::new_with_trace(STATE.init(State::new()), ipc_mem, TRACE.init(TraceBuffer::new())).await;
|
||||
unwrap!(spawner.spawn(modem_task(runner)));
|
||||
unwrap!(spawner.spawn(trace_task(uart, tracer)));
|
||||
|
||||
let config = embassy_net::Config::default();
|
||||
|
||||
// Generate "random" seed. nRF91 has no RNG, TODO figure out something...
|
||||
let seed = 123456;
|
||||
|
||||
// Init network stack
|
||||
static RESOURCES: StaticCell<StackResources<2>> = StaticCell::new();
|
||||
static STACK: StaticCell<Stack<embassy_net_nrf91::NetDriver<'static>>> = StaticCell::new();
|
||||
let stack = &*STACK.init(Stack::new(
|
||||
device,
|
||||
config,
|
||||
RESOURCES.init(StackResources::<2>::new()),
|
||||
seed,
|
||||
));
|
||||
|
||||
unwrap!(spawner.spawn(net_task(stack)));
|
||||
|
||||
static CONTROL: StaticCell<context::Control<'static>> = StaticCell::new();
|
||||
let control = CONTROL.init(context::Control::new(control, 0).await);
|
||||
|
||||
unwrap!(spawner.spawn(control_task(
|
||||
control,
|
||||
context::Config {
|
||||
apn: b"iot.nat.es",
|
||||
auth_prot: context::AuthProt::Pap,
|
||||
auth: Some((b"orange", b"orange")),
|
||||
},
|
||||
stack
|
||||
)));
|
||||
|
||||
stack.wait_config_up().await;
|
||||
|
||||
let mut rx_buffer = [0; 4096];
|
||||
let mut tx_buffer = [0; 4096];
|
||||
loop {
|
||||
let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
||||
socket.set_timeout(Some(Duration::from_secs(10)));
|
||||
|
||||
info!("Connecting...");
|
||||
let host_addr = embassy_net::Ipv4Address::from_str("45.79.112.203").unwrap();
|
||||
if let Err(e) = socket.connect((host_addr, 4242)).await {
|
||||
warn!("connect error: {:?}", e);
|
||||
Timer::after_secs(10).await;
|
||||
continue;
|
||||
}
|
||||
info!("Connected to {:?}", socket.remote_endpoint());
|
||||
|
||||
let msg = b"Hello world!\n";
|
||||
for _ in 0..10 {
|
||||
if let Err(e) = socket.write_all(msg).await {
|
||||
warn!("write error: {:?}", e);
|
||||
break;
|
||||
}
|
||||
info!("txd: {}", core::str::from_utf8(msg).unwrap());
|
||||
Timer::after_secs(1).await;
|
||||
}
|
||||
Timer::after_secs(4).await;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user