Skip to content

Commit 7d5ee6a

Browse files
committed
idle state
1 parent c557277 commit 7d5ee6a

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

src/timer.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ pub enum Polarity {
6464
ActiveLow,
6565
}
6666

67+
/// Output Idle state
68+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
69+
pub enum IdleState {
70+
Reset,
71+
Set,
72+
}
73+
6774
/// Interrupt events
6875
#[derive(Clone, Copy, PartialEq, Eq)]
6976
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@@ -239,7 +246,7 @@ pub type CCR4<T> = CCR<T, 3>;
239246
pub struct DMAR<T>(T);
240247

241248
mod sealed {
242-
use super::{Channel, Event, Ocm, Polarity};
249+
use super::{Channel, Event, Ocm, Polarity, IdleState};
243250
pub trait General {
244251
type Width: Into<u32> + From<u16>;
245252
fn max_auto_reload() -> u32;
@@ -266,6 +273,7 @@ mod sealed {
266273

267274
pub trait WithPwmCommon: General {
268275
const CH_NUMBER: u8;
276+
const COMP_CH_NUMBER: u8;
269277
fn read_cc_value(channel: u8) -> u32;
270278
fn set_cc_value(channel: u8, value: u32);
271279
fn enable_channel(channel: u8, b: bool);
@@ -277,6 +285,7 @@ mod sealed {
277285
fn enable_nchannel(channel: u8, b: bool);
278286
fn set_dtg_value(value: u8);
279287
fn read_dtg_value() -> u8;
288+
fn idle_state(channel: u8, comp: bool, s: IdleState);
280289
}
281290

282291
pub trait WithPwm: WithPwmCommon {
@@ -411,6 +420,7 @@ macro_rules! hal {
411420
$(
412421
impl WithPwmCommon for $TIM {
413422
const CH_NUMBER: u8 = $cnum;
423+
const COMP_CH_NUMBER: u8 = $cnum;
414424

415425
#[inline(always)]
416426
fn read_cc_value(c: u8) -> u32 {
@@ -450,7 +460,7 @@ macro_rules! hal {
450460
#[inline(always)]
451461
fn set_nchannel_polarity(c: u8, p: Polarity) {
452462
let tim = unsafe { &*<$TIM>::ptr() };
453-
if c < Self::CH_NUMBER {
463+
if c < Self::COMP_CH_NUMBER {
454464
unsafe { bb::write(&tim.ccer, c*4 + 3, p == Polarity::ActiveLow); }
455465
}
456466
}
@@ -461,7 +471,7 @@ macro_rules! hal {
461471
fn enable_nchannel(c: u8, b: bool) {
462472
let $aoe = ();
463473
let tim = unsafe { &*<$TIM>::ptr() };
464-
if c < Self::CH_NUMBER {
474+
if c < Self::COMP_CH_NUMBER {
465475
unsafe { bb::write(&tim.ccer, c*4 + 2, b); }
466476
}
467477
}
@@ -473,6 +483,15 @@ macro_rules! hal {
473483
let tim = unsafe { &*<$TIM>::ptr() };
474484
tim.bdtr.read().dtg().bits()
475485
}
486+
fn idle_state(c: u8, comp: bool, s: IdleState) {
487+
let tim = unsafe { &*<$TIM>::ptr() };
488+
if !comp && (c < Self::CH_NUMBER) {
489+
unsafe { bb::write(&tim.cr2, c*2 + 8, s == IdleState::Set); }
490+
}
491+
if comp && (c < Self::COMP_CH_NUMBER) {
492+
unsafe { bb::write(&tim.cr2, c*2 + 9, s == IdleState::Set); }
493+
}
494+
}
476495
}
477496
)?
478497

src/timer/pwm.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
//! and change their polarity with `set_polarity` and `set_complementary_polarity`.
3535
3636
use super::{
37-
compute_arr_presc, Advanced, Channel, FTimer, Instance, Ocm, Polarity, Timer, WithPwm,
37+
compute_arr_presc, Advanced, Channel, FTimer, Instance, Ocm, Polarity, Timer, WithPwm, IdleState,
3838
};
3939
use crate::rcc::Clocks;
4040
use core::marker::PhantomData;
@@ -263,6 +263,16 @@ impl<TIM: Instance + WithPwm + Advanced, const C: u8> PwmChannel<TIM, C, true> {
263263
pub fn enable_complementary(&mut self) {
264264
TIM::enable_nchannel(C, true);
265265
}
266+
267+
#[inline]
268+
pub fn set_idle_state(&mut self, s: IdleState) {
269+
TIM::idle_state(C, false, s);
270+
}
271+
272+
#[inline]
273+
pub fn set_complementary_idle_state(&mut self, s: IdleState) {
274+
TIM::idle_state(C, true, s);
275+
}
266276
}
267277

268278
pub struct PwmHz<TIM, P, PINS>
@@ -412,22 +422,36 @@ where
412422
TIM: Instance + WithPwm + Advanced,
413423
PINS: Pins<TIM, P>,
414424
{
425+
#[inline]
415426
pub fn enable_complementary(&mut self, channel: Channel) {
416427
TIM::enable_nchannel(PINS::check_complementary_used(channel) as u8, true)
417428
}
418429

430+
#[inline]
419431
pub fn disable_complementary(&mut self, channel: Channel) {
420432
TIM::enable_nchannel(PINS::check_complementary_used(channel) as u8, false)
421433
}
422434

435+
#[inline]
423436
pub fn set_dead_time(&mut self, dts_ticks: u16) {
424437
let bits = pack_ceil_dead_time(dts_ticks);
425438
TIM::set_dtg_value(bits);
426439
}
427440

441+
#[inline]
428442
pub fn get_dead_time(&self) -> u16 {
429443
unpack_dead_time(TIM::read_dtg_value())
430444
}
445+
446+
#[inline]
447+
pub fn set_idle_state(&mut self, channel: Channel, s: IdleState) {
448+
TIM::idle_state(PINS::check_used(channel) as u8, false, s);
449+
}
450+
451+
#[inline]
452+
pub fn set_complementary_idle_state(&mut self, channel: Channel, s: IdleState) {
453+
TIM::idle_state(PINS::check_complementary_used(channel) as u8, true, s);
454+
}
431455
}
432456

433457
pub struct Pwm<TIM, P, PINS, const FREQ: u32>
@@ -578,22 +602,36 @@ where
578602
TIM: Instance + WithPwm + Advanced,
579603
PINS: Pins<TIM, P>,
580604
{
605+
#[inline]
581606
pub fn enable_complementary(&mut self, channel: Channel) {
582607
TIM::enable_nchannel(PINS::check_complementary_used(channel) as u8, true)
583608
}
584609

610+
#[inline]
585611
pub fn disable_complementary(&mut self, channel: Channel) {
586612
TIM::enable_nchannel(PINS::check_complementary_used(channel) as u8, false)
587613
}
588614

615+
#[inline]
589616
pub fn set_dead_time(&mut self, dts_ticks: u16) {
590617
let bits = pack_ceil_dead_time(dts_ticks);
591618
TIM::set_dtg_value(bits);
592619
}
593620

621+
#[inline]
594622
pub fn get_dead_time(&self) -> u16 {
595623
unpack_dead_time(TIM::read_dtg_value())
596624
}
625+
626+
#[inline]
627+
pub fn set_idle_state(&mut self, channel: Channel, s: IdleState) {
628+
TIM::idle_state(PINS::check_used(channel) as u8, false, s);
629+
}
630+
631+
#[inline]
632+
pub fn set_complementary_idle_state(&mut self, channel: Channel, s: IdleState) {
633+
TIM::idle_state(PINS::check_complementary_used(channel) as u8, true, s);
634+
}
597635
}
598636

599637
const fn pack_ceil_dead_time(dts_ticks: u16) -> u8 {

0 commit comments

Comments
 (0)