Merge pull request #2484 from Murmele/main

use constant for the pwm clock
This commit is contained in:
Dario Nieuwenhuis 2024-01-26 23:40:02 +00:00 committed by GitHub
commit c37e483b3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -47,6 +47,8 @@ pub enum Error {
}
const MAX_SEQUENCE_LEN: usize = 32767;
/// The used pwm clock frequency
pub const PWM_CLK_HZ: u32 = 16_000_000;
impl<'d, T: Instance> SequencePwm<'d, T> {
/// Create a new 1-channel PWM
@ -713,6 +715,13 @@ impl<'d, T: Instance> SimplePwm<'d, T> {
pwm
}
/// Returns the enable state of the pwm counter
#[inline(always)]
pub fn is_enabled(&self) -> bool {
let r = T::regs();
r.enable.read().enable().bit_is_set()
}
/// Enables the PWM generator.
#[inline(always)]
pub fn enable(&self) {
@ -788,7 +797,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 +805,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
}