From 3252eaa060d8efb79f99511ac40a26be9cf287f9 Mon Sep 17 00:00:00 2001 From: xoviat Date: Tue, 27 Jun 2023 20:30:58 -0500 Subject: stm32/hrtim: add example impl. --- examples/stm32f334/.cargo/config.toml | 9 +++++ examples/stm32f334/Cargo.toml | 26 +++++++++++++++ examples/stm32f334/build.rs | 5 +++ examples/stm32f334/src/bin/hello.rs | 23 +++++++++++++ examples/stm32f334/src/bin/pwm.rs | 63 +++++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+) create mode 100644 examples/stm32f334/.cargo/config.toml create mode 100644 examples/stm32f334/Cargo.toml create mode 100644 examples/stm32f334/build.rs create mode 100644 examples/stm32f334/src/bin/hello.rs create mode 100644 examples/stm32f334/src/bin/pwm.rs (limited to 'examples') diff --git a/examples/stm32f334/.cargo/config.toml b/examples/stm32f334/.cargo/config.toml new file mode 100644 index 000000000..7f3fda529 --- /dev/null +++ b/examples/stm32f334/.cargo/config.toml @@ -0,0 +1,9 @@ +[target.'cfg(all(target_arch = "arm", target_os = "none"))'] +# replace STM32F429ZITx with your chip as listed in `probe-rs-cli chip list` +runner = "probe-rs-cli run --chip STM32F303ZETx" + +[build] +target = "thumbv7em-none-eabihf" + +[env] +DEFMT_LOG = "trace" diff --git a/examples/stm32f334/Cargo.toml b/examples/stm32f334/Cargo.toml new file mode 100644 index 000000000..6410891a1 --- /dev/null +++ b/examples/stm32f334/Cargo.toml @@ -0,0 +1,26 @@ +[package] +edition = "2021" +name = "embassy-stm32f3-examples" +version = "0.1.0" +license = "MIT OR Apache-2.0" + +[dependencies] +embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } +embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["nightly", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] } +embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f334r8", "unstable-pac", "memory-x", "time-driver-any", "exti"] } +embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } +embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } + +defmt = "0.3" +defmt-rtt = "0.4" + +cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] } +cortex-m-rt = "0.7.0" +embedded-hal = "0.2.6" +panic-probe = { version = "0.3", features = ["print-defmt"] } +futures = { version = "0.3.17", default-features = false, features = ["async-await"] } +heapless = { version = "0.7.5", default-features = false } +nb = "1.0.0" +embedded-storage = "0.3.0" +static_cell = { version = "1.1", features = ["nightly"]} diff --git a/examples/stm32f334/build.rs b/examples/stm32f334/build.rs new file mode 100644 index 000000000..8cd32d7ed --- /dev/null +++ b/examples/stm32f334/build.rs @@ -0,0 +1,5 @@ +fn main() { + println!("cargo:rustc-link-arg-bins=--nmagic"); + println!("cargo:rustc-link-arg-bins=-Tlink.x"); + println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); +} diff --git a/examples/stm32f334/src/bin/hello.rs b/examples/stm32f334/src/bin/hello.rs new file mode 100644 index 000000000..65773210d --- /dev/null +++ b/examples/stm32f334/src/bin/hello.rs @@ -0,0 +1,23 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use defmt::info; +use embassy_executor::Spawner; +use embassy_stm32::time::Hertz; +use embassy_stm32::Config; +use embassy_time::{Duration, Timer}; +use {defmt_rtt as _, panic_probe as _}; + +#[embassy_executor::main] +async fn main(_spawner: Spawner) -> ! { + let mut config = Config::default(); + config.rcc.hse = Some(Hertz(8_000_000)); + config.rcc.sysclk = Some(Hertz(16_000_000)); + let _p = embassy_stm32::init(config); + + loop { + info!("Hello World!"); + Timer::after(Duration::from_secs(1)).await; + } +} diff --git a/examples/stm32f334/src/bin/pwm.rs b/examples/stm32f334/src/bin/pwm.rs new file mode 100644 index 000000000..cc2ea8617 --- /dev/null +++ b/examples/stm32f334/src/bin/pwm.rs @@ -0,0 +1,63 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use defmt::*; +use embassy_executor::Spawner; +use embassy_stm32::pwm::advanced_pwm::*; +use embassy_stm32::pwm::Channel; +use embassy_stm32::time::khz; +use embassy_time::{Duration, Timer}; +use {defmt_rtt as _, panic_probe as _}; + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let p = embassy_stm32::init(Default::default()); + info!("Hello World!"); + + let ch1 = PwmPin::new_cha(p.PA8); + let ch1n = ComplementaryPwmPin::new_cha(p.PA9); + let mut pwm = AdvancedPwm::new( + p.HRTIM1, + Some(ch1), + Some(ch1n), + None, + None, + None, + None, + None, + None, + None, + None, + ); + + pwm.set_dead_time(0); + + let mut buck_converter = BridgeConverter::new(pwm.ch_a, khz(100)); + + buck_converter.set_duty(0, u16::MAX); + + // note: if the pins are not passed into the advanced pwm struct, they will not be output + let mut boost_converter = BridgeConverter::new(pwm.ch_b, khz(100)); + + boost_converter.set_duty(0, 0); + + // let max = pwm.get_max_duty(); + // pwm.set_dead_time(max / 1024); + // + // pwm.enable(Channel::Ch1); + // + // info!("PWM initialized"); + // info!("PWM max duty {}", max); + // + // loop { + // pwm.set_duty(Channel::Ch1, 0); + // Timer::after(Duration::from_millis(300)).await; + // pwm.set_duty(Channel::Ch1, max / 4); + // Timer::after(Duration::from_millis(300)).await; + // pwm.set_duty(Channel::Ch1, max / 2); + // Timer::after(Duration::from_millis(300)).await; + // pwm.set_duty(Channel::Ch1, max - 1); + // Timer::after(Duration::from_millis(300)).await; + // } +} -- cgit From 8c4997c5fcd8c25d68865b9e7537e0add50eac24 Mon Sep 17 00:00:00 2001 From: xoviat Date: Thu, 29 Jun 2023 21:05:41 -0500 Subject: stm32/hrtim: impl. bridge, dead-time part. res. --- examples/stm32f334/src/bin/pwm.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/stm32f334/src/bin/pwm.rs b/examples/stm32f334/src/bin/pwm.rs index cc2ea8617..20a621372 100644 --- a/examples/stm32f334/src/bin/pwm.rs +++ b/examples/stm32f334/src/bin/pwm.rs @@ -31,16 +31,17 @@ async fn main(_spawner: Spawner) { None, ); - pwm.set_dead_time(0); - let mut buck_converter = BridgeConverter::new(pwm.ch_a, khz(100)); - buck_converter.set_duty(0, u16::MAX); + buck_converter.set_primary_duty(0); + buck_converter.set_secondary_duty(0); + buck_converter.set_dead_time(0); // note: if the pins are not passed into the advanced pwm struct, they will not be output let mut boost_converter = BridgeConverter::new(pwm.ch_b, khz(100)); - boost_converter.set_duty(0, 0); + boost_converter.set_primary_duty(0); + boost_converter.set_secondary_duty(0); // let max = pwm.get_max_duty(); // pwm.set_dead_time(max / 1024); -- cgit From 6e13f5b387d8f0f948b5874bc300925b015947d9 Mon Sep 17 00:00:00 2001 From: xoviat Date: Fri, 30 Jun 2023 18:33:22 -0500 Subject: rustfmt --- examples/stm32f334/src/bin/pwm.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/stm32f334/src/bin/pwm.rs b/examples/stm32f334/src/bin/pwm.rs index 20a621372..1b5d509e7 100644 --- a/examples/stm32f334/src/bin/pwm.rs +++ b/examples/stm32f334/src/bin/pwm.rs @@ -5,9 +5,7 @@ use defmt::*; use embassy_executor::Spawner; use embassy_stm32::pwm::advanced_pwm::*; -use embassy_stm32::pwm::Channel; use embassy_stm32::time::khz; -use embassy_time::{Duration, Timer}; use {defmt_rtt as _, panic_probe as _}; #[embassy_executor::main] @@ -17,7 +15,7 @@ async fn main(_spawner: Spawner) { let ch1 = PwmPin::new_cha(p.PA8); let ch1n = ComplementaryPwmPin::new_cha(p.PA9); - let mut pwm = AdvancedPwm::new( + let pwm = AdvancedPwm::new( p.HRTIM1, Some(ch1), Some(ch1n), -- cgit From 21a86531955facbdba5583b5218e20f1ca6dc23e Mon Sep 17 00:00:00 2001 From: xoviat Date: Sat, 1 Jul 2023 17:32:16 -0500 Subject: hrtim: minor cleanup --- examples/stm32f334/.cargo/config.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/stm32f334/.cargo/config.toml b/examples/stm32f334/.cargo/config.toml index 7f3fda529..caf947be6 100644 --- a/examples/stm32f334/.cargo/config.toml +++ b/examples/stm32f334/.cargo/config.toml @@ -1,6 +1,6 @@ [target.'cfg(all(target_arch = "arm", target_os = "none"))'] # replace STM32F429ZITx with your chip as listed in `probe-rs-cli chip list` -runner = "probe-rs-cli run --chip STM32F303ZETx" +runner = "probe-run --chip STM32F334R8" [build] target = "thumbv7em-none-eabihf" -- cgit From aceba1c03fc45179d4e910fad254a31191cf0c44 Mon Sep 17 00:00:00 2001 From: xoviat Date: Sat, 1 Jul 2023 21:47:44 -0500 Subject: hrtim: fix example and auto adjust psc. --- examples/stm32f334/src/bin/button.rs | 27 +++++++++++++++ examples/stm32f334/src/bin/pwm.rs | 65 ++++++++++++++++++++---------------- 2 files changed, 64 insertions(+), 28 deletions(-) create mode 100644 examples/stm32f334/src/bin/button.rs (limited to 'examples') diff --git a/examples/stm32f334/src/bin/button.rs b/examples/stm32f334/src/bin/button.rs new file mode 100644 index 000000000..599c0f27d --- /dev/null +++ b/examples/stm32f334/src/bin/button.rs @@ -0,0 +1,27 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use defmt::*; +use embassy_executor::Spawner; +use embassy_stm32::gpio::{Level, Output, Speed}; +use embassy_time::{Duration, Timer}; +use {defmt_rtt as _, panic_probe as _}; + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + info!("Hello World!"); + + let p = embassy_stm32::init(Default::default()); + + let mut out1 = Output::new(p.PA8, Level::Low, Speed::High); + + out1.set_high(); + Timer::after(Duration::from_millis(500)).await; + out1.set_low(); + + Timer::after(Duration::from_millis(500)).await; + info!("end program"); + + cortex_m::asm::bkpt(); +} diff --git a/examples/stm32f334/src/bin/pwm.rs b/examples/stm32f334/src/bin/pwm.rs index 1b5d509e7..364119744 100644 --- a/examples/stm32f334/src/bin/pwm.rs +++ b/examples/stm32f334/src/bin/pwm.rs @@ -5,12 +5,20 @@ use defmt::*; use embassy_executor::Spawner; use embassy_stm32::pwm::advanced_pwm::*; -use embassy_stm32::time::khz; +use embassy_stm32::time::{khz, mhz}; +use embassy_stm32::Config; +use embassy_time::{Duration, Timer}; use {defmt_rtt as _, panic_probe as _}; #[embassy_executor::main] async fn main(_spawner: Spawner) { - let p = embassy_stm32::init(Default::default()); + let mut config: Config = Default::default(); + config.rcc.sysclk = Some(mhz(64)); + config.rcc.hclk = Some(mhz(64)); + config.rcc.pclk1 = Some(mhz(32)); + config.rcc.pclk2 = Some(mhz(64)); + + let p = embassy_stm32::init(config); info!("Hello World!"); let ch1 = PwmPin::new_cha(p.PA8); @@ -29,34 +37,35 @@ async fn main(_spawner: Spawner) { None, ); - let mut buck_converter = BridgeConverter::new(pwm.ch_a, khz(100)); - - buck_converter.set_primary_duty(0); - buck_converter.set_secondary_duty(0); - buck_converter.set_dead_time(0); - - // note: if the pins are not passed into the advanced pwm struct, they will not be output - let mut boost_converter = BridgeConverter::new(pwm.ch_b, khz(100)); + info!("pwm constructed"); - boost_converter.set_primary_duty(0); - boost_converter.set_secondary_duty(0); + let mut buck_converter = BridgeConverter::new(pwm.ch_a, khz(5)); - // let max = pwm.get_max_duty(); - // pwm.set_dead_time(max / 1024); + // embassy_stm32::pac::HRTIM1 + // .tim(0) + // .setr(0) + // .modify(|w| w.set_sst(Activeeffect::SETACTIVE)); // - // pwm.enable(Channel::Ch1); + // Timer::after(Duration::from_millis(500)).await; // - // info!("PWM initialized"); - // info!("PWM max duty {}", max); - // - // loop { - // pwm.set_duty(Channel::Ch1, 0); - // Timer::after(Duration::from_millis(300)).await; - // pwm.set_duty(Channel::Ch1, max / 4); - // Timer::after(Duration::from_millis(300)).await; - // pwm.set_duty(Channel::Ch1, max / 2); - // Timer::after(Duration::from_millis(300)).await; - // pwm.set_duty(Channel::Ch1, max - 1); - // Timer::after(Duration::from_millis(300)).await; - // } + // embassy_stm32::pac::HRTIM1 + // .tim(0) + // .rstr(0) + // .modify(|w| w.set_srt(Inactiveeffect::SETINACTIVE)); + + let max_duty = buck_converter.get_max_compare_value(); + + info!("max compare value: {}", max_duty); + + buck_converter.set_dead_time(max_duty / 20); + buck_converter.set_primary_duty(max_duty / 2); + buck_converter.set_secondary_duty(3 * max_duty / 4); + + buck_converter.start(); + + Timer::after(Duration::from_millis(500)).await; + + info!("end program"); + + cortex_m::asm::bkpt(); } -- cgit From 28136579e98ea4d9745867d7c1ffff0ad826b504 Mon Sep 17 00:00:00 2001 From: xoviat Date: Fri, 28 Jul 2023 17:07:08 -0500 Subject: stm32/hrtim: extract into mod --- examples/stm32f334/src/bin/pwm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/stm32f334/src/bin/pwm.rs b/examples/stm32f334/src/bin/pwm.rs index 364119744..2660b10c5 100644 --- a/examples/stm32f334/src/bin/pwm.rs +++ b/examples/stm32f334/src/bin/pwm.rs @@ -4,7 +4,7 @@ use defmt::*; use embassy_executor::Spawner; -use embassy_stm32::pwm::advanced_pwm::*; +use embassy_stm32::hrtim::*; use embassy_stm32::time::{khz, mhz}; use embassy_stm32::Config; use embassy_time::{Duration, Timer}; -- cgit