mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-21 22:32:29 +00:00
Merge pull request #3310 from Ecco/lptim
stm32: Add a first Lptim driver
This commit is contained in:
commit
7fde354b21
@ -72,7 +72,7 @@ rand_core = "0.6.3"
|
||||
sdio-host = "0.5.0"
|
||||
critical-section = "1.1"
|
||||
#stm32-metapac = { version = "15" }
|
||||
stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-5ef354f3e49f790e47f5c818f243459742c9b83b" }
|
||||
stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-ad00827345b4b758b2453082809d6e3b634b5364" }
|
||||
|
||||
vcell = "0.1.3"
|
||||
nb = "1.0.0"
|
||||
@ -99,7 +99,7 @@ proc-macro2 = "1.0.36"
|
||||
quote = "1.0.15"
|
||||
|
||||
#stm32-metapac = { version = "15", default-features = false, features = ["metadata"]}
|
||||
stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-5ef354f3e49f790e47f5c818f243459742c9b83b", default-features = false, features = ["metadata"] }
|
||||
stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-ad00827345b4b758b2453082809d6e3b634b5364", default-features = false, features = ["metadata"] }
|
||||
|
||||
[features]
|
||||
default = ["rt"]
|
||||
|
@ -1015,6 +1015,9 @@ fn main() {
|
||||
(("hrtim", "CHE2"), quote!(crate::hrtim::ChannelEComplementaryPin)),
|
||||
(("hrtim", "CHF1"), quote!(crate::hrtim::ChannelFPin)),
|
||||
(("hrtim", "CHF2"), quote!(crate::hrtim::ChannelFComplementaryPin)),
|
||||
(("lptim", "CH1"), quote!(crate::lptim::Channel1Pin)),
|
||||
(("lptim", "CH2"), quote!(crate::lptim::Channel2Pin)),
|
||||
(("lptim", "OUT"), quote!(crate::lptim::OutputPin)),
|
||||
(("sdmmc", "CK"), quote!(crate::sdmmc::CkPin)),
|
||||
(("sdmmc", "CMD"), quote!(crate::sdmmc::CmdPin)),
|
||||
(("sdmmc", "D0"), quote!(crate::sdmmc::D0Pin)),
|
||||
|
@ -4,7 +4,7 @@
|
||||
#![allow(missing_docs)] // TODO
|
||||
#![cfg_attr(adc_f3_v2, allow(unused))]
|
||||
|
||||
#[cfg(not(adc_f3_v2))]
|
||||
#[cfg(not(any(adc_f3_v2, adc_u5)))]
|
||||
#[cfg_attr(adc_f1, path = "f1.rs")]
|
||||
#[cfg_attr(adc_f3, path = "f3.rs")]
|
||||
#[cfg_attr(adc_f3_v1_1, path = "f3_v1_1.rs")]
|
||||
@ -19,14 +19,16 @@ mod _version;
|
||||
use core::marker::PhantomData;
|
||||
|
||||
#[allow(unused)]
|
||||
#[cfg(not(adc_f3_v2))]
|
||||
#[cfg(not(any(adc_f3_v2, adc_u5)))]
|
||||
pub use _version::*;
|
||||
#[cfg(any(adc_f1, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))]
|
||||
use embassy_sync::waitqueue::AtomicWaker;
|
||||
|
||||
#[cfg(not(any(adc_u5)))]
|
||||
pub use crate::pac::adc::vals;
|
||||
#[cfg(not(any(adc_f1, adc_f3_v2)))]
|
||||
#[cfg(not(any(adc_f1, adc_f3_v2, adc_u5)))]
|
||||
pub use crate::pac::adc::vals::Res as Resolution;
|
||||
#[cfg(not(any(adc_u5)))]
|
||||
pub use crate::pac::adc::vals::SampleTime;
|
||||
use crate::peripherals;
|
||||
|
||||
@ -36,7 +38,7 @@ dma_trait!(RxDma, Instance);
|
||||
pub struct Adc<'d, T: Instance> {
|
||||
#[allow(unused)]
|
||||
adc: crate::PeripheralRef<'d, T>,
|
||||
#[cfg(not(any(adc_f3_v2, adc_f3_v1_1)))]
|
||||
#[cfg(not(any(adc_f3_v2, adc_f3_v1_1, adc_u5)))]
|
||||
sample_time: SampleTime,
|
||||
}
|
||||
|
||||
@ -57,7 +59,7 @@ impl State {
|
||||
trait SealedInstance {
|
||||
#[allow(unused)]
|
||||
fn regs() -> crate::pac::adc::Adc;
|
||||
#[cfg(not(any(adc_f1, adc_v1, adc_l0, adc_f3_v2, adc_f3_v1_1, adc_g0)))]
|
||||
#[cfg(not(any(adc_f1, adc_v1, adc_l0, adc_f3_v2, adc_f3_v1_1, adc_g0, adc_u5)))]
|
||||
#[allow(unused)]
|
||||
fn common_regs() -> crate::pac::adccommon::AdcCommon;
|
||||
#[cfg(any(adc_f1, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))]
|
||||
@ -163,7 +165,7 @@ foreach_adc!(
|
||||
crate::pac::$inst
|
||||
}
|
||||
|
||||
#[cfg(not(any(adc_f1, adc_v1, adc_l0, adc_f3_v2, adc_f3_v1_1, adc_g0)))]
|
||||
#[cfg(not(any(adc_f1, adc_v1, adc_l0, adc_f3_v2, adc_f3_v1_1, adc_g0, adc_u5)))]
|
||||
fn common_regs() -> crate::pac::adccommon::AdcCommon {
|
||||
return crate::pac::$common_inst
|
||||
}
|
||||
@ -200,7 +202,7 @@ macro_rules! impl_adc_pin {
|
||||
/// Get the maximum reading value for this resolution.
|
||||
///
|
||||
/// This is `2**n - 1`.
|
||||
#[cfg(not(any(adc_f1, adc_f3_v2)))]
|
||||
#[cfg(not(any(adc_f1, adc_f3_v2, adc_u5)))]
|
||||
pub const fn resolution_to_max_count(res: Resolution) -> u32 {
|
||||
match res {
|
||||
#[cfg(adc_v4)]
|
||||
|
@ -89,6 +89,8 @@ pub mod i2s;
|
||||
pub mod ipcc;
|
||||
#[cfg(feature = "low-power")]
|
||||
pub mod low_power;
|
||||
#[cfg(lptim)]
|
||||
pub mod lptim;
|
||||
#[cfg(ltdc)]
|
||||
pub mod ltdc;
|
||||
#[cfg(opamp)]
|
||||
|
18
embassy-stm32/src/lptim/channel.rs
Normal file
18
embassy-stm32/src/lptim/channel.rs
Normal file
@ -0,0 +1,18 @@
|
||||
/// Timer channel.
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum Channel {
|
||||
/// Channel 1.
|
||||
Ch1,
|
||||
/// Channel 2.
|
||||
Ch2,
|
||||
}
|
||||
|
||||
impl Channel {
|
||||
/// Get the channel index (0..1)
|
||||
pub fn index(&self) -> usize {
|
||||
match self {
|
||||
Channel::Ch1 => 0,
|
||||
Channel::Ch2 => 1,
|
||||
}
|
||||
}
|
||||
}
|
48
embassy-stm32/src/lptim/mod.rs
Normal file
48
embassy-stm32/src/lptim/mod.rs
Normal file
@ -0,0 +1,48 @@
|
||||
//! Low-power timer (LPTIM)
|
||||
|
||||
pub mod pwm;
|
||||
pub mod timer;
|
||||
|
||||
use crate::rcc::RccPeripheral;
|
||||
|
||||
/// Timer channel.
|
||||
#[cfg(any(lptim_v2a, lptim_v2b))]
|
||||
mod channel;
|
||||
#[cfg(any(lptim_v2a, lptim_v2b))]
|
||||
pub use channel::Channel;
|
||||
|
||||
pin_trait!(OutputPin, BasicInstance);
|
||||
pin_trait!(Channel1Pin, BasicInstance);
|
||||
pin_trait!(Channel2Pin, BasicInstance);
|
||||
|
||||
pub(crate) trait SealedInstance: RccPeripheral {
|
||||
fn regs() -> crate::pac::lptim::Lptim;
|
||||
}
|
||||
pub(crate) trait SealedBasicInstance: RccPeripheral {}
|
||||
|
||||
/// LPTIM basic instance trait.
|
||||
#[allow(private_bounds)]
|
||||
pub trait BasicInstance: SealedBasicInstance + 'static {}
|
||||
|
||||
/// LPTIM instance trait.
|
||||
#[allow(private_bounds)]
|
||||
pub trait Instance: BasicInstance + SealedInstance + 'static {}
|
||||
|
||||
foreach_interrupt! {
|
||||
($inst:ident, lptim, LPTIM, GLOBAL, $irq:ident) => {
|
||||
impl SealedInstance for crate::peripherals::$inst {
|
||||
fn regs() -> crate::pac::lptim::Lptim {
|
||||
crate::pac::$inst
|
||||
}
|
||||
}
|
||||
impl SealedBasicInstance for crate::peripherals::$inst {
|
||||
}
|
||||
impl BasicInstance for crate::peripherals::$inst {}
|
||||
impl Instance for crate::peripherals::$inst {}
|
||||
};
|
||||
($inst:ident, lptim, LPTIM_BASIC, GLOBAL, $irq:ident) => {
|
||||
impl SealedBasicInstance for crate::peripherals::$inst {
|
||||
}
|
||||
impl BasicInstance for crate::peripherals::$inst {}
|
||||
};
|
||||
}
|
168
embassy-stm32/src/lptim/pwm.rs
Normal file
168
embassy-stm32/src/lptim/pwm.rs
Normal file
@ -0,0 +1,168 @@
|
||||
//! PWM driver.
|
||||
|
||||
use core::marker::PhantomData;
|
||||
|
||||
use embassy_hal_internal::{into_ref, PeripheralRef};
|
||||
|
||||
use super::timer::Timer;
|
||||
#[cfg(not(any(lptim_v2a, lptim_v2b)))]
|
||||
use super::OutputPin;
|
||||
#[cfg(any(lptim_v2a, lptim_v2b))]
|
||||
use super::{channel::Channel, timer::ChannelDirection, Channel1Pin, Channel2Pin};
|
||||
use super::{BasicInstance, Instance};
|
||||
use crate::gpio::{AfType, AnyPin, OutputType, Speed};
|
||||
use crate::time::Hertz;
|
||||
use crate::Peripheral;
|
||||
|
||||
/// Output marker type.
|
||||
pub enum Output {}
|
||||
/// Channel 1 marker type.
|
||||
pub enum Ch1 {}
|
||||
/// Channel 2 marker type.
|
||||
pub enum Ch2 {}
|
||||
|
||||
/// PWM pin wrapper.
|
||||
///
|
||||
/// This wraps a pin to make it usable with PWM.
|
||||
pub struct PwmPin<'d, T, C> {
|
||||
_pin: PeripheralRef<'d, AnyPin>,
|
||||
phantom: PhantomData<(T, C)>,
|
||||
}
|
||||
|
||||
macro_rules! channel_impl {
|
||||
($new_chx:ident, $channel:ident, $pin_trait:ident) => {
|
||||
impl<'d, T: BasicInstance> PwmPin<'d, T, $channel> {
|
||||
#[doc = concat!("Create a new ", stringify!($channel), " PWM pin instance.")]
|
||||
pub fn $new_chx(pin: impl Peripheral<P = impl $pin_trait<T>> + 'd) -> Self {
|
||||
into_ref!(pin);
|
||||
critical_section::with(|_| {
|
||||
pin.set_low();
|
||||
pin.set_as_af(
|
||||
pin.af_num(),
|
||||
AfType::output(OutputType::PushPull, Speed::VeryHigh),
|
||||
);
|
||||
});
|
||||
PwmPin {
|
||||
_pin: pin.map_into(),
|
||||
phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(not(any(lptim_v2a, lptim_v2b)))]
|
||||
channel_impl!(new, Output, OutputPin);
|
||||
#[cfg(any(lptim_v2a, lptim_v2b))]
|
||||
channel_impl!(new_ch1, Ch1, Channel1Pin);
|
||||
#[cfg(any(lptim_v2a, lptim_v2b))]
|
||||
channel_impl!(new_ch2, Ch2, Channel2Pin);
|
||||
|
||||
/// PWM driver.
|
||||
pub struct Pwm<'d, T: Instance> {
|
||||
inner: Timer<'d, T>,
|
||||
}
|
||||
|
||||
#[cfg(not(any(lptim_v2a, lptim_v2b)))]
|
||||
impl<'d, T: Instance> Pwm<'d, T> {
|
||||
/// Create a new PWM driver.
|
||||
pub fn new(tim: impl Peripheral<P = T> + 'd, _output_pin: PwmPin<'d, T, Output>, freq: Hertz) -> Self {
|
||||
Self::new_inner(tim, freq)
|
||||
}
|
||||
|
||||
/// Set the duty.
|
||||
///
|
||||
/// The value ranges from 0 for 0% duty, to [`get_max_duty`](Self::get_max_duty) for 100% duty, both included.
|
||||
pub fn set_duty(&mut self, duty: u16) {
|
||||
assert!(duty <= self.get_max_duty());
|
||||
self.inner.set_compare_value(duty)
|
||||
}
|
||||
|
||||
/// Get the duty.
|
||||
///
|
||||
/// The value ranges from 0 for 0% duty, to [`get_max_duty`](Self::get_max_duty) for 100% duty, both included.
|
||||
pub fn get_duty(&self) -> u16 {
|
||||
self.inner.get_compare_value()
|
||||
}
|
||||
|
||||
fn post_init(&mut self) {}
|
||||
}
|
||||
|
||||
#[cfg(any(lptim_v2a, lptim_v2b))]
|
||||
impl<'d, T: Instance> Pwm<'d, T> {
|
||||
/// Create a new PWM driver.
|
||||
pub fn new(
|
||||
tim: impl Peripheral<P = T> + 'd,
|
||||
_ch1_pin: Option<PwmPin<'d, T, Ch1>>,
|
||||
_ch2_pin: Option<PwmPin<'d, T, Ch2>>,
|
||||
freq: Hertz,
|
||||
) -> Self {
|
||||
Self::new_inner(tim, freq)
|
||||
}
|
||||
|
||||
/// Enable the given channel.
|
||||
pub fn enable(&mut self, channel: Channel) {
|
||||
self.inner.enable_channel(channel, true);
|
||||
}
|
||||
|
||||
/// Disable the given channel.
|
||||
pub fn disable(&mut self, channel: Channel) {
|
||||
self.inner.enable_channel(channel, false);
|
||||
}
|
||||
|
||||
/// Check whether given channel is enabled
|
||||
pub fn is_enabled(&self, channel: Channel) -> bool {
|
||||
self.inner.get_channel_enable_state(channel)
|
||||
}
|
||||
|
||||
/// Set the duty for a given channel.
|
||||
///
|
||||
/// The value ranges from 0 for 0% duty, to [`get_max_duty`](Self::get_max_duty) for 100% duty, both included.
|
||||
pub fn set_duty(&mut self, channel: Channel, duty: u16) {
|
||||
assert!(duty <= self.get_max_duty());
|
||||
self.inner.set_compare_value(channel, duty)
|
||||
}
|
||||
|
||||
/// Get the duty for a given channel.
|
||||
///
|
||||
/// The value ranges from 0 for 0% duty, to [`get_max_duty`](Self::get_max_duty) for 100% duty, both included.
|
||||
pub fn get_duty(&self, channel: Channel) -> u16 {
|
||||
self.inner.get_compare_value(channel)
|
||||
}
|
||||
|
||||
fn post_init(&mut self) {
|
||||
[Channel::Ch1, Channel::Ch2].iter().for_each(|&channel| {
|
||||
self.inner.set_channel_direction(channel, ChannelDirection::OutputPwm);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Instance> Pwm<'d, T> {
|
||||
fn new_inner(tim: impl Peripheral<P = T> + 'd, freq: Hertz) -> Self {
|
||||
let mut this = Self { inner: Timer::new(tim) };
|
||||
|
||||
this.inner.enable();
|
||||
this.set_frequency(freq);
|
||||
|
||||
this.post_init();
|
||||
|
||||
this.inner.continuous_mode_start();
|
||||
|
||||
this
|
||||
}
|
||||
|
||||
/// Set PWM frequency.
|
||||
///
|
||||
/// Note: when you call this, the max duty value changes, so you will have to
|
||||
/// call `set_duty` on all channels with the duty calculated based on the new max duty.
|
||||
pub fn set_frequency(&mut self, frequency: Hertz) {
|
||||
self.inner.set_frequency(frequency);
|
||||
}
|
||||
|
||||
/// Get max duty value.
|
||||
///
|
||||
/// This value depends on the configured frequency and the timer's clock rate from RCC.
|
||||
pub fn get_max_duty(&self) -> u16 {
|
||||
self.inner.get_max_compare_value() + 1
|
||||
}
|
||||
}
|
18
embassy-stm32/src/lptim/timer/channel_direction.rs
Normal file
18
embassy-stm32/src/lptim/timer/channel_direction.rs
Normal file
@ -0,0 +1,18 @@
|
||||
use crate::pac::lptim::vals;
|
||||
|
||||
/// Direction of a low-power timer channel
|
||||
pub enum ChannelDirection {
|
||||
/// Use channel as a PWM output
|
||||
OutputPwm,
|
||||
/// Use channel as an input capture
|
||||
InputCapture,
|
||||
}
|
||||
|
||||
impl From<ChannelDirection> for vals::Ccsel {
|
||||
fn from(direction: ChannelDirection) -> Self {
|
||||
match direction {
|
||||
ChannelDirection::OutputPwm => vals::Ccsel::OUTPUTCOMPARE,
|
||||
ChannelDirection::InputCapture => vals::Ccsel::INPUTCAPTURE,
|
||||
}
|
||||
}
|
||||
}
|
133
embassy-stm32/src/lptim/timer/mod.rs
Normal file
133
embassy-stm32/src/lptim/timer/mod.rs
Normal file
@ -0,0 +1,133 @@
|
||||
//! Low-level timer driver.
|
||||
mod prescaler;
|
||||
|
||||
use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef};
|
||||
|
||||
#[cfg(any(lptim_v2a, lptim_v2b))]
|
||||
use super::channel::Channel;
|
||||
#[cfg(any(lptim_v2a, lptim_v2b))]
|
||||
mod channel_direction;
|
||||
#[cfg(any(lptim_v2a, lptim_v2b))]
|
||||
pub use channel_direction::ChannelDirection;
|
||||
use prescaler::Prescaler;
|
||||
|
||||
use super::Instance;
|
||||
use crate::rcc;
|
||||
use crate::time::Hertz;
|
||||
|
||||
/// Low-level timer driver.
|
||||
pub struct Timer<'d, T: Instance> {
|
||||
_tim: PeripheralRef<'d, T>,
|
||||
}
|
||||
|
||||
impl<'d, T: Instance> Timer<'d, T> {
|
||||
/// Create a new timer driver.
|
||||
pub fn new(tim: impl Peripheral<P = T> + 'd) -> Self {
|
||||
into_ref!(tim);
|
||||
|
||||
rcc::enable_and_reset::<T>();
|
||||
|
||||
Self { _tim: tim }
|
||||
}
|
||||
|
||||
/// Enable the timer.
|
||||
pub fn enable(&self) {
|
||||
T::regs().cr().modify(|w| w.set_enable(true));
|
||||
}
|
||||
|
||||
/// Disable the timer.
|
||||
pub fn disable(&self) {
|
||||
T::regs().cr().modify(|w| w.set_enable(false));
|
||||
}
|
||||
|
||||
/// Start the timer in single pulse mode.
|
||||
pub fn single_mode_start(&self) {
|
||||
T::regs().cr().modify(|w| w.set_sngstrt(true));
|
||||
}
|
||||
|
||||
/// Start the timer in continuous mode.
|
||||
pub fn continuous_mode_start(&self) {
|
||||
T::regs().cr().modify(|w| w.set_cntstrt(true));
|
||||
}
|
||||
|
||||
/// Set the frequency of how many times per second the timer counts up to the max value or down to 0.
|
||||
pub fn set_frequency(&self, frequency: Hertz) {
|
||||
let f = frequency.0;
|
||||
assert!(f > 0);
|
||||
|
||||
let pclk_f = T::frequency().0;
|
||||
|
||||
let pclk_ticks_per_timer_period = pclk_f / f;
|
||||
|
||||
let psc = Prescaler::from_ticks(pclk_ticks_per_timer_period);
|
||||
let arr = psc.scale_down(pclk_ticks_per_timer_period);
|
||||
|
||||
T::regs().cfgr().modify(|r| r.set_presc((&psc).into()));
|
||||
T::regs().arr().modify(|r| r.set_arr(arr.into()));
|
||||
}
|
||||
|
||||
/// Get the timer frequency.
|
||||
pub fn get_frequency(&self) -> Hertz {
|
||||
let pclk_f = T::frequency();
|
||||
let arr = T::regs().arr().read().arr();
|
||||
let psc = Prescaler::from(T::regs().cfgr().read().presc());
|
||||
|
||||
pclk_f / psc.scale_up(arr)
|
||||
}
|
||||
|
||||
/// Get the clock frequency of the timer (before prescaler is applied).
|
||||
pub fn get_clock_frequency(&self) -> Hertz {
|
||||
T::frequency()
|
||||
}
|
||||
|
||||
/// Get max compare value. This depends on the timer frequency and the clock frequency from RCC.
|
||||
pub fn get_max_compare_value(&self) -> u16 {
|
||||
T::regs().arr().read().arr()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(lptim_v2a, lptim_v2b))]
|
||||
impl<'d, T: Instance> Timer<'d, T> {
|
||||
/// Enable/disable a channel.
|
||||
pub fn enable_channel(&self, channel: Channel, enable: bool) {
|
||||
T::regs().ccmr(0).modify(|w| {
|
||||
w.set_cce(channel.index(), enable);
|
||||
});
|
||||
}
|
||||
|
||||
/// Get enable/disable state of a channel
|
||||
pub fn get_channel_enable_state(&self, channel: Channel) -> bool {
|
||||
T::regs().ccmr(0).read().cce(channel.index())
|
||||
}
|
||||
|
||||
/// Set compare value for a channel.
|
||||
pub fn set_compare_value(&self, channel: Channel, value: u16) {
|
||||
T::regs().ccr(channel.index()).modify(|w| w.set_ccr(value));
|
||||
}
|
||||
|
||||
/// Get compare value for a channel.
|
||||
pub fn get_compare_value(&self, channel: Channel) -> u16 {
|
||||
T::regs().ccr(channel.index()).read().ccr()
|
||||
}
|
||||
|
||||
/// Set channel direction.
|
||||
#[cfg(any(lptim_v2a, lptim_v2b))]
|
||||
pub fn set_channel_direction(&self, channel: Channel, direction: ChannelDirection) {
|
||||
T::regs()
|
||||
.ccmr(0)
|
||||
.modify(|w| w.set_ccsel(channel.index(), direction.into()));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(any(lptim_v2a, lptim_v2b)))]
|
||||
impl<'d, T: Instance> Timer<'d, T> {
|
||||
/// Set compare value for a channel.
|
||||
pub fn set_compare_value(&self, value: u16) {
|
||||
T::regs().cmp().modify(|w| w.set_cmp(value));
|
||||
}
|
||||
|
||||
/// Get compare value for a channel.
|
||||
pub fn get_compare_value(&self) -> u16 {
|
||||
T::regs().cmp().read().cmp()
|
||||
}
|
||||
}
|
90
embassy-stm32/src/lptim/timer/prescaler.rs
Normal file
90
embassy-stm32/src/lptim/timer/prescaler.rs
Normal file
@ -0,0 +1,90 @@
|
||||
//! Low-level timer driver.
|
||||
|
||||
use crate::pac::lptim::vals;
|
||||
|
||||
pub enum Prescaler {
|
||||
Div1,
|
||||
Div2,
|
||||
Div4,
|
||||
Div8,
|
||||
Div16,
|
||||
Div32,
|
||||
Div64,
|
||||
Div128,
|
||||
}
|
||||
|
||||
impl From<&Prescaler> for vals::Presc {
|
||||
fn from(prescaler: &Prescaler) -> Self {
|
||||
match prescaler {
|
||||
Prescaler::Div1 => vals::Presc::DIV1,
|
||||
Prescaler::Div2 => vals::Presc::DIV2,
|
||||
Prescaler::Div4 => vals::Presc::DIV4,
|
||||
Prescaler::Div8 => vals::Presc::DIV8,
|
||||
Prescaler::Div16 => vals::Presc::DIV16,
|
||||
Prescaler::Div32 => vals::Presc::DIV32,
|
||||
Prescaler::Div64 => vals::Presc::DIV64,
|
||||
Prescaler::Div128 => vals::Presc::DIV128,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<vals::Presc> for Prescaler {
|
||||
fn from(prescaler: vals::Presc) -> Self {
|
||||
match prescaler {
|
||||
vals::Presc::DIV1 => Prescaler::Div1,
|
||||
vals::Presc::DIV2 => Prescaler::Div2,
|
||||
vals::Presc::DIV4 => Prescaler::Div4,
|
||||
vals::Presc::DIV8 => Prescaler::Div8,
|
||||
vals::Presc::DIV16 => Prescaler::Div16,
|
||||
vals::Presc::DIV32 => Prescaler::Div32,
|
||||
vals::Presc::DIV64 => Prescaler::Div64,
|
||||
vals::Presc::DIV128 => Prescaler::Div128,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Prescaler> for u32 {
|
||||
fn from(prescaler: &Prescaler) -> Self {
|
||||
match prescaler {
|
||||
Prescaler::Div1 => 1,
|
||||
Prescaler::Div2 => 2,
|
||||
Prescaler::Div4 => 4,
|
||||
Prescaler::Div8 => 8,
|
||||
Prescaler::Div16 => 16,
|
||||
Prescaler::Div32 => 32,
|
||||
Prescaler::Div64 => 64,
|
||||
Prescaler::Div128 => 128,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u32> for Prescaler {
|
||||
fn from(prescaler: u32) -> Self {
|
||||
match prescaler {
|
||||
1 => Prescaler::Div1,
|
||||
2 => Prescaler::Div2,
|
||||
4 => Prescaler::Div4,
|
||||
8 => Prescaler::Div8,
|
||||
16 => Prescaler::Div16,
|
||||
32 => Prescaler::Div32,
|
||||
64 => Prescaler::Div64,
|
||||
128 => Prescaler::Div128,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Prescaler {
|
||||
pub fn from_ticks(ticks: u32) -> Self {
|
||||
// We need to scale down to a 16-bit range
|
||||
(ticks >> 16).next_power_of_two().into()
|
||||
}
|
||||
|
||||
pub fn scale_down(&self, ticks: u32) -> u16 {
|
||||
(ticks / u32::from(self)).try_into().unwrap()
|
||||
}
|
||||
|
||||
pub fn scale_up(&self, ticks: u16) -> u32 {
|
||||
u32::from(self) * ticks as u32
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user