aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorkbleeke <[email protected]>2023-04-28 20:53:09 +0200
committerkbleeke <[email protected]>2023-05-01 16:34:30 +0200
commitb612976cc7e9fcce7b547b348adaaabe73c487d0 (patch)
tree243a126df97fd504a527b386a2dc81b08fdb45ab /examples
parent5659269c8fb2f7d03d4a903e4ad48c8268668f0a (diff)
add wifi scan example
Diffstat (limited to 'examples')
-rw-r--r--examples/rpi-pico-w/src/bin/tcp_server.rs (renamed from examples/rpi-pico-w/src/main.rs)5
-rw-r--r--examples/rpi-pico-w/src/bin/wifi_scan.rs81
2 files changed, 83 insertions, 3 deletions
diff --git a/examples/rpi-pico-w/src/main.rs b/examples/rpi-pico-w/src/bin/tcp_server.rs
index 944beaac0..036f79308 100644
--- a/examples/rpi-pico-w/src/main.rs
+++ b/examples/rpi-pico-w/src/bin/tcp_server.rs
@@ -48,9 +48,8 @@ async fn main(spawner: Spawner) {
48 48
49 let p = embassy_rp::init(Default::default()); 49 let p = embassy_rp::init(Default::default());
50 50
51 // Include the WiFi firmware and Country Locale Matrix (CLM) blobs. 51 let fw = include_bytes!("../../../../firmware/43439A0.bin");
52 let fw = include_bytes!("../../../firmware/43439A0.bin"); 52 let clm = include_bytes!("../../../../firmware/43439A0_clm.bin");
53 let clm = include_bytes!("../../../firmware/43439A0_clm.bin");
54 53
55 // To make flashing faster for development, you may want to flash the firmwares independently 54 // To make flashing faster for development, you may want to flash the firmwares independently
56 // at hardcoded addresses, instead of baking them into the program with `include_bytes!`: 55 // at hardcoded addresses, instead of baking them into the program with `include_bytes!`:
diff --git a/examples/rpi-pico-w/src/bin/wifi_scan.rs b/examples/rpi-pico-w/src/bin/wifi_scan.rs
new file mode 100644
index 000000000..da8fadfd8
--- /dev/null
+++ b/examples/rpi-pico-w/src/bin/wifi_scan.rs
@@ -0,0 +1,81 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4#![feature(async_fn_in_trait)]
5#![allow(incomplete_features)]
6
7use core::str;
8
9use cyw43_pio::PioSpi;
10use defmt::*;
11use embassy_executor::Spawner;
12use embassy_net::Stack;
13use embassy_rp::gpio::{Level, Output};
14use embassy_rp::peripherals::{DMA_CH0, PIN_23, PIN_25};
15use embassy_rp::pio::{Pio0, PioPeripheral, PioStateMachineInstance, Sm0};
16use static_cell::StaticCell;
17use {defmt_rtt as _, panic_probe as _};
18
19macro_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]
28async fn wifi_task(
29 runner: cyw43::Runner<
30 'static,
31 Output<'static, PIN_23>,
32 PioSpi<PIN_25, PioStateMachineInstance<Pio0, Sm0>, DMA_CH0>,
33 >,
34) -> ! {
35 runner.run().await
36}
37
38#[embassy_executor::task]
39async fn net_task(stack: &'static Stack<cyw43::NetDriver<'static>>) -> ! {
40 stack.run().await
41}
42
43#[embassy_executor::main]
44async fn main(spawner: Spawner) {
45 info!("Hello World!");
46
47 let p = embassy_rp::init(Default::default());
48
49 let fw = include_bytes!("../../../../firmware/43439A0.bin");
50 let clm = include_bytes!("../../../../firmware/43439A0_clm.bin");
51
52 // To make flashing faster for development, you may want to flash the firmwares independently
53 // at hardcoded addresses, instead of baking them into the program with `include_bytes!`:
54 // probe-rs-cli download 43439A0.bin --format bin --chip RP2040 --base-address 0x10100000
55 // probe-rs-cli download 43439A0_clm.bin --format bin --chip RP2040 --base-address 0x10140000
56 //let fw = unsafe { core::slice::from_raw_parts(0x10100000 as *const u8, 224190) };
57 //let clm = unsafe { core::slice::from_raw_parts(0x10140000 as *const u8, 4752) };
58
59 let pwr = Output::new(p.PIN_23, Level::Low);
60 let cs = Output::new(p.PIN_25, Level::High);
61
62 let (_, sm, _, _, _) = p.PIO0.split();
63 let dma = p.DMA_CH0;
64 let spi = PioSpi::new(sm, cs, p.PIN_24, p.PIN_29, dma);
65
66 let state = singleton!(cyw43::State::new());
67 let (_net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw).await;
68 unwrap!(spawner.spawn(wifi_task(runner)));
69
70 control.init(clm).await;
71 control
72 .set_power_management(cyw43::PowerManagementMode::PowerSave)
73 .await;
74
75 let mut scanner = control.scan().await;
76 while let Some(bss) = scanner.next().await {
77 if let Ok(ssid_str) = str::from_utf8(&bss.ssid) {
78 info!("scanned {} == {:x}", ssid_str, bss.bssid);
79 }
80 }
81}