stm32-hal icon indicating copy to clipboard operation
stm32-hal copied to clipboard

Implementation of SpiBus

Open saosebastiao opened this issue 9 months ago • 2 comments

Hello, I'm trying to use the embedded_hal traits to represent features of a custom board, and I'm running into a problem with the SPI implementation:

use embedded_hal::{spi::MODE_1};
use embedded_hal_bus::spi::{ExclusiveDevice, NoDelay};
use stm32_hal2::{
    gpio::{Pin, PinMode, Port }, 
    pac, 
    spi::{BaudRate, Spi, SpiConfig },
};
use super::encoder::As5047p;
use {defmt_rtt as _, panic_probe as _};

pub struct MyBoard {
    pub led1: Pin,
    pub led2: Pin,
    pub encoder: As5047p<ExclusiveDevice<Spi<pac::SPI1>, Pin>>,
}

impl MyBoard {
    pub fn new(dp: pac::Peripherals) -> Self {
        // Configure LED pins
        let mut led1 = Pin::new(Port::B, 14, PinMode::Output);
        let mut led2 = Pin::new(Port::C, 6, PinMode::Output);
        
        // Set LEDs high (they are active low)
        led1.set_high();
        led2.set_high();

        // SPI1 pins with correct alternate functions
        let sck = Pin::new(Port::A, 5, PinMode::Alt(5));  // AF5 for SPI1_SCK
        let mosi = Pin::new(Port::A, 7, PinMode::Alt(5)); // AF5 for SPI1_MOSI
        let miso = Pin::new(Port::B, 4, PinMode::Alt(5)); // AF5 for SPI1_MISO
        let mut cs = Pin::new(Port::B, 2, PinMode::Output);
        cs.set_high();

        // Configure SPI
        let spi_cfg = SpiConfig {
            mode: MODE_1,
            // `SpiConfig::default` is mode 0, full duplex, with software CS.
            ..Default::default()
        };
        let spi = Spi::new(
            dp.SPI1,
            spi_cfg,
            BaudRate::Div32, // Eg 80Mhz apb clock / 32 = 2.5Mhz SPI clock.
        );

        // Create the exclusive device for the encoder
        let spi_device = ExclusiveDevice::new_no_delay(spi, cs).unwrap();
        let encoder = As5047p::new(spi_device);

        Self {
            led1,
            led2,
            encoder,
        }
    }
}

I'm getting the following errors:

^^^^^^^^^^ the trait `SpiBus` is not implemented for `stm32_hal2::spi::Spi<stm32_hal2::pac::SPI1>`, which is required by `ExclusiveDevice<stm32_hal2::spi::Spi<stm32_hal2::pac::SPI1>, stm32_hal2::gpio::Pin, NoDelay>: SpiDevice`

I can't seem to figure out how to get around this error. Is there an implementation of the embedded_hal SpiBus trait for the Spi struct?

saosebastiao avatar Jan 04 '25 01:01 saosebastiao