diff options
| -rw-r--r-- | examples/stm32g0/src/bin/hf_timer.rs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/examples/stm32g0/src/bin/hf_timer.rs b/examples/stm32g0/src/bin/hf_timer.rs new file mode 100644 index 000000000..475568a23 --- /dev/null +++ b/examples/stm32g0/src/bin/hf_timer.rs | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::info; | ||
| 5 | use {defmt_rtt as _, panic_probe as _}; | ||
| 6 | |||
| 7 | use embassy_executor::Spawner; | ||
| 8 | use embassy_stm32::{ | ||
| 9 | gpio::OutputType, | ||
| 10 | pac, | ||
| 11 | pac::rcc::vals::Tim1sel, | ||
| 12 | rcc::{ClockSrc, Config as RccConfig, PllConfig, PllSource, Pllm, Plln, Pllq, Pllr}, | ||
| 13 | time::khz, | ||
| 14 | timer::{ | ||
| 15 | complementary_pwm::{ComplementaryPwm, ComplementaryPwmPin}, | ||
| 16 | simple_pwm::PwmPin, | ||
| 17 | Channel, | ||
| 18 | }, | ||
| 19 | Config as PeripheralConfig, | ||
| 20 | }; | ||
| 21 | |||
| 22 | #[embassy_executor::main] | ||
| 23 | async fn main(_spawner: Spawner) { | ||
| 24 | let mut rcc_config = RccConfig::default(); | ||
| 25 | rcc_config.mux = ClockSrc::PLL(PllConfig { | ||
| 26 | source: PllSource::HSI, | ||
| 27 | m: Pllm::DIV1, | ||
| 28 | n: Plln::MUL16, | ||
| 29 | r: Pllr::DIV4, // CPU clock comes from PLLR (HSI (16MHz) / 1 * 16 / 4 = 64MHz) | ||
| 30 | q: Some(Pllq::DIV2), // TIM1 or TIM15 can be sourced from PLLQ (HSI (16MHz) / 1 * 16 / 2 = 128MHz) | ||
| 31 | p: None, | ||
| 32 | }); | ||
| 33 | |||
| 34 | let mut peripheral_config = PeripheralConfig::default(); | ||
| 35 | peripheral_config.rcc = rcc_config; | ||
| 36 | |||
| 37 | let p = embassy_stm32::init(peripheral_config); | ||
| 38 | |||
| 39 | // configure TIM1 mux to select PLLQ as clock source | ||
| 40 | // https://www.st.com/resource/en/reference_manual/rm0444-stm32g0x1-advanced-armbased-32bit-mcus-stmicroelectronics.pdf | ||
| 41 | // RM0444 page 210 | ||
| 42 | // RCC - Peripherals Independent Clock Control Register - bit 22 -> 1 | ||
| 43 | pac::RCC.ccipr().modify(|w| w.set_tim1sel(Tim1sel::PLL1_Q)); | ||
| 44 | |||
| 45 | let ch1 = PwmPin::new_ch1(p.PA8, OutputType::PushPull); | ||
| 46 | let ch1n = ComplementaryPwmPin::new_ch1(p.PA7, OutputType::PushPull); | ||
| 47 | |||
| 48 | let mut pwm = ComplementaryPwm::new( | ||
| 49 | p.TIM1, | ||
| 50 | Some(ch1), | ||
| 51 | Some(ch1n), | ||
| 52 | None, | ||
| 53 | None, | ||
| 54 | None, | ||
| 55 | None, | ||
| 56 | None, | ||
| 57 | None, | ||
| 58 | khz(512), | ||
| 59 | Default::default(), | ||
| 60 | ); | ||
| 61 | |||
| 62 | let max = pwm.get_max_duty(); | ||
| 63 | info!("Max duty: {}", max); | ||
| 64 | |||
| 65 | pwm.set_duty(Channel::Ch1, max / 2); | ||
| 66 | pwm.enable(Channel::Ch1); | ||
| 67 | |||
| 68 | loop {} | ||
| 69 | } | ||
