From 86aa77175f73fdb4973844d7e1e38cca708137fb Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sun, 13 Oct 2024 13:36:32 -0700 Subject: [PATCH] can't seem to flash the board --- src/main.rs | 63 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index c07e55c..eae31c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,50 +1,59 @@ #![no_std] #![no_main] +use cortex_m::asm; use defmt_rtt as _; #[allow(deprecated)] -use hal::pwm::{tim16, tim2, tim3}; +use hal::pwm::tim2; +use hal::{adc, flash::FlashExt, gpio::GpioExt, hal::PwmPin, pac, prelude::*, rcc::RccExt}; use panic_halt as _; -use stm32f3xx_hal::{ - self as hal, adc, flash::FlashExt, gpio::GpioExt, hal::PwmPin, pac, prelude::*, rcc::RccExt, -}; +use stm32f3xx_hal as hal; static POT_MAX: u16 = 4095; -mod motor; -//use motor::*; - #[cortex_m_rt::entry] fn main() -> ! { - let sp = pac::Peripherals::take().unwrap(); - let mut flash = sp.FLASH.constrain(); - let mut rcc = sp.RCC.constrain(); - let clocks = rcc.cfgr.freeze(&mut flash.acr); - let mut gpiob = sp.GPIOB.split(&mut rcc.ahb); - let mut led = gpiob - .pb2 - .into_push_pull_output(&mut gpiob.moder, &mut gpiob.otyper); + let dp = pac::Peripherals::take().unwrap(); + // Configure our clocks + let mut flash = dp.FLASH.constrain(); + let mut rcc = dp.RCC.constrain(); + let clocks = rcc.cfgr.sysclk(16.MHz()).freeze(&mut flash.acr); + + let mut gpiob = dp.GPIOB.split(&mut rcc.ahb); + let mut gpioa = dp.GPIOA.split(&mut rcc.ahb); + let led = gpioa + .pa5 + .into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl); let mut pot = gpiob.pb1.into_analog(&mut gpiob.moder, &mut gpiob.pupdr); - let cp = cortex_m::Peripherals::take().unwrap(); - let mut delay = hal::delay::Delay::new(cp.SYST, clocks); - - let adc_common = adc::CommonAdc::new(sp.ADC1_2, &clocks, &mut rcc.ahb); + let adc_common = adc::CommonAdc::new(dp.ADC1_2, &clocks, &mut rcc.ahb); let mut padc = adc::Adc::new( - sp.ADC1, + dp.ADC1, adc::config::Config::default(), &clocks, &adc_common, ); - cortex_m::asm::wfi(); - loop { - let pot_out: u16 = padc.read(&mut pot).unwrap_or(0); - let pot_out = (POT_MAX - pot_out) as f32 / POT_MAX as f32; - let pot_out = (4000.0 * pot_out) as u32; + // TIM2 + // + // A 32-bit timer, so we can set a larger resolution + #[allow(deprecated)] + let tim2_channels = tim2( + dp.TIM2, + 160000, // resolution of duty cycle + 50.Hz(), // frequency of period + &clocks, // To get the timer's clock speed + ); - led.toggle().unwrap(); - delay.delay_ms(pot_out); + let mut led_pwm = tim2_channels.0.output_to_pa5(led); + let max = led_pwm.get_max_duty(); + + loop { + asm::wfi(); + let pot_out: u16 = padc.read(&mut pot).unwrap_or(0); + let pot_out = (pot_out as f32 / POT_MAX as f32) as u32; + led_pwm.set_duty(max * pot_out); + led_pwm.enable(); } }