Skip to content

Hrtim #192

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

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"rust-analyzer.check.allTargets": false,
"rust-analyzer.check.extraArgs": [
"--examples",
"--bins",
],
"rust-analyzer.cargo.target": "thumbv7em-none-eabihf",
"rust-analyzer.cargo.features": [
"stm32g484",
"defmt",
"cordic"
"cordic",
"hrtim"
]
}
68 changes: 58 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fugit = "0.3.7"
stm32-usbd = { version = "0.7.0", optional = true }
fixed = { version = "1.28.0", optional = true }
embedded-io = "0.6"
stm32-hrtim = { git = "https://github.com/usbalbin/stm32-hrtim", rev = "ce1da43264a59b932eaf01651eba20b9cd0d96ba", optional = true }

[dependencies.cortex-m]
version = "0.7.7"
Expand Down Expand Up @@ -81,9 +82,9 @@ usb = ["dep:stm32-usbd"]
stm32g431 = ["stm32g4/stm32g431", "cat2"]
stm32g441 = ["stm32g4/stm32g441", "cat2"]
stm32g473 = ["stm32g4/stm32g473", "cat3", "adc3", "adc4", "adc5"]
stm32g474 = ["stm32g4/stm32g474", "cat3", "adc3", "adc4", "adc5"]
stm32g474 = ["stm32g4/stm32g474", "cat3", "adc3", "adc4", "adc5", "hrtim", "stm32-hrtim/stm32g474"]
stm32g483 = ["stm32g4/stm32g483", "cat3", "adc3", "adc4", "adc5"]
stm32g484 = ["stm32g4/stm32g484", "cat3", "adc3", "adc4", "adc5"]
stm32g484 = ["stm32g4/stm32g484", "cat3", "adc3", "adc4", "adc5", "hrtim", "stm32-hrtim/stm32g484"]
stm32g491 = ["stm32g4/stm32g491", "cat4", "adc3"]
stm32g4a1 = ["stm32g4/stm32g4a1", "cat4", "adc3"]
log-itm = ["cortex-m-log/itm"]
Expand All @@ -95,6 +96,7 @@ defmt = [
"nb/defmt-0-3",
"embedded-hal/defmt-03",
"embedded-io/defmt-03",
"stm32-hrtim?/defmt"
]
cordic = ["dep:fixed"]
adc3 = []
Expand All @@ -107,6 +109,7 @@ cat3 = []
cat4 = []

can = ["dep:fdcan"]
hrtim = ["dep:stm32-hrtim"]

[profile.dev]
codegen-units = 1
Expand All @@ -127,6 +130,17 @@ codegen-units = 1
incremental = false
lto = true

[[test]]
name = "nucleo-g474"
harness = false

[[test]]
name = "nucleo-g474_w_jumpers"
harness = false

[lib]
test = false

[[example]]
name = "can-echo"
required-features = ["can"]
Expand All @@ -143,13 +157,47 @@ required-features = ["usb"]
name = "cordic"
required-features = ["cordic"]

[[test]]
name = "nucleo-g474"
harness = false
[[example]]
name = "hrtim-adc-trigger"
required-features = ["hrtim"]
path = "examples/hrtim/adc-trigger.rs"

[[test]]
name = "nucleo-g474_w_jumpers"
harness = false
[[example]]
name = "hrtim-capture"
required-features = ["hrtim"]
path = "examples/hrtim/capture.rs"

[lib]
test = false
[[example]]
name = "hrtim-capture-dma"
required-features = ["hrtim"]
path = "examples/hrtim/capture-dma.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"
162 changes: 162 additions & 0 deletions examples/hrtim/adc-trigger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#![no_std]
#![no_main]

#[path = "../utils/mod.rs"]
mod utils;
use utils::logger::info;

/// Example showcasing the use of the HRTIM peripheral to trigger the ADC at various points of the switch cycle off HRTIM_TIMA
use cortex_m_rt::entry;
use stm32_hrtim::{
compare_register::HrCompareRegister, output::HrOutput, timer::HrTimer, HrParts, HrPwmAdvExt,
Pscl4,
};
use stm32g4xx_hal::{
adc::{self, temperature::Temperature, AdcClaim, AdcCommonExt, Vref},
delay::SYSTDelayExt,
dma::{self, channel::DMAExt, config::DmaConfig, TransferExt},
gpio::GpioExt,
hrtim::{HrControltExt, HrPwmBuilderExt},
pwr::PwrExt,
rcc::{self, RccExt},
stm32::{CorePeripherals, Peripherals},
};

#[entry]
fn main() -> ! {
const VREF: f32 = 3.3;

info!("start");

let dp = Peripherals::take().unwrap();
let cp = CorePeripherals::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::channel::Channels { ch1: 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 HrParts {
mut timer,
mut cr1,
mut cr3,
mut cr4,
out: (mut out1, mut out2),
..
} = dp
.HRTIM_TIMA
.pwm_advanced((pin_a, pin_b))
.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 adc12_common = dp.ADC12_COMMON.claim(Default::default(), &mut rcc);
let mut adc = adc12_common.claim(dp.ADC1, &mut delay);

adc.set_external_trigger((
adc::config::TriggerMode::RisingEdge,
(&hr_control.adc_trigger1).into(),
));
adc12_common.enable_temperature();
adc.set_continuous(adc::config::Continuous::Discontinuous);
adc.reset_sequence();
adc.configure_channel(
&pa0,
adc::config::Sequence::One,
adc::config::SampleTime::Cycles_640_5,
);
adc.configure_channel(
&Temperature,
adc::config::Sequence::Two,
adc::config::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(adc::config::Dma::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);
}
}
Loading
Loading