Added a pwm update event interrupt and one test to show the functionality

This commit is contained in:
anton smeenk
2023-11-10 14:05:19 +01:00
parent 6ff91851b1
commit 17ab454c8e
3 changed files with 173 additions and 0 deletions

View File

@ -1,6 +1,8 @@
use core::marker::PhantomData;
use embassy_hal_internal::{into_ref, PeripheralRef};
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::signal::Signal;
use super::*;
#[allow(unused_imports)]
@ -8,6 +10,28 @@ use crate::gpio::sealed::{AFType, Pin};
use crate::gpio::{AnyPin, OutputType};
use crate::time::Hertz;
use crate::Peripheral;
use crate::_generated::interrupt::typelevel::Interrupt;
// Declare a signal to awake user code for signaling the update interrupt id happen
static SIGNAL_UPDATE: Signal<CriticalSectionRawMutex, usize> = Signal::new();
pub struct InterruptHandler<T: CaptureCompare16bitInstance> {
_phantom: PhantomData<T>,
}
impl<T: CaptureCompare16bitInstance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandler<T> {
unsafe fn on_interrupt() {
let regs = T::regs();
let sr = regs.sr().read();
if sr.uif() {
SIGNAL_UPDATE.signal(0);
// clear the flag
critical_section::with(|_| {
regs.sr().modify(|w| w.set_uif(false));
})
}
}
}
pub struct Ch1;
pub struct Ch2;
@ -82,6 +106,9 @@ impl<'d, T: CaptureCompare16bitInstance> SimplePwm<'d, T> {
.set_output_compare_mode(Channel::Ch3, OutputCompareMode::PwmMode1);
this.inner
.set_output_compare_mode(Channel::Ch4, OutputCompareMode::PwmMode1);
T::Interrupt::unpend();
unsafe { T::Interrupt::enable() };
this
}
@ -106,6 +133,14 @@ impl<'d, T: CaptureCompare16bitInstance> SimplePwm<'d, T> {
self.inner.get_max_compare_value() + 1
}
pub fn enable_update_interrupt(&mut self, enable: bool) {
self.inner.enable_update_interrupt(enable);
}
pub async fn wait_update_interrupt(&self) {
_ = SIGNAL_UPDATE.wait().await;
}
pub fn set_duty(&mut self, channel: Channel, duty: u16) {
assert!(duty <= self.get_max_duty());
self.inner.set_compare_value(channel, duty)