Skip to content

Commit dd58876

Browse files
committed
Document the system_clock module
1 parent 2751b26 commit dd58876

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/system_clock.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Provides initialization and time-keeping functions for the system clock (`systick`).
2+
13
use core::convert::TryFrom;
24
use core::sync::atomic::{AtomicUsize, Ordering};
35
use stm32f7::stm32f7x6::{RCC, SYST};
@@ -6,28 +8,49 @@ static TICKS: AtomicUsize = AtomicUsize::new(0);
68
static SYSTEM_CLOCK_SPEED: AtomicUsize = AtomicUsize::new(0);
79
static FREQUENCY: AtomicUsize = AtomicUsize::new(0);
810

11+
/// Increases the global tick count by 1.
912
pub fn tick() {
1013
TICKS.fetch_add(1, Ordering::AcqRel);
1114
}
1215

16+
/// Returns the current global tick count.
1317
pub fn ticks() -> usize {
1418
TICKS.load(Ordering::Acquire)
1519
}
1620

21+
/// Returns the elapsed milliseconds since [`tick()`] was first called.
22+
///
23+
/// [`tick()`]: self::tick
1724
pub fn ms() -> usize {
1825
ticks_to_ms(ticks())
1926
}
2027

28+
/// Wait for the specified number of ticks.
29+
///
30+
/// This function spins the thread in a while loop until the [`tick()`] function was invoked
31+
/// `ticks` times.
2132
pub fn wait_ticks(ticks: usize) {
2233
let current = self::ticks();
2334
let desired = current + ticks;
2435
while self::ticks() != desired {}
2536
}
2637

38+
/// Wait for the specified number of milliseconds.
39+
///
40+
/// This function spins the thread in a while loop until the specified number of milliseconds
41+
/// have passed. This function is based on [`wait_ticks`] and [`ms_to_ticks`].
42+
///
43+
/// [`wait_ticks`]: self::wait_ticks
44+
/// [`ms_to_ticks`]: self::ms_to_ticks
2745
pub fn wait_ms(ms: usize) {
2846
wait_ticks(ms_to_ticks(ms));
2947
}
3048

49+
/// Initializes the system clock (systick) of the stm32f7-discovery board to the specified
50+
/// frequency.
51+
///
52+
/// After calling this function, the interrupt handler for the systick interrupt should call
53+
/// [`tick()`] on each invocation to update the global tick counter in this module.
3154
pub fn init(Hz(frequency): Hz, systick: &mut SYST, rcc: &RCC) {
3255
use cortex_m::peripheral::syst::SystClkSource;
3356
use stm32f7::stm32f7x6::rcc::pllcfgr::PLLPR;
@@ -62,19 +85,32 @@ pub fn init(Hz(frequency): Hz, systick: &mut SYST, rcc: &RCC) {
6285
systick.enable_counter();
6386
}
6487

88+
/// Returns the frequency of the system clock.
89+
///
90+
/// This is the frequency that was passed to [`init`].
91+
///
92+
/// [`init`]: self::init
6593
pub fn system_clock_speed() -> Hz {
6694
Hz(SYSTEM_CLOCK_SPEED.load(Ordering::Acquire))
6795
}
6896

6997
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
7098
#[repr(transparent)]
99+
/// A frequency in Hz.
71100
pub struct Hz(pub usize);
72101

102+
/// Translates the passed tick number to a number of milliseconds.
103+
///
104+
/// Depends on the [`system_clock_speed`](self::system_clock_speed).
73105
pub fn ticks_to_ms(ticks: usize) -> usize {
74106
let frequency = FREQUENCY.load(Ordering::Acquire);
75107
(ticks * 1000) / frequency
76108
}
77109

110+
111+
/// Translates the passed number of milliseconds to a number of ticks.
112+
///
113+
/// Depends on the [`system_clock_speed`](self::system_clock_speed).
78114
pub fn ms_to_ticks(ms: usize) -> usize {
79115
let frequency = FREQUENCY.load(Ordering::Acquire);
80116
let ticks_x1000 = frequency * ms;

0 commit comments

Comments
 (0)