diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-05-30 21:54:32 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-05-30 21:54:32 +0000 |
| commit | 3f90620343a5413423da8b84008cf468ea957d54 (patch) | |
| tree | 2b456e04f83b22cad28632034680e0227292a7f8 /examples | |
| parent | f5d0d28ac3cfcb74eaa59bbe984b7969a0743724 (diff) | |
| parent | 3f35a8876ee65d030e32ab2444bc0abb29d88382 (diff) | |
Merge pull request #1512 from embassy-rs/cyw43
Merge cyw43 into main repo
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rp/Cargo.toml | 3 | ||||
| -rw-r--r-- | examples/rp/src/bin/wifi_ap_tcp_server.rs | 139 | ||||
| -rw-r--r-- | examples/rp/src/bin/wifi_scan.rs | 75 | ||||
| -rw-r--r-- | examples/rp/src/bin/wifi_tcp_server.rs | 146 |
4 files changed, 363 insertions, 0 deletions
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml index ffeb69f15..f77377a6f 100644 --- a/examples/rp/Cargo.toml +++ b/examples/rp/Cargo.toml | |||
| @@ -19,6 +19,8 @@ embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["ti | |||
| 19 | lora-phy = { version = "1" } | 19 | lora-phy = { version = "1" } |
| 20 | lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"] } | 20 | lorawan-device = { version = "0.10.0", default-features = false, features = ["async", "external-lora-phy"] } |
| 21 | lorawan = { version = "0.7.3", default-features = false, features = ["default-crypto"] } | 21 | lorawan = { version = "0.7.3", default-features = false, features = ["default-crypto"] } |
| 22 | cyw43 = { path = "../../cyw43", features = ["defmt", "firmware-logs"] } | ||
| 23 | cyw43-pio = { path = "../../cyw43-pio", features = ["defmt", "overclock"] } | ||
| 22 | 24 | ||
| 23 | defmt = "0.3" | 25 | defmt = "0.3" |
| 24 | defmt-rtt = "0.4" | 26 | defmt-rtt = "0.4" |
| @@ -36,6 +38,7 @@ st7789 = "0.6.1" | |||
| 36 | display-interface = "0.4.1" | 38 | display-interface = "0.4.1" |
| 37 | byte-slice-cast = { version = "1.2.0", default-features = false } | 39 | byte-slice-cast = { version = "1.2.0", default-features = false } |
| 38 | smart-leds = "0.3.0" | 40 | smart-leds = "0.3.0" |
| 41 | heapless = "0.7.15" | ||
| 39 | 42 | ||
| 40 | embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.10" } | 43 | embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.10" } |
| 41 | embedded-hal-async = "0.2.0-alpha.1" | 44 | embedded-hal-async = "0.2.0-alpha.1" |
diff --git a/examples/rp/src/bin/wifi_ap_tcp_server.rs b/examples/rp/src/bin/wifi_ap_tcp_server.rs new file mode 100644 index 000000000..15264524e --- /dev/null +++ b/examples/rp/src/bin/wifi_ap_tcp_server.rs | |||
| @@ -0,0 +1,139 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | #![feature(async_fn_in_trait)] | ||
| 5 | #![allow(incomplete_features)] | ||
| 6 | |||
| 7 | use core::str::from_utf8; | ||
| 8 | |||
| 9 | use cyw43_pio::PioSpi; | ||
| 10 | use defmt::*; | ||
| 11 | use embassy_executor::Spawner; | ||
| 12 | use embassy_net::tcp::TcpSocket; | ||
| 13 | use embassy_net::{Config, Stack, StackResources}; | ||
| 14 | use embassy_rp::gpio::{Level, Output}; | ||
| 15 | use embassy_rp::peripherals::{DMA_CH0, PIN_23, PIN_25, PIO0}; | ||
| 16 | use embassy_rp::pio::Pio; | ||
| 17 | use embassy_time::Duration; | ||
| 18 | use embedded_io::asynch::Write; | ||
| 19 | use static_cell::StaticCell; | ||
| 20 | use {defmt_rtt as _, panic_probe as _}; | ||
| 21 | |||
| 22 | macro_rules! singleton { | ||
| 23 | ($val:expr) => {{ | ||
| 24 | type T = impl Sized; | ||
| 25 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); | ||
| 26 | STATIC_CELL.init_with(move || $val) | ||
| 27 | }}; | ||
| 28 | } | ||
| 29 | |||
| 30 | #[embassy_executor::task] | ||
| 31 | async fn wifi_task( | ||
| 32 | runner: cyw43::Runner<'static, Output<'static, PIN_23>, PioSpi<'static, PIN_25, PIO0, 0, DMA_CH0>>, | ||
| 33 | ) -> ! { | ||
| 34 | runner.run().await | ||
| 35 | } | ||
| 36 | |||
| 37 | #[embassy_executor::task] | ||
| 38 | async fn net_task(stack: &'static Stack<cyw43::NetDriver<'static>>) -> ! { | ||
| 39 | stack.run().await | ||
| 40 | } | ||
| 41 | |||
| 42 | #[embassy_executor::main] | ||
| 43 | async fn main(spawner: Spawner) { | ||
| 44 | info!("Hello World!"); | ||
| 45 | |||
| 46 | let p = embassy_rp::init(Default::default()); | ||
| 47 | |||
| 48 | let fw = include_bytes!("../../../../cyw43-firmware/43439A0.bin"); | ||
| 49 | let clm = include_bytes!("../../../../cyw43-firmware/43439A0_clm.bin"); | ||
| 50 | |||
| 51 | // To make flashing faster for development, you may want to flash the firmwares independently | ||
| 52 | // at hardcoded addresses, instead of baking them into the program with `include_bytes!`: | ||
| 53 | // probe-rs-cli download 43439A0.bin --format bin --chip RP2040 --base-address 0x10100000 | ||
| 54 | // probe-rs-cli download 43439A0_clm.bin --format bin --chip RP2040 --base-address 0x10140000 | ||
| 55 | //let fw = unsafe { core::slice::from_raw_parts(0x10100000 as *const u8, 224190) }; | ||
| 56 | //let clm = unsafe { core::slice::from_raw_parts(0x10140000 as *const u8, 4752) }; | ||
| 57 | |||
| 58 | let pwr = Output::new(p.PIN_23, Level::Low); | ||
| 59 | let cs = Output::new(p.PIN_25, Level::High); | ||
| 60 | let mut pio = Pio::new(p.PIO0); | ||
| 61 | let spi = PioSpi::new(&mut pio.common, pio.sm0, pio.irq0, cs, p.PIN_24, p.PIN_29, p.DMA_CH0); | ||
| 62 | |||
| 63 | let state = singleton!(cyw43::State::new()); | ||
| 64 | let (net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw).await; | ||
| 65 | unwrap!(spawner.spawn(wifi_task(runner))); | ||
| 66 | |||
| 67 | control.init(clm).await; | ||
| 68 | control | ||
| 69 | .set_power_management(cyw43::PowerManagementMode::PowerSave) | ||
| 70 | .await; | ||
| 71 | |||
| 72 | // Use a link-local address for communication without DHCP server | ||
| 73 | let config = Config::Static(embassy_net::StaticConfig { | ||
| 74 | address: embassy_net::Ipv4Cidr::new(embassy_net::Ipv4Address::new(169, 254, 1, 1), 16), | ||
| 75 | dns_servers: heapless::Vec::new(), | ||
| 76 | gateway: None, | ||
| 77 | }); | ||
| 78 | |||
| 79 | // Generate random seed | ||
| 80 | let seed = 0x0123_4567_89ab_cdef; // chosen by fair dice roll. guarenteed to be random. | ||
| 81 | |||
| 82 | // Init network stack | ||
| 83 | let stack = &*singleton!(Stack::new( | ||
| 84 | net_device, | ||
| 85 | config, | ||
| 86 | singleton!(StackResources::<2>::new()), | ||
| 87 | seed | ||
| 88 | )); | ||
| 89 | |||
| 90 | unwrap!(spawner.spawn(net_task(stack))); | ||
| 91 | |||
| 92 | //control.start_ap_open("cyw43", 5).await; | ||
| 93 | control.start_ap_wpa2("cyw43", "password", 5).await; | ||
| 94 | |||
| 95 | // And now we can use it! | ||
| 96 | |||
| 97 | let mut rx_buffer = [0; 4096]; | ||
| 98 | let mut tx_buffer = [0; 4096]; | ||
| 99 | let mut buf = [0; 4096]; | ||
| 100 | |||
| 101 | loop { | ||
| 102 | let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); | ||
| 103 | socket.set_timeout(Some(Duration::from_secs(10))); | ||
| 104 | |||
| 105 | control.gpio_set(0, false).await; | ||
| 106 | info!("Listening on TCP:1234..."); | ||
| 107 | if let Err(e) = socket.accept(1234).await { | ||
| 108 | warn!("accept error: {:?}", e); | ||
| 109 | continue; | ||
| 110 | } | ||
| 111 | |||
| 112 | info!("Received connection from {:?}", socket.remote_endpoint()); | ||
| 113 | control.gpio_set(0, true).await; | ||
| 114 | |||
| 115 | loop { | ||
| 116 | let n = match socket.read(&mut buf).await { | ||
| 117 | Ok(0) => { | ||
| 118 | warn!("read EOF"); | ||
| 119 | break; | ||
| 120 | } | ||
| 121 | Ok(n) => n, | ||
| 122 | Err(e) => { | ||
| 123 | warn!("read error: {:?}", e); | ||
| 124 | break; | ||
| 125 | } | ||
| 126 | }; | ||
| 127 | |||
| 128 | info!("rxd {}", from_utf8(&buf[..n]).unwrap()); | ||
| 129 | |||
| 130 | match socket.write_all(&buf[..n]).await { | ||
| 131 | Ok(()) => {} | ||
| 132 | Err(e) => { | ||
| 133 | warn!("write error: {:?}", e); | ||
| 134 | break; | ||
| 135 | } | ||
| 136 | }; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } | ||
diff --git a/examples/rp/src/bin/wifi_scan.rs b/examples/rp/src/bin/wifi_scan.rs new file mode 100644 index 000000000..aa5e5a399 --- /dev/null +++ b/examples/rp/src/bin/wifi_scan.rs | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | #![feature(async_fn_in_trait)] | ||
| 5 | #![allow(incomplete_features)] | ||
| 6 | |||
| 7 | use core::str; | ||
| 8 | |||
| 9 | use cyw43_pio::PioSpi; | ||
| 10 | use defmt::*; | ||
| 11 | use embassy_executor::Spawner; | ||
| 12 | use embassy_net::Stack; | ||
| 13 | use embassy_rp::gpio::{Level, Output}; | ||
| 14 | use embassy_rp::peripherals::{DMA_CH0, PIN_23, PIN_25, PIO0}; | ||
| 15 | use embassy_rp::pio::Pio; | ||
| 16 | use static_cell::StaticCell; | ||
| 17 | use {defmt_rtt as _, panic_probe as _}; | ||
| 18 | |||
| 19 | macro_rules! singleton { | ||
| 20 | ($val:expr) => {{ | ||
| 21 | type T = impl Sized; | ||
| 22 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); | ||
| 23 | STATIC_CELL.init_with(move || $val) | ||
| 24 | }}; | ||
| 25 | } | ||
| 26 | |||
| 27 | #[embassy_executor::task] | ||
| 28 | async fn wifi_task( | ||
| 29 | runner: cyw43::Runner<'static, Output<'static, PIN_23>, PioSpi<'static, PIN_25, PIO0, 0, DMA_CH0>>, | ||
| 30 | ) -> ! { | ||
| 31 | runner.run().await | ||
| 32 | } | ||
| 33 | |||
| 34 | #[embassy_executor::task] | ||
| 35 | async fn net_task(stack: &'static Stack<cyw43::NetDriver<'static>>) -> ! { | ||
| 36 | stack.run().await | ||
| 37 | } | ||
| 38 | |||
| 39 | #[embassy_executor::main] | ||
| 40 | async fn main(spawner: Spawner) { | ||
| 41 | info!("Hello World!"); | ||
| 42 | |||
| 43 | let p = embassy_rp::init(Default::default()); | ||
| 44 | |||
| 45 | let fw = include_bytes!("../../../../cyw43-firmware/43439A0.bin"); | ||
| 46 | let clm = include_bytes!("../../../../cyw43-firmware/43439A0_clm.bin"); | ||
| 47 | |||
| 48 | // To make flashing faster for development, you may want to flash the firmwares independently | ||
| 49 | // at hardcoded addresses, instead of baking them into the program with `include_bytes!`: | ||
| 50 | // probe-rs-cli download 43439A0.bin --format bin --chip RP2040 --base-address 0x10100000 | ||
| 51 | // probe-rs-cli download 43439A0_clm.bin --format bin --chip RP2040 --base-address 0x10140000 | ||
| 52 | //let fw = unsafe { core::slice::from_raw_parts(0x10100000 as *const u8, 224190) }; | ||
| 53 | //let clm = unsafe { core::slice::from_raw_parts(0x10140000 as *const u8, 4752) }; | ||
| 54 | |||
| 55 | let pwr = Output::new(p.PIN_23, Level::Low); | ||
| 56 | let cs = Output::new(p.PIN_25, Level::High); | ||
| 57 | let mut pio = Pio::new(p.PIO0); | ||
| 58 | let spi = PioSpi::new(&mut pio.common, pio.sm0, pio.irq0, cs, p.PIN_24, p.PIN_29, p.DMA_CH0); | ||
| 59 | |||
| 60 | let state = singleton!(cyw43::State::new()); | ||
| 61 | let (_net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw).await; | ||
| 62 | unwrap!(spawner.spawn(wifi_task(runner))); | ||
| 63 | |||
| 64 | control.init(clm).await; | ||
| 65 | control | ||
| 66 | .set_power_management(cyw43::PowerManagementMode::PowerSave) | ||
| 67 | .await; | ||
| 68 | |||
| 69 | let mut scanner = control.scan().await; | ||
| 70 | while let Some(bss) = scanner.next().await { | ||
| 71 | if let Ok(ssid_str) = str::from_utf8(&bss.ssid) { | ||
| 72 | info!("scanned {} == {:x}", ssid_str, bss.bssid); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
diff --git a/examples/rp/src/bin/wifi_tcp_server.rs b/examples/rp/src/bin/wifi_tcp_server.rs new file mode 100644 index 000000000..eafa25f68 --- /dev/null +++ b/examples/rp/src/bin/wifi_tcp_server.rs | |||
| @@ -0,0 +1,146 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | #![feature(async_fn_in_trait)] | ||
| 5 | #![allow(incomplete_features)] | ||
| 6 | |||
| 7 | use core::str::from_utf8; | ||
| 8 | |||
| 9 | use cyw43_pio::PioSpi; | ||
| 10 | use defmt::*; | ||
| 11 | use embassy_executor::Spawner; | ||
| 12 | use embassy_net::tcp::TcpSocket; | ||
| 13 | use embassy_net::{Config, Stack, StackResources}; | ||
| 14 | use embassy_rp::gpio::{Level, Output}; | ||
| 15 | use embassy_rp::peripherals::{DMA_CH0, PIN_23, PIN_25, PIO0}; | ||
| 16 | use embassy_rp::pio::Pio; | ||
| 17 | use embassy_time::Duration; | ||
| 18 | use embedded_io::asynch::Write; | ||
| 19 | use static_cell::StaticCell; | ||
| 20 | use {defmt_rtt as _, panic_probe as _}; | ||
| 21 | |||
| 22 | macro_rules! singleton { | ||
| 23 | ($val:expr) => {{ | ||
| 24 | type T = impl Sized; | ||
| 25 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); | ||
| 26 | STATIC_CELL.init_with(move || $val) | ||
| 27 | }}; | ||
| 28 | } | ||
| 29 | |||
| 30 | #[embassy_executor::task] | ||
| 31 | async fn wifi_task( | ||
| 32 | runner: cyw43::Runner<'static, Output<'static, PIN_23>, PioSpi<'static, PIN_25, PIO0, 0, DMA_CH0>>, | ||
| 33 | ) -> ! { | ||
| 34 | runner.run().await | ||
| 35 | } | ||
| 36 | |||
| 37 | #[embassy_executor::task] | ||
| 38 | async fn net_task(stack: &'static Stack<cyw43::NetDriver<'static>>) -> ! { | ||
| 39 | stack.run().await | ||
| 40 | } | ||
| 41 | |||
| 42 | #[embassy_executor::main] | ||
| 43 | async fn main(spawner: Spawner) { | ||
| 44 | info!("Hello World!"); | ||
| 45 | |||
| 46 | let p = embassy_rp::init(Default::default()); | ||
| 47 | |||
| 48 | let fw = include_bytes!("../../../../cyw43-firmware/43439A0.bin"); | ||
| 49 | let clm = include_bytes!("../../../../cyw43-firmware/43439A0_clm.bin"); | ||
| 50 | |||
| 51 | // To make flashing faster for development, you may want to flash the firmwares independently | ||
| 52 | // at hardcoded addresses, instead of baking them into the program with `include_bytes!`: | ||
| 53 | // probe-rs-cli download 43439A0.bin --format bin --chip RP2040 --base-address 0x10100000 | ||
| 54 | // probe-rs-cli download 43439A0_clm.bin --format bin --chip RP2040 --base-address 0x10140000 | ||
| 55 | //let fw = unsafe { core::slice::from_raw_parts(0x10100000 as *const u8, 224190) }; | ||
| 56 | //let clm = unsafe { core::slice::from_raw_parts(0x10140000 as *const u8, 4752) }; | ||
| 57 | |||
| 58 | let pwr = Output::new(p.PIN_23, Level::Low); | ||
| 59 | let cs = Output::new(p.PIN_25, Level::High); | ||
| 60 | let mut pio = Pio::new(p.PIO0); | ||
| 61 | let spi = PioSpi::new(&mut pio.common, pio.sm0, pio.irq0, cs, p.PIN_24, p.PIN_29, p.DMA_CH0); | ||
| 62 | |||
| 63 | let state = singleton!(cyw43::State::new()); | ||
| 64 | let (net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw).await; | ||
| 65 | unwrap!(spawner.spawn(wifi_task(runner))); | ||
| 66 | |||
| 67 | control.init(clm).await; | ||
| 68 | control | ||
| 69 | .set_power_management(cyw43::PowerManagementMode::PowerSave) | ||
| 70 | .await; | ||
| 71 | |||
| 72 | let config = Config::Dhcp(Default::default()); | ||
| 73 | //let config = embassy_net::Config::Static(embassy_net::Config { | ||
| 74 | // address: Ipv4Cidr::new(Ipv4Address::new(192, 168, 69, 2), 24), | ||
| 75 | // dns_servers: Vec::new(), | ||
| 76 | // gateway: Some(Ipv4Address::new(192, 168, 69, 1)), | ||
| 77 | //}); | ||
| 78 | |||
| 79 | // Generate random seed | ||
| 80 | let seed = 0x0123_4567_89ab_cdef; // chosen by fair dice roll. guarenteed to be random. | ||
| 81 | |||
| 82 | // Init network stack | ||
| 83 | let stack = &*singleton!(Stack::new( | ||
| 84 | net_device, | ||
| 85 | config, | ||
| 86 | singleton!(StackResources::<2>::new()), | ||
| 87 | seed | ||
| 88 | )); | ||
| 89 | |||
| 90 | unwrap!(spawner.spawn(net_task(stack))); | ||
| 91 | |||
| 92 | loop { | ||
| 93 | //control.join_open(env!("WIFI_NETWORK")).await; | ||
| 94 | match control.join_wpa2(env!("WIFI_NETWORK"), env!("WIFI_PASSWORD")).await { | ||
| 95 | Ok(_) => break, | ||
| 96 | Err(err) => { | ||
| 97 | info!("join failed with status={}", err.status); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | // And now we can use it! | ||
| 103 | |||
| 104 | let mut rx_buffer = [0; 4096]; | ||
| 105 | let mut tx_buffer = [0; 4096]; | ||
| 106 | let mut buf = [0; 4096]; | ||
| 107 | |||
| 108 | loop { | ||
| 109 | let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); | ||
| 110 | socket.set_timeout(Some(Duration::from_secs(10))); | ||
| 111 | |||
| 112 | control.gpio_set(0, false).await; | ||
| 113 | info!("Listening on TCP:1234..."); | ||
| 114 | if let Err(e) = socket.accept(1234).await { | ||
| 115 | warn!("accept error: {:?}", e); | ||
| 116 | continue; | ||
| 117 | } | ||
| 118 | |||
| 119 | info!("Received connection from {:?}", socket.remote_endpoint()); | ||
| 120 | control.gpio_set(0, true).await; | ||
| 121 | |||
| 122 | loop { | ||
| 123 | let n = match socket.read(&mut buf).await { | ||
| 124 | Ok(0) => { | ||
| 125 | warn!("read EOF"); | ||
| 126 | break; | ||
| 127 | } | ||
| 128 | Ok(n) => n, | ||
| 129 | Err(e) => { | ||
| 130 | warn!("read error: {:?}", e); | ||
| 131 | break; | ||
| 132 | } | ||
| 133 | }; | ||
| 134 | |||
| 135 | info!("rxd {}", from_utf8(&buf[..n]).unwrap()); | ||
| 136 | |||
| 137 | match socket.write_all(&buf[..n]).await { | ||
| 138 | Ok(()) => {} | ||
| 139 | Err(e) => { | ||
| 140 | warn!("write error: {:?}", e); | ||
| 141 | break; | ||
| 142 | } | ||
| 143 | }; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | } | ||
