Skip to content

Commit 9701382

Browse files
committed
timer: use u32 for high-level api
1 parent a1236b1 commit 9701382

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

embassy-stm32/src/timer/complementary_pwm.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,20 +177,20 @@ impl<'d, T: AdvancedInstance4Channel> ComplementaryPwm<'d, T> {
177177
/// Get max duty value.
178178
///
179179
/// This value depends on the configured frequency and the timer's clock rate from RCC.
180-
pub fn get_max_duty(&self) -> u16 {
180+
pub fn get_max_duty(&self) -> u32 {
181181
if self.inner.get_counting_mode().is_center_aligned() {
182-
unwrap!(self.inner.get_max_compare_value().try_into())
182+
self.inner.get_max_compare_value().into()
183183
} else {
184-
unwrap!(self.inner.get_max_compare_value().try_into()) + 1
184+
self.inner.get_max_compare_value().into() + 1
185185
}
186186
}
187187

188188
/// Set the duty for a given channel.
189189
///
190190
/// The value ranges from 0 for 0% duty, to [`get_max_duty`](Self::get_max_duty) for 100% duty, both included.
191-
pub fn set_duty(&mut self, channel: Channel, duty: u16) {
191+
pub fn set_duty(&mut self, channel: Channel, duty: u32) {
192192
assert!(duty <= self.get_max_duty());
193-
self.inner.set_compare_value(channel, duty.into())
193+
self.inner.set_compare_value(channel, unwrap!(duty.try_into()))
194194
}
195195

196196
/// Set the output polarity for a given channel.
@@ -318,7 +318,7 @@ impl<'d, T: AdvancedInstance4Channel> embedded_hal_02::Pwm for ComplementaryPwm<
318318
}
319319

320320
fn set_duty(&mut self, channel: Self::Channel, duty: Self::Duty) {
321-
assert!(duty <= self.get_max_duty());
321+
assert!(duty <= unwrap!(self.get_max_duty().try_into()));
322322
self.inner.set_compare_value(channel, unwrap!(duty.try_into()))
323323
}
324324

embassy-stm32/src/timer/simple_pwm.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,16 @@ impl<'d, T: GeneralInstance4Channel> SimplePwmChannel<'d, T> {
9999
/// Get max duty value.
100100
///
101101
/// This value depends on the configured frequency and the timer's clock rate from RCC.
102-
pub fn max_duty_cycle(&self) -> u16 {
103-
let max: u32 = self.timer.get_max_compare_value().into();
104-
assert!(max < u16::MAX as u32);
105-
max as u16 + 1
102+
pub fn max_duty_cycle(&self) -> u32 {
103+
self.timer.get_max_compare_value().into() + 1
106104
}
107105

108106
/// Set the duty for a given channel.
109107
///
110108
/// The value ranges from 0 for 0% duty, to [`max_duty_cycle`](Self::max_duty_cycle) for 100% duty, both included.
111-
pub fn set_duty_cycle(&mut self, duty: u16) {
109+
pub fn set_duty_cycle(&mut self, duty: u32) {
112110
assert!(duty <= (*self).max_duty_cycle());
113-
self.timer.set_compare_value(self.channel, duty.into())
111+
self.timer.set_compare_value(self.channel, unwrap!(duty.try_into()))
114112
}
115113

116114
/// Set the duty cycle to 0%, or always inactive.
@@ -127,21 +125,21 @@ impl<'d, T: GeneralInstance4Channel> SimplePwmChannel<'d, T> {
127125
///
128126
/// The caller is responsible for ensuring that `num` is less than or equal to `denom`,
129127
/// and that `denom` is not zero.
130-
pub fn set_duty_cycle_fraction(&mut self, num: u16, denom: u16) {
128+
pub fn set_duty_cycle_fraction(&mut self, num: u32, denom: u32) {
131129
assert!(denom != 0);
132130
assert!(num <= denom);
133131
let duty = u32::from(num) * u32::from(self.max_duty_cycle()) / u32::from(denom);
134132

135133
// This is safe because we know that `num <= denom`, so `duty <= self.max_duty_cycle()` (u16)
136134
#[allow(clippy::cast_possible_truncation)]
137-
self.set_duty_cycle(duty as u16);
135+
self.set_duty_cycle(unwrap!(duty.try_into()));
138136
}
139137

140138
/// Set the duty cycle to `percent / 100`
141139
///
142140
/// The caller is responsible for ensuring that `percent` is less than or equal to 100.
143141
pub fn set_duty_cycle_percent(&mut self, percent: u8) {
144-
self.set_duty_cycle_fraction(u16::from(percent), 100)
142+
self.set_duty_cycle_fraction(percent as u32, 100)
145143
}
146144

147145
/// Get the duty for a given channel.
@@ -334,10 +332,8 @@ impl<'d, T: GeneralInstance4Channel> SimplePwm<'d, T> {
334332
/// Get max duty value.
335333
///
336334
/// This value depends on the configured frequency and the timer's clock rate from RCC.
337-
pub fn max_duty_cycle(&self) -> u16 {
338-
let max: u32 = self.inner.get_max_compare_value().into();
339-
assert!(max < u16::MAX as u32);
340-
max as u16 + 1
335+
pub fn max_duty_cycle(&self) -> u32 {
336+
self.inner.get_max_compare_value().into() + 1
341337
}
342338

343339
/// Generate a sequence of PWM waveform
@@ -417,11 +413,11 @@ impl<'d, T: GeneralInstance4Channel> embedded_hal_1::pwm::ErrorType for SimplePw
417413

418414
impl<'d, T: GeneralInstance4Channel> embedded_hal_1::pwm::SetDutyCycle for SimplePwmChannel<'d, T> {
419415
fn max_duty_cycle(&self) -> u16 {
420-
self.max_duty_cycle()
416+
unwrap!(self.max_duty_cycle().try_into())
421417
}
422418

423419
fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> {
424-
self.set_duty_cycle(duty);
420+
self.set_duty_cycle(duty.into());
425421
Ok(())
426422
}
427423

@@ -436,7 +432,7 @@ impl<'d, T: GeneralInstance4Channel> embedded_hal_1::pwm::SetDutyCycle for Simpl
436432
}
437433

438434
fn set_duty_cycle_fraction(&mut self, num: u16, denom: u16) -> Result<(), Self::Error> {
439-
self.set_duty_cycle_fraction(num, denom);
435+
self.set_duty_cycle_fraction(num.into(), denom.into());
440436
Ok(())
441437
}
442438

examples/stm32f4/src/bin/pwm_complementary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async fn main(_spawner: Spawner) {
3333
);
3434

3535
let max = pwm.get_max_duty();
36-
pwm.set_dead_time(max / 1024);
36+
pwm.set_dead_time((max / 1024) as u16);
3737

3838
pwm.enable(Channel::Ch1);
3939

examples/stm32f4/src/bin/ws2812_pwm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async fn main(_spawner: Spawner) {
6161
// construct ws2812 non-return-to-zero (NRZ) code bit by bit
6262
// ws2812 only need 24 bits for each LED, but we add one bit more to keep PWM output low
6363

64-
let max_duty = ws2812_pwm.max_duty_cycle();
64+
let max_duty = ws2812_pwm.max_duty_cycle() as u16;
6565
let n0 = 8 * max_duty / 25; // ws2812 Bit 0 high level timing
6666
let n1 = 2 * n0; // ws2812 Bit 1 high level timing
6767

0 commit comments

Comments
 (0)