Skip to content

Commit 1c72714

Browse files
authored
Merge pull request #125 from thalesfragoso/pwm-impl
PWM implementation
2 parents 723b740 + 878e43d commit 1c72714

File tree

6 files changed

+1302
-702
lines changed

6 files changed

+1302
-702
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Changed
1111

1212
- Wait 16 cycles after setting prescalers for some clock domains to follow manual.
13+
- Fixed `TIM9` `pclk` and `ppre`.
1314

1415
### Added
1516

1617
- Implement `timer::Cancel` trait for `Timer<SYST>`.
18+
- Added PWM support and example.
1719

1820
## [v0.7.0] - 2020-03-07
1921

examples/pwm.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
use stm32f4xx_hal::{prelude::*, pwm, stm32};
11+
12+
#[entry]
13+
fn main() -> ! {
14+
if let Some(dp) = stm32::Peripherals::take() {
15+
// Set up the system clock.
16+
let rcc = dp.RCC.constrain();
17+
let clocks = rcc.cfgr.freeze();
18+
19+
let gpioa = dp.GPIOA.split();
20+
let channels = (
21+
gpioa.pa8.into_alternate_af1(),
22+
gpioa.pa9.into_alternate_af1(),
23+
);
24+
25+
let pwm = pwm::tim1(dp.TIM1, channels, clocks, 20u32.khz());
26+
let (mut ch1, _ch2) = pwm;
27+
let max_duty = ch1.get_max_duty();
28+
ch1.set_duty(max_duty / 2);
29+
ch1.enable();
30+
}
31+
32+
loop {
33+
cortex_m::asm::nop();
34+
}
35+
}

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ pub mod rng;
138138
#[cfg(feature = "device-selected")]
139139
pub mod prelude;
140140
#[cfg(feature = "device-selected")]
141+
pub mod pwm;
142+
#[cfg(feature = "device-selected")]
141143
pub mod qei;
142144
#[cfg(feature = "device-selected")]
143145
pub mod rcc;

0 commit comments

Comments
 (0)