Skip to content

Commit b782375

Browse files
bors[bot]AdinAck
andauthored
163: Add Basic Complementary Channel Support to PWM r=therealprof a=AdinAck ## Motivation Issue stm32-rs#162 created by *myself*. TL;DR: PWM module did not support complementary channels, it should. ## Change(s) Description - Add CHxN pins, (traits?), and structs and whatnot - Set `OSSR` and `CCxNE` registers accordingly ## Testing I have only confirmed it works on CH1/CH1N, but I don't see why it wouldn't work on the others. ## Warning This is my first time using Rust. Co-authored-by: Adin Ackerman <[email protected]>
2 parents 8f410db + ab065fc commit b782375

File tree

4 files changed

+367
-4
lines changed

4 files changed

+367
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- PWM complementary output capability for TIM1 with new example to demonstrate
13+
1014
### Changed
1115

1216
- Updated the `cast` dependency from 0.2 to 0.3

examples/pwm_complementary.rs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#![deny(unsafe_code)]
2+
#![no_main]
3+
#![no_std]
4+
5+
// Halt on panic
6+
use panic_halt as _;
7+
8+
use cortex_m;
9+
use cortex_m_rt::entry;
10+
11+
use stm32f0xx_hal as hal;
12+
13+
use hal::{delay::Delay, pac, prelude::*, pwm};
14+
15+
#[entry]
16+
fn main() -> ! {
17+
if let Some(mut dp) = pac::Peripherals::take() {
18+
// Set up the system clock.
19+
let mut rcc = dp.RCC.configure().sysclk(8.mhz()).freeze(&mut dp.FLASH);
20+
21+
let gpioa = dp.GPIOA.split(&mut rcc);
22+
let channels = cortex_m::interrupt::free(move |cs| {
23+
(
24+
gpioa.pa8.into_alternate_af2(cs), // on TIM1_CH1
25+
gpioa.pa7.into_alternate_af2(cs), // on TIM1_CH1N
26+
)
27+
});
28+
29+
let pwm = pwm::tim1(dp.TIM1, channels, &mut rcc, 20u32.khz());
30+
let (mut ch1, mut ch1n) = pwm;
31+
let max_duty = ch1.get_max_duty();
32+
ch1.set_duty(max_duty / 2);
33+
ch1.enable();
34+
ch1n.enable();
35+
36+
// simple duty sweep
37+
if let Some(cp) = cortex_m::Peripherals::take() {
38+
let mut delay = Delay::new(cp.SYST, &rcc);
39+
40+
let steps = 100;
41+
42+
loop {
43+
for i in 0..steps {
44+
ch1.set_duty(max_duty / steps * i);
45+
delay.delay_ms(30u16);
46+
}
47+
48+
for i in (1..steps).rev() {
49+
ch1.set_duty(max_duty / steps * i);
50+
delay.delay_ms(30u16);
51+
}
52+
}
53+
}
54+
}
55+
56+
// something went wrong when acquiring peripheral access
57+
loop {
58+
cortex_m::asm::nop();
59+
}
60+
}

0 commit comments

Comments
 (0)