embassy-rs / embassy

Modern embedded framework, using Rust and async.
https://embassy.dev
Apache License 2.0
5.31k stars 732 forks source link

Future for Timer Update Interrupt #3108

Open shufps opened 3 months ago

shufps commented 3 months ago

Hi,

I'm trying to get a DDS to work but currently there seems not to be a way to have a future waiting for the timer update interrupt or timer compare interrupt

Unfortunately this is needed because DDS is completly different to waveforms that are played back from RAM via DMA.

It requires to calculate the next PWM value based on a fixed point phase accumulator pointing to a sine table but this requires to update the PWM value after each timer update interrupt - better would be timer compare interrupt.

I saw in the low_level.rs that there is some function that can return if a timer interrupt happened but it would be a "bruteforce"-polling method that wastes a lot of CPU power. Better would be a Future that awaits in some task.

I can contribute some DDS code example if someone could point me in the right direction.

Thank you very much, Thomas

shufps commented 3 months ago

Mmhmm maybe I can C&P parts from the Input Capture Interrupt :thinking:

shufps commented 3 months ago

I think this should work with minimum overhead:

#[interrupt]
fn TIM2() {
    unsafe {
        // get next value of DDS
        DDS_AKKU = DDS_AKKU.wrapping_add(DDS_INCR);
        let value = DDS_SINE_DATA[(DDS_AKKU >> 24) as usize];
        pac::TIM2.ccr(3).modify(|w| w.set_ccr(value as u16));
    }
}
shufps commented 3 months ago

https://github.com/embassy-rs/embassy/pull/3127

romainreignier commented 3 months ago

I don't know what do you mean by DDS but this PR might interest you for the futures: https://github.com/embassy-rs/embassy/pull/3029

shufps commented 3 months ago

I don't know what do you mean by DDS

DDS means "direct digital synthesis" - it's like the opposite of digitizing an analog audio into digital samles.

DDS creates digital samples of a sine wave that is then converted to analog external.

The trick on DDS is that you can generate arbitrary sine frequencies (actually doesn't need to be sine, can be any periodic waveform) - like you can digitize arbitrary sine wave frequencies (ofc with respect to Nyquist) of an analogue signal.

It's a little bit of an edge case, not so much used, but it's needed for digital signal processing often.

Oh nice to see that I'm not the only one who would need Futures for Timers :raised_hands: