|
| 1 | +//! This example shows off the FrameTransaction mode of hardware chip select functionality. |
| 2 | +//! |
| 3 | +//! For more docs, see https://docs.rs/stm32h7xx-hal/latest/stm32h7xx_hal/spi/index.html |
| 4 | +//! |
| 5 | +
|
| 6 | +#![deny(warnings)] |
| 7 | +#![no_main] |
| 8 | +#![no_std] |
| 9 | + |
| 10 | +use cortex_m_rt::entry; |
| 11 | +use cortex_m_semihosting::debug; |
| 12 | +use embedded_hal::spi::{Operation, SpiDevice}; |
| 13 | +mod utilities; |
| 14 | +use spi::Spi; |
| 15 | +use stm32h5xx_hal::{ |
| 16 | + pac, |
| 17 | + prelude::*, |
| 18 | + spi::{self, CommunicationMode}, |
| 19 | +}; |
| 20 | + |
| 21 | +use log::info; |
| 22 | + |
| 23 | +#[entry] |
| 24 | +fn main() -> ! { |
| 25 | + utilities::logger::init(); |
| 26 | + // let cp = cortex_m::Peripherals::take().unwrap(); |
| 27 | + let dp = pac::Peripherals::take().unwrap(); |
| 28 | + |
| 29 | + // Constrain and Freeze power |
| 30 | + info!("Setup PWR... "); |
| 31 | + let pwr = dp.PWR.constrain(); |
| 32 | + let pwrcfg = pwr.freeze(); |
| 33 | + |
| 34 | + // Constrain and Freeze clock |
| 35 | + info!("Setup RCC... "); |
| 36 | + let rcc = dp.RCC.constrain(); |
| 37 | + let ccdr = rcc |
| 38 | + .sys_ck(192.MHz()) |
| 39 | + .pll1_q_ck(64.MHz()) |
| 40 | + .freeze(pwrcfg, &dp.SBS); |
| 41 | + |
| 42 | + // Acquire the GPIOB peripheral. This also enables the clock for |
| 43 | + // GPIOB in the RCC register. |
| 44 | + let gpiob = dp.GPIOB.split(ccdr.peripheral.GPIOB); |
| 45 | + |
| 46 | + let sck = gpiob.pb13.into_alternate(); |
| 47 | + let miso = gpiob.pb14.into_alternate(); |
| 48 | + let mosi = gpiob.pb15.into_alternate(); |
| 49 | + // Because we want to use the hardware chip select, we need to provide that too |
| 50 | + let hcs = gpiob.pb4.into_alternate(); |
| 51 | + |
| 52 | + info!(""); |
| 53 | + info!("stm32h5xx-hal example - SPI Frame Transactions"); |
| 54 | + info!(""); |
| 55 | + |
| 56 | + // Initialise the SPI peripheral. |
| 57 | + let mut spi: Spi<_, u8> = dp.SPI2.spi( |
| 58 | + // Give ownership of the pins |
| 59 | + (sck, miso, mosi, hcs), |
| 60 | + // Create a config with the hardware chip select given |
| 61 | + spi::Config::new(spi::MODE_0) |
| 62 | + // Put 1 us idle time between every word sent |
| 63 | + .inter_word_delay(0.000001) |
| 64 | + // Specify that we use the hardware cs |
| 65 | + .hardware_cs(spi::HardwareCS { |
| 66 | + // See the docs of the HardwareCSMode to see what the different modes do |
| 67 | + mode: spi::HardwareCSMode::FrameTransaction, |
| 68 | + // Put 1 us between the CS being asserted and the first clock |
| 69 | + assertion_delay: 0.000001, |
| 70 | + // Our CS should be high when not active and low when asserted |
| 71 | + polarity: spi::Polarity::IdleHigh, |
| 72 | + }) |
| 73 | + .communication_mode(CommunicationMode::SimplexTransmitter), |
| 74 | + 1.MHz(), |
| 75 | + ccdr.peripheral.SPI2, |
| 76 | + &ccdr.clocks, |
| 77 | + ); |
| 78 | + |
| 79 | + spi.write(&[0, 1, 2]).unwrap(); |
| 80 | + spi.write(&[0, 1, 2, 3, 4, 5, 6]).unwrap(); |
| 81 | + |
| 82 | + // Compose multiple operations into a single compound transfer using Operations |
| 83 | + let mut ops = [ |
| 84 | + Operation::Write(&[0x11u8, 0x22, 0x33]), |
| 85 | + Operation::Write(&[0x44u8, 0x55, 0x66]), |
| 86 | + ]; |
| 87 | + |
| 88 | + spi.transaction(&mut ops).unwrap(); |
| 89 | + |
| 90 | + loop { |
| 91 | + debug::exit(debug::EXIT_SUCCESS); |
| 92 | + } |
| 93 | +} |
0 commit comments