use constant for the pwm clock

Description: So it can be used outside of the crate to calculate max duty
This commit is contained in:
Martin Marmsoler 2024-01-26 20:55:53 +01:00
parent 31fa0aebd8
commit dd2577fcf0

View file

@ -47,6 +47,7 @@ pub enum Error {
}
const MAX_SEQUENCE_LEN: usize = 32767;
pub const PWM_CLK_HZ: u32 = 16_000_000;
impl<'d, T: Instance> SequencePwm<'d, T> {
/// Create a new 1-channel PWM
@ -788,7 +789,7 @@ impl<'d, T: Instance> SimplePwm<'d, T> {
/// Sets the PWM output frequency.
#[inline(always)]
pub fn set_period(&self, freq: u32) {
let clk = 16_000_000u32 >> (self.prescaler() as u8);
let clk = PWM_CLK_HZ >> (self.prescaler() as u8);
let duty = clk / freq;
self.set_max_duty(duty.min(32767) as u16);
}
@ -796,7 +797,7 @@ impl<'d, T: Instance> SimplePwm<'d, T> {
/// Returns the PWM output frequency.
#[inline(always)]
pub fn period(&self) -> u32 {
let clk = 16_000_000u32 >> (self.prescaler() as u8);
let clk = PWM_CLK_HZ >> (self.prescaler() as u8);
let max_duty = self.max_duty() as u32;
clk / max_duty
}