From 45e474726fb3c0eef3a07b8ef9493bfa520fca39 Mon Sep 17 00:00:00 2001 From: Daniel Bulant Date: Mon, 11 May 2026 13:15:04 +0200 Subject: [PATCH] debug work --- pico/src/bin/logger.rs | 36 ++++++++++++++++ pico/src/bin/scan.rs | 93 ++++++++++++++++++++++++++++++++++++++++++ pico/src/net.rs | 70 +++++++++++++++++++++++++------ 3 files changed, 186 insertions(+), 13 deletions(-) create mode 100644 pico/src/bin/logger.rs create mode 100644 pico/src/bin/scan.rs diff --git a/pico/src/bin/logger.rs b/pico/src/bin/logger.rs new file mode 100644 index 0000000..ed2333e --- /dev/null +++ b/pico/src/bin/logger.rs @@ -0,0 +1,36 @@ +//! This example shows how to use USB (Universal Serial Bus) in the RP2040 chip. +//! +//! This creates the possibility to send log::info/warn/error/debug! to USB serial port. + +#![no_std] +#![no_main] + +use embassy_executor::Spawner; +use embassy_rp::bind_interrupts; +use embassy_rp::peripherals::USB; +use embassy_rp::usb::{Driver, InterruptHandler}; +use embassy_time::Timer; +use {defmt_rtt as _, panic_probe as _}; + +bind_interrupts!(struct Irqs { + USBCTRL_IRQ => InterruptHandler; +}); + +#[embassy_executor::task] +async fn logger_task(driver: Driver<'static, USB>) { + embassy_usb_logger::run!(1024, log::LevelFilter::Info, driver); +} + +#[embassy_executor::main] +async fn main(spawner: Spawner) { + let p = embassy_rp::init(Default::default()); + let driver = Driver::new(p.USB, Irqs); + spawner.spawn(logger_task(driver).unwrap()); + + let mut counter = 0; + loop { + counter += 1; + log::info!("Tick {}", counter); + Timer::after_secs(1).await; + } +} diff --git a/pico/src/bin/scan.rs b/pico/src/bin/scan.rs new file mode 100644 index 0000000..6c992bc --- /dev/null +++ b/pico/src/bin/scan.rs @@ -0,0 +1,93 @@ +//! This example uses the RP Pico W board Wifi chip (cyw43). +//! Scans Wifi for ssid names. + +#![no_std] +#![no_main] +#![allow(async_fn_in_trait)] + +use core::str; + +use cyw43::aligned_bytes; +use cyw43_pio::{DEFAULT_CLOCK_DIVIDER, PioSpi}; +use defmt::*; +use embassy_executor::Spawner; +use embassy_rp::gpio::{Level, Output}; +use embassy_rp::peripherals::{DMA_CH0, PIO0, USB}; +use embassy_rp::pio::{InterruptHandler, Pio}; +use embassy_rp::{bind_interrupts, dma, usb}; +use embassy_time::Timer; +use static_cell::StaticCell; +use {defmt_rtt as _, panic_probe as _}; + +bind_interrupts!(struct Irqs { + PIO0_IRQ_0 => InterruptHandler; + USBCTRL_IRQ => usb::InterruptHandler; + DMA_IRQ_0 => dma::InterruptHandler; +}); + +#[embassy_executor::task] +async fn cyw43_task( + runner: cyw43::Runner<'static, cyw43::SpiBus, PioSpi<'static, PIO0, 0>>>, +) -> ! { + runner.run().await +} +#[embassy_executor::task] +async fn logger_task(usb: embassy_rp::Peri<'static, embassy_rp::peripherals::USB>) { + let driver = embassy_rp::usb::Driver::new(usb, Irqs); + + embassy_usb_logger::run!(1024, log::LevelFilter::Info, driver); +} + +#[embassy_executor::main] +async fn main(spawner: Spawner) { + let p = embassy_rp::init(Default::default()); + spawner.spawn(unwrap!(logger_task(p.USB))); + + info!("Hello World!"); + + let fw = aligned_bytes!("../../firmware/43439A0.bin"); + let clm = aligned_bytes!("../../firmware/43439A0_clm.bin"); + let nvram = aligned_bytes!("../../firmware/nvram_rp2040.bin"); + + // To make flashing faster for development, you may want to flash the firmwares independently + // at hardcoded addresses, instead of baking them into the program with `include_bytes!`: + // probe-rs download 43439A0.bin --binary-format bin --chip RP2040 --base-address 0x10100000 + // probe-rs download 43439A0_clm.bin --binary-format bin --chip RP2040 --base-address 0x10140000 + //let fw = unsafe { core::slice::from_raw_parts(0x10100000 as *const u8, 230321) }; + //let clm = unsafe { core::slice::from_raw_parts(0x10140000 as *const u8, 4752) }; + + let pwr = Output::new(p.PIN_23, Level::Low); + let cs = Output::new(p.PIN_25, Level::High); + let mut pio = Pio::new(p.PIO0, Irqs); + let spi = PioSpi::new( + &mut pio.common, + pio.sm0, + DEFAULT_CLOCK_DIVIDER, + pio.irq0, + cs, + p.PIN_24, + p.PIN_29, + dma::Channel::new(p.DMA_CH0, Irqs), + ); + + static STATE: StaticCell = StaticCell::new(); + let state = STATE.init(cyw43::State::new()); + let (_net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw, nvram).await; + spawner.spawn(unwrap!(cyw43_task(runner))); + + control.init(clm).await; + control + .set_power_management(cyw43::PowerManagementMode::PowerSave) + .await; + + loop { + info!("scan loop"); + let mut scanner = control.scan(Default::default()).await; + while let Some(bss) = scanner.next().await { + if let Ok(ssid_str) = str::from_utf8(&bss.ssid) { + info!("scanned {} == {:x}", ssid_str, bss.bssid); + } + } + Timer::after_millis(1000).await; + } +} diff --git a/pico/src/net.rs b/pico/src/net.rs index c0fd38d..d7dd4f9 100644 --- a/pico/src/net.rs +++ b/pico/src/net.rs @@ -9,7 +9,7 @@ use crate::{ Irqs, TARGET_IP, TARGET_PORT, WIFI_NETWORK, WIFI_PASSWORD, main_loop, tcp_read_loop, tcp_write_loop, }; -use cyw43::{JoinOptions, aligned_bytes}; +use cyw43::{JoinOptions, ScanOptions, aligned_bytes}; use cyw43_pio::{DEFAULT_CLOCK_DIVIDER, PioSpi}; use defmt::*; use embassy_executor::Spawner; @@ -19,7 +19,7 @@ use embassy_rp::gpio::{Level, Output}; use embassy_rp::peripherals::{DMA_CH0, PIN_23, PIN_24, PIN_25, PIN_29, PIO0}; use embassy_rp::pio::Pio; use embassy_rp::{Peri, dma}; -use embassy_time::Duration; +use embassy_time::{Duration, Timer}; use static_cell::StaticCell; use ufmt::uwrite; @@ -78,7 +78,7 @@ pub async fn network_setup_task( control.init(clm).await; control - .set_power_management(cyw43::PowerManagementMode::PowerSave) + .set_power_management(cyw43::PowerManagementMode::Performance) .await; static RESOURCES: StaticCell> = StaticCell::new(); @@ -89,27 +89,71 @@ pub async fn network_setup_task( rng.next_u64(), ); spawner.spawn(unwrap!(net_task(runner))); - uwrite!(SCREEN_BUFFER.lock().await.line1(), "con"); + Timer::after_millis(1000).await; + uwrite!(SCREEN_BUFFER.lock().await.line1(), "con - scan"); + + info!("scan started"); + + let mut opts = ScanOptions::default(); + opts.scan_type = cyw43::ScanType::Active; + let mut scanner = control.scan(opts).await; + + while let Some(bss) = scanner.next().await { + let mut buf = SCREEN_BUFFER.lock().await; + if let Ok(ssid_str) = str::from_utf8(&bss.ssid) { + info!("scanned {} == {:x}", ssid_str, bss.bssid); + uwrite!(buf.line1(), "{}", ssid_str); + // uwrite!(buf.line2(), "{:x}", bss.bssid); + } else { + info!("scanned {:x}", bss.bssid); + uwrite!(buf.line1(), "not-utf8"); + // uwrite!(buf.line2(), "{:x}", bss.bssid); + } + drop(buf); + Timer::after_millis(500).await; + } + + info!("scan completed"); + + uwrite!(SCREEN_BUFFER.lock().await.line1(), "scan end."); + Timer::after_millis(500).await; + drop(scanner); while let Err(err) = control .join(WIFI_NETWORK, JoinOptions::new(WIFI_PASSWORD.as_bytes())) .await { - let num = match err { - cyw43::JoinError::AuthenticationFailure => 200, - cyw43::JoinError::JoinFailure(e) => e, - cyw43::JoinError::NetworkNotFound => 201, + let mut buf = SCREEN_BUFFER.lock().await; + buf.clear(); + match err { + cyw43::JoinError::AuthenticationFailure => { + uwrite!(buf.line1(), "join failure"); + uwrite!(buf.line2(), "bad password"); + info!("authentication failure"); + } + cyw43::JoinError::JoinFailure(e) => { + uwrite!(buf.line1(), "join failure"); + uwrite!(buf.line2(), "code {}", e); + info!("join failure: {}", e); + } + cyw43::JoinError::NetworkNotFound => { + uwrite!(buf.line1(), "join failure"); + uwrite!(buf.line2(), "wifi not found"); + info!("network not found"); + } }; - uwrite!(SCREEN_BUFFER.lock().await.line1(), "join {}", num); + Timer::after_millis(200).await; } - uwrite!(SCREEN_BUFFER.lock().await.line1(), "link."); - stack.wait_link_up().await; - let mut buf = SCREEN_BUFFER.lock().await; buf.clear(); - uwrite!(buf.line1(), "dhcp."); + uwrite!(buf.line1(), "link."); drop(buf); + + stack.wait_link_up().await; + + uwrite!(SCREEN_BUFFER.lock().await.line1(), "dhcp."); + stack.wait_config_up().await; let cfg = wait_for_config(stack).await; let local_addr = cfg.address.address();