From ce7353fba4e2a133087f0312ae47184aa180642e Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Tue, 12 Jul 2022 07:52:16 +0200 Subject: Hook up embassy-net. IT WORKS. --- examples/rpi-pico-w/Cargo.toml | 9 +++++ examples/rpi-pico-w/src/main.rs | 78 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 84 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/rpi-pico-w/Cargo.toml b/examples/rpi-pico-w/Cargo.toml index 8dbcb20d4..9e1d75470 100644 --- a/examples/rpi-pico-w/Cargo.toml +++ b/examples/rpi-pico-w/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" cyw43 = { path = "../../", features = ["defmt"]} embassy = { version = "0.1.0", features = ["defmt", "defmt-timestamp-uptime"] } embassy-rp = { version = "0.1.0", features = ["defmt", "unstable-traits", "nightly", "unstable-pac"] } +embassy-net = { version = "0.1.0", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } atomic-polyfill = "0.1.5" defmt = "0.3" @@ -20,13 +21,21 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.8" } embedded-hal-async = { version = "0.1.0-alpha.1" } +embedded-io = { version = "0.3.0", features = ["async", "defmt"] } +heapless = "0.7.15" [patch.crates-io] embassy = { git = "https://github.com/embassy-rs/embassy", rev = "5f43c1d37e9db847c7861fe0bd821db62edba9f6" } embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "5f43c1d37e9db847c7861fe0bd821db62edba9f6" } +embassy-net = { git = "https://github.com/embassy-rs/embassy", rev = "5f43c1d37e9db847c7861fe0bd821db62edba9f6" } #embassy = { path = "/home/dirbaio/embassy/embassy/embassy" } #embassy-rp = { path = "/home/dirbaio/embassy/embassy/embassy-rp" } +#embassy-net = { path = "/home/dirbaio/embassy/embassy/embassy-net" } +#smoltcp = { path = "./smoltcp" } + +#[patch."https://github.com/smoltcp-rs/smoltcp"] +#smoltcp = { path = "./smoltcp" } [profile.dev] debug = 2 diff --git a/examples/rpi-pico-w/src/main.rs b/examples/rpi-pico-w/src/main.rs index 6d1614147..e08ee8e95 100644 --- a/examples/rpi-pico-w/src/main.rs +++ b/examples/rpi-pico-w/src/main.rs @@ -8,9 +8,13 @@ use defmt::{assert, assert_eq, panic, *}; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy::util::Forever; +use embassy_net::tcp::TcpSocket; +use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources}; use embassy_rp::gpio::{Flex, Level, Output, Pin}; use embassy_rp::peripherals::{PIN_23, PIN_24, PIN_25, PIN_29}; use embassy_rp::Peripherals; +use embedded_io::asynch::{Read, Write}; +use heapless::Vec; use {defmt_rtt as _, panic_probe as _}; macro_rules! forever { @@ -26,6 +30,11 @@ async fn wifi_task(runner: cyw43::Runner<'static, PIN_23, PIN_25, PIN_29, PIN_24 runner.run().await } +#[embassy::task] +async fn net_task(stack: &'static Stack>) -> ! { + stack.run().await +} + #[embassy::main] async fn main(spawner: Spawner, p: Peripherals) { info!("Hello World!"); @@ -45,8 +54,71 @@ async fn main(spawner: Spawner, p: Peripherals) { spawner.spawn(wifi_task(runner)).unwrap(); - control.init().await; + let net_device = control.init().await; + + control.join_open("MikroTik-951589").await; + //control.join_wpa2("MikroTik-951589", "asdfasdfasdfasdf").await; + + let config = embassy_net::ConfigStrategy::Dhcp; + //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { + // address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24), + // dns_servers: Vec::new(), + // gateway: Some(Ipv4Address::new(192, 168, 69, 1)), + //}); + + // Generate random seed + let seed = 0x0123_4567_89ab_cdef; // chosen by fair dice roll. guarenteed to be random. + + // Init network stack + let stack = &*forever!(Stack::new( + net_device, + config, + forever!(StackResources::<1, 2, 8>::new()), + seed + )); + + unwrap!(spawner.spawn(net_task(stack))); + + // And now we can use it! + + let mut rx_buffer = [0; 4096]; + let mut tx_buffer = [0; 4096]; + let mut buf = [0; 4096]; + + loop { + let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); + socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); + + info!("Listening on TCP:1234..."); + if let Err(e) = socket.accept(1234).await { + warn!("accept error: {:?}", e); + continue; + } + + info!("Received connection from {:?}", socket.remote_endpoint()); + + loop { + let n = match socket.read(&mut buf).await { + Ok(0) => { + warn!("read EOF"); + break; + } + Ok(n) => n, + Err(e) => { + warn!("read error: {:?}", e); + break; + } + }; + + info!("rxd {:02x}", &buf[..n]); - //control.join_open("MikroTik-951589").await; - control.join_wpa2("MikroTik-951589", "fasdfasdfasdf").await; + match socket.write_all(&buf[..n]).await { + Ok(()) => {} + Err(e) => { + warn!("write error: {:?}", e); + break; + } + }; + } + } } -- cgit