Compare commits
3 commits
8069017df0
...
1f9d4ae284
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1f9d4ae284 | ||
|
|
86aa77175f | ||
|
|
842d8b26a6 |
4 changed files with 49 additions and 26 deletions
|
|
@ -1,8 +1,9 @@
|
||||||
[target.thumbv7m-none-eabi]
|
[target.thumbv7m-none-eabi]
|
||||||
|
runner = 'probe-rs run --chip STM32F302R8Tx'
|
||||||
# uncomment this to make `cargo run` execute programs on QEMU
|
# uncomment this to make `cargo run` execute programs on QEMU
|
||||||
# runner = "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
|
# runner = "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
|
||||||
|
|
||||||
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
#[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
||||||
rustflags = []
|
rustflags = []
|
||||||
|
|
||||||
[build]
|
[build]
|
||||||
|
|
|
||||||
4
.rustfmt.toml
Normal file
4
.rustfmt.toml
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
imports_granularity = "Crate"
|
||||||
|
group_imports = "StdExternalCrate"
|
||||||
|
wrap_comments = true
|
||||||
|
edition = "2021"
|
||||||
|
|
@ -25,3 +25,7 @@ bench = false
|
||||||
codegen-units = 1 # better optimizations
|
codegen-units = 1 # better optimizations
|
||||||
debug = true # symbols are nice and they don't increase the size on Flash
|
debug = true # symbols are nice and they don't increase the size on Flash
|
||||||
lto = true # better optimizations
|
lto = true # better optimizations
|
||||||
|
|
||||||
|
[profile.dev.package."*"]
|
||||||
|
opt-level = "z"
|
||||||
|
debug = true
|
||||||
|
|
|
||||||
64
src/main.rs
64
src/main.rs
|
|
@ -1,45 +1,59 @@
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
use stm32f3xx_hal::{self as hal, adc, pac, prelude::*};
|
use cortex_m::asm;
|
||||||
|
use defmt_rtt as _;
|
||||||
use {defmt_rtt as _, panic_halt as _};
|
#[allow(deprecated)]
|
||||||
|
use hal::pwm::tim2;
|
||||||
|
use hal::{adc, flash::FlashExt, gpio::GpioExt, hal::PwmPin, pac, prelude::*, rcc::RccExt};
|
||||||
|
use panic_halt as _;
|
||||||
|
use stm32f3xx_hal as hal;
|
||||||
|
|
||||||
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 rcc = sp.RCC.constrain();
|
|
||||||
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 mut flash = sp.FLASH.constrain();
|
let adc_common = adc::CommonAdc::new(dp.ADC1_2, &clocks, &mut rcc.ahb);
|
||||||
let clocks = rcc.cfgr.freeze(&mut flash.acr);
|
|
||||||
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 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,
|
||||||
);
|
);
|
||||||
|
|
||||||
loop {
|
// TIM2
|
||||||
let pot_out: u16 = padc.read(&mut pot).unwrap_or(0);
|
//
|
||||||
let pot_out = (POT_MAX - pot_out) as f32 / POT_MAX as f32;
|
// A 32-bit timer, so we can set a larger resolution
|
||||||
let pot_out = (4000.0 * pot_out) as u32;
|
#[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();
|
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