forked from stm32-rs/stm32h7xx-hal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pwm.rs
84 lines (67 loc) · 1.83 KB
/
pwm.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#![deny(warnings)]
#![no_main]
#![no_std]
use cortex_m::asm;
use cortex_m_rt::entry;
#[macro_use]
mod utilities;
use stm32h7xx_hal::{pac, prelude::*};
use log::info;
#[entry]
fn main() -> ! {
utilities::logger::init();
let dp = pac::Peripherals::take().expect("Cannot take peripherals");
// Constrain and Freeze power
info!("Setup PWR... ");
let pwr = dp.PWR.constrain();
let pwrcfg = example_power!(pwr).freeze();
// Constrain and Freeze clock
info!("Setup RCC... ");
let rcc = dp.RCC.constrain();
let ccdr = rcc.sys_ck(8.mhz()).freeze(pwrcfg, &dp.SYSCFG);
// Acquire the GPIOE peripheral. This also enables the clock for
// GPIOE in the RCC register.
let gpioa = dp.GPIOA.split(ccdr.peripheral.GPIOA);
let gpiob = dp.GPIOB.split(ccdr.peripheral.GPIOB);
// Select PWM output pins
let pins = (
gpioa.pa8.into_alternate_af1(),
gpioa.pa9.into_alternate_af1(),
gpioa.pa10.into_alternate_af1(),
);
info!("");
info!("stm32h7xx-hal example - PWM");
info!("");
// Configure PWM at 10kHz
let (mut pwm, ..) =
dp.TIM1
.pwm(pins, 10.khz(), ccdr.peripheral.TIM1, &ccdr.clocks);
// Output PWM on PA8
let max = pwm.get_max_duty();
pwm.set_duty(max / 2);
info!("50%");
pwm.enable();
asm::bkpt();
info!("25%");
pwm.set_duty(max / 4);
asm::bkpt();
info!("12.5%");
pwm.set_duty(max / 8);
asm::bkpt();
info!("100%");
pwm.set_duty(max);
asm::bkpt();
let mut pwm = dp.TIM12.pwm(
gpiob.pb14.into_alternate_af2(),
10.khz(),
ccdr.peripheral.TIM12,
&ccdr.clocks,
);
// Output PWM on PB14
let max = pwm.get_max_duty();
pwm.set_duty(max / 2);
pwm.enable();
loop {
cortex_m::asm::nop()
}
}