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