Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HRTIM #146

Closed
wants to merge 85 commits into from
Closed

HRTIM #146

Show file tree
Hide file tree
Changes from 75 commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
e3c068e
WIP
usbalbin May 9, 2023
e078a63
Untested sort of maybe working chaos
usbalbin May 20, 2023
c50ba20
Dual channel support and cargo fmt
usbalbin May 20, 2023
c83eb2a
Almost...
usbalbin May 20, 2023
a74e315
Still untested but might actually work, maybe
usbalbin May 20, 2023
aac01f6
Adjust doc
usbalbin May 20, 2023
2e570cd
Enable outputs in example
usbalbin May 20, 2023
9b3f820
Further cleanup
usbalbin May 20, 2023
10f5edf
Tested and verified on NUCLEO-G474RE
usbalbin Jun 7, 2023
182e84a
hrtim_simple also tested and verified on NUCLEO-G474RE
usbalbin Jun 7, 2023
26f795b
Hrtim cleanup, add pins for all timers but HRTIM_TIMF
usbalbin Jun 9, 2023
bb70f52
Several changes and fixes
usbalbin Jun 20, 2023
d9f1b3f
Start work on hrtim fault
usbalbin Jun 12, 2023
2832f9e
Add hrtim fault example
usbalbin Jun 20, 2023
0e4367f
TOO MANY CHANGES
usbalbin Jun 28, 2023
1e5786d
Add ADC triggers among a lot of things to hrtim
usbalbin Aug 25, 2023
0814706
Disable push pull
usbalbin Aug 25, 2023
832a087
NOT DONE - Losts of hrtim stuff
usbalbin Sep 4, 2023
62ec4c7
Multiple hrtim changes
usbalbin Oct 24, 2023
7a17a54
HRTIM: Fix bad pin mapping
usbalbin Oct 24, 2023
16302ca
HRTIM - Rework EventSource system and add deadtime support
usbalbin Oct 24, 2023
2baed51
HRTIM refactor
usbalbin Nov 3, 2023
95cd969
More hrtim refactor
usbalbin Nov 4, 2023
b3e3e34
EEV
usbalbin Nov 5, 2023
4fbea36
More work on hrtim eev
usbalbin Nov 6, 2023
0158346
even more eev hrtim
usbalbin Nov 6, 2023
c125087
more eev hrtim
usbalbin Nov 7, 2023
7ddc5da
Update hrtim examples. TODO: Make sure timers are started
usbalbin Nov 8, 2023
8e947de
HRTIM examples
usbalbin Nov 8, 2023
a53ebba
Fix warnings for hrtim
usbalbin Nov 8, 2023
6d46313
HRTIM - Traitify adc_triggers
usbalbin Jan 6, 2024
167cc79
HRTIM: Start work on capture support
usbalbin Jan 17, 2024
3554c5c
HRTIM: Add capture.rs
usbalbin Jan 18, 2024
c0adecb
HRTIM: Impl CompareEvent for more types
usbalbin Jan 18, 2024
237cc86
Fmt
usbalbin Jan 18, 2024
c86ae89
HRTIM: Traitify events
usbalbin Jan 18, 2024
d1a02b9
HRTIM: Fix bugs caused by traitification
usbalbin Jan 19, 2024
e086e65
HRTIM: Update adc-trigger example for traitification changes
usbalbin Jan 19, 2024
355b4c7
HRTIM: Start timer and set trigger points in adc-trigger example
usbalbin Jan 19, 2024
1cd72e2
HRTIM: Refactor adc trigger
usbalbin Jan 19, 2024
8886a78
HRTIM: Refactor adc trigger - example hrtim-adc-trigger
usbalbin Jan 19, 2024
8fb088d
HRTIM: adc trigger impl into
usbalbin Jan 19, 2024
7a03587
HRTIM: adc-trigger example set duty
usbalbin Jan 19, 2024
8a229b1
HRTIM: Move clear_repetition_interrupt to trait
usbalbin Jan 19, 2024
10251b7
HRTIM: Capture interrupts and clippy
usbalbin Jan 22, 2024
bcbe0fe
HRTIM: Capture example
usbalbin Jan 22, 2024
322cb60
HRTIM: Create feature for all devices supporting hrtim
usbalbin Jan 22, 2024
765715b
HRTIM: Some fixes for capture example
usbalbin Jan 22, 2024
21cf9f8
HRTIM: Fix warning in capture example
usbalbin Jan 22, 2024
8667e71
HRTIM: Add forgotten traitification impls
usbalbin Jan 22, 2024
97d8c52
HRTIM: Update examples
usbalbin Jan 22, 2024
bf15715
HRTIM: Capture cleanup
usbalbin Jan 23, 2024
822ee3d
HRTIM: Avoid unneeded type parameters on HrTimer trait
usbalbin Jan 24, 2024
a59386b
HRTIM: Move some inherent methods to traits on HrTim
usbalbin Jan 24, 2024
32cdce4
Fix incorrect mapping of DMA channel to DMAMUX channel
usbalbin Sep 7, 2023
dfc555b
OPAMP - Add more adc pin impls for opamp follower
usbalbin Apr 3, 2024
098a803
HRTIM - Add derives for some types
usbalbin Apr 18, 2024
81760bd
HRTIM - Allow using capture module with DMA
usbalbin Apr 19, 2024
de53acf
HRTIM - Fix example
usbalbin Apr 19, 2024
b6da727
UART - Fix example
usbalbin Apr 19, 2024
96c2528
HRTIM - Prevent future dataraces for DMA
usbalbin Apr 19, 2024
8c7c490
HRTIM - Fix error in
usbalbin Apr 19, 2024
146b8d0
HRTIM - Fix bugs in capture module with get_signed and some restructu…
usbalbin May 7, 2024
642ad5b
HRTIM - Wrong type in AdcTrigger impl for TimerPeriod and TimerReset
usbalbin May 7, 2024
4f5d119
HRTIM - Multiple changes
usbalbin Jun 19, 2024
681cf2f
Use stm32g4 from gitea registry
usbalbin Jun 19, 2024
b773b36
HRTIM - Make sure output pins are not set to alternate mode intil end…
usbalbin Jun 26, 2024
6aa974f
Revert "Use stm32g4 from gitea registry"
usbalbin Aug 17, 2024
e45b57c
Update for new pac
usbalbin Aug 17, 2024
231808b
Revert "OPAMP - Add more adc pin impls for opamp follower"
usbalbin Nov 7, 2024
cf956bf
HRTIM - Fix some errors due to new pac
usbalbin Nov 7, 2024
b988a2e
fmt
usbalbin Nov 7, 2024
bb961cb
HRTIM - Update examples
usbalbin Nov 7, 2024
2ff3d07
HRTIM - Update examples
usbalbin Nov 7, 2024
0a893cc
HRTIM - Fix some warnings
usbalbin Nov 9, 2024
469b993
Update for new pac changes
usbalbin Nov 16, 2024
6af8dcc
HRTIM - Use non_exhaustive on EevInput to make its constructor private
usbalbin Nov 16, 2024
b524806
Avoid using PhantomData<()> to make constructor private
usbalbin Nov 17, 2024
fd92940
Update to pac changes
usbalbin Nov 18, 2024
5795c71
Use latest pac
usbalbin Nov 18, 2024
657f52b
Use latest pac
usbalbin Nov 20, 2024
87bb961
HRTIM - Clean up macro
usbalbin Nov 25, 2024
a79d4b4
HRTIM - Fix some clippy warnings and fmt
usbalbin Nov 25, 2024
1ea1347
HRTIM - Fix some clippy warnings and fmt - examples
usbalbin Nov 25, 2024
558969f
Bump pac
usbalbin Nov 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ static_assertions = "1.1"
fugit = "0.3.5"
stm32-usbd = { version = "0.7.0", optional = true }
fixed = { version = "1.28.0", optional = true }
embedded-io = "0.6.1"

