#![deny(unsafe_code)] #![no_std] #![no_main] use panic_halt as _; use cortex_m_semihosting::hprintln; // logs messages to the host; requires a debugger use nb::block; use stm32f1xx_hal::{ prelude::*, pac, i2c, timer::Timer, }; use cortex_m_rt::entry; use embedded_hal::digital::v2::OutputPin; #[entry] fn main() -> ! { // Get access to the core peripherals from the cortex-m crate let cp = cortex_m::Peripherals::take().unwrap(); // 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(); let mut afio = dp.AFIO.constrain(&mut rcc.apb2); // 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 GPIOC peripheral let mut gpioc = dp.GPIOC.split(&mut rcc.apb2); let mut gpiob = dp.GPIOB.split(&mut rcc.apb2); let scl = gpiob.pb6.into_alternate_open_drain(&mut gpiob.crl); let sda = gpiob.pb7.into_alternate_open_drain(&mut gpiob.crl); let mut i2c = i2c::BlockingI2c::i2c1(dp.I2C1, (scl, sda), &mut afio.mapr, i2c::Mode::Standard{frequency: 400_000}, clocks, &mut rcc.apb1, 5, 1, 5, 5); hprintln!("I2C setup").unwrap(); // Configure gpio C pin 13 as a push-pull output. The `crh` register is passed to the function // in order to configure the port. For pins 0-7, crl should be passed instead. let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh); // Configure the syst timer to trigger an update every second let mut timer = Timer::syst(cp.SYST, 1.hz(), clocks); // Wait for the timer to trigger an update and change the state of the LED loop { block!(timer.wait()).unwrap(); led.set_high().unwrap(); let res = i2c.write(96, &[0x23, 0x82]); if res.is_ok() { hprintln!("write worked!").unwrap(); } else { hprintln!("write failed!").unwrap(); } block!(timer.wait()).unwrap(); led.set_low().unwrap(); } }