-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.rs
59 lines (49 loc) · 1.29 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#![deny(warnings)]
#![no_std]
#![no_main]
// used to provide panic_implementation
#[allow(unused)]
use panic_abort;
use hal::gpio;
use hal::prelude::*;
use hal::timer;
use cortex_m_rt::{entry, exception, ExceptionFrame};
use cortex_m::asm;
#[entry]
fn main() -> ! {
let device = hal::pac::Peripherals::take().unwrap();
let mut flash = device.FLASH.constrain();
let mut rcc = device.RCC.constrain();
let pin = device.GPIOB.split(&mut rcc.ahb).pb8.pull_type(gpio::PullUp);
let clocks = rcc
.cfgr
.sysclk(64.mhz())
.pclk1(32.mhz())
.freeze(&mut flash.acr);
let tim = timer::tim4::Timer::new(device.TIM4, 1.mhz(), clocks);
let (channels, mut tim) = tim.use_pwm();
let mut pwm = pin.to_pwm(channels.2, gpio::MediumSpeed);
pwm.enable();
tim.enable();
loop {
for i in 10..50 {
pwm.set_duty(i);
tick_delay(25000);
}
for i in 10..50 {
pwm.set_duty(50 - i);
tick_delay(25000);
}
}
}
#[exception]
unsafe fn HardFault(ef: &ExceptionFrame) -> ! {
panic!("HardFault at {:#?}", ef);
}
#[exception]
unsafe fn DefaultHandler(irqn: i16) {
panic!("Unhandled exception (IRQn = {})", irqn);
}
fn tick_delay(ticks: usize) {
(0..ticks).for_each(|_| asm::nop());
}