-
Notifications
You must be signed in to change notification settings - Fork 180
/
pwm.rs
116 lines (82 loc) · 3.08 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! Testing PWM output for pre-defined pin combination: all pins for default mapping
#![deny(unsafe_code)]
#![allow(clippy::empty_loop)]
#![no_main]
#![no_std]
use panic_halt as _;
use cortex_m::asm;
use cortex_m_rt::entry;
use stm32f1xx_hal::{
pac,
prelude::*,
time::ms,
timer::{Channel, Tim2NoRemap},
};
#[entry]
fn main() -> ! {
let p = pac::Peripherals::take().unwrap();
let mut flash = p.FLASH.constrain();
let rcc = p.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);
let mut afio = p.AFIO.constrain();
let mut gpioa = p.GPIOA.split();
// let mut gpiob = p.GPIOB.split();
// TIM2
let c1 = gpioa.pa0.into_alternate_push_pull(&mut gpioa.crl);
let c2 = gpioa.pa1.into_alternate_push_pull(&mut gpioa.crl);
let c3 = gpioa.pa2.into_alternate_push_pull(&mut gpioa.crl);
// If you don't want to use all channels, just leave some out
// let c4 = gpioa.pa3.into_alternate_push_pull(&mut gpioa.crl);
let pins = (c1, c2, c3);
// TIM3
// let c1 = gpioa.pa6.into_alternate_push_pull(&mut gpioa.crl);
// let c2 = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
// let c3 = gpiob.pb0.into_alternate_push_pull(&mut gpiob.crl);
// let c4 = gpiob.pb1.into_alternate_push_pull(&mut gpiob.crl);
// TIM4 (Only available with the "medium" density feature)
// let c1 = gpiob.pb6.into_alternate_push_pull(&mut gpiob.crl);
// let c2 = gpiob.pb7.into_alternate_push_pull(&mut gpiob.crl);
// let c3 = gpiob.pb8.into_alternate_push_pull(&mut gpiob.crh);
// let c4 = gpiob.pb9.into_alternate_push_pull(&mut gpiob.crh);
//let mut pwm =
// Timer::new(p.TIM2, &clocks).pwm_hz::<Tim2NoRemap, _, _>(pins, &mut afio.mapr, 1.kHz());
// or
let mut pwm = p
.TIM2
.pwm_hz::<Tim2NoRemap, _, _>(pins, &mut afio.mapr, 1.kHz(), &clocks);
// Enable clock on each of the channels
pwm.enable(Channel::C1);
pwm.enable(Channel::C2);
pwm.enable(Channel::C3);
//// Operations affecting all defined channels on the Timer
// Adjust period to 0.5 seconds
pwm.set_period(ms(500).into_rate());
asm::bkpt();
// Return to the original frequency
pwm.set_period(1.kHz());
asm::bkpt();
let max = pwm.get_max_duty();
//// Operations affecting single channels can be accessed through
//// the Pwm object or via dereferencing to the pin.
// Use the Pwm object to set C3 to full strength
pwm.set_duty(Channel::C3, max);
asm::bkpt();
// Use the Pwm object to set C3 to be dim
pwm.set_duty(Channel::C3, max / 4);
asm::bkpt();
// Use the Pwm object to set C3 to be zero
pwm.set_duty(Channel::C3, 0);
asm::bkpt();
// Extract the PwmChannel for C3
let mut pwm_channel = pwm.split().2;
// Use the PwmChannel object to set C3 to be full strength
pwm_channel.set_duty(max);
asm::bkpt();
// Use the PwmChannel object to set C3 to be dim
pwm_channel.set_duty(max / 4);
asm::bkpt();
// Use the PwmChannel object to set C3 to be zero
pwm_channel.set_duty(0);
asm::bkpt();
loop {}
}