Skip to content

Commit

Permalink
Merge #138
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bors[bot] and jonfin committed May 25, 2021
2 parents 8970b06 + 196bda0 commit 73b0fac
Show file tree
Hide file tree
Showing 5 changed files with 816 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

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

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

Expand Down
38 changes: 38 additions & 0 deletions examples/pwm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#![deny(unsafe_code)]
#![no_main]
#![no_std]

// Halt on panic
use panic_halt as _;

use cortex_m_rt::entry;

use stm32f0xx_hal as hal;

use hal::{pac, prelude::*, pwm};

#[entry]
fn main() -> ! {
if let Some(mut dp) = pac::Peripherals::take() {
// Set up the system clock.
let mut rcc = dp.RCC.configure().sysclk(8.mhz()).freeze(&mut dp.FLASH);

let gpioa = dp.GPIOA.split(&mut rcc);
let channels = cortex_m::interrupt::free(move |cs| {
(
gpioa.pa8.into_alternate_af2(cs),
gpioa.pa9.into_alternate_af2(cs),
)
});

let pwm = pwm::tim1(dp.TIM1, channels, &mut rcc, 20u32.khz());
let (mut ch1, _ch2) = pwm;
let max_duty = ch1.get_max_duty();
ch1.set_duty(max_duty / 2);
ch1.enable();
}

loop {
cortex_m::asm::nop();
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub mod i2c;
#[cfg(feature = "device-selected")]
pub mod prelude;
#[cfg(feature = "device-selected")]
pub mod pwm;
#[cfg(feature = "device-selected")]
pub mod rcc;
#[cfg(feature = "device-selected")]
pub mod serial;
Expand Down
Loading

0 comments on commit 73b0fac

Please sign in to comment.