Skip to content

spi.rs needs work #222

@phansel

Description

@phansel

https://github.com/stm32-rs/stm32g4xx-hal/blob/main/src/spi.rs

  • embedded_hal_old has not been fully removed / replaced
  • hal_02 is still used in the spi example
  • little to no explanatory comments (compare: stm32h7xx-hal)
    • unclear what functions like set_bidi() actually do; do they convert it to full-duplex, or to half-duplex on a 3-wire interface?
  • same issue: many instances of names abbreviated beyond being comprehensible

So far, I haven't been able to correctly read+write to an SPI device from stm32g474 with this HAL yet.

  • Responses (on MISO) to writes (on MOSI) are either not captured by the device or are captured incorrectly.
  • Reads, when they don't crash, make little temporal sense
    • 0x22 on the bus -> reports as [u8; 3] as [0x0c, 0x0c, 0x0c].
  • With certain orders of operation, the microcontroller spontaneously panics.
  • transfer() does not work at all. SCK sits at idle the entire time.

The intuitive API (spi.read(), spi.write()) type system is also broken in a way I have never seen before. rustc gets confused by spi.read() and isn't sure if it's a trait of the ADC, I2C, or different types of SPI.
spi.read(&mut out_buf) has to be fully specified as stm32g4xx_hal::prelude::SpiBus::read to even compile, and even then, the SPI read behavior is unreliable.

error[E0034]: multiple applicable items in scope
   --> src/main.rs:147:9
    |
147 |     spi.read(&mut read_buf);
    |         ^^^^ multiple `read` found
    |
note: candidate #1 is defined in the trait `_embedded_hal_adc_OneShot`
   --> /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/embedded-hal-0.2.7/src/adc.rs:97:5
    |
97  |     fn read(&mut self, pin: &mut Pin) -> nb::Result<Word, Self::Error>;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: candidate #2 is defined in the trait `stm32g4xx_hal::prelude::I2c`
   --> /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/embedded-hal-1.0.0/src/i2c.rs:333:5
    |
333 |     fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error> {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: candidate #3 is defined in the trait `stm32g4xx_hal::prelude::SpiBus`
   --> /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/embedded-hal-1.0.0/src/spi.rs:446:5
    |
446 |     fn read(&mut self, words: &mut [Word]) -> Result<(), Self::Error>;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: candidate #4 is defined in an impl of the trait `_embedded_hal_spi_FullDuplex` for the type `Spi<SPI, PINS>`
help: disambiguate the method for candidate #1
    |
147 -     spi.read(&mut read_buf);
147 +     _embedded_hal_adc_OneShot::read(&mut spi, &mut read_buf);
    |
help: disambiguate the method for candidate #2
    |
147 -     spi.read(&mut read_buf);
147 +     stm32g4xx_hal::prelude::I2c::read(&mut spi, &mut read_buf);
    |
help: disambiguate the method for candidate #3
    |
147 -     spi.read(&mut read_buf);
147 +     stm32g4xx_hal::prelude::SpiBus::read(&mut spi, &mut read_buf);
    |
help: disambiguate the method for candidate #4
    |
147 -     spi.read(&mut read_buf);
147 +     _embedded_hal_spi_FullDuplex::read(&mut spi, &mut read_buf);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions