-
Notifications
You must be signed in to change notification settings - Fork 51
WIP: HRTIM #96
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
Closed
Closed
WIP: HRTIM #96
Changes from all commits
Commits
Show all changes
58 commits
Select commit
Hold shift + click to select a range
224f0d5
WIP
usbalbin 58f44f3
Untested sort of maybe working chaos
usbalbin 108c7b3
Dual channel support and cargo fmt
usbalbin ec9ec6f
Almost...
usbalbin af66e36
Still untested but might actually work, maybe
usbalbin 7e1a45c
Adjust doc
usbalbin b56e2fd
Enable outputs in example
usbalbin f2de47f
Further cleanup
usbalbin a5e7f3a
Tested and verified on NUCLEO-G474RE
usbalbin 2dcae09
hrtim_simple also tested and verified on NUCLEO-G474RE
usbalbin db94990
Hrtim cleanup, add pins for all timers but HRTIM_TIMF
usbalbin e059464
Several changes and fixes
usbalbin ec58b83
Start work on hrtim fault
usbalbin 68feb9f
Add hrtim fault example
usbalbin ce22d9c
TOO MANY CHANGES
usbalbin 688a867
Add ADC triggers among a lot of things to hrtim
usbalbin fccd37b
Disable push pull
usbalbin 86b7cef
NOT DONE - Losts of hrtim stuff
usbalbin a5ea91f
Multiple hrtim changes
usbalbin 7ddac7b
HRTIM: Fix bad pin mapping
usbalbin b615f95
HRTIM - Rework EventSource system and add deadtime support
usbalbin f89dee3
HRTIM refactor
usbalbin 6c3d60d
More hrtim refactor
usbalbin 76b2125
EEV
usbalbin 83706b3
More work on hrtim eev
usbalbin 35bff4f
even more eev hrtim
usbalbin 916a422
more eev hrtim
usbalbin 291014e
Update hrtim examples. TODO: Make sure timers are started
usbalbin 741a4db
HRTIM examples
usbalbin 1942ca7
Fix warnings for hrtim
usbalbin 2217a97
HRTIM - Traitify adc_triggers
usbalbin 4213ce0
HRTIM: Start work on capture support
usbalbin 0350eb0
HRTIM: Add capture.rs
usbalbin 20205c7
HRTIM: Impl CompareEvent for more types
usbalbin 5a846ef
Fmt
usbalbin 5dd9335
HRTIM: Traitify events
usbalbin e089ce5
HRTIM: Fix bugs caused by traitification
usbalbin a6ed404
HRTIM: Update adc-trigger example for traitification changes
usbalbin 27075e1
HRTIM: Start timer and set trigger points in adc-trigger example
usbalbin b7bacde
HRTIM: Refactor adc trigger
usbalbin 0e763b9
HRTIM: Refactor adc trigger - example hrtim-adc-trigger
usbalbin 1626954
HRTIM: adc trigger impl into
usbalbin 944985b
HRTIM: adc-trigger example set duty
usbalbin eb075b0
HRTIM: Move clear_repetition_interrupt to trait
usbalbin ab4c3b5
HRTIM: Capture interrupts and clippy
usbalbin 8a7e208
HRTIM: Capture example
usbalbin a823f21
HRTIM: Create feature for all devices supporting hrtim
usbalbin 2924811
HRTIM: Some fixes for capture example
usbalbin 6e1d446
HRTIM: Fix warning in capture example
usbalbin b97dada
HRTIM: Add forgotten traitification impls
usbalbin 749c064
HRTIM: Update examples
usbalbin 47d7a43
HRTIM: Capture cleanup
usbalbin 6887958
HRTIM: Avoid unneeded type parameters on HrTimer trait
usbalbin 5ba64f8
HRTIM: Move some inherent methods to traits on HrTim
usbalbin 066f4a3
HRTIM - Add derives for some types
usbalbin 8ebf645
HRTIM - Allow using capture module with DMA
usbalbin 6c619a9
HRTIM - Fix example
usbalbin f8018d1
HRTIM - Prevent future dataraces for DMA
usbalbin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
#![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 defmt_rtt as _; // global logger | ||
use panic_probe as _; | ||
|
||
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::{gpioa::PA8, gpioa::PA9, Alternate, GpioExt, AF13}, | ||
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: PA8<Alternate<AF13>> = gpioa.pa8.into_alternate(); | ||
let pin_b: PA9<Alternate<AF13>> = gpioa.pa9.into_alternate(); | ||
|
||
// ...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); | ||
|
||
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#![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 defmt_rtt as _; // global logger | ||
use panic_probe as _; | ||
|
||
use utils::logger::info; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
use stm32g4xx_hal as hal; | ||
|
||
use hal::{ | ||
dma::{config::DmaConfig, stream::DMAExt, TransferExt}, | ||
gpio::{gpioa::PA8, Alternate, 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: PA8<Alternate<AF13>> = gpioa.pa8.into_alternate(); | ||
|
||
// 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); | ||
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.