@@ -6,21 +6,27 @@ use embedded_hal::blocking::delay::{DelayMs, DelayUs};
6
6
/// System timer (SysTick) as a delay provider.
7
7
pub struct Delay {
8
8
syst : SYST ,
9
- ahb_frequency : u32 ,
9
+ frequency : u32 ,
10
10
}
11
11
12
12
impl Delay {
13
13
/// Configures the system timer (SysTick) as a delay provider.
14
14
///
15
15
/// `ahb_frequency` is a frequency of the AHB bus in Hz.
16
16
#[ inline]
17
- pub fn new ( mut syst : SYST , ahb_frequency : u32 ) -> Self {
18
- syst. set_clock_source ( SystClkSource :: Core ) ;
17
+ pub fn new ( syst : SYST , ahb_frequency : u32 ) -> Self {
18
+ Self :: with_source ( syst, ahb_frequency, SystClkSource :: Core )
19
+ }
19
20
20
- Delay {
21
- syst,
22
- ahb_frequency,
23
- }
21
+ /// Configures the system timer (SysTick) as a delay provider
22
+ /// with a clock source.
23
+ ///
24
+ /// `frequency` is the frequency of your `clock_source` in Hz.
25
+ #[ inline]
26
+ pub fn with_source ( mut syst : SYST , frequency : u32 , clock_source : SystClkSource ) -> Self {
27
+ syst. set_clock_source ( clock_source) ;
28
+
29
+ Delay { syst, frequency }
24
30
}
25
31
26
32
/// Releases the system timer (SysTick) resource.
@@ -32,7 +38,7 @@ impl Delay {
32
38
/// Delay using the Cortex-M systick for a certain duration, in µs.
33
39
#[ allow( clippy:: missing_inline_in_public_items) ]
34
40
pub fn delay_us ( & mut self , us : u32 ) {
35
- let ticks = ( u64:: from ( us) ) * ( u64:: from ( self . ahb_frequency ) ) / 1_000_000 ;
41
+ let ticks = ( u64:: from ( us) ) * ( u64:: from ( self . frequency ) ) / 1_000_000 ;
36
42
37
43
let full_cycles = ticks >> 24 ;
38
44
if full_cycles > 0 {
0 commit comments