[dependencies.cortex-m]
version = "0.7.7"
Expand Down Expand Up @@ -86,11 +87,12 @@ stm32g431 = ["stm32g4/stm32g431"]
stm32g441 = ["stm32g4/stm32g441"]
stm32g471 = ["stm32g4/stm32g471"]
stm32g473 = ["stm32g4/stm32g473"]
stm32g474 = ["stm32g4/stm32g474"]
stm32g474 = ["stm32g4/stm32g474", "hrtim"]
stm32g483 = ["stm32g4/stm32g483"]
stm32g484 = ["stm32g4/stm32g484"]
stm32g484 = ["stm32g4/stm32g484", "hrtim"]
stm32g491 = ["stm32g4/stm32g491"]
stm32g4a1 = ["stm32g4/stm32g4a1"]
hrtim = []
log-itm = ["cortex-m-log/itm"]
log-rtt = []
log-semihost = ["cortex-m-log/semihosting"]
Expand Down Expand Up @@ -120,3 +122,43 @@ required-features = ["usb"]
[[example]]
name = "cordic"
required-features = ["cordic"]

[[example]]
name = "hrtim-adc-trigger"
required-features = ["hrtim"]
path = "examples/hrtim/adc-trigger.rs"

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

[[example]]
name = "hrtim-eev-comp"
required-features = ["hrtim"]
path = "examples/hrtim/eev-comp.rs"

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

