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

Hrtim5 #141

Closed
wants to merge 88 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
c3d47ec
Use latest pac
usbalbin Dec 16, 2023
7120240
Update DMA for new pac
usbalbin Dec 16, 2023
f5bedb3
Update for new pac
usbalbin Dec 16, 2023
48db23b
Fix clippy warnings
usbalbin Dec 18, 2023
75500fe
fmt
usbalbin Aug 6, 2024
23d0c66
Add G91 and G4A1 to CI
usbalbin Aug 6, 2024
04c2bb3
Add support for G491 and G4A1
usbalbin Aug 6, 2024
8286455
rename device feature
usbalbin Aug 7, 2024
369754d
Update for new pac with write and modify not returning unit type
usbalbin Nov 7, 2024
851dd97
Update for new pac with write and modify not returning unit type - fo…
usbalbin Nov 7, 2024
d3744e6
Fix some leftover G491 things
usbalbin Nov 7, 2024
698b932
g491 and g4a1 does not have ADC4 and 5
usbalbin Nov 7, 2024
9a6f41e
Update src/fdcan.rs
usbalbin Nov 7, 2024
f9cfab9
Slightly demacrofy DMA
usbalbin Nov 7, 2024
5f8052b
WIP
usbalbin May 9, 2023
5228780
Untested sort of maybe working chaos
usbalbin May 20, 2023
14cb9d2
Dual channel support and cargo fmt
usbalbin May 20, 2023
4ca91c7
Almost...
usbalbin May 20, 2023
24b782f
Still untested but might actually work, maybe
usbalbin May 20, 2023
f86bb64
Adjust doc
usbalbin May 20, 2023
96a8cb6
Enable outputs in example
usbalbin May 20, 2023
7b655c0
Further cleanup
usbalbin May 20, 2023
c6100ce
Tested and verified on NUCLEO-G474RE
usbalbin Jun 7, 2023
279c1e2
hrtim_simple also tested and verified on NUCLEO-G474RE
usbalbin Jun 7, 2023
0daad66
Hrtim cleanup, add pins for all timers but HRTIM_TIMF
usbalbin Jun 9, 2023
72aab8b
Several changes and fixes
usbalbin Jun 20, 2023
43a1058
Start work on hrtim fault
usbalbin Jun 12, 2023
06ecbec
Add hrtim fault example
usbalbin Jun 20, 2023
9b25a6f
TOO MANY CHANGES
usbalbin Jun 28, 2023
0211438
Add ADC triggers among a lot of things to hrtim
usbalbin Aug 25, 2023
1256207
Disable push pull
usbalbin Aug 25, 2023
799e166
NOT DONE - Losts of hrtim stuff
usbalbin Sep 4, 2023
d426e52
Multiple hrtim changes
usbalbin Oct 24, 2023
41e8774
HRTIM: Fix bad pin mapping
usbalbin Oct 24, 2023
982524a
HRTIM - Rework EventSource system and add deadtime support
usbalbin Oct 24, 2023
2bb7b3b
HRTIM refactor
usbalbin Nov 3, 2023
826a32c
More hrtim refactor
usbalbin Nov 4, 2023
d507460
EEV
usbalbin Nov 5, 2023
6a7c270
More work on hrtim eev
usbalbin Nov 6, 2023
fd6adea
even more eev hrtim
usbalbin Nov 6, 2023
30d117f
more eev hrtim
usbalbin Nov 7, 2023
2053ecd
Update hrtim examples. TODO: Make sure timers are started
usbalbin Nov 8, 2023
3d07861
HRTIM examples
usbalbin Nov 8, 2023
89f0706
Fix warnings for hrtim
usbalbin Nov 8, 2023
7004ec6
HRTIM - Traitify adc_triggers
usbalbin Jan 6, 2024
72dd9c2
HRTIM: Start work on capture support
usbalbin Jan 17, 2024
189f1cd
HRTIM: Add capture.rs
usbalbin Jan 18, 2024
d6a6996
HRTIM: Impl CompareEvent for more types
usbalbin Jan 18, 2024
dfb2642
Fmt
usbalbin Jan 18, 2024
a214200
HRTIM: Traitify events
usbalbin Jan 18, 2024
27aa85e
HRTIM: Fix bugs caused by traitification
usbalbin Jan 19, 2024
6beb611
HRTIM: Update adc-trigger example for traitification changes
usbalbin Jan 19, 2024
24d1bf1
HRTIM: Start timer and set trigger points in adc-trigger example
usbalbin Jan 19, 2024
7f54235
HRTIM: Refactor adc trigger
usbalbin Jan 19, 2024
8051ee4
HRTIM: Refactor adc trigger - example hrtim-adc-trigger
usbalbin Jan 19, 2024
6011806
HRTIM: adc trigger impl into
usbalbin Jan 19, 2024
47f0970
HRTIM: adc-trigger example set duty
usbalbin Jan 19, 2024
df47539
HRTIM: Move clear_repetition_interrupt to trait
usbalbin Jan 19, 2024
7f93d53
HRTIM: Capture interrupts and clippy
usbalbin Jan 22, 2024
ae1a371
HRTIM: Capture example
usbalbin Jan 22, 2024
cc84bce
HRTIM: Create feature for all devices supporting hrtim
usbalbin Jan 22, 2024
00fd46c
HRTIM: Some fixes for capture example
usbalbin Jan 22, 2024
b2da099
HRTIM: Fix warning in capture example
usbalbin Jan 22, 2024
88545c1
HRTIM: Add forgotten traitification impls
usbalbin Jan 22, 2024
cdea5a5
HRTIM: Update examples
usbalbin Jan 22, 2024
c9408ac
HRTIM: Capture cleanup
usbalbin Jan 23, 2024
5527508
HRTIM: Avoid unneeded type parameters on HrTimer trait
usbalbin Jan 24, 2024
6cd1ff8
HRTIM: Move some inherent methods to traits on HrTim
usbalbin Jan 24, 2024
0e471e9
Fix incorrect mapping of DMA channel to DMAMUX channel
usbalbin Sep 7, 2023
d3fc23d
OPAMP - Add more adc pin impls for opamp follower
usbalbin Apr 3, 2024
e8ef322
HRTIM - Add derives for some types
usbalbin Apr 18, 2024
d3d17fd
HRTIM - Allow using capture module with DMA
usbalbin Apr 19, 2024
6e9a34e
HRTIM - Fix example
usbalbin Apr 19, 2024
3ea35f6
UART - Fix example
usbalbin Apr 19, 2024
07eb18d
HRTIM - Prevent future dataraces for DMA
usbalbin Apr 19, 2024
352cada
HRTIM - Fix error in
usbalbin Apr 19, 2024
da19e8f
HRTIM - Fix bugs in capture module with get_signed and some restructu…
usbalbin May 7, 2024
f0507de
HRTIM - Wrong type in AdcTrigger impl for TimerPeriod and TimerReset
usbalbin May 7, 2024
516b246
HRTIM - Multiple changes
usbalbin Jun 19, 2024
4f6ac37
Use stm32g4 from gitea registry
usbalbin Jun 19, 2024
8b9c0c9
HRTIM - Make sure output pins are not set to alternate mode intil end…
usbalbin Jun 26, 2024
a994773
Revert "Use stm32g4 from gitea registry"
usbalbin Aug 17, 2024
a42558f
Update for new pac
usbalbin Aug 17, 2024
849e484
Revert "OPAMP - Add more adc pin impls for opamp follower"
usbalbin Nov 7, 2024
ad146a0
HRTIM - Fix some errors due to new pac
usbalbin Nov 7, 2024
dd918b4
fmt
usbalbin Nov 7, 2024
f9099f8
HRTIM - Update examples
usbalbin Nov 7, 2024
3453795
HRTIM - Update examples
usbalbin Nov 7, 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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ jobs:
- stm32g474
- stm32g483
- stm32g484
#- stm32g491 # Does not seem ready yet
#- stm32g4a1 # Does not seem ready yet
- stm32g491 # Does not seem ready yet
- stm32g4a1 # Does not seem ready yet
features:
- log-rtt,defmt
# TODO: -log-rtt # log-rtt without defmt, more combos?
Expand Down
49 changes: 46 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ version = "0.0.2"

[dependencies]
nb = "0.1.1"
stm32g4 = "0.15.1"
#stm32g4 = { git = "https://github.com/stm32-rs/stm32-rs-nightlies" } #"0.15.1"
stm32g4 = { version = "0.17.0", package = "stm32g4-staging" }
paste = "1.0"
bitflags = "1.2"
vcell = "0.1"
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 @@ -85,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 @@ -119,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
2 changes: 1 addition & 1 deletion examples/flash_with_rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ mod app {

unsafe {
let mut flash = &(*stm32g4xx_hal::stm32::FLASH::ptr());
flash.acr.modify(|_, w| {
flash.acr().modify(|_, w| {
w.latency().bits(0b1000) // 8 wait states
});
}
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