rust-stm32-blinky/src/main.rs

82 lines
2.4 KiB
Rust

#![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;
mod si5153;
#[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);
let mut si_pll = si5153::Si5153::new(&i2c);
si_pll.init(&mut i2c, 25000000, 800000000, 500000000);
si_pll.set_ms_source(&mut i2c, si5153::Multisynth::MS0, si5153::PLL::A);
si_pll.set_ms_freq(&mut i2c, si5153::Multisynth::MS0, 7_000_000);
si_pll.enable_ms_output(&mut i2c, si5153::Multisynth::MS0);
// Wait for the timer to trigger an update and change the state of the LED
loop {
block!(timer.wait()).unwrap();
led.set_high().unwrap();
block!(timer.wait()).unwrap();
led.set_low().unwrap();
}
}