[[example]]
name = "hrtim-flt-comp"
required-features = ["hrtim"]
path = "examples/hrtim/flt-comp.rs"

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

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

[[example]]
name = "hrtim-master"
required-features = ["hrtim"]
path = "examples/hrtim/master.rs"
2 changes: 1 addition & 1 deletion examples/adc-one-shot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use stm32g4xx_hal as hal;

use cortex_m_rt::entry;

use log::info;
use utils::logger::info;

#[macro_use]
mod utils;
Expand Down
157 changes: 157 additions & 0 deletions examples/hrtim/adc-trigger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#![no_std]
#![no_main]

/// Example showcasing the use of the HRTIM peripheral to trigger the ADC at various points of the switch cycle off HRTIM_TIMA

#[path = "../utils/mod.rs"]
mod utils;

use cortex_m_rt::entry;
use utils::logger::info;

#[entry]
fn main() -> ! {
use hal::adc;
use stm32g4xx_hal as hal;

use hal::{
adc::{
config::{Continuous, Dma as AdcDma, SampleTime, Sequence},
AdcClaim, ClockSource, Temperature, Vref,
},
delay::SYSTDelayExt,
dma::{self, config::DmaConfig, stream::DMAExt, TransferExt},
gpio::GpioExt,
hrtim::compare_register::HrCompareRegister,
hrtim::control::HrControltExt,
hrtim::output::HrOutput,
hrtim::timer::HrTimer,
hrtim::HrPwmAdvExt,
hrtim::Pscl4,
pwr::PwrExt,
rcc::{self, RccExt},
stm32::Peripherals,
};

const VREF: f32 = 3.3;

info!("start");

let dp = Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");

// Set system frequency to 16MHz * 15/1/2 = 120MHz
// This would lead to HrTim running at 120MHz * 32 = 3.84...
info!("rcc");
let pwr = dp.PWR.constrain().freeze();
let mut rcc = dp.RCC.freeze(
rcc::Config::pll().pll_cfg(rcc::PllConfig {
mux: rcc::PllSrc::HSI,
n: rcc::PllNMul::MUL_15,
m: rcc::PllMDiv::DIV_1,
r: Some(rcc::PllRDiv::DIV_2),

..Default::default()
}),
pwr,
);

let mut delay = cp.SYST.delay(&rcc.clocks);

let dma::stream::StreamsTuple(dma1ch1, ..) = dp.DMA1.split(&rcc);
let config = DmaConfig::default()
.transfer_complete_interrupt(true)
.circular_buffer(true)
.memory_increment(true);

info!("Setup Gpio");
let gpioa = dp.GPIOA.split(&mut rcc);
let pa0 = gpioa.pa0.into_analog();

let pin_a = gpioa.pa8;
let pin_b = gpioa.pa9;

// ...with a prescaler of 4 this gives us a HrTimer with a tick rate of 960MHz
// With max the max period set, this would be 960MHz/2^16 ~= 15kHz...
let prescaler = Pscl4;

// . .
// . 50% .
// ------ ------
//out1 | | | |
// | | | |
// -------- ---------- --------
// . ^ ^
// . | |
//AD samlp pa0 temp
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
.HRTIM_TIMA
.pwm_advanced((pin_a, pin_b), &mut rcc)
.prescaler(prescaler)
.period(period)
.finalize(&mut hr_control);

cr1.set_duty(period / 2);
cr3.set_duty(period / 3);
cr4.set_duty((2 * u32::from(period) / 3) as u16);

hr_control.adc_trigger1.enable_source(&cr3);
hr_control.adc_trigger1.enable_source(&cr4);

out1.enable_rst_event(&cr1); // Set low on compare match with cr1
out2.enable_rst_event(&cr1);

out1.enable_set_event(&timer); // Set high at new period
out2.enable_set_event(&timer);

info!("Setup Adc1");
let mut adc = dp
.ADC1
.claim(ClockSource::SystemClock, &rcc, &mut delay, true);

adc.set_external_trigger((
adc::config::TriggerMode::RisingEdge,
&hr_control.adc_trigger1,
));
adc.enable_temperature(&dp.ADC12_COMMON);
adc.set_continuous(Continuous::Discontinuous);
adc.reset_sequence();
adc.configure_channel(&pa0, Sequence::One, SampleTime::Cycles_640_5);
adc.configure_channel(&Temperature, Sequence::Two, SampleTime::Cycles_640_5);

info!("Setup DMA");
let first_buffer = cortex_m::singleton!(: [u16; 10] = [0; 10]).unwrap();

let mut transfer = dma1ch1.into_circ_peripheral_to_memory_transfer(
adc.enable_dma(AdcDma::Continuous),
&mut first_buffer[..],
config,
);

transfer.start(|adc| adc.start_conversion());

out1.enable();
out2.enable();

timer.start(&mut hr_control.control);

loop {
let mut b = [0_u16; 4];
let r = transfer.read_exact(&mut b);

info!("read: {}", r);
assert!(r == b.len());

let millivolts = Vref::sample_to_millivolts((b[0] + b[2]) / 2);
info!("pa3: {}mV", millivolts);
let temp = Temperature::temperature_to_degrees_centigrade(
(b[1] + b[3]) / 2,
VREF,
adc::config::Resolution::Twelve,
);
info!("temp: {}℃C", temp);
}
}
130 changes: 130 additions & 0 deletions examples/hrtim/capture-dma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#![no_std]
#![no_main]

