ryan-summers / shared-bus-rtic

Provides macros and type definitions for using shared-bus in an RTIC application
MIT License
16 stars 3 forks source link

the trait `hal::prelude::_embedded_hal_spi_FullDuplex<u8>` is not implemented for `&shared_bus_rtic::CommonBus<...` #1

Closed BlinkyStitt closed 3 years ago

BlinkyStitt commented 3 years ago

Hello! This crate looks like exactly what I need!

I have this crate working with radio-sx127x, but I'm having trouble with embedded-sdmmc.

If I pass my_spi directly instead of using shared_spi_manager.acquire(), embedded_sdmmc is happy, but I need the bus to be shared. Here's my (very novice) repo: https://github.com/WyseNynja/smart-compass

And here's the relevant code in main.rs:

let my_spi = hal::spi_master(
    &mut clocks,
    24.mhz(),
    device.SERCOM4,
    &mut device.PM,
    pins.sck,
    pins.mosi,
    pins.miso,
    &mut pins.port,
);

let shared_spi_manager = shared_bus_rtic::new!(my_spi, SPIMaster);

// setup the radio (works)
let radio_spi = shared_spi_manager.acquire();

let my_radio = network::Radio::new(
    radio_spi,
    rfm95_cs,
    rfm95_busy_fake,
    rfm95_int,
    rfm95_rst,
    delay,
);

// setup sd card (does NOT work)
let sd_spi = shared_spi_manager.acquire();

let sd_spi = embedded_sdmmc::SdMmcSpi::new(sd_spi, sdcard_cs);
$ cargo check
    Checking smart-compass-v1 v0.1.0 (/Users/bwstitt/code/rust-embed/smart-compass-v1)
error[E0277]: the trait bound `&shared_bus_rtic::CommonBus<hal::sercom::SPIMaster4<hal::sercom::Sercom4Pad0<hal::gpio::Pa12<hal::gpio::PfD>>, hal::sercom::Sercom4Pad2<hal::gpio::Pb10<hal::gpio::PfD>>, hal::sercom::Sercom4Pad3<hal::gpio::Pb11<hal::gpio::PfD>>>>: hal::prelude::_embedded_hal_spi_FullDuplex<u8>` is not satisfied
   --> src/main.rs:170:22
    |
170 |         let sd_spi = embedded_sdmmc::SdMmcSpi::new(sd_spi, sdcard_cs);
    |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `hal::prelude::_embedded_hal_spi_FullDuplex<u8>` is not implemented for `&shared_bus_rtic::CommonBus<hal::sercom::SPIMaster4<hal::sercom::Sercom4Pad0<hal::gpio::Pa12<hal::gpio::PfD>>, hal::sercom::Sercom4Pad2<hal::gpio::Pb10<hal::gpio::PfD>>, hal::sercom::Sercom4Pad3<hal::gpio::Pb11<hal::gpio::PfD>>>>`
    |
    = note: required by `embedded_sdmmc::sdmmc::SdMmcSpi::<SPI, CS>::new`

error[E0277]: the trait bound `&shared_bus_rtic::CommonBus<hal::sercom::SPIMaster4<hal::sercom::Sercom4Pad0<hal::gpio::Pa12<hal::gpio::PfD>>, hal::sercom::Sercom4Pad2<hal::gpio::Pb10<hal::gpio::PfD>>, hal::sercom::Sercom4Pad3<hal::gpio::Pb11<hal::gpio::PfD>>>>: hal::prelude::_embedded_hal_spi_FullDuplex<u8>` is not satisfied
   --> src/main.rs:170:22
    |
170 |         let sd_spi = embedded_sdmmc::SdMmcSpi::new(sd_spi, sdcard_cs);
    |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `hal::prelude::_embedded_hal_spi_FullDuplex<u8>` is not implemented for `&shared_bus_rtic::CommonBus<hal::sercom::SPIMaster4<hal::sercom::Sercom4Pad0<hal::gpio::Pa12<hal::gpio::PfD>>, hal::sercom::Sercom4Pad2<hal::gpio::Pb10<hal::gpio::PfD>>, hal::sercom::Sercom4Pad3<hal::gpio::Pb11<hal::gpio::PfD>>>>`
    | 
   ::: /Users/bwstitt/.cargo/registry/src/github.com-1ecc6299db9ec823/embedded-sdmmc-0.3.0/src/sdmmc.rs:20:10
    |
20  |     SPI: embedded_hal::spi::FullDuplex<u8>,
    |          --------------------------------- required by this bound in `embedded_sdmmc::sdmmc::SdMmcSpi`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
error: could not compile `smart-compass-v1`.

To learn more, run the command again with --verbose.

The radio only requires Spi: _embedded_hal_blocking_spi_Transfer<u8, Error = SpiError> + _embedded_hal_blocking_spi_Write<u8, Error = SpiError> and not FullDuplex.

I checked Cargo.lock and I only have one version of embedded-hal (0.2.4).

Possibly related: I'm currently using a feather_m0 (because it's what I have), but I'm thinking it would be better to use a STM32F3DISCOVERY like they use in the rust embedded book. I don't think this is the problem though because it is working without shared-bus.

ryan-summers commented 3 years ago

This occurs because we rely on a custom re-implementation of the embedded-hal traits for the "proxy-bus" CommonBus<Bus>. Right now, it only implements e-h::blocking::spi::Transfer and e-g::blocking::spi::Write. We can likely extend it to also implement e-h::spi::FullDuplex as well.

I've been using this primarily for i2c devices, where the landscape of e-h traits is much simpler at the moment. Let me see if I can get a branch going

ryan-summers commented 3 years ago

@WyseNynja Would you mind trying out the feature/spi-full-duplex branch?

Edit your Cargo.toml

[dependencies.shared-bus-rtic]
git = "https://github.com/ryan-summers/shared-bus-rtic"
branch = "feature/spi-full-duplex"

Let me know if that fixes things for you. If it does, we can merge and cut a new release.

BlinkyStitt commented 3 years ago

Thank you! I think it is working. At the very least it compiles. I don't currently have hardware setup to test this though. I will in the next few weeks.