1
+ //! Provides initialization and time-keeping functions for the system clock (`systick`).
2
+
1
3
use core:: convert:: TryFrom ;
2
4
use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
3
5
use stm32f7:: stm32f7x6:: { RCC , SYST } ;
@@ -6,28 +8,49 @@ static TICKS: AtomicUsize = AtomicUsize::new(0);
6
8
static SYSTEM_CLOCK_SPEED : AtomicUsize = AtomicUsize :: new ( 0 ) ;
7
9
static FREQUENCY : AtomicUsize = AtomicUsize :: new ( 0 ) ;
8
10
11
+ /// Increases the global tick count by 1.
9
12
pub fn tick ( ) {
10
13
TICKS . fetch_add ( 1 , Ordering :: AcqRel ) ;
11
14
}
12
15
16
+ /// Returns the current global tick count.
13
17
pub fn ticks ( ) -> usize {
14
18
TICKS . load ( Ordering :: Acquire )
15
19
}
16
20
21
+ /// Returns the elapsed milliseconds since [`tick()`] was first called.
22
+ ///
23
+ /// [`tick()`]: self::tick
17
24
pub fn ms ( ) -> usize {
18
25
ticks_to_ms ( ticks ( ) )
19
26
}
20
27
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.
21
32
pub fn wait_ticks ( ticks : usize ) {
22
33
let current = self :: ticks ( ) ;
23
34
let desired = current + ticks;
24
35
while self :: ticks ( ) != desired { }
25
36
}
26
37
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
27
45
pub fn wait_ms ( ms : usize ) {
28
46
wait_ticks ( ms_to_ticks ( ms) ) ;
29
47
}
30
48
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.
31
54
pub fn init ( Hz ( frequency) : Hz , systick : & mut SYST , rcc : & RCC ) {
32
55
use cortex_m:: peripheral:: syst:: SystClkSource ;
33
56
use stm32f7:: stm32f7x6:: rcc:: pllcfgr:: PLLPR ;
@@ -62,19 +85,32 @@ pub fn init(Hz(frequency): Hz, systick: &mut SYST, rcc: &RCC) {
62
85
systick. enable_counter ( ) ;
63
86
}
64
87
88
+ /// Returns the frequency of the system clock.
89
+ ///
90
+ /// This is the frequency that was passed to [`init`].
91
+ ///
92
+ /// [`init`]: self::init
65
93
pub fn system_clock_speed ( ) -> Hz {
66
94
Hz ( SYSTEM_CLOCK_SPEED . load ( Ordering :: Acquire ) )
67
95
}
68
96
69
97
#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord ) ]
70
98
#[ repr( transparent) ]
99
+ /// A frequency in Hz.
71
100
pub struct Hz ( pub usize ) ;
72
101
102
+ /// Translates the passed tick number to a number of milliseconds.
103
+ ///
104
+ /// Depends on the [`system_clock_speed`](self::system_clock_speed).
73
105
pub fn ticks_to_ms ( ticks : usize ) -> usize {
74
106
let frequency = FREQUENCY . load ( Ordering :: Acquire ) ;
75
107
( ticks * 1000 ) / frequency
76
108
}
77
109
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).
78
114
pub fn ms_to_ticks ( ms : usize ) -> usize {
79
115
let frequency = FREQUENCY . load ( Ordering :: Acquire ) ;
80
116
let ticks_x1000 = frequency * ms;
0 commit comments