1
1
//! 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
+ //! ```
2
29
3
30
use cortex_m:: peripheral:: DWT ;
4
31
@@ -9,14 +36,64 @@ use crate::rcc::Clocks;
9
36
pub struct Bps ( pub u32 ) ;
10
37
11
38
/// 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
+ /// ```
12
54
#[ derive( Clone , Copy , PartialEq , Debug ) ]
13
55
pub struct Hertz ( pub u32 ) ;
14
56
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
+ /// ```
16
76
#[ derive( Clone , Copy , PartialEq , Debug ) ]
17
77
pub struct KiloHertz ( pub u32 ) ;
18
78
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
+ /// ```
20
97
#[ derive( Clone , Copy , PartialEq , Debug ) ]
21
98
pub struct MegaHertz ( pub u32 ) ;
22
99
0 commit comments