/// 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

#[path = "../utils/mod.rs"]
mod utils;

use cortex_m_rt::entry;
use utils::logger::info;

#[entry]
fn main() -> ! {
use stm32g4xx_hal as hal;

use hal::{
dma::{config::DmaConfig, stream::DMAExt, TransferExt},
gpio::{GpioExt, AF13},
hrtim::{
capture, compare_register::HrCompareRegister, control::HrControltExt, external_event,
external_event::ToExternalEventSource, output::HrOutput, timer::HrSlaveTimerCpt,
timer::HrTimer, HrPwmAdvExt, Pscl128,
},
pwr::PwrExt,
rcc::{self, RccExt},
stm32::Peripherals,
};
use info;

info!("start");

let dp = Peripherals::take().unwrap();

// Set system frequency to 16MHz * 15/1/2 = 120MHz
// This would lead to HrTim running at 120MHz * 32 = 3.84...
info!("rcc");
let pwr = dp.PWR.constrain().freeze();
let mut rcc = dp.RCC.freeze(
rcc::Config::pll().pll_cfg(rcc::PllConfig {
mux: rcc::PLLSrc::HSI,
n: rcc::PllNMul::MUL_15,
m: rcc::PllMDiv::DIV_1,
r: Some(rcc::PllRDiv::DIV_2),

..Default::default()
}),
pwr,
);

info!("Setup Gpio");
let gpioa = dp.GPIOA.split(&mut rcc);
let gpiob = dp.GPIOB.split(&mut rcc);

// PA8 (D7 on Nucleo G474RE)
let pin_a = gpioa.pa8;

// PB5 (D4 on Nucleo G474RE)
let input = gpiob.pb5.into_pull_down_input();

// ...with a prescaler of 128 this gives us a HrTimer with a tick rate of 30MHz
// With max the max period set, this would be 30MHz/2^16 ~= 458Hz...
let prescaler = Pscl128;

// t1 t2 .
// | | .
// v v .
// . .
// . 50% .
// ------ ------
//out1 | | | |
// | | | |
// -------- ---------- --------
let period = 0xFFFF;
let (mut hr_control, _flt_inputs, eev_inputs) =
dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration();

let eev_input6 = eev_inputs
.eev_input6
.bind(input)
.edge_or_polarity(external_event::EdgeOrPolarity::Edge(
external_event::Edge::Both,
))
.finalize(&mut hr_control);

let mut hr_control = hr_control.constrain();
let (timer, (mut cr1, _cr2, _cr3, _cr4), mut out1, dma_ch) = 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
cr1.set_duty(period / 2);

let (mut timer, mut capture, _capture_ch2) = timer.split_capture();
timer.start(&mut hr_control.control);
out1.enable();

capture.enable_interrupt(true, &mut hr_control);
capture.add_event(&eev_input6);

info!("Setup DMA");
let streams = dp.DMA1.split(&rcc);
let config = DmaConfig::default()
.transfer_complete_interrupt(false)
.circular_buffer(true)
.memory_increment(true);

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),
&mut first_buffer[..],
config,
);

transfer.start(|_| ());

let mut old_duty = 0;
loop {
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);
cr1.set_duty(duty as u16);
info!("Capture: t1: {}, t2: {}, duty: {}, ", t1, t2, old_duty);
old_duty = duty;
}
}
}
Loading
Loading