From 0225e6d07483e9afad99472c37bf7fb88de04470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael?= <40151420+ldericher@users.noreply.github.com> Date: Wed, 28 Feb 2024 13:02:02 +0000 Subject: [PATCH] 72 MHz clock (HSE+PLL), timer experiments --- src/main.rs | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index fe6edf0..c44adca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ use panic_halt as _; use nb::block; use cortex_m_rt::entry; -use stm32f1xx_hal::{pac, prelude::*, timer::Timer}; +use stm32f1xx_hal::{pac, prelude::*, rcc::Config, timer::Timer}; #[entry] fn main() -> ! { @@ -30,7 +30,20 @@ fn main() -> ! { // 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); + let clocks = rcc.cfgr.freeze_with_config( + Config { + // HSE frequency + hse: Some(8_000_000), + // PLLMUL represented by an integer -2 + pllmul: Some(9 - 2), + // PCLK1 freq must be 36 MHz or less + ppre1: stm32f1xx_hal::rcc::PPre::Div2, + // ADCCLK freq must be 14 MHz or less + adcpre: pac::rcc::cfgr::ADCPRE_A::Div6, + ..Default::default() + }, + &mut flash.acr, + ); // Acquire the GPIOC peripheral let mut gpioc = dp.GPIOC.split(); @@ -38,15 +51,27 @@ fn main() -> ! { // 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, &clocks).counter_hz(); - timer.start(1.Hz()).unwrap(); + // at 72 MHz, timer of 1 Hz overflows, use 10 Hz instead (8 Hz experimental minimum) + timer.start(10.Hz()).unwrap(); + + // equivalent timers: + // let mut timer = Timer::syst(cp.SYST, &clocks).counter_us(); // us resolution + // let mut timer: SysCounter<1000> = Timer::syst(cp.SYST, &clocks).counter(); // ms resolution + // timer.start(100.millis()).unwrap(); // Wait for the timer to trigger an update and change the state of the LED loop { - block!(timer.wait()).unwrap(); + for _ in 0..10 { + block!(timer.wait()).unwrap(); + } led.set_high(); - block!(timer.wait()).unwrap(); + + for _ in 0..10 { + block!(timer.wait()).unwrap(); + } led.set_low(); } }