Skip to content

Add initial PWM support #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ stm32f723 = ["svd-f7x3", "device-selected", "usb_hs_phy"]
stm32f730 = ["svd-f730", "device-selected", "usb_hs_phy", "fmc"]
stm32f732 = ["svd-f7x2", "device-selected"]
stm32f733 = ["svd-f7x3", "device-selected", "usb_hs_phy"]
stm32f745 = ["svd-f745", "device-selected", "gpioj", "gpiok", "fmc"]
stm32f746 = ["svd-f7x6", "device-selected", "gpioj", "gpiok", "ltdc", "fmc", "has-can"]
stm32f756 = ["svd-f7x6", "device-selected", "gpioj", "gpiok", "ltdc", "fmc"]
stm32f765 = ["svd-f765", "device-selected", "gpioj", "gpiok", "fmc"]
stm32f767 = ["svd-f7x7", "device-selected", "gpioj", "gpiok", "ltdc", "fmc"]
stm32f769 = ["svd-f7x9", "device-selected", "gpioj", "gpiok", "ltdc", "fmc"]
stm32f777 = ["svd-f7x7", "device-selected", "gpioj", "gpiok", "ltdc", "fmc"]
stm32f778 = ["svd-f7x9", "device-selected", "gpioj", "gpiok", "ltdc", "fmc"]
stm32f779 = ["svd-f7x9", "device-selected", "gpioj", "gpiok", "ltdc", "fmc"]
stm32f745 = ["svd-f745", "device-selected", "gpioj", "gpiok", "tim_adv", "fmc"]
stm32f746 = ["svd-f7x6", "device-selected", "gpioj", "gpiok", "tim_adv", "ltdc", "fmc", "has-can"]
stm32f756 = ["svd-f7x6", "device-selected", "gpioj", "gpiok", "tim_adv", "ltdc", "fmc"]
stm32f765 = ["svd-f765", "device-selected", "gpioj", "gpiok", "tim_adv", "fmc"]
stm32f767 = ["svd-f7x7", "device-selected", "gpioj", "gpiok", "tim_adv", "ltdc", "fmc"]
stm32f769 = ["svd-f7x9", "device-selected", "gpioj", "gpiok", "tim_adv", "ltdc", "fmc"]
stm32f777 = ["svd-f7x7", "device-selected", "gpioj", "gpiok", "tim_adv", "ltdc", "fmc"]
stm32f778 = ["svd-f7x9", "device-selected", "gpioj", "gpiok", "tim_adv", "ltdc", "fmc"]
stm32f779 = ["svd-f7x9", "device-selected", "gpioj", "gpiok", "tim_adv", "ltdc", "fmc"]

fmc_lcd = ["display-interface"]

Expand All @@ -101,6 +101,7 @@ has-can = []

gpioj = []
gpiok = []
tim_adv = []

[profile.dev]
incremental = false
Expand All @@ -113,54 +114,62 @@ lto = true

[[example]]
name = "blinky_delay"
required-features = ["stm32f746", "rt"]
required-features = ["rt"] # stm32f746

[[example]]
name = "blinky"
required-features = ["stm32f746", "rt"]
required-features = ["device-selected", "rt"] # stm32f746

[[example]]
name = "flash"
required-features = ["stm32f746", "rt"]
required-features = ["device-selected", "rt"] # stm32f746

[[example]]
name = "fmc"
required-features = ["stm32f746", "rt", "fmc"]
required-features = ["device-selected", "rt", "fmc"] # stm32f746

[[example]]
name = "hello"
required-features = ["stm32f746", "rt"]
required-features = ["device-selected", "rt"] # stm32f746

[[example]]
name = "pwm"
required-features = ["device-selected", "rt"] # stm32f767

[[example]]
name = "pwm_advanced"
required-features = ["device-selected", "rt", "tim_adv"] # stm32f767

[[example]]
name = "serial_delay"
required-features = ["stm32f746", "rt"]
required-features = ["device-selected", "rt"] # stm32f746

[[example]]
name = "serial_echo"
required-features = ["stm32f746", "rt"]
required-features = ["device-selected", "rt"] # stm32f746

[[example]]
name = "timer"
required-features = ["stm32f746", "rt"]
required-features = ["device-selected", "rt"] # stm32f746

[[example]]
name = "stm32f7disco-screen"
required-features = ["stm32f746", "rt"]
required-features = ["device-selected", "rt"] # stm32f746

[[example]]
name = "exti_button"
required-features = ["stm32f767", "rt"]
required-features = ["device-selected", "rt"] # stm32f746

[[example]]
name = "usb_serial"
required-features = ["stm32f723", "rt", "synopsys-usb-otg"]
required-features = ["device-selected", "rt", "synopsys-usb-otg"] # stm32f723

[[example]]
name = "rng"

[[example]]
name = "stm32f7disco-qspi-flash"
required-features = ["stm32f746", "rt"]
required-features = ["device-selected", "rt"] # stm32f746

