Skip to content
Snippets Groups Projects
Select Git revision
  • c1c9b6b58483716a9fe2bb5dcf9b1d3d59536130
  • main default protected
2 results

pb200_286.rs

Blame
  • user avatar
    david authored
    c1c9b6b5
    History
    pb200_286.rs 2.03 KiB
    extern crate panic_halt;
    
    use cortex_m::prelude::_embedded_hal_blocking_spi_Transfer;
    use stm32l4xx_hal::gpio::*;
    use stm32l4xx_hal::pac::*;
    use stm32l4xx_hal::rcc::Clocks;
    use stm32l4xx_hal::spi::Spi;
    
    use stm32l4xx_hal::rcc::*;
    use stm32l4xx_hal::{
        hal::spi::{Mode, Phase, Polarity},
        prelude::*,
    };
    
    pub fn get_connexion(
        clocks: Clocks,
        mut apb1r1:&mut APB1R1,
    
        spi3: SPI3,
        sclk: Pin<Alternate<PushPull, 6>, H8, 'C', 10>,
        miso: Pin<Alternate<PushPull, 6>, H8, 'C', 11>,
        mosi_dummy: Pin<Alternate<PushPull, 6>, H8, 'C', 12>,
    ) -> Spi<
        SPI3,
        (
            Pin<Alternate<PushPull, 6>, H8, 'C', 10>,
            Pin<Alternate<PushPull, 6>, H8, 'C', 11>,
            Pin<Alternate<PushPull, 6>, H8, 'C', 12>,
        ),
    > {
        Spi::spi3(
            spi3,
            (sclk, miso, mosi_dummy),
            Mode {
                phase: Phase::CaptureOnFirstTransition,
                polarity: Polarity::IdleLow,
            },
            1000.kHz(),
            clocks,
            &mut apb1r1,
        )
    }
    
    /// Retrieve luminosity data from a Pmod ALS: Ambient Light Sensor
    /// INPUT :
    ///          1: spi -> The SPI module with which you would like to communicate
    ///          2: cs -> The corresponding Chip Select for this SPI module
    ///          3: data -> The reference in which the data will be written
    /// OUTPUT :
    ///          NONE
    
    pub fn get_data(
        spi: &mut Spi<
            SPI3,
            (
                Pin<Alternate<PushPull, 6>, H8, 'C', 10>,
                Pin<Alternate<PushPull, 6>, H8, 'C', 11>,
                Pin<Alternate<PushPull, 6>, H8, 'C', 12>,
            ),
        >,
        cs: &mut Pin<Output<PushPull>, L8, 'B', 6>,
        luminosity: &mut i32,
    ) {
        // Set Chip Select to low to start data exchange
        cs.set_low();
    
        // Retrieve the data
        let mut buffer = [0x00, 0x00];
        spi.transfer(&mut buffer).unwrap();
    
        // Set Chip Select to high to stop data exchange
        cs.set_high();
    
        // Data is send in 2 bytes, with 3 leading and 4 trailing zeros
        let mut lux = buffer[0] << 3;
        lux |= buffer[1] >> 4;
    
        // Store Data in struct
        *luminosity = lux as i32;
    }