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

Add more methods to Pwm<T> #140

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
51 changes: 47 additions & 4 deletions src/timer/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,50 @@ macro_rules! pwm {
let psc = (ratio - 1) / 0xffff;
let arr = ratio / (psc + 1) - 1;

self.set_prescaler_register(psc as u16);
self.set_period_register(arr as u16);

unsafe {
self.tim.psc.write(|w| w.psc().bits(psc as u16));
self.tim.arr.write(|w| w.$arr().bits(arr as u16));
self.tim.cr1.write(|w| w.cen().set_bit());
}
}

/// Set only period register
pub fn set_period_register(&mut self, period: u16) {
unsafe {
self.tim.arr.write(|w| w.$arr().bits(period as u16));
$(
self.tim.arr.modify(|_, w| w.$arr_h().bits((arr >> 16) as u16));
self.tim.arr.modify(|_, w| w.$arr_h().bits((period >> 16) as u16));
Copy link
Author

@usbalbin usbalbin Apr 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pwm! {
    TIM1: (tim1, arr),
    TIM3: (tim3, arr_l, arr_h), // should this really have a `arr_h`?
    TIM14: (tim14, arr),
    TIM16: (tim16, arr),
    TIM17: (tim17, arr),
}

I must say that I am a bit confused about the 16/32bit for TIM3. From what I understand from the reference manual for g0x0 TIM3 is just 16-bit. reference manual for g0x1 says pretty much the same thing except it adds a TIM2 which is 32-bit.

Looking at the pac, TIM3 does have an arr_h(inherited from tim2?), same thing when looking in the reference manual...

Also is it really safe to write to arr in two steps without the pwm signal going crazy in between the stores?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct me if I am wrong but from what I can gather, TIM3 is 16bits but does share the same memory config structure as TIM2(which is 32bit)

)*
self.tim.cr1.write(|w| w.cen().set_bit())
}
}

/// Get value of period register, this is often times the same as max duty cycle
pub fn get_period_register(&mut self) -> u16 {
self.tim.arr.read().bits() as u16
}

/// Set only prescale register
pub fn set_prescaler_register(&mut self, prescaler: u16) {
unsafe {
self.tim.psc.write(|w| w.psc().bits(prescaler));
}
}

/// Set prescaler to be able to reach `freq` or higher
///
/// NOTE: Actual lowest reachable frequency may be lower than requested
/// due to rounding errors
/// NOTE: This will not update the period register
pub fn set_min_frequency(&mut self, freq: Hertz) {
let ratio = self.clk / freq;
let psc = (ratio - 1) / 0xffff;

unsafe {
self.tim.psc.write(|w| w.psc().bits(psc as u16));
}
}

/// Starts listening
pub fn listen(&mut self) {
self.tim.dier.write(|w| w.uie().set_bit());
Expand All @@ -131,6 +166,14 @@ macro_rules! pwm {
self.tim.sr.modify(|_, w| w.uif().clear_bit());
}

pub fn start_timer(&mut self) {
self.tim.cr1.modify(|w| w.cen().set_bit())
}

pub fn pause_timer() {
self.tim.cr1.modify(|w| w.cen().clear_bit())
}

/// Resets counter value
pub fn reset(&mut self) {
self.tim.cnt.reset();
Expand Down