Skip to content

Commit

Permalink
HRTIM - Fix some clippy warnings and fmt - examples
Browse files Browse the repository at this point in the history
  • Loading branch information
usbalbin committed Nov 25, 2024
1 parent a79d4b4 commit 1ea1347
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 112 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ name = "hrtim-capture"
required-features = ["hrtim"]
path = "examples/hrtim/capture.rs"

[[example]]
name = "hrtim-capture-dma"
required-features = ["hrtim"]
path = "examples/hrtim/capture-dma.rs"

[[example]]
name = "hrtim-eev-comp"
required-features = ["hrtim"]
Expand Down
10 changes: 9 additions & 1 deletion examples/hrtim/adc-trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
mod utils;

use cortex_m_rt::entry;
use stm32g4xx_hal::hrtim::HrParts;
use utils::logger::info;

#[entry]
Expand Down Expand Up @@ -87,7 +88,14 @@ fn main() -> ! {
let period = 0xFFFF;
let (hr_control, ..) = dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration();
let mut hr_control = hr_control.constrain();
let (mut timer, (mut cr1, _cr2, mut cr3, mut cr4), (mut out1, mut out2), ..) = dp
let HrParts {
mut timer,
mut cr1,
mut cr3,
mut cr4,
out: (mut out1, mut out2),
..
} = dp
.HRTIM_TIMA
.pwm_advanced((pin_a, pin_b), &mut rcc)
.prescaler(prescaler)
Expand Down
23 changes: 17 additions & 6 deletions examples/hrtim/capture-dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
mod utils;

use cortex_m_rt::entry;
use stm32g4xx_hal::hrtim::{timer::TimerSplitCapture, HrParts};
use utils::logger::info;

#[entry]
Expand All @@ -15,7 +16,7 @@ fn main() -> ! {

use hal::{
dma::{config::DmaConfig, stream::DMAExt, TransferExt},
gpio::{GpioExt, AF13},
gpio::GpioExt,
hrtim::{
capture, compare_register::HrCompareRegister, control::HrControltExt, external_event,
external_event::ToExternalEventSource, output::HrOutput, timer::HrSlaveTimerCpt,
Expand All @@ -37,7 +38,7 @@ fn main() -> ! {
let pwr = dp.PWR.constrain().freeze();
let mut rcc = dp.RCC.freeze(
rcc::Config::pll().pll_cfg(rcc::PllConfig {
mux: rcc::PLLSrc::HSI,
mux: rcc::PllSrc::HSI,
n: rcc::PllNMul::MUL_15,
m: rcc::PllMDiv::DIV_1,
r: Some(rcc::PllRDiv::DIV_2),
Expand Down Expand Up @@ -83,7 +84,13 @@ fn main() -> ! {
.finalize(&mut hr_control);

let mut hr_control = hr_control.constrain();
let (timer, (mut cr1, _cr2, _cr3, _cr4), mut out1, dma_ch) = dp
let HrParts {
timer,
mut cr1,
out: mut out1,
dma_channel,
..
} = dp
.HRTIM_TIMA
.pwm_advanced(pin_a, &mut rcc)
.prescaler(prescaler)
Expand All @@ -93,7 +100,11 @@ fn main() -> ! {
out1.enable_set_event(&timer); // Set high at new period
cr1.set_duty(period / 2);

let (mut timer, mut capture, _capture_ch2) = timer.split_capture();
let TimerSplitCapture {
mut timer,
ch1: mut capture,
..
} = timer.split_capture();
timer.start(&mut hr_control.control);
out1.enable();

Expand All @@ -109,7 +120,7 @@ fn main() -> ! {

let first_buffer = cortex_m::singleton!(: [u32; 16] = [0; 16]).unwrap();
let mut transfer = streams.0.into_circ_peripheral_to_memory_transfer(
capture.enable_dma(dma_ch),
capture.enable_dma(dma_channel),
&mut first_buffer[..],
config,
);
Expand All @@ -121,7 +132,7 @@ fn main() -> ! {
for duty in (u32::from(period) / 10)..(9 * u32::from(period) / 10) {
let mut data = [0; 2];
transfer.read_exact(&mut data);
let [t1, t2] = data.map(capture::dma_value_to_signed);
let [t1, t2] = data.map(|x| capture::dma_value_to_signed(x, period));
cr1.set_duty(duty as u16);
info!("Capture: t1: {}, t2: {}, duty: {}, ", t1, t2, old_duty);
old_duty = duty;
Expand Down
14 changes: 10 additions & 4 deletions examples/hrtim/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
mod utils;

use cortex_m_rt::entry;
use stm32g4xx_hal::hrtim::HrParts;
use utils::logger::info;

#[entry]
Expand Down Expand Up @@ -79,19 +80,24 @@ fn main() -> ! {
.finalize(&mut hr_control);

let mut hr_control = hr_control.constrain();
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1, ..) = dp
let HrParts {
mut timer,
mut cr1,
mut out,
..
} = dp
.HRTIM_TIMA
.pwm_advanced(pin_a, &mut rcc)
.prescaler(prescaler)
.period(period)
.finalize(&mut hr_control);

out1.enable_rst_event(&cr1); // Set low on compare match with cr1
out1.enable_set_event(&timer); // Set high at new period
out.enable_rst_event(&cr1); // Set low on compare match with cr1
out.enable_set_event(&timer); // Set high at new period

cr1.set_duty(period / 2);
timer.start(&mut hr_control.control);
out1.enable();
out.enable();

let capture = timer.capture_ch1();
capture.enable_interrupt(true, &mut hr_control);
Expand Down
16 changes: 11 additions & 5 deletions examples/hrtim/eev-comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
mod utils;

use cortex_m_rt::entry;
use stm32g4xx_hal::hrtim::HrParts;
use utils::logger::info;

#[entry]
Expand Down Expand Up @@ -107,20 +108,25 @@ fn main() -> ! {
// ------------------------- .--------------------------------------
// . . * . . .
// . . * . . .
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1, ..) = dp
let HrParts {
mut timer,
mut cr1,
mut out,
..
} = dp
.HRTIM_TIMA
.pwm_advanced(pin_a, &mut rcc)
.prescaler(prescaler)
.eev_cfg(EevCfgs::default().eev4(EevCfg::default()))
.period(0xFFFF)
.finalize(&mut hr_control);

out1.enable_rst_event(&cr1); // Set low on compare match with cr1
out1.enable_rst_event(&eev_input4);
out1.enable_set_event(&timer); // Set high at new period
out.enable_rst_event(&cr1); // Set low on compare match with cr1
out.enable_rst_event(&eev_input4);
out.enable_set_event(&timer); // Set high at new period
cr1.set_duty(timer.get_period() / 3);

out1.enable();
out.enable();
timer.start(&mut hr_control.control);

info!("Started");
Expand Down
16 changes: 11 additions & 5 deletions examples/hrtim/eev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
mod utils;

use cortex_m_rt::entry;
use stm32g4xx_hal::hrtim::HrParts;
use utils::logger::info;

#[entry]
Expand Down Expand Up @@ -79,20 +80,25 @@ fn main() -> ! {
// ------------------------- ------------------------------------------
// . . * . . .
// . . * . . .
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1, ..) = dp
let HrParts {
mut timer,
mut cr1,
mut out,
..
} = dp
.HRTIM_TIMA
.pwm_advanced(pin_a, &mut rcc)
.prescaler(prescaler)
.eev_cfg(EevCfgs::default())
.period(0xFFFF)
.finalize(&mut hr_control);

out1.enable_rst_event(&cr1); // Set low on compare match with cr1
out1.enable_rst_event(&eev_input3);
out1.enable_set_event(&timer); // Set high at new period
out.enable_rst_event(&cr1); // Set low on compare match with cr1
out.enable_rst_event(&eev_input3);
out.enable_set_event(&timer); // Set high at new period
cr1.set_duty(timer.get_period() / 3);

out1.enable();
out.enable();
timer.start(&mut hr_control.control);

info!("Started");
Expand Down
8 changes: 7 additions & 1 deletion examples/hrtim/flt-comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
mod utils;

use cortex_m_rt::entry;
use stm32g4xx_hal::hrtim::HrParts;
use utils::logger::info;

#[entry]
Expand Down Expand Up @@ -108,7 +109,12 @@ fn main() -> ! {
// ----------------------------------------- --------------------------
// . . . * . .
// . . . * . .
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1, ..) = dp
let HrParts {
mut timer,
mut cr1,
out: mut out1,
..
} = dp
.HRTIM_TIMA
.pwm_advanced(pin_a, &mut rcc)
.prescaler(prescaler)
Expand Down
18 changes: 12 additions & 6 deletions examples/hrtim/flt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
mod utils;

use cortex_m_rt::entry;
use stm32g4xx_hal::hrtim::HrParts;
use utils::logger::info;

#[entry]
Expand Down Expand Up @@ -74,7 +75,12 @@ fn main() -> ! {
// ----------------------------------------- --------------------------
// . . . * . .
// . . . * . .
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), mut out1, ..) = dp
let HrParts {
mut timer,
mut cr1,
mut out,
..
} = dp
.HRTIM_TIMA
.pwm_advanced(pin_a, &mut rcc)
.prescaler(prescaler)
Expand All @@ -92,23 +98,23 @@ fn main() -> ! {
// as normal
.finalize(&mut hr_control);

out1.enable_rst_event(&cr1); // Set low on compare match with cr1
out1.enable_set_event(&timer); // Set high at new period
out.enable_rst_event(&cr1); // Set low on compare match with cr1
out.enable_set_event(&timer); // Set high at new period
cr1.set_duty(timer.get_period() / 3);
//unsafe {((HRTIM_COMMON::ptr() as *mut u8).offset(0x14) as *mut u32).write_volatile(1); }
out1.enable();
out.enable();
timer.start(&mut hr_control.control);

info!("Started");

loop {
for _ in 0..5 {
delay.delay(500_u32.millis());
info!("State: {:?}", out1.get_state());
info!("State: {:?}", out.get_state());
}
if hr_control.fault_3.is_fault_active() {
hr_control.fault_3.clear_fault(); // Clear fault every 5s
out1.enable();
out.enable();
info!("failt cleared, and output reenabled");
}
}
Expand Down
8 changes: 7 additions & 1 deletion examples/hrtim/hrtim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
mod utils;

use cortex_m_rt::entry;
use stm32g4xx_hal::hrtim::HrParts;
use utils::logger::info;

#[entry]
Expand Down Expand Up @@ -69,7 +70,12 @@ fn main() -> ! {
let (hr_control, ..) = dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration();
let mut hr_control = hr_control.constrain();

let (mut timer, (mut cr1, _cr2, _cr3, _cr4), (mut out1, mut out2), ..) = dp
let HrParts {
mut timer,
mut cr1,
out: (mut out1, mut out2),
..
} = dp
.HRTIM_TIMA
.pwm_advanced((pin_a, pin_b), &mut rcc)
.prescaler(prescaler)
Expand Down
14 changes: 12 additions & 2 deletions examples/hrtim/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
mod utils;

use cortex_m_rt::entry;
use stm32g4xx_hal::hrtim::HrParts;
use utils::logger::info;

#[entry]
Expand Down Expand Up @@ -68,7 +69,12 @@ fn main() -> ! {
let (hr_control, ..) = dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration();
let mut hr_control = hr_control.constrain();

let (mut timer, (mut cr1, _cr2, _cr3, _cr4), (mut out1, mut out2), ..) = dp
let HrParts {
mut timer,
mut cr1,
out: (mut out1, mut out2),
..
} = dp
.HRTIM_TIMA
.pwm_advanced((pin_a, pin_b), &mut rcc)
.prescaler(prescaler)
Expand All @@ -80,7 +86,11 @@ fn main() -> ! {
.timer_mode(HrTimerMode::SingleShotRetriggerable)
.finalize(&mut hr_control);

let (mut mtimer, (mut mcr1, _mcr2, _mcr3, _mcr4), ..) = dp
let HrParts {
timer: mut mtimer,
cr1: mut mcr1,
..
} = dp
.HRTIM_MASTER
.pwm_advanced((), &mut rcc)
.prescaler(prescaler)
Expand Down
Loading

0 comments on commit 1ea1347

Please sign in to comment.