[[example]]
name = "can-echo"
Expand Down
32 changes: 32 additions & 0 deletions examples/pwm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//! Test the PWM

//#![deny(warnings)]
#![no_std]
#![no_main]

extern crate panic_semihosting;

use cortex_m_rt::entry;

use stm32f7xx_hal::{pac, prelude::*};

#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();

let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze();

let gpioa = dp.GPIOA.split();
let channels = (gpioa.pa0.into_alternate(), gpioa.pa1.into_alternate());

let pwm = dp.TIM2.pwm(channels, 20_000u32.Hz(), &clocks);
let (mut ch1, mut ch2) = pwm;
let max_duty = ch1.get_max_duty();
ch1.set_duty(max_duty / 4);
ch1.enable();
ch2.set_duty(max_duty / 2);
ch2.enable();

loop {}
}
59 changes: 59 additions & 0 deletions examples/pwm_advanced.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//! Test the PWM

#![deny(warnings)]
#![no_std]
#![no_main]

extern crate panic_semihosting;

use cortex_m_rt::entry;

use stm32f7xx_hal::pwm::{FaultMonitor, Polarity};
use stm32f7xx_hal::{pac, prelude::*};

#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();

let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze();

let gpioa = dp.GPIOA.split();
let gpiob = dp.GPIOB.split();
let gpioc = dp.GPIOC.split();
let gpioe = dp.GPIOE.split();

let mut led_red = gpiob.pb14.into_push_pull_output();
let btn = gpioc.pc13.into_pull_down_input();

let channels = (gpioa.pa8.into_alternate(), gpioa.pa9.into_alternate());
let (mut control, pwm) = dp
.TIM1
.pwm_advanced(channels, &clocks)
.prescaler(2)
.period(250)
.with_deadtime(200_000.nanoseconds())
.with_break_pin(gpioe.pe15.into_alternate(), Polarity::ActiveHigh)
.center_aligned()
.finalize();

let (mut ch1, mut ch2) = pwm;
let max_duty = ch1.get_max_duty();
ch1.set_duty(max_duty / 4);
ch1.enable();
ch2.set_duty(max_duty / 2);
ch2.enable();

loop {
// Fault is active, turn on LED
if control.is_fault_active() {
led_red.set_high();
}

// Clear the fault when pressing the USER button
if btn.is_high() {
led_red.set_low();
control.clear_fault();
}
}
}
4 changes: 3 additions & 1 deletion examples/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use cortex_m_semihosting::hio;
use cortex_m::peripheral::NVIC;

use stm32f7xx_hal::{
embedded_time::rate::Extensions,
interrupt, pac,
prelude::*,
timer::{Event, Timer},
Expand All @@ -32,7 +33,8 @@ fn main() -> ! {
unsafe {
NVIC::unmask(pac::Interrupt::TIM2);
}
let mut timer = Timer::tim2(dp.TIM2, 1.Hz(), clocks, &mut rcc.apb1);
let mut timer = Timer::new(dp.TIM2, &clocks).count_down();
timer.start(1.Hz());
timer.listen(Event::TimeOut);

loop {}
Expand Down
4 changes: 2 additions & 2 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::gpio::gpioh::{PH4, PH5, PH7, PH8};
use crate::gpio::AlternateOD;
use crate::hal::blocking::i2c::{Read, Write, WriteRead};
use crate::pac::{DWT, I2C1, I2C2, I2C3};
use crate::rcc::{Clocks, Enable, GetBusFreq, RccBus, Reset};
use crate::rcc::{BusClock, Clocks, Enable, RccBus, Reset};
use nb::Error::{Other, WouldBlock};
use nb::{Error as NbError, Result as NbResult};

Expand Down Expand Up @@ -410,7 +410,7 @@ macro_rules! hal {
$I2CX::enable(apb);
$I2CX::reset(apb);

let pclk = <$I2CX as RccBus>::Bus::get_frequency(&clocks).0;
let pclk = <$I2CX as RccBus>::Bus::clock(&clocks).0;

let mut i2c = I2c { i2c, pins, mode, pclk };
i2c.init();
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ pub mod fmc_lcd;
#[cfg(feature = "device-selected")]
pub mod gpio;

#[cfg(feature = "device-selected")]
pub mod pwm;

#[cfg(feature = "device-selected")]
pub mod dac;

Expand Down
3 changes: 3 additions & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ pub use crate::fmc::FmcExt as _stm327xx_hal_fmc_FmcExt;
pub use crate::gpio::GpioExt as _stm327xx_hal_gpio_GpioExt;
pub use crate::hal::digital::v2::{InputPin, OutputPin};
pub use crate::hal::prelude::*;
pub use crate::hal::timer::Cancel;
pub use crate::pwm::PwmAdvExt as _stm32f7xx_hal_pwm_PwmAdvExt;
pub use crate::pwm::PwmExt as _stm32f7xx_hal_pwm_PwmExt;
pub use crate::rcc::RccExt as _stm32f7xx_hal_rcc_RccExt;
pub use crate::rng::RngExt as _;
Loading