Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of SpiBus #118

Open
saosebastiao opened this issue Jan 4, 2025 · 2 comments
Open

Implementation of SpiBus #118

saosebastiao opened this issue Jan 4, 2025 · 2 comments

Comments

@saosebastiao
Copy link

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?

@Angelo13C
Copy link

The trait doesn't seem to be implemented for the struct

@David-OConnor
Copy link
Owner

Yea; it's not. The EH support in this lib is quite limited. I'd merge a PR though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants