can't seem to flash the board
This commit is contained in:
parent
842d8b26a6
commit
86aa77175f
1 changed files with 36 additions and 27 deletions
63
src/main.rs
63
src/main.rs
|
|
@ -1,50 +1,59 @@
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
|
use cortex_m::asm;
|
||||||
use defmt_rtt as _;
|
use defmt_rtt as _;
|
||||||
#[allow(deprecated)]
|
#[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 panic_halt as _;
|
||||||
use stm32f3xx_hal::{
|
use stm32f3xx_hal as hal;
|
||||||
self as hal, adc, flash::FlashExt, gpio::GpioExt, hal::PwmPin, pac, prelude::*, rcc::RccExt,
|
|
||||||
};
|
|
||||||
|
|
||||||
static POT_MAX: u16 = 4095;
|
static POT_MAX: u16 = 4095;
|
||||||
|
|
||||||
mod motor;
|
|
||||||
//use motor::*;
|
|
||||||
|
|
||||||
#[cortex_m_rt::entry]
|
#[cortex_m_rt::entry]
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
let sp = pac::Peripherals::take().unwrap();
|
let dp = 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);
|
|
||||||
|
|
||||||
|
// 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 mut pot = gpiob.pb1.into_analog(&mut gpiob.moder, &mut gpiob.pupdr);
|
||||||
|
|
||||||
let cp = cortex_m::Peripherals::take().unwrap();
|
let adc_common = adc::CommonAdc::new(dp.ADC1_2, &clocks, &mut rcc.ahb);
|
||||||
let mut delay = hal::delay::Delay::new(cp.SYST, clocks);
|
|
||||||
|
|
||||||
let adc_common = adc::CommonAdc::new(sp.ADC1_2, &clocks, &mut rcc.ahb);
|
|
||||||
let mut padc = adc::Adc::new(
|
let mut padc = adc::Adc::new(
|
||||||
sp.ADC1,
|
dp.ADC1,
|
||||||
adc::config::Config::default(),
|
adc::config::Config::default(),
|
||||||
&clocks,
|
&clocks,
|
||||||
&adc_common,
|
&adc_common,
|
||||||
);
|
);
|
||||||
|
|
||||||
cortex_m::asm::wfi();
|
// TIM2
|
||||||
loop {
|
//
|
||||||
let pot_out: u16 = padc.read(&mut pot).unwrap_or(0);
|
// A 32-bit timer, so we can set a larger resolution
|
||||||
let pot_out = (POT_MAX - pot_out) as f32 / POT_MAX as f32;
|
#[allow(deprecated)]
|
||||||
let pot_out = (4000.0 * pot_out) as u32;
|
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();
|
let mut led_pwm = tim2_channels.0.output_to_pa5(led);
|
||||||
delay.delay_ms(pot_out);
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue