diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-05-30 23:22:34 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-05-30 23:26:29 +0200 |
| commit | 3f35a8876ee65d030e32ab2444bc0abb29d88382 (patch) | |
| tree | 2b456e04f83b22cad28632034680e0227292a7f8 /examples/rp/src/bin | |
| parent | b3bbe5eb2d0f7ec6c8cae6e0486cb46a433fe11a (diff) | |
cyw43: adapt build to main embassy repo.
Diffstat (limited to 'examples/rp/src/bin')
| -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 |
3 files changed, 360 insertions, 0 deletions
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 | } | ||
