diff options
| author | Liu Hancheng <[email protected]> | 2025-01-01 17:05:48 +0800 |
|---|---|---|
| committer | Liu Hancheng <[email protected]> | 2025-01-01 17:05:48 +0800 |
| commit | cbc7a9fe5b9bfda8a53316cd231d6a4b7a3bbfd9 (patch) | |
| tree | ea385395fe9f8ac1abbeef0e22c3d5aacf0cbe69 /embassy-stm32 | |
| parent | 667dfa34b525f727936d621ba91001fa25d80426 (diff) | |
feat: Add 32-bit timer support for waveform function
Diffstat (limited to 'embassy-stm32')
| -rw-r--r-- | embassy-stm32/src/timer/low_level.rs | 4 | ||||
| -rw-r--r-- | embassy-stm32/src/timer/simple_pwm.rs | 39 |
2 files changed, 34 insertions, 9 deletions
diff --git a/embassy-stm32/src/timer/low_level.rs b/embassy-stm32/src/timer/low_level.rs index 7360d6aef..448069ab3 100644 --- a/embassy-stm32/src/timer/low_level.rs +++ b/embassy-stm32/src/timer/low_level.rs | |||
| @@ -235,6 +235,10 @@ impl<'d, T: CoreInstance> Timer<'d, T> { | |||
| 235 | self.regs_core().cnt().write(|r| r.set_cnt(0)); | 235 | self.regs_core().cnt().write(|r| r.set_cnt(0)); |
| 236 | } | 236 | } |
| 237 | 237 | ||
| 238 | pub fn get_bits(&self) -> TimerBits { | ||
| 239 | T::BITS | ||
| 240 | } | ||
| 241 | |||
| 238 | /// Set the frequency of how many times per second the timer counts up to the max value or down to 0. | 242 | /// Set the frequency of how many times per second the timer counts up to the max value or down to 0. |
| 239 | /// | 243 | /// |
| 240 | /// This means that in the default edge-aligned mode, | 244 | /// This means that in the default edge-aligned mode, |
diff --git a/embassy-stm32/src/timer/simple_pwm.rs b/embassy-stm32/src/timer/simple_pwm.rs index 56fb1871e..c6808593a 100644 --- a/embassy-stm32/src/timer/simple_pwm.rs +++ b/embassy-stm32/src/timer/simple_pwm.rs | |||
| @@ -6,6 +6,7 @@ use core::mem::ManuallyDrop; | |||
| 6 | use embassy_hal_internal::{into_ref, PeripheralRef}; | 6 | use embassy_hal_internal::{into_ref, PeripheralRef}; |
| 7 | 7 | ||
| 8 | use super::low_level::{CountingMode, OutputCompareMode, OutputPolarity, Timer}; | 8 | use super::low_level::{CountingMode, OutputCompareMode, OutputPolarity, Timer}; |
| 9 | use super::TimerBits; | ||
| 9 | use super::{Channel, Channel1Pin, Channel2Pin, Channel3Pin, Channel4Pin, GeneralInstance4Channel}; | 10 | use super::{Channel, Channel1Pin, Channel2Pin, Channel3Pin, Channel4Pin, GeneralInstance4Channel}; |
| 10 | use crate::gpio::{AfType, AnyPin, OutputType, Speed}; | 11 | use crate::gpio::{AfType, AnyPin, OutputType, Speed}; |
| 11 | use crate::time::Hertz; | 12 | use crate::time::Hertz; |
| @@ -365,7 +366,7 @@ macro_rules! impl_waveform_chx { | |||
| 365 | /// | 366 | /// |
| 366 | /// Note: | 367 | /// Note: |
| 367 | /// you will need to provide corresponding TIMx_CHy DMA channel to use this method. | 368 | /// you will need to provide corresponding TIMx_CHy DMA channel to use this method. |
| 368 | pub async fn $fn_name(&mut self, dma: impl Peripheral<P = impl super::$dma_ch<T>>, duty: &[u16]) { | 369 | pub async fn $fn_name(&mut self, dma: impl Peripheral<P = impl super::$dma_ch<T>>, duty: &[u8]) { |
| 369 | use crate::pac::timer::vals::Ccds; | 370 | use crate::pac::timer::vals::Ccds; |
| 370 | 371 | ||
| 371 | into_ref!(dma); | 372 | into_ref!(dma); |
| @@ -406,14 +407,34 @@ macro_rules! impl_waveform_chx { | |||
| 406 | ..Default::default() | 407 | ..Default::default() |
| 407 | }; | 408 | }; |
| 408 | 409 | ||
| 409 | Transfer::new_write( | 410 | match self.inner.get_bits() { |
| 410 | &mut dma, | 411 | TimerBits::Bits16 => { |
| 411 | req, | 412 | // the data must be aligned to double words |
| 412 | duty, | 413 | assert!(duty.len() % 2 == 0); |
| 413 | self.inner.regs_gp16().ccr(cc_channel.index()).as_ptr() as *mut _, | 414 | let duty = core::slice::from_raw_parts(duty.as_ptr() as *const u16, duty.len() / 2); |
| 414 | dma_transfer_option, | 415 | Transfer::new_write( |
| 415 | ) | 416 | &mut dma, |
| 416 | .await | 417 | req, |
| 418 | duty, | ||
| 419 | self.inner.regs_gp16().ccr(cc_channel.index()).as_ptr() as *mut _, | ||
| 420 | dma_transfer_option, | ||
| 421 | ) | ||
| 422 | .await | ||
| 423 | } | ||
| 424 | TimerBits::Bits32 => { | ||
| 425 | // the data must be aligned to quad words | ||
| 426 | assert!(duty.len() % 4 == 0); | ||
| 427 | let duty = core::slice::from_raw_parts(duty.as_ptr() as *const u32, duty.len() / 4); | ||
| 428 | Transfer::new_write( | ||
| 429 | &mut dma, | ||
| 430 | req, | ||
| 431 | duty, | ||
| 432 | self.inner.regs_gp16().ccr(cc_channel.index()).as_ptr() as *mut _, | ||
| 433 | dma_transfer_option, | ||
| 434 | ) | ||
| 435 | .await | ||
| 436 | } | ||
| 437 | }; | ||
| 417 | }; | 438 | }; |
| 418 | 439 | ||
| 419 | // restore output compare state | 440 | // restore output compare state |
