diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-07-12 07:52:16 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-07-12 07:52:16 +0200 |
| commit | ce7353fba4e2a133087f0312ae47184aa180642e (patch) | |
| tree | 2209f0e2be099f90fdd9df8b5111610e11bae6ac /examples | |
| parent | f60407feb3c2b2f3a364bd075dee2840220a5314 (diff) | |
Hook up embassy-net. IT WORKS.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rpi-pico-w/Cargo.toml | 9 | ||||
| -rw-r--r-- | examples/rpi-pico-w/src/main.rs | 78 |
2 files changed, 84 insertions, 3 deletions
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" | |||
| 8 | cyw43 = { path = "../../", features = ["defmt"]} | 8 | cyw43 = { path = "../../", features = ["defmt"]} |
| 9 | embassy = { version = "0.1.0", features = ["defmt", "defmt-timestamp-uptime"] } | 9 | embassy = { version = "0.1.0", features = ["defmt", "defmt-timestamp-uptime"] } |
| 10 | embassy-rp = { version = "0.1.0", features = ["defmt", "unstable-traits", "nightly", "unstable-pac"] } | 10 | embassy-rp = { version = "0.1.0", features = ["defmt", "unstable-traits", "nightly", "unstable-pac"] } |
| 11 | embassy-net = { version = "0.1.0", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } | ||
| 11 | atomic-polyfill = "0.1.5" | 12 | atomic-polyfill = "0.1.5" |
| 12 | 13 | ||
| 13 | defmt = "0.3" | 14 | defmt = "0.3" |
| @@ -20,13 +21,21 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa | |||
| 20 | 21 | ||
| 21 | embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.8" } | 22 | embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.8" } |
| 22 | embedded-hal-async = { version = "0.1.0-alpha.1" } | 23 | embedded-hal-async = { version = "0.1.0-alpha.1" } |
| 24 | embedded-io = { version = "0.3.0", features = ["async", "defmt"] } | ||
| 25 | heapless = "0.7.15" | ||
| 23 | 26 | ||
| 24 | 27 | ||
| 25 | [patch.crates-io] | 28 | [patch.crates-io] |
| 26 | embassy = { git = "https://github.com/embassy-rs/embassy", rev = "5f43c1d37e9db847c7861fe0bd821db62edba9f6" } | 29 | embassy = { git = "https://github.com/embassy-rs/embassy", rev = "5f43c1d37e9db847c7861fe0bd821db62edba9f6" } |
| 27 | embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "5f43c1d37e9db847c7861fe0bd821db62edba9f6" } | 30 | embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "5f43c1d37e9db847c7861fe0bd821db62edba9f6" } |
| 31 | embassy-net = { git = "https://github.com/embassy-rs/embassy", rev = "5f43c1d37e9db847c7861fe0bd821db62edba9f6" } | ||
| 28 | #embassy = { path = "/home/dirbaio/embassy/embassy/embassy" } | 32 | #embassy = { path = "/home/dirbaio/embassy/embassy/embassy" } |
| 29 | #embassy-rp = { path = "/home/dirbaio/embassy/embassy/embassy-rp" } | 33 | #embassy-rp = { path = "/home/dirbaio/embassy/embassy/embassy-rp" } |
| 34 | #embassy-net = { path = "/home/dirbaio/embassy/embassy/embassy-net" } | ||
| 35 | #smoltcp = { path = "./smoltcp" } | ||
| 36 | |||
| 37 | #[patch."https://github.com/smoltcp-rs/smoltcp"] | ||
| 38 | #smoltcp = { path = "./smoltcp" } | ||
| 30 | 39 | ||
| 31 | [profile.dev] | 40 | [profile.dev] |
| 32 | debug = 2 | 41 | 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, *}; | |||
| 8 | use embassy::executor::Spawner; | 8 | use embassy::executor::Spawner; |
| 9 | use embassy::time::{Duration, Timer}; | 9 | use embassy::time::{Duration, Timer}; |
| 10 | use embassy::util::Forever; | 10 | use embassy::util::Forever; |
| 11 | use embassy_net::tcp::TcpSocket; | ||
| 12 | use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources}; | ||
| 11 | use embassy_rp::gpio::{Flex, Level, Output, Pin}; | 13 | use embassy_rp::gpio::{Flex, Level, Output, Pin}; |
| 12 | use embassy_rp::peripherals::{PIN_23, PIN_24, PIN_25, PIN_29}; | 14 | use embassy_rp::peripherals::{PIN_23, PIN_24, PIN_25, PIN_29}; |
| 13 | use embassy_rp::Peripherals; | 15 | use embassy_rp::Peripherals; |
| 16 | use embedded_io::asynch::{Read, Write}; | ||
| 17 | use heapless::Vec; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | 18 | use {defmt_rtt as _, panic_probe as _}; |
| 15 | 19 | ||
| 16 | macro_rules! forever { | 20 | macro_rules! forever { |
| @@ -26,6 +30,11 @@ async fn wifi_task(runner: cyw43::Runner<'static, PIN_23, PIN_25, PIN_29, PIN_24 | |||
| 26 | runner.run().await | 30 | runner.run().await |
| 27 | } | 31 | } |
| 28 | 32 | ||
| 33 | #[embassy::task] | ||
| 34 | async fn net_task(stack: &'static Stack<cyw43::NetDevice<'static>>) -> ! { | ||
| 35 | stack.run().await | ||
| 36 | } | ||
| 37 | |||
| 29 | #[embassy::main] | 38 | #[embassy::main] |
| 30 | async fn main(spawner: Spawner, p: Peripherals) { | 39 | async fn main(spawner: Spawner, p: Peripherals) { |
| 31 | info!("Hello World!"); | 40 | info!("Hello World!"); |
| @@ -45,8 +54,71 @@ async fn main(spawner: Spawner, p: Peripherals) { | |||
| 45 | 54 | ||
| 46 | spawner.spawn(wifi_task(runner)).unwrap(); | 55 | spawner.spawn(wifi_task(runner)).unwrap(); |
| 47 | 56 | ||
| 48 | control.init().await; | 57 | let net_device = control.init().await; |
| 58 | |||
| 59 | control.join_open("MikroTik-951589").await; | ||
| 60 | //control.join_wpa2("MikroTik-951589", "asdfasdfasdfasdf").await; | ||
| 61 | |||
| 62 | let config = embassy_net::ConfigStrategy::Dhcp; | ||
| 63 | //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { | ||
| 64 | // address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24), | ||
| 65 | // dns_servers: Vec::new(), | ||
| 66 | // gateway: Some(Ipv4Address::new(192, 168, 69, 1)), | ||
| 67 | //}); | ||
| 68 | |||
| 69 | // Generate random seed | ||
| 70 | let seed = 0x0123_4567_89ab_cdef; // chosen by fair dice roll. guarenteed to be random. | ||
| 71 | |||
| 72 | // Init network stack | ||
| 73 | let stack = &*forever!(Stack::new( | ||
| 74 | net_device, | ||
| 75 | config, | ||
| 76 | forever!(StackResources::<1, 2, 8>::new()), | ||
| 77 | seed | ||
| 78 | )); | ||
| 79 | |||
| 80 | unwrap!(spawner.spawn(net_task(stack))); | ||
| 81 | |||
| 82 | // And now we can use it! | ||
| 83 | |||
| 84 | let mut rx_buffer = [0; 4096]; | ||
| 85 | let mut tx_buffer = [0; 4096]; | ||
| 86 | let mut buf = [0; 4096]; | ||
| 87 | |||
| 88 | loop { | ||
| 89 | let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); | ||
| 90 | socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); | ||
| 91 | |||
| 92 | info!("Listening on TCP:1234..."); | ||
| 93 | if let Err(e) = socket.accept(1234).await { | ||
| 94 | warn!("accept error: {:?}", e); | ||
| 95 | continue; | ||
| 96 | } | ||
| 97 | |||
| 98 | info!("Received connection from {:?}", socket.remote_endpoint()); | ||
| 99 | |||
| 100 | loop { | ||
| 101 | let n = match socket.read(&mut buf).await { | ||
| 102 | Ok(0) => { | ||
| 103 | warn!("read EOF"); | ||
| 104 | break; | ||
| 105 | } | ||
| 106 | Ok(n) => n, | ||
| 107 | Err(e) => { | ||
| 108 | warn!("read error: {:?}", e); | ||
| 109 | break; | ||
| 110 | } | ||
| 111 | }; | ||
| 112 | |||
| 113 | info!("rxd {:02x}", &buf[..n]); | ||
| 49 | 114 | ||
| 50 | //control.join_open("MikroTik-951589").await; | 115 | match socket.write_all(&buf[..n]).await { |
| 51 | control.join_wpa2("MikroTik-951589", "fasdfasdfasdf").await; | 116 | Ok(()) => {} |
| 117 | Err(e) => { | ||
| 118 | warn!("write error: {:?}", e); | ||
| 119 | break; | ||
| 120 | } | ||
| 121 | }; | ||
| 122 | } | ||
| 123 | } | ||
| 52 | } | 124 | } |
