Skip to content

Commit

Permalink
Bump embassy-sync to 0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasKoch committed Jun 17, 2024
1 parent 27ed97e commit 967d75f
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmt = { version = "^0.3", optional = true }

embedded-hal = "1.0"
embassy-time = "0.3"
embassy-sync = "0.5"
embassy-sync = "0.6"
embassy-futures = "0.1"

embedded-nal-async = { version = "0.7" }
Expand Down
3 changes: 2 additions & 1 deletion examples/rpi-pico/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ embassy-time = { version = "0.3", features = [
"defmt",
"defmt-timestamp-uptime",
] }
embassy-sync = { version = "0.5" }
embassy-sync = { version = "0.6" }
embassy-rp = { version = "0.1.0", features = [
"defmt",
"unstable-pac",
Expand Down Expand Up @@ -80,6 +80,7 @@ embassy-futures = { path = "../../../embassy/embassy-futures" }
embassy-executor = { path = "../../../embassy/embassy-executor" }
ublox-sockets = { path = "../../../ublox-sockets" }
no-std-net = { path = "../../../no-std-net" }
atat = { path = "../../../atat/atat" }

[profile.dev]
debug = 2
Expand Down
65 changes: 41 additions & 24 deletions examples/rpi-pico/src/bin/embassy-perf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,40 +61,57 @@ async fn main(spawner: Spawner) {

let p = embassy_rp::init(Default::default());

let rst = Output::new(p.PIN_26, Level::High);

let (tx_pin, rx_pin, rts_pin, cts_pin, uart) =
(p.PIN_24, p.PIN_25, p.PIN_23, p.PIN_22, p.UART1);

let tx_buf = &mut make_static!([0u8; 64])[..];
let rx_buf = &mut make_static!([0u8; 64])[..];
let mut config = uart::Config::default();
config.baudrate = 115200;
let uart = uart::BufferedUart::new_with_rtscts(
uart, Irqs, tx_pin, rx_pin, rts_pin, cts_pin, tx_buf, rx_buf, config,
let rst_pin = OutputOpenDrain::new(p.PIN_26.degrade(), Level::High);

static TX_BUF: StaticCell<[u8; 32]> = StaticCell::new();
static RX_BUF: StaticCell<[u8; 32]> = StaticCell::new();
let wifi_uart = uart::BufferedUart::new_with_rtscts(
p.UART1,
Irqs,
p.PIN_24,
p.PIN_25,
p.PIN_23,
p.PIN_22,
TX_BUF.init([0; 32]),
RX_BUF.init([0; 32]),
uart::Config::default(),
);
let (rx, tx) = uart.split();

let buffers = &*make_static!(atat::Buffers::new());
let (ingress, client) = buffers.split(
common::TxWrap(tx),
EdmDigester::default(),
atat::Config::new(),
static RESOURCES: StaticCell<Resources<CMD_BUF_SIZE, INGRESS_BUF_SIZE, URC_CAPACITY>> =
StaticCell::new();

let mut runner = Runner::new(
wifi_uart.split(),
RESOURCES.init(Resources::new()),
WifiConfig { rst_pin },
);
defmt::unwrap!(spawner.spawn(ingress_task(ingress, rx)));

let state = make_static!(State::new(client));
let (net_device, mut control, runner) = new(state, &buffers.urc_channel, rst).await;
static PPP_STATE: StaticCell<embassy_net_ppp::State<2, 2>> = StaticCell::new();
let net_device = runner.ppp_stack(PPP_STATE.init(embassy_net_ppp::State::new()));

defmt::unwrap!(spawner.spawn(wifi_task(runner)));
// Generate random seed
let seed = 0x0123_4567_89ab_cdef; // chosen by fair dice roll. guarenteed to be random.

// Init network stack
let stack = &*make_static!(UbloxStack::new(
static STACK: StaticCell<Stack<embassy_net_ppp::Device<'static>>> = StaticCell::new();
static STACK_RESOURCES: StaticCell<StackResources<6>> = StaticCell::new();

let stack = &*STACK.init(Stack::new(
net_device,
make_static!(StackResources::<4>::new()),
embassy_net::Config::default(),
STACK_RESOURCES.init(StackResources::new()),
seed,
));

defmt::unwrap!(spawner.spawn(net_task(stack)));
static CONTROL_RESOURCES: StaticCell<ControlResources> = StaticCell::new();
let mut control = runner.control(CONTROL_RESOURCES.init(ControlResources::new()), &stack);

spawner.spawn(net_task(stack)).unwrap();
spawner.spawn(ppp_task(runner, &stack)).unwrap();

stack.wait_config_up().await;

Timer::after(Duration::from_secs(1)).await;

loop {
match control.join_wpa2(WIFI_NETWORK, WIFI_PASSWORD).await {
Expand Down
81 changes: 61 additions & 20 deletions examples/rpi-pico/src/bin/embassy-smoltcp-ppp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]

#[cfg(not(feature = "ppp"))]
compile_error!("You must enable the `ppp` feature flag to build this example");
Expand All @@ -9,42 +10,66 @@ use defmt::*;
use embassy_executor::Spawner;
use embassy_net::tcp::TcpSocket;
use embassy_net::{Ipv4Address, Stack, StackResources};
use embassy_rp::gpio::{AnyPin, Level, Output, Pin};
use embassy_rp::gpio::{AnyPin, Level, Output, OutputOpenDrain, Pin};
use embassy_rp::peripherals::UART1;
use embassy_rp::uart::{BufferedInterruptHandler, BufferedUart};
use embassy_rp::uart::{BufferedInterruptHandler, BufferedUart, BufferedUartRx, BufferedUartTx};
use embassy_rp::{bind_interrupts, uart};
use embassy_time::{Duration, Timer};
use embedded_tls::Aes128GcmSha256;
use embedded_tls::TlsConfig;
use embedded_tls::TlsConnection;
use embedded_tls::TlsContext;
use embedded_tls::UnsecureProvider;
use embedded_tls::{Aes128GcmSha256, MaxFragmentLength};
use rand_chacha::rand_core::SeedableRng;
use rand_chacha::ChaCha8Rng;
use reqwless::headers::ContentType;
use reqwless::request::Request;
use reqwless::request::RequestBuilder as _;
use reqwless::response::Response;
use static_cell::StaticCell;
use ublox_short_range::asynch::{PPPRunner, Resources};
use ublox_short_range::asynch::control::ControlResources;
use ublox_short_range::asynch::{Resources, Runner};
use {defmt_rtt as _, panic_probe as _};

const CMD_BUF_SIZE: usize = 128;
const INGRESS_BUF_SIZE: usize = 512;
const URC_CAPACITY: usize = 2;

pub struct WifiConfig {
pub rst_pin: OutputOpenDrain<'static>,
}

impl<'a> ublox_short_range::WifiConfig<'a> for WifiConfig {
type ResetPin = OutputOpenDrain<'static>;

const PPP_CONFIG: embassy_net_ppp::Config<'a> = embassy_net_ppp::Config {
username: b"",
password: b"",
};

fn reset_pin(&mut self) -> Option<&mut Self::ResetPin> {
Some(&mut self.rst_pin)
}
}

#[embassy_executor::task]
async fn net_task(stack: &'static Stack<embassy_net_ppp::Device<'static>>) -> ! {
stack.run().await
}

#[embassy_executor::task]
async fn ppp_task(
mut runner: PPPRunner<'static, Output<'static>, INGRESS_BUF_SIZE, URC_CAPACITY>,
interface: BufferedUart<'static, UART1>,
mut runner: Runner<
'static,
BufferedUartRx<'static, UART1>,
BufferedUartTx<'static, UART1>,
WifiConfig,
INGRESS_BUF_SIZE,
URC_CAPACITY,
>,
stack: &'static embassy_net::Stack<embassy_net_ppp::Device<'static>>,
) -> ! {
runner.run(interface, stack).await
runner.run(stack).await
}

bind_interrupts!(struct Irqs {
Expand All @@ -55,7 +80,7 @@ bind_interrupts!(struct Irqs {
async fn main(spawner: Spawner) {
let p = embassy_rp::init(Default::default());

let rst = Output::new(p.PIN_26.degrade(), Level::High);
let rst_pin = OutputOpenDrain::new(p.PIN_26.degrade(), Level::High);

static TX_BUF: StaticCell<[u8; 32]> = StaticCell::new();
static RX_BUF: StaticCell<[u8; 32]> = StaticCell::new();
Expand All @@ -74,15 +99,21 @@ async fn main(spawner: Spawner) {
static RESOURCES: StaticCell<Resources<CMD_BUF_SIZE, INGRESS_BUF_SIZE, URC_CAPACITY>> =
StaticCell::new();

let (net_device, mut control, runner) =
ublox_short_range::asynch::new_ppp(RESOURCES.init(Resources::new()), rst);
let mut runner = Runner::new(
wifi_uart.split(),
RESOURCES.init(Resources::new()),
WifiConfig { rst_pin },
);

static PPP_STATE: StaticCell<embassy_net_ppp::State<2, 2>> = StaticCell::new();
let net_device = runner.ppp_stack(PPP_STATE.init(embassy_net_ppp::State::new()));

// Generate random seed
let seed = 0x0123_4567_89ab_cdef; // chosen by fair dice roll. guarenteed to be random.

// Init network stack
static STACK: StaticCell<Stack<embassy_net_ppp::Device<'static>>> = StaticCell::new();
static STACK_RESOURCES: StaticCell<StackResources<3>> = StaticCell::new();
static STACK_RESOURCES: StaticCell<StackResources<6>> = StaticCell::new();

let stack = &*STACK.init(Stack::new(
net_device,
Expand All @@ -91,19 +122,19 @@ async fn main(spawner: Spawner) {
seed,
));

static CONTROL_RESOURCES: StaticCell<ControlResources> = StaticCell::new();
let mut control = runner.control(CONTROL_RESOURCES.init(ControlResources::new()), &stack);

spawner.spawn(net_task(stack)).unwrap();
spawner.spawn(ppp_task(runner, wifi_uart, &stack)).unwrap();
spawner.spawn(ppp_task(runner, &stack)).unwrap();

stack.wait_config_up().await;

Timer::after(Duration::from_secs(1)).await;

control
.set_hostname("Factbird-duo-wifi-test")
.await
.unwrap();
control.set_hostname("Ublox-wifi-test").await.ok();

control.join_open("Test").await;
control.join_wpa2("MyAccessPoint", "12345678").await;

info!("We have network!");

Expand All @@ -129,7 +160,9 @@ async fn main(spawner: Spawner) {

let mut read_record_buffer = [0; 16384];
let mut write_record_buffer = [0; 16384];
let config = TlsConfig::new().with_server_name(hostname);
let config = TlsConfig::new()
// .with_max_fragment_length(MaxFragmentLength::Bits11)
.with_server_name(hostname);
let mut tls = TlsConnection::new(socket, &mut read_record_buffer, &mut write_record_buffer);

tls.open(TlsContext::new(
Expand All @@ -147,9 +180,17 @@ async fn main(spawner: Spawner) {
.build();
request.write(&mut tls).await.unwrap();

let mut rx_buf = [0; 4096];
let mut rx_buf = [0; 1024];
let mut body_buf = [0; 8192];
let response = Response::read(&mut tls, reqwless::request::Method::GET, &mut rx_buf)
.await
.unwrap();
info!("{=[u8]:a}", rx_buf);
let len = response
.body()
.reader()
.read_to_end(&mut body_buf)
.await
.unwrap();

info!("{=[u8]:a}", &body_buf[..len]);
}

0 comments on commit 967d75f

Please sign in to comment.