@@ -6,21 +6,27 @@ use embedded_hal::blocking::delay::{DelayMs, DelayUs};
66/// System timer (SysTick) as a delay provider.
77pub struct Delay {
88 syst : SYST ,
9- ahb_frequency : u32 ,
9+ frequency : u32 ,
1010}
1111
1212impl Delay {
1313 /// Configures the system timer (SysTick) as a delay provider.
1414 ///
1515 /// `ahb_frequency` is a frequency of the AHB bus in Hz.
1616 #[ 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+ }
1920
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 }
2430 }
2531
2632 /// Releases the system timer (SysTick) resource.
@@ -32,7 +38,7 @@ impl Delay {
3238 /// Delay using the Cortex-M systick for a certain duration, in µs.
3339 #[ allow( clippy:: missing_inline_in_public_items) ]
3440 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 ;
3642
3743 let full_cycles = ticks >> 24 ;
3844 if full_cycles > 0 {
0 commit comments