rp-rs / rp-hal-boards

Board Support Packages for RP2040 based PCBs
199 stars 82 forks source link

rp2040/pico W pins to talk to CYW43? #38

Closed brandonros closed 1 year ago

brandonros commented 1 year ago

From the datasheet:

image

Which I think would translate to this in code:

 let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
    let mut spi: spi_pio::Spi<'_, _, _, _, _, _, 8> = spi_pio::Spi::new(
        (&mut pio, sm0),
        (pins.gpio24, pins.gpio24, pins.gpio29), // (miso, mosi, sclk)
        embedded_hal::spi::MODE_0,
        16u32.MHz(),
        clocks.peripheral_clock.freq(),
    )
    .ok()
    .unwrap();
    let spi_cs = pins.gpio25.into_push_pull_output();

but these pins are "not found"?

error[E0609]: no field `gpio24` on type `rp_pico::Pins`
  --> src/main.rs:52:15
   |
52 |         (pins.gpio24, pins.gpio24, pins.gpio29), // (miso, mosi, sclk)
   |               ^^^^^^ help: a field with a similar name exists: `gpio2`

error[E0609]: no field `gpio24` on type `rp_pico::Pins`
  --> src/main.rs:52:28
   |
52 |         (pins.gpio24, pins.gpio24, pins.gpio29), // (miso, mosi, sclk)
   |                            ^^^^^^ help: a field with a similar name exists: `gpio2`

error[E0609]: no field `gpio29` on type `rp_pico::Pins`
  --> src/main.rs:52:41
   |
52 |         (pins.gpio24, pins.gpio24, pins.gpio29), // (miso, mosi, sclk)
   |                                         ^^^^^^ help: a field with a similar name exists: `gpio2`

error[E0609]: no field `gpio25` on type `rp_pico::Pins`
  --> src/main.rs:59:23
   |
59 |     let spi_cs = pins.gpio25.into_push_pull_output();
   |                       ^^^^^^ help: a field with a similar name exists: `gpio2`

Does something need to be updated to support rp-picow instead of just plain rp-pico?

brandonros commented 1 year ago

I see now the names are:

let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
    let mut spi: spi_pio::Spi<'_, _, _, _, _, _, 8> = spi_pio::Spi::new(
        (&mut pio, sm0),
        (pins.vbus_detect, pins.vbus_detect, pins.voltage_monitor), // (miso, mosi, sclk)
        embedded_hal::spi::MODE_0,
        16u32.MHz(),
        clocks.peripheral_clock.freq(),
    )
    .ok()
    .unwrap();
    let spi_cs = pins.led.into_push_pull_output();

But it seems like MOSI + MISO being the same pin isn't supported?

 $ cargo build
   Compiling my-cyw43-driver v0.1.0 (/Users/brandon/Desktop/my-cyw43-driver)
warning: unused variable: `spi_cs`
  --> src/main.rs:59:9
   |
59 |     let spi_cs = pins.led.into_push_pull_output();
   |         ^^^^^^ help: if this is intentional, prefix it with an underscore: `_spi_cs`
   |
   = note: `#[warn(unused_variables)]` on by default

error[E0382]: use of moved value: `pins.vbus_detect`
  --> src/main.rs:52:28
   |
52 |         (pins.vbus_detect, pins.vbus_detect, pins.voltage_monitor), // (miso, mosi, sclk)
   |          ----------------  ^^^^^^^^^^^^^^^^ value used here after move
   |          |
   |          value moved here
   |
   = note: move occurs because `pins.vbus_detect` has type `rp_pico::rp2040_hal::gpio::Pin<Gpio24, rp_pico::rp2040_hal::gpio::Disabled<rp_pico::rp2040_hal::gpio::PullDown>>`, which does not implement the `Copy` trait

Will keep seeing if I can workaround it/if I am understanding it correctly

jannic commented 1 year ago

AFAIK the hardware SPI implementation of the rp2040 doesn't support this mode. Two possible solutions: a) easy but slow, bit-bang SPI. https://github.com/rp-rs/rp-hal/pull/380/files#diff-2f2f98170fea596b486eaa0345625a8eea400be05746cf90ffa1df2577114b3b b) use PIO: https://github.com/embassy-rs/embassy/blob/main/cyw43-pio/src/lib.rs