Skip to content

Commit c1fa57b

Browse files
committed
add constructor aliases
1 parent 39574dd commit c1fa57b

File tree

6 files changed

+37
-22
lines changed

6 files changed

+37
-22
lines changed

examples/pwm.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use stm32f1xx_hal::{
1313
pac,
1414
prelude::*,
1515
pwm::Channel,
16-
time::MilliSeconds,
16+
time::ms,
1717
timer::{Tim2NoRemap, Timer},
1818
};
1919

@@ -62,8 +62,7 @@ fn main() -> ! {
6262
//// Operations affecting all defined channels on the Timer
6363

6464
// Adjust period to 0.5 seconds
65-
let m500: MilliSeconds = 500.millis();
66-
pwm.set_period(m500.into_rate());
65+
pwm.set_period(ms(500).into_rate());
6766

6867
asm::bkpt();
6968

src/adc.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use crate::gpio::gpiof;
1111
use crate::gpio::Analog;
1212
use crate::gpio::{gpioa, gpiob, gpioc};
1313
use crate::rcc::{Clocks, Enable, Reset};
14+
use crate::time::kHz;
1415
use core::sync::atomic::{self, Ordering};
1516
use cortex_m::asm::delay;
1617
use embedded_dma::StaticWriteBuffer;
17-
use fugit::{HertzU32 as Hertz, RateExtU32};
1818

1919
#[cfg(any(feature = "stm32f103", feature = "connectivity"))]
2020
use crate::pac::ADC2;
@@ -206,11 +206,9 @@ macro_rules! adc_hal {
206206
// The manual states that we need to wait two ADC clocks cycles after power-up
207207
// before starting calibration, we already delayed in the power-up process, but
208208
// if the adc clock is too low that was not enough.
209-
let m2_5: Hertz = 2500.kHz();
210-
if s.clocks.adcclk() < m2_5 {
209+
if s.clocks.adcclk() < kHz(2500) {
211210
let two_adc_cycles = s.clocks.sysclk() / s.clocks.adcclk() * 2;
212-
let k800: Hertz = 800.kHz();
213-
let already_delayed = s.clocks.sysclk() / k800;
211+
let already_delayed = s.clocks.sysclk() / kHz(800);
214212
if two_adc_cycles > already_delayed {
215213
delay(two_adc_cycles - already_delayed);
216214
}
@@ -272,8 +270,7 @@ macro_rules! adc_hal {
272270
// this time can be found in the datasheets.
273271
// Here we are delaying for approximately 1us, considering 1.25 instructions per
274272
// cycle. Do we support a chip which needs more than 1us ?
275-
let k800: Hertz = 800.kHz();
276-
delay(self.clocks.sysclk() / k800);
273+
delay(self.clocks.sysclk() / kHz(800));
277274
}
278275

279276
fn power_down(&mut self) {

src/i2c.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use crate::gpio::{Alternate, OpenDrain};
1010
use crate::hal::blocking::i2c::{Read, Write, WriteRead};
1111
use crate::pac::{DWT, I2C1, I2C2, RCC};
1212
use crate::rcc::{BusClock, Clocks, Enable, Reset};
13+
use crate::time::{kHz, Hertz};
1314
use core::ops::Deref;
14-
use fugit::{HertzU32 as Hertz, RateExtU32};
1515
use nb::Error::{Other, WouldBlock};
1616
use nb::{Error as NbError, Result as NbResult};
1717

@@ -74,8 +74,7 @@ impl Mode {
7474

7575
impl From<Hertz> for Mode {
7676
fn from(frequency: Hertz) -> Self {
77-
let k100: Hertz = 100.kHz();
78-
if frequency <= k100 {
77+
if frequency <= kHz(100) {
7978
Self::Standard { frequency }
8079
} else {
8180
Self::Fast {
@@ -159,8 +158,7 @@ where
159158

160159
let pclk1 = I2C::clock(&clocks);
161160

162-
let k400: Hertz = 400.kHz();
163-
assert!(mode.get_frequency() <= k400);
161+
assert!(mode.get_frequency() <= kHz(400));
164162

165163
let mut i2c = I2c {
166164
i2c,

src/rcc.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::pac::{rcc, PWR, RCC};
44

55
use crate::flash::ACR;
6+
use crate::time::MHz;
67
use fugit::{HertzU32 as Hertz, RateExtU32};
78

89
use crate::backup_domain::BackupDomain;
@@ -190,12 +191,10 @@ impl CFGR {
190191
// adjust flash wait states
191192
#[cfg(any(feature = "stm32f103", feature = "connectivity"))]
192193
unsafe {
193-
let m24: Hertz = 24.MHz();
194-
let m48: Hertz = 48.MHz();
195194
acr.acr().write(|w| {
196-
w.latency().bits(if clocks.sysclk <= m24 {
195+
w.latency().bits(if clocks.sysclk <= MHz(24) {
197196
0b000
198-
} else if clocks.sysclk <= m48 {
197+
} else if clocks.sysclk <= MHz(48) {
199198
0b001
200199
} else {
201200
0b010

src/rtc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
use crate::pac::{RCC, RTC};
55

66
use crate::backup_domain::BackupDomain;
7-
use crate::time::Hertz;
7+
use crate::time::{Hertz, Hz};
88

99
use core::convert::Infallible;
1010
use core::marker::PhantomData;
1111

1212
// The LSE runs at at 32 768 hertz unless an external clock is provided
13-
const LSE_HERTZ: Hertz = Hertz::from_raw(32_768);
14-
const LSI_HERTZ: Hertz = Hertz::from_raw(40_000);
13+
const LSE_HERTZ: Hertz = Hz(32_768);
14+
const LSI_HERTZ: Hertz = Hz(40_000);
1515

1616
/// RTC clock source HSE clock divided by 128 (type state)
1717
pub struct RtcClkHseDiv128;

src/time.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
//! assert_eq!(freq_khz, freq_mhz);
2828
//! ```
2929
30+
#![allow(non_snake_case)]
31+
3032
use core::ops;
3133
use cortex_m::peripheral::{DCB, DWT};
3234

@@ -53,6 +55,26 @@ impl U32Ext for u32 {
5355
}
5456
}
5557

58+
pub const fn Hz(val: u32) -> Hertz {
59+
Hertz::from_raw(val)
60+
}
61+
62+
pub const fn kHz(val: u32) -> KiloHertz {
63+
KiloHertz::from_raw(val)
64+
}
65+
66+
pub const fn MHz(val: u32) -> MegaHertz {
67+
MegaHertz::from_raw(val)
68+
}
69+
70+
pub const fn ms(val: u32) -> MilliSeconds {
71+
MilliSeconds::from_ticks(val)
72+
}
73+
74+
pub const fn us(val: u32) -> MicroSeconds {
75+
MicroSeconds::from_ticks(val)
76+
}
77+
5678
/// Macro to implement arithmetic operations (e.g. multiplication, division)
5779
/// for wrapper types.
5880
macro_rules! impl_arithmetic {

0 commit comments

Comments
 (0)