Skip to content

Commit 749c064

Browse files
committed
HRTIM: Update examples
1 parent b97dada commit 749c064

File tree

9 files changed

+112
-49
lines changed

9 files changed

+112
-49
lines changed

examples/hrtim/adc-trigger.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
#![no_std]
22
#![no_main]
33

4+
/// Example showcasing the use of the HRTIM peripheral to trigger the ADC at various points of the switch cycle off HRTIM_TIMA
5+
6+
#[path = "../utils/mod.rs"]
7+
mod utils;
8+
49
use cortex_m_rt::entry;
510

611
use defmt_rtt as _; // global logger
712
use panic_probe as _;
813

14+
use utils::logger::info;
15+
916
#[entry]
1017
fn main() -> ! {
1118
use hal::adc;
1219
use stm32g4xx_hal as hal;
1320

14-
use defmt::info;
1521
use hal::{
1622
adc::{
1723
config::{Continuous, Dma as AdcDma, SampleTime, Sequence},

examples/hrtim/capture.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
#![no_std]
22
#![no_main]
33

4+
/// Example showcasing the use of the HRTIM peripheral's capture function to detect phase shift between a digital event and the output of HRTIM_TIMA
5+
6+
#[path = "../utils/mod.rs"]
7+
mod utils;
8+
49
use cortex_m_rt::entry;
510

611
use defmt_rtt as _; // global logger
712
use panic_probe as _;
813

14+
use utils::logger::info;
15+
916
#[entry]
1017
fn main() -> ! {
1118
use stm32g4xx_hal as hal;
1219

13-
use defmt::info;
1420
use hal::{
1521
gpio::{gpioa::PA8, Alternate, GpioExt, AF13},
1622
hrtim::{
@@ -22,6 +28,7 @@ fn main() -> ! {
2228
rcc::{self, RccExt},
2329
stm32::Peripherals,
2430
};
31+
use info;
2532

2633
info!("start");
2734

@@ -101,7 +108,12 @@ fn main() -> ! {
101108
let value = capture.get_signed();
102109
cr1.set_duty(duty as u16);
103110
capture.clear_interrupt();
104-
info!("Capture: {:?}, duty: {}, diff: {}", value, old_duty, value - old_duty as i32);
111+
info!(
112+
"Capture: {:?}, duty: {}, diff: {}",
113+
value,
114+
old_duty,
115+
value - old_duty as i32
116+
);
105117
old_duty = duty;
106118
}
107119
}

examples/hrtim/eev-comp.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
//This example puts the timer in PWM mode using the specified pin with a frequency of 100Hz and a duty cycle of 50%.
2-
#![no_main]
31
#![no_std]
2+
#![no_main]
3+
4+
/// Example showcasing the use of the HRTIM peripheral together with a comparator to implement a cycle by cycle current limit.
5+
/// Once the comparator input exceeds the reference set by the DAC, the output is set low thus limiting the pulse width and in turn the current.
6+
7+
#[path = "../utils/mod.rs"]
8+
mod utils;
49

510
use cortex_m_rt::entry;
611

712
use defmt_rtt as _; // global logger
813
use panic_probe as _;
914

15+
use utils::logger::info;
16+
1017
#[entry]
1118
fn main() -> ! {
1219
use hal::comparator;
@@ -116,17 +123,17 @@ fn main() -> ! {
116123
.finalize(&mut hr_control);
117124

118125
out1.enable_rst_event(&cr1); // Set low on compare match with cr1
119-
out1.enable_rst_event(eev_input4);
126+
out1.enable_rst_event(&eev_input4);
120127
out1.enable_set_event(&timer); // Set high at new period
121128
cr1.set_duty(timer.get_period() / 3);
122129

123130
out1.enable();
124131
timer.start(&mut hr_control);
125132

126-
defmt::info!("Started");
133+
info!("Started");
127134

128135
loop {
129-
defmt::info!(
136+
info!(
130137
"Comp: {}, pending: {}",
131138
comp1.output(),
132139
comp1.is_pending(&exti)

examples/hrtim/eev.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
//This example puts the timer in PWM mode using the specified pin with a frequency of 100Hz and a duty cycle of 50%.
2-
#![no_main]
31
#![no_std]
2+
#![no_main]
43

5-
use cortex_m_rt::entry;
4+
/// Example showcasing the use of the HRTIM peripheral together with a digital input to implement a cycle by cycle current limit.
5+
/// Once the digital input goes high, the output is set low thus limiting the pulse width and in turn the current.
66
7-
//mod utils;
7+
#[path = "../utils/mod.rs"]
8+
mod utils;
9+
10+
use cortex_m_rt::entry;
811

912
use defmt_rtt as _; // global logger
1013
use panic_probe as _;
1114

15+
use utils::logger::info;
16+
1217
#[entry]
1318
fn main() -> ! {
1419
use hal::gpio::gpioa::PA8;
@@ -90,14 +95,14 @@ fn main() -> ! {
9095
.finalize(&mut hr_control);
9196

9297
out1.enable_rst_event(&cr1); // Set low on compare match with cr1
93-
out1.enable_rst_event(eev_input3);
98+
out1.enable_rst_event(&eev_input3);
9499
out1.enable_set_event(&timer); // Set high at new period
95100
cr1.set_duty(timer.get_period() / 3);
96101

97102
out1.enable();
98103
timer.start(&mut hr_control);
99104

100-
defmt::info!("Started");
105+
info!("Started");
101106

102107
loop {
103108
cortex_m::asm::nop()

examples/hrtim/flt-comp.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
//This example puts the timer in PWM mode using the specified pin with a frequency of 100Hz and a duty cycle of 50%.
2-
#![no_main]
31
#![no_std]
2+
#![no_main]
3+
4+
/// Example showcasing the use of the HRTIM peripheral together with a comparator to implement a current fault.
5+
/// Once the comparator input exceeds the reference set by the DAC, the output is forced low and put into a fault state.
6+
7+
#[path = "../utils/mod.rs"]
8+
mod utils;
49

510
use cortex_m_rt::entry;
6-
//mod utils;
711

812
use defmt_rtt as _; // global logger
913
use panic_probe as _;
1014

15+
use utils::logger::info;
16+
1117
#[entry]
1218
fn main() -> ! {
1319
use hal::comparator::{ComparatorExt, ComparatorSplit, Config, Hysteresis};
@@ -110,7 +116,7 @@ fn main() -> ! {
110116
// ----------------------------------------- --------------------------
111117
// . . . * . .
112118
// . . . * . .
113-
let (timer, (mut cr1, _cr2, _cr3, _cr4), mut out1) = dp
119+
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1) = dp
114120
.HRTIM_TIMA
115121
.pwm_advanced(pin_a, &mut rcc)
116122
.prescaler(prescaler)
@@ -125,13 +131,14 @@ fn main() -> ! {
125131
cr1.set_duty(timer.get_period() / 3);
126132
//unsafe {((HRTIM_COMMON::ptr() as *mut u8).offset(0x14) as *mut u32).write_volatile(1); }
127133
out1.enable();
134+
timer.start(&mut hr_control);
128135

129-
defmt::info!("Started");
136+
info!("Started");
130137

131138
loop {
132139
for _ in 0..5 {
133140
//delay.delay(500_u32.millis());
134-
defmt::info!(
141+
info!(
135142
"State: {}, comp: {}, is_fault_active: {}, pc1: {}",
136143
out1.get_state(),
137144
comp3.output(),
@@ -142,7 +149,7 @@ fn main() -> ! {
142149
if hr_control.fault_5.is_fault_active() {
143150
hr_control.fault_5.clear_fault(); // Clear fault every 5s
144151
out1.enable();
145-
defmt::info!("failt cleared, and output reenabled");
152+
info!("failt cleared, and output reenabled");
146153
}
147154
}
148155
}

examples/hrtim/flt.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
//This example puts the timer in PWM mode using the specified pin with a frequency of 100Hz and a duty cycle of 50%.
2-
#![no_main]
31
#![no_std]
2+
#![no_main]
3+
4+
/// Example showcasing the use of the HRTIM peripheral together with a comparator to implement a current fault.
5+
/// Once the digital input goes high, the output is forced low and put into a fault state.
6+
7+
#[path = "../utils/mod.rs"]
8+
mod utils;
49

510
use cortex_m_rt::entry;
6-
//mod utils;
711

812
use defmt_rtt as _; // global logger
913
use panic_probe as _;
1014

15+
use utils::logger::info;
16+
1117
#[entry]
1218
fn main() -> ! {
1319
use hal::gpio::gpioa::PA8;
@@ -76,7 +82,7 @@ fn main() -> ! {
7682
// ----------------------------------------- --------------------------
7783
// . . . * . .
7884
// . . . * . .
79-
let (timer, (mut cr1, _cr2, _cr3, _cr4), mut out1) = dp
85+
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1) = dp
8086
.HRTIM_TIMA
8187
.pwm_advanced(pin_a, &mut rcc)
8288
.prescaler(prescaler)
@@ -99,18 +105,19 @@ fn main() -> ! {
99105
cr1.set_duty(timer.get_period() / 3);
100106
//unsafe {((HRTIM_COMMON::ptr() as *mut u8).offset(0x14) as *mut u32).write_volatile(1); }
101107
out1.enable();
108+
timer.start(&mut hr_control);
102109

103-
defmt::info!("Started");
110+
info!("Started");
104111

105112
loop {
106113
for _ in 0..5 {
107114
delay.delay(500_u32.millis());
108-
defmt::info!("State: {}", out1.get_state());
115+
info!("State: {}", out1.get_state());
109116
}
110117
if hr_control.fault_3.is_fault_active() {
111118
hr_control.fault_3.clear_fault(); // Clear fault every 5s
112119
out1.enable();
113-
defmt::info!("failt cleared, and output reenabled");
120+
info!("failt cleared, and output reenabled");
114121
}
115122
}
116123
}

examples/hrtim/hrtim.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
//This example puts the timer in PWM mode using the specified pin with a frequency of 100Hz and a duty cycle of 50%.
2-
#![no_main]
31
#![no_std]
2+
#![no_main]
3+
4+
/// Add description
5+
6+
#[path = "../utils/mod.rs"]
7+
mod utils;
48

59
use cortex_m_rt::entry;
610

711
use defmt_rtt as _; // global logger
812
use panic_probe as _;
913

14+
use utils::logger::info;
15+
1016
#[entry]
1117
fn main() -> ! {
1218
use fugit::ExtU32;
@@ -27,6 +33,8 @@ fn main() -> ! {
2733
use stm32g4xx_hal as hal;
2834
extern crate cortex_m_rt as rt;
2935

36+
info!("Initializing...");
37+
3038
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
3139
let cp = stm32::CorePeripherals::take().expect("cannot take core");
3240
// Set system frequency to 16MHz * 15/1/2 = 120MHz

examples/hrtim/master.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
//This example puts the timer in PWM mode using the specified pin with a frequency of 100Hz and a duty cycle of 50%.
2-
#![no_main]
31
#![no_std]
2+
#![no_main]
3+
4+
/// Add description
5+
6+
#[path = "../utils/mod.rs"]
7+
mod utils;
48

59
use cortex_m_rt::entry;
610

11+
use defmt_rtt as _; // global logger
12+
use panic_probe as _;
13+
14+
use utils::logger::info;
15+
716
#[entry]
817
fn main() -> ! {
918
use fugit::ExtU32;
@@ -16,9 +25,7 @@ fn main() -> ! {
1625
use hal::hrtim::output::HrOutput;
1726
use hal::hrtim::timer::HrTimer;
1827
use hal::hrtim::HrPwmAdvExt;
19-
use hal::hrtim::{
20-
event::TimerAResetEventSource, HrTimerMode, MasterPreloadSource, PreloadSource, Pscl4,
21-
};
28+
use hal::hrtim::{HrTimerMode, MasterPreloadSource, PreloadSource, Pscl4};
2229
use hal::prelude::*;
2330
use hal::pwr::PwrExt;
2431
use hal::rcc;
@@ -89,7 +96,7 @@ fn main() -> ! {
8996
.finalize(&mut hr_control);
9097

9198
// Run in sync with master timer
92-
timer.enable_reset_event(TimerAResetEventSource::MasterPeriod);
99+
timer.enable_reset_event(&mtimer);
93100

94101
out1.enable_rst_event(&mcr1); // Set low on compare match with cr1
95102
out2.enable_rst_event(&mcr1);
@@ -101,13 +108,13 @@ fn main() -> ! {
101108
out2.enable();
102109

103110
let tima = unsafe { &*stm32g4xx_hal::stm32::HRTIM_TIMA::ptr() };
104-
defmt::info!("set1r: {}", tima.seta1r.read().bits());
105-
defmt::info!("rst1r: {}", tima.rsta1r.read().bits());
111+
info!("set1r: {}", tima.seta1r.read().bits());
112+
info!("rst1r: {}", tima.rsta1r.read().bits());
106113

107-
defmt::info!("set2r: {}", tima.seta2r.read().bits());
108-
defmt::info!("rst2r: {}", tima.rsta2r.read().bits());
114+
info!("set2r: {}", tima.seta2r.read().bits());
115+
info!("rst2r: {}", tima.rsta2r.read().bits());
109116

110-
defmt::info!("Running");
117+
info!("Running");
111118

112119
loop {
113120
// Step frequency from 18kHz to about 180kHz(half of that when only looking at one pin)
@@ -119,7 +126,7 @@ fn main() -> ! {
119126
mtimer.set_period(new_period);
120127
timer.set_period(new_period - 1000);
121128

122-
defmt::info!(
129+
info!(
123130
"period: {}, duty: {}, get_duty: {}, get_period: {}",
124131
new_period,
125132
new_period / 3,

0 commit comments

Comments
 (0)