Skip to content

Commit 73b0fac

Browse files
bors[bot]jonfin
andauthored
Merge #138
138: PWM support r=therealprof a=jonfin Add PWM support based on the stm32f4xx_hal implementation. It is mostly a copy from stm32f4xx_hal with some modification. Only change to stm32f4xx_hal: it uses the recommended order for setting the register from DM00042534 I did some testing based on a stm32f030c8 chip. fixes #26 Co-authored-by: Jonas <jonas@finger-ling.de>
2 parents 8970b06 + 196bda0 commit 73b0fac

File tree

5 files changed

+816
-0
lines changed

5 files changed

+816
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1717
### Added
1818

1919
- Add CAN bus abstraction based on the [bxcan] crate.
20+
- Add PWM output generation based on the timers.
2021

2122
[bxcan]: https://crates.io/crates/bxcan
2223

examples/pwm.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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_rt::entry;
9+
10+
use stm32f0xx_hal as hal;
11+
12+
use hal::{pac, prelude::*, pwm};
13+
14+
#[entry]
15+
fn main() -> ! {
16+
if let Some(mut dp) = pac::Peripherals::take() {
17+
// Set up the system clock.
18+
let mut rcc = dp.RCC.configure().sysclk(8.mhz()).freeze(&mut dp.FLASH);
19+
20+
let gpioa = dp.GPIOA.split(&mut rcc);
21+
let channels = cortex_m::interrupt::free(move |cs| {
22+
(
23+
gpioa.pa8.into_alternate_af2(cs),
24+
gpioa.pa9.into_alternate_af2(cs),
25+
)
26+
});
27+
28+
let pwm = pwm::tim1(dp.TIM1, channels, &mut rcc, 20u32.khz());
29+
let (mut ch1, _ch2) = pwm;
30+
let max_duty = ch1.get_max_duty();
31+
ch1.set_duty(max_duty / 2);
32+
ch1.enable();
33+
}
34+
35+
loop {
36+
cortex_m::asm::nop();
37+
}
38+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ pub mod i2c;
4646
#[cfg(feature = "device-selected")]
4747
pub mod prelude;
4848
#[cfg(feature = "device-selected")]
49+
pub mod pwm;
50+
#[cfg(feature = "device-selected")]
4951
pub mod rcc;
5052
#[cfg(feature = "device-selected")]
5153
pub mod serial;

0 commit comments

Comments
 (0)