11//! Time units
2+ //!
3+ //! See [`Hertz`], [`KiloHertz`] and [`MegaHertz`] for creating increasingly higher frequencies.
4+ //!
5+ //! The [`U32Ext`] trait adds various methods like `.hz()`, `.mhz()`, etc to the `u32` primitive type,
6+ //! allowing it to be converted into frequencies.
7+ //!
8+ //! # Examples
9+ //!
10+ //! ## Create a 2 MHz frequency
11+ //!
12+ //! This example demonstrates various ways of creating a 2 MHz (2_000_000 Hz) frequency. They are
13+ //! all equivalent, however the `2.mhz()` variant should be preferred for readability.
14+ //!
15+ //! ```rust
16+ //! use stm32f1xx_hal::{
17+ //! time::Hertz,
18+ //! // Imports U32Ext trait
19+ //! prelude::*,
20+ //! };
21+ //!
22+ //! let freq_hz = 2_000_000.hz();
23+ //! let freq_khz = 2_000.khz();
24+ //! let freq_mhz = 2.mhz();
25+ //!
26+ //! assert_eq!(freq_hz, freq_khz);
27+ //! assert_eq!(freq_khz, freq_mhz);
28+ //! ```
229
330use cortex_m:: peripheral:: DWT ;
431
@@ -9,14 +36,64 @@ use crate::rcc::Clocks;
936pub struct Bps ( pub u32 ) ;
1037
1138/// Hertz
39+ ///
40+ /// Create a frequency specified in [Hertz](https://en.wikipedia.org/wiki/Hertz).
41+ ///
42+ /// See also [`KiloHertz`] and [`MegaHertz`] for semantically correct ways of creating higher
43+ /// frequencies.
44+ ///
45+ /// # Examples
46+ ///
47+ /// ## Create an 60 Hz frequency
48+ ///
49+ /// ```rust
50+ /// use stm32f1xx_hal::time::Hertz;
51+ ///
52+ /// let freq = 60.hz();
53+ /// ```
1254#[ derive( Clone , Copy , PartialEq , Debug ) ]
1355pub struct Hertz ( pub u32 ) ;
1456
15- /// KiloHertz
57+ /// Kilohertz
58+ ///
59+ /// Create a frequency specified in kilohertz.
60+ ///
61+ /// See also [`Hertz`] and [`MegaHertz`] for semantically correct ways of creating lower or higher
62+ /// frequencies.
63+ ///
64+ /// # Examples
65+ ///
66+ /// ## Create a 100 Khz frequency
67+ ///
68+ /// This example creates a 100 KHz frequency. This could be used to set an I2C data rate or PWM
69+ /// frequency, etc.
70+ ///
71+ /// ```rust
72+ /// use stm32f1xx_hal::time::Hertz;
73+ ///
74+ /// let freq = 100.khz();
75+ /// ```
1676#[ derive( Clone , Copy , PartialEq , Debug ) ]
1777pub struct KiloHertz ( pub u32 ) ;
1878
19- /// MegaHertz
79+ /// Megahertz
80+ ///
81+ /// Create a frequency specified in kilohertz.
82+ ///
83+ /// See also [`Hertz`] and [`KiloHertz`] for semantically correct ways of creating lower
84+ /// frequencies.
85+ ///
86+ /// # Examples
87+ ///
88+ /// ## Create a an 8 MHz frequency
89+ ///
90+ /// This example creates an 8 MHz frequency that could be used to configure an SPI peripheral, etc.
91+ ///
92+ /// ```rust
93+ /// use stm32f1xx_hal::time::Hertz;
94+ ///
95+ /// let freq = 8.mhz();
96+ /// ```
2097#[ derive( Clone , Copy , PartialEq , Debug ) ]
2198pub struct MegaHertz ( pub u32 ) ;
2299
0 commit comments