#![no_std] #![no_main] /** Transmits data over an SPI port using DMA */ use panic_halt as _; use cortex_m_rt::entry; use stm32f1xx_hal::{ pac, prelude::*, spi::{Mode, Phase, Polarity, Spi}, }; #[entry] fn main() -> ! { // Get access to the device specific peripherals from the peripheral access crate let dp = pac::Peripherals::take().unwrap(); // Take ownership over the raw flash and rcc devices and convert them into the corresponding // HAL structs let mut flash = dp.FLASH.constrain(); let mut rcc = dp.RCC.constrain(); // Freeze the configuration of all the clocks in the system and store the frozen frequencies in // `clocks` let clocks = rcc.cfgr.freeze(&mut flash.acr); // Acquire the GPIOA peripheral let mut gpiob = dp.GPIOB.split(&mut rcc.apb2); let hx711_spi_pins = ( gpiob.pb13.into_alternate_push_pull(&mut gpiob.crh), gpiob.pb14.into_floating_input(&mut gpiob.crh), gpiob.pb15.into_alternate_push_pull(&mut gpiob.crh), ); let hx711_spi = spi::Spi::spi2(device.SPI2, hx711_spi_pins, spi::MODE_1, 1.mhz(), clocks); let tim_delay = device.TIM1.delay::<1_000_000>(&clocks); let mut hx711_sensor = Hx711::new(hx711_spi, tim_delay); hx711_sensor.reset().unwrap(); hx711_sensor.set_mode(hx711_spi::Mode::ChAGain128).unwrap(); // x128 works up to +-20mV // Set up the DMA device let dma = dp.DMA1.split(&mut rcc.ahb); // Connect the SPI device to the DMA let spi_dma = spi.with_tx_dma(dma.5); // Start a DMA transfer let transfer = spi_dma.write(b"hello, world"); // Wait for it to finnish. The transfer takes ownership over the SPI device // and the data being sent anb those things are returned by transfer.wait let (_buffer, _spi_dma) = transfer.wait(); loop {} }