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 #70

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ paste = "1.0"
bitflags = "1.2"
vcell = "0.1"
static_assertions = "1.1"
fugit = "0.3.5"

[dependencies.cortex-m]
version = "0.7.7"
Expand Down
5 changes: 3 additions & 2 deletions examples/blinky_delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use hal::delay::DelayFromCountDownTimer;
use hal::prelude::*;
use hal::rcc::Config;
use hal::stm32;
use hal::time::ExtU32;
use hal::timer::Timer;
use stm32g4xx_hal as hal;

Expand Down Expand Up @@ -34,13 +35,13 @@ fn main() -> ! {

info!("Init Timer2 delay");
let timer2 = Timer::new(dp.TIM2, &rcc.clocks);
let mut delay_tim2 = DelayFromCountDownTimer::new(timer2.start_count_down(100.ms()));
let mut delay_tim2 = DelayFromCountDownTimer::new(timer2.start_count_down(100.millis()));

loop {
info!("Toggle");
led.toggle().unwrap();
info!("SYST delay");
delay_syst.delay(1000.ms());
delay_syst.delay(1000.millis());
info!("Toggle");
led.toggle().unwrap();
info!("TIM2 delay");
Expand Down
4 changes: 2 additions & 2 deletions examples/can-echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::hal::{
nb::block,
rcc::{Config, RccExt, SysClockSrc},
stm32::Peripherals,
time::U32Ext,
time::RateExtU32,
};
use fdcan::{
config::NominalBitTiming,
Expand Down Expand Up @@ -47,7 +47,7 @@ fn main() -> ! {
let dp = Peripherals::take().unwrap();
let _cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");
let rcc = dp.RCC.constrain();
let mut rcc = rcc.freeze(Config::new(SysClockSrc::HSE(24.mhz())));
let mut rcc = rcc.freeze(Config::new(SysClockSrc::HSE(24.MHz())));

info!("Split GPIO");

Expand Down
104 changes: 104 additions & 0 deletions examples/hrtim.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//This example puts the timer in PWM mode using the specified pin with a frequency of 100Hz and a duty cycle of 50%.
#![no_main]
#![no_std]

use cortex_m_rt::entry;

use defmt_rtt as _; // global logger
use panic_probe as _;

#[cfg(not(any(feature = "stm32g474", feature = "stm32g484")))]
#[entry]
fn main() -> ! {
#[allow(clippy::empty_loop)]
loop {}
}

#[cfg(any(feature = "stm32g474", feature = "stm32g484"))]
#[entry]
fn main() -> ! {
use fugit::ExtU32;
use hal::gpio::gpioa::PA8;
use hal::gpio::gpioa::PA9;
use hal::gpio::Alternate;
use hal::gpio::AF13;
use hal::prelude::*;
use hal::pwm::hrtim::EventSource;
use hal::pwm::hrtim::HrCompareRegister;
use hal::pwm::hrtim::HrControltExt;
use hal::pwm::hrtim::HrOutput;
use hal::pwm::hrtim::HrPwmAdvExt;
use hal::pwm::hrtim::HrTimer;
use hal::pwm::hrtim::Pscl4;
use hal::rcc;
use hal::stm32;
use stm32g4xx_hal as hal;
extern crate cortex_m_rt as rt;

let dp = stm32::Peripherals::take().expect("cannot take peripherals");
let cp = stm32::CorePeripherals::take().expect("cannot take core");
// Set system frequency to 16MHz * 75/4/2 = 150MHz
// This would lead to HrTim running at 150MHz * 32 = 4.8GHz...
let mut rcc = dp.RCC.freeze(rcc::Config::pll().pll_cfg(rcc::PllConfig {
mux: rcc::PLLSrc::HSI,
n: rcc::PllNMul::MUL_75,
m: rcc::PllMDiv::DIV_4,
r: Some(rcc::PllRDiv::DIV_2),
..Default::default()
}));

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

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

let gpioa = dp.GPIOA.split(&mut rcc);
let pin_a: PA8<Alternate<AF13>> = gpioa.pa8.into_alternate();
let pin_b: PA9<Alternate<AF13>> = gpioa.pa9.into_alternate();

// . . . .
// . 30% . . .
// ---- . .---- .
//out1 | | . | | .
// | | . | | .
// -------- ---------------------------- --------------------
// . .---- . .----
//out2 . | | . | |
// . | | . | |
// ------------------------ ---------------------------- ----
// . . . .
// . . . .
let (mut fault_control, _) = dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration();
let (mut timer, (mut cr1, _cr2, _cr3, _cr4), (mut out1, mut out2)) = dp
.HRTIM_TIMA
.pwm_advanced((pin_a, pin_b), &mut rcc)
.prescaler(prescaler)
.period(0xFFFF)
.push_pull_mode(true) // Set push pull mode, out1 and out2 are
// alternated every period with one being
// inactive and the other getting to output its wave form
// as normal
.finalize(&mut fault_control);

out1.enable_rst_event(EventSource::Cr1); // Set low on compare match with cr1
out2.enable_rst_event(EventSource::Cr1);

out1.enable_set_event(EventSource::Period); // Set high at new period
out2.enable_set_event(EventSource::Period);

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

loop {
// Step frequency from 18kHz to about 180kHz(half of that when only looking at one pin)
for i in 1..10 {
let new_period = u16::MAX / i;

cr1.set_duty(new_period / 3);
timer.set_period(new_period);

delay.delay(500_u32.millis());
}
}
}
79 changes: 79 additions & 0 deletions examples/hrtim_simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//This example puts the timer in PWM mode using the specified pin with a frequency of 100Hz and a duty cycle of 50%.
#![no_main]
#![no_std]

use cortex_m_rt::entry;

//mod utils;

use defmt_rtt as _; // global logger
use panic_probe as _;

#[cfg(not(any(feature = "stm32g474", feature = "stm32g484")))]
#[entry]
fn main() -> ! {
#[allow(clippy::empty_loop)]
loop {}
}

#[cfg(any(feature = "stm32g474", feature = "stm32g484"))]
#[entry]
fn main() -> ! {
use hal::gpio::gpioa::PA8;
use hal::gpio::gpioa::PA9;
use hal::gpio::Alternate;
use hal::gpio::AF13;
use hal::prelude::*;
use hal::pwm::hrtim::{HrControltExt, HrPwmExt};
use hal::rcc;
use hal::stm32;
use hal::time::RateExtU32;
use stm32g4xx_hal as hal;
extern crate cortex_m_rt as rt;
let dp = stm32::Peripherals::take().expect("cannot take peripherals");

// Set system frequency to 16MHz * 75/4/2 = 150MHz
// This would lead to HrTim running at 150MHz * 32 = 4.8GHz...
let mut rcc = dp.RCC.freeze(rcc::Config::pll().pll_cfg(rcc::PllConfig {
n: rcc::PllNMul::MUL_75,
m: rcc::PllMDiv::DIV_4,
r: Some(rcc::PllRDiv::DIV_2),
..Default::default()
}));

let gpioa = dp.GPIOA.split(&mut rcc);
let pin_a: PA8<Alternate<AF13>> = gpioa.pa8.into_alternate();
let pin_b: PA9<Alternate<AF13>> = gpioa.pa9.into_alternate();

// . . .
// . 33% . .
// ---- .---- .----
//out1 | | | | | |
// | | | | | |
// ------ ------------ ------------ ---------
// . .
// 50% . .
// -------- .-------- .--------
//out2 | | | | | |
// | | | | | |
// ------ -------- -------- -----
// . . .
// . . .

let (mut control, _) = dp.HRTIM_COMMON.hr_control(&mut rcc).wait_for_calibration();
let (mut p1, mut p2) = dp
.HRTIM_TIMA
.pwm((pin_a, pin_b), 20_u32.kHz(), &mut control, &mut rcc);
let max_duty = p1.get_max_duty();

p1.set_duty(max_duty / 3); // Set output 1 to about 33%
p2.set_duty(max_duty / 2); // Set output 2 to 50%

// Enable the outputs
p1.enable();
p2.enable();

loop {
cortex_m::asm::nop()
}
}
5 changes: 3 additions & 2 deletions examples/i2c-bme680.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use hal::delay::DelayFromCountDownTimer;
use hal::i2c::Config;
use hal::prelude::*;
use hal::stm32;
use hal::time::{ExtU32, RateExtU32};
use hal::timer::Timer;
use stm32g4xx_hal as hal;

Expand All @@ -32,11 +33,11 @@ fn main() -> ! {
let sda = gpiob.pb9.into_alternate_open_drain();
let scl = gpiob.pb8.into_alternate_open_drain();

let i2c = dp.I2C1.i2c(sda, scl, Config::new(100.khz()), &mut rcc);
let i2c = dp.I2C1.i2c(sda, scl, Config::new(100.kHz()), &mut rcc);

let mut delayer = cp.SYST.delay(&rcc.clocks);
let timer2 = Timer::new(dp.TIM2, &rcc.clocks);
let mut delay = DelayFromCountDownTimer::new(timer2.start_count_down(100.ms()));
let mut delay = DelayFromCountDownTimer::new(timer2.start_count_down(100.millis()));

let mut dev =
Bme680::init(i2c, &mut delayer, I2CAddress::Secondary).expect("Init function failed");
Expand Down
5 changes: 3 additions & 2 deletions examples/i2c-mpu6050.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use hal::i2c::Config;
use hal::prelude::*;
use hal::stm32;
use hal::time::{ExtU32, RateExtU32};
use stm32g4xx_hal as hal;

use cortex_m_rt::entry;
Expand All @@ -28,7 +29,7 @@ fn main() -> ! {
let sda = gpiob.pb9.into_alternate_open_drain();
let scl = gpiob.pb8.into_alternate_open_drain();

let i2c = dp.I2C1.i2c(sda, scl, Config::new(100.khz()), &mut rcc);
let i2c = dp.I2C1.i2c(sda, scl, Config::new(100.kHz()), &mut rcc);

let mut mpu = Mpu6050::new(i2c);
let mut delay = cp.SYST.delay(&rcc.clocks);
Expand All @@ -39,6 +40,6 @@ fn main() -> ! {
let gyro = mpu.get_gyro().expect("cannot read gyro");
let temp = mpu.get_temp().expect("cannot read temperature");
info!("acc: {:?}, gyro: {:?}, temp: {:?}C", acc, gyro, temp);
delay.delay(250.ms());
delay.delay(250.millis());
}
}
3 changes: 2 additions & 1 deletion examples/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use hal::i2c::Config;
use hal::prelude::*;
use hal::stm32;
use hal::time::RateExtU32;
use stm32g4xx_hal as hal;

use cortex_m_rt::entry;
Expand All @@ -25,7 +26,7 @@ fn main() -> ! {
let sda = gpiob.pb9.into_alternate_open_drain();
let scl = gpiob.pb8.into_alternate_open_drain();

let mut i2c = dp.I2C1.i2c(sda, scl, Config::new(40.khz()), &mut rcc);
let mut i2c = dp.I2C1.i2c(sda, scl, Config::new(40.kHz()), &mut rcc);
// Alternatively, it is possible to specify the exact timing as follows (see the documentation
// of with_timing() for an explanation of the constant):
//let mut i2c = dp
Expand Down
3 changes: 2 additions & 1 deletion examples/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use hal::gpio::Alternate;
use hal::gpio::AF6;
use hal::prelude::*;
use hal::stm32;
use hal::time::RateExtU32;
use stm32g4xx_hal as hal;
mod utils;
extern crate cortex_m_rt as rt;
Expand All @@ -19,7 +20,7 @@ fn main() -> ! {
let gpioa = dp.GPIOA.split(&mut rcc);
let pin: PA8<Alternate<AF6>> = gpioa.pa8.into_alternate();

let mut pwm = dp.TIM1.pwm(pin, 100.hz(), &mut rcc);
let mut pwm = dp.TIM1.pwm(pin, 100.Hz(), &mut rcc);

pwm.set_duty(pwm.get_max_duty() / 2);
pwm.enable();
Expand Down
19 changes: 15 additions & 4 deletions examples/spi-example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@
#![no_std]

use crate::hal::{
block, delay::DelayFromCountDownTimer, gpio::gpioa::PA5, gpio::gpioa::PA6, gpio::gpioa::PA7,
gpio::Alternate, gpio::AF5, prelude::*, rcc::Config, spi, stm32::Peripherals, timer::Timer,
block,
delay::DelayFromCountDownTimer,
gpio::gpioa::PA5,
gpio::gpioa::PA6,
gpio::gpioa::PA7,
gpio::Alternate,
gpio::AF5,
prelude::*,
rcc::Config,
spi,
stm32::Peripherals,
time::{ExtU32, RateExtU32},
timer::Timer,
};

use cortex_m_rt::entry;
Expand All @@ -25,7 +36,7 @@ fn main() -> ! {
let rcc = dp.RCC.constrain();
let mut rcc = rcc.freeze(Config::hsi());
let timer2 = Timer::new(dp.TIM2, &rcc.clocks);
let mut delay_tim2 = DelayFromCountDownTimer::new(timer2.start_count_down(100.ms()));
let mut delay_tim2 = DelayFromCountDownTimer::new(timer2.start_count_down(100.millis()));

let gpioa = dp.GPIOA.split(&mut rcc);
let sclk: PA5<Alternate<AF5>> = gpioa.pa5.into_alternate();
Expand All @@ -34,7 +45,7 @@ fn main() -> ! {

let mut spi = dp
.SPI1
.spi((sclk, miso, mosi), spi::MODE_0, 400.khz(), &mut rcc);
.spi((sclk, miso, mosi), spi::MODE_0, 400.kHz(), &mut rcc);
let mut cs = gpioa.pa8.into_push_pull_output();
cs.set_high().unwrap();

Expand Down
3 changes: 2 additions & 1 deletion examples/spi-sd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use hal::rcc::Config;
use hal::spi;
use hal::stm32;
use hal::stm32::Peripherals;
use hal::time::RateExtU32;
use hal::timer::Timer;
use stm32g4xx_hal as hal;

Expand Down Expand Up @@ -50,7 +51,7 @@ fn main() -> ! {

let mut spi = dp
.SPI2
.spi((sck, miso, mosi), spi::MODE_0, 400.khz(), &mut rcc);
.spi((sck, miso, mosi), spi::MODE_0, 400.kHz(), &mut rcc);

struct Clock;

Expand Down
3 changes: 2 additions & 1 deletion examples/uart-dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use core::fmt::Write;
use hal::dma::{config::DmaConfig, stream::DMAExt, TransferExt};
use hal::prelude::*;
use hal::serial::*;
use hal::time::ExtU32;
use hal::{rcc, stm32};
use stm32g4xx_hal as hal;

Expand Down Expand Up @@ -70,7 +71,7 @@ fn main() -> ! {
loop {
while !transfer.get_transfer_complete_flag() {}

delay_syst.delay(1000.ms());
delay_syst.delay(1000.millis());
led.toggle().unwrap();
transfer.restart(|_tx| {});
}
Expand Down
Loading