-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpwm.rs
104 lines (73 loc) · 2.53 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
//! Testing PWM output for pre-defined pin combination: all pins for default mapping
#![deny(unsafe_code)]
#![no_main]
#![no_std]
use panic_halt as _;
use cortex_m::asm;
use cortex_m_rt::entry;
use embedded_hal::pwm::SetDutyCycle;
use gd32f1x0_hal::{
gpio::{gpioa::PA11, OutputMode, PullMode},
pac,
prelude::*,
pwm::Channel,
time::U32Ext,
timer::Timer,
};
#[entry]
fn main() -> ! {
let p = pac::Peripherals::take().unwrap();
let mut rcu = p.rcu.constrain();
let mut flash = p.fmc.constrain();
let clocks = rcu.cfgr.freeze(&mut flash.ws);
let mut gpioa = p.gpioa.split(&mut rcu.ahb);
// TIMER0
let c0 = gpioa
.pa8
.into_alternate(&mut gpioa.config, PullMode::Floating, OutputMode::PushPull);
let c1 = gpioa
.pa9
.into_alternate(&mut gpioa.config, PullMode::Floating, OutputMode::PushPull);
let c2 = gpioa
.pa10
.into_alternate(&mut gpioa.config, PullMode::Floating, OutputMode::PushPull);
// If you don't want to use all channels, just leave some out
let pins = (Some(c0), Some(c1), Some(c2), None::<PA11<_>>);
let mut pwm = Timer::timer0(p.timer0, &clocks, &mut rcu.apb2).pwm(pins, 1.khz());
// Enable clock on each of the channels
pwm.enable(Channel::C0);
pwm.enable(Channel::C1);
pwm.enable(Channel::C2);
//// Operations affecting all defined channels on the Timer
// Adjust period to 0.5 seconds
pwm.set_period(500.ms());
asm::bkpt();
// Return to the original frequency
pwm.set_period(1.khz());
asm::bkpt();
let max = pwm.max_duty_cycle();
//// Operations affecting single channels can be accessed through
//// the Pwm object or via dereferencing to the pin.
// Use the Pwm object to set C2 to full strength
pwm.set_duty_cycle(Channel::C2, max);
asm::bkpt();
// Use the Pwm object to set C2 to be dim
pwm.set_duty_cycle(Channel::C2, max / 4);
asm::bkpt();
// Use the Pwm object to set C2 to be zero
pwm.set_duty_cycle(Channel::C2, 0);
asm::bkpt();
// Extract the PwmChannel for C2
let mut pwm_channel = pwm.split().2.unwrap();
// Use the PwmChannel object to set C2 to be full strength
pwm_channel.set_duty_cycle(max).unwrap();
asm::bkpt();
// Use the PwmChannel object to set C2 to be dim
pwm_channel.set_duty_cycle(max / 4).unwrap();
asm::bkpt();
// Use the PwmChannel object to set C2 to be zero
pwm_channel.set_duty_cycle(0).unwrap();
asm::bkpt();
#[allow(clippy::empty_loop)]
loop {}
}