A personal rust firmware for the Badger 2040 W
1use cyw43::Control;
2use cyw43_pio::PioSpi;
3use defmt::unwrap;
4use embassy_executor::Spawner;
5use embassy_net_wiznet::Device;
6use embassy_rp::bind_interrupts;
7use embassy_rp::gpio::{Level, Output};
8use embassy_rp::peripherals::{DMA_CH0, PIO0};
9use embassy_rp::peripherals::{PIN_23, PIN_24, PIN_25, PIN_29};
10use embassy_rp::pio::{InterruptHandler, Pio};
11use static_cell::StaticCell;
12
13bind_interrupts!(struct Irqs {
14 PIO0_IRQ_0 => InterruptHandler<PIO0>;
15});
16
17#[embassy_executor::task]
18async fn cyw43_task(
19 runner: cyw43::Runner<'static, Output<'static>, PioSpi<'static, PIO0, 0, DMA_CH0>>,
20) -> ! {
21 runner.run().await
22}
23
24pub async fn setup_cyw43<'a>(
25 pio0: PIO0,
26 p_23: PIN_23,
27 p_24: PIN_24,
28 p_25: PIN_25,
29 p_29: PIN_29,
30 dma_ch0: DMA_CH0,
31 spawner: Spawner,
32) -> (Device<'a>, Control<'a>) {
33 let fw = include_bytes!("../cyw43-firmware/43439A0.bin");
34 let clm = include_bytes!("../cyw43-firmware/43439A0_clm.bin");
35 // let btfw = include_bytes!("../cyw43-firmware/43439A0_btfw.bin");
36
37 // To make flashing faster for development, you may want to flash the firmwares independently
38 // at hardcoded addresses, instead of baking them into the program with `include_bytes!`:
39 // probe-rs download 43439A0.bin --binary-format bin --chip RP2040 --base-address 0x10100000
40 // probe-rs download 43439A0_clm.bin --binary-format bin --chip RP2040 --base-address 0x10140000
41 // probe-rs download 43439A0_btfw.bin --binary-format bin --chip RP2040 --base-address 0x10141400
42 // let fw = unsafe { core::slice::from_raw_parts(0x10100000 as *const u8, 224190) };
43 // let clm = unsafe { core::slice::from_raw_parts(0x10140000 as *const u8, 4752) };
44 // let btfw = unsafe { core::slice::from_raw_parts(0x10141400 as *const u8, 6164) };
45
46 let pwr = Output::new(p_23, Level::Low);
47 let cs = Output::new(p_25, Level::High);
48 let mut pio = Pio::new(pio0, Irqs);
49 let spi = PioSpi::new(&mut pio.common, pio.sm0, pio.irq0, cs, p_24, p_29, dma_ch0);
50 // let input = Input::new(p_29, Pull::Up);
51
52 static STATE: StaticCell<cyw43::State> = StaticCell::new();
53 let state = STATE.init(cyw43::State::new());
54 let (net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw).await;
55 unwrap!(spawner.spawn(cyw43_task(runner)));
56
57 control.init(clm).await;
58 control
59 .set_power_management(cyw43::PowerManagementMode::PowerSave)
60 .await;
61 (net_device, control)
62}