Skip to content

Commit 854526e

Browse files
authored
Add docs for Hertz structs (#186)
* Add docs for Hertz struct * Add docs for KiloHertz, MegaHertz and time mod * Remove RTFM example * Add deny(intra_doc_link_resolution_failure)
1 parent 80161be commit 854526e

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
//! [README]: https://github.com/stm32-rs/stm32f1xx-hal/tree/v0.5.3
6464
6565
#![no_std]
66+
#![deny(intra_doc_link_resolution_failure)]
6667

6768
// If no target specified, print error message.
6869
#[cfg(not(any(feature = "stm32f100", feature = "stm32f101", feature = "stm32f103")))]

src/time.rs

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
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
330
use cortex_m::peripheral::DWT;
431

@@ -9,14 +36,64 @@ use crate::rcc::Clocks;
936
pub 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)]
1355
pub 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)]
1777
pub 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)]
2198
pub struct MegaHertz(pub u32);
2299

0 commit comments

Comments
 (0)