Skip to content
This repository was archived by the owner on Aug 9, 2022. It is now read-only.

Commit b1a2ea1

Browse files
committed
sleep function support 64 bit NanoSeconds
Allows ~ Ininite delays instead of ~ 4.3s delays
1 parent ddf5a5d commit b1a2ea1

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

src/clock_control/mod.rs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,12 @@ pub struct ClockControl {
249249
dfs: dfs::DFS,
250250
}
251251

252-
/// Function only available once clock if frozen
253-
pub fn sleep<T: Into<NanoSeconds>>(time: T) {
252+
/// Sleep by spinning
253+
///
254+
/// *Note: Function only available once clock if frozen.embedded_hal*
255+
///
256+
/// *Note: Maximum duration is 2e32-1 ns ~ 4.29s *
257+
pub fn sleep<T: Into<NanoSecondsU64>>(time: T) {
254258
unsafe { CLOCK_CONTROL.as_ref().unwrap().delay(time) };
255259
}
256260

@@ -440,7 +444,7 @@ impl ClockControl {
440444
};
441445

442446
let estimated_time = (Hertz(1_000_000) * (slow_cycles as u32) / slow_freq).us();
443-
let estimated_cycle_count = 2 * self.time_to_cpu_cycles(estimated_time);
447+
let estimated_cycle_count = 2 * (self.time_to_cpu_cycles(estimated_time) as u32);
444448

445449
let max_cycle_count = 0x01FFFFFF; // bit 7:31 = 25 bits
446450
if estimated_cycle_count > max_cycle_count {
@@ -527,6 +531,7 @@ impl ClockControl {
527531
fn init<T: Into<Hertz> + Copy>(&mut self, xtal_frequency: T) -> Result<&mut Self, Error> {
528532
// if auto is selected check if the frequency has already been stored during
529533
// a previous run in the scratch register
534+
530535
if xtal_frequency.into() == XTAL_FREQUENCY_AUTO {
531536
self.xtal_frequency = match self.xtal_frequency_from_scratch() {
532537
Ok(frequency) => frequency,
@@ -585,14 +590,20 @@ impl ClockControl {
585590
}
586591

587592
/// calculate the number of cpu cycles from a time at the current CPU frequency
588-
fn time_to_cpu_cycles<T: Into<NanoSeconds>>(&self, time: T) -> u32 {
589-
(((self.cpu_frequency / Hertz(1_000_000)) as u64) * (u32::from(time.into()) as u64) / 1000)
590-
as u32
593+
fn time_to_cpu_cycles<T: Into<NanoSecondsU64>>(&self, time: T) -> u64 {
594+
((self.cpu_frequency / Hertz(1_000_000)) as u64) * u64::from(time.into()) / 1000
591595
}
592596

593597
/// delay a certain time by spinning
594-
fn delay<T: Into<NanoSeconds>>(&self, time: T) {
595-
delay(self.time_to_cpu_cycles(time));
598+
fn delay<T: Into<NanoSecondsU64>>(&self, time: T) {
599+
let cycles = self.time_to_cpu_cycles(time);
600+
let lower = cycles as u32;
601+
let upper = cycles >> 32;
602+
603+
delay(lower);
604+
for _ in 0..(upper * 2) {
605+
delay(u32::MAX / 2);
606+
}
596607
}
597608

598609
/// Check if a value from RTC_XTAL_FREQ_REG or RTC_APB_FREQ_REG are valid clocks
@@ -626,17 +637,11 @@ impl ClockControl {
626637
T2: Into<Hertz> + Copy + PartialOrd,
627638
T3: Into<Hertz> + Copy + PartialOrd,
628639
{
629-
match cpu_source_default {
630-
CPUSource::APLL => return Err(Error::UnsupportedFreqConfig),
631-
_ => {}
632-
}
633-
match cpu_source_locked {
634-
CPUSource::APLL => return Err(Error::UnsupportedFreqConfig),
635-
_ => {}
636-
}
637-
match cpu_source_apb_locked {
638-
CPUSource::APLL => return Err(Error::UnsupportedFreqConfig),
639-
_ => {}
640+
if cpu_source_default == CPUSource::APLL
641+
|| cpu_source_locked == CPUSource::APLL
642+
|| cpu_source_apb_locked == CPUSource::APLL
643+
{
644+
return Err(Error::UnsupportedFreqConfig);
640645
}
641646

642647
if cpu_frequency_default.into() < CPU_FREQ_MIN
@@ -653,6 +658,12 @@ impl ClockControl {
653658
return Err(Error::FrequencyTooHigh);
654659
}
655660

661+
if cpu_frequency_default.into() > cpu_frequency_apb_locked.into()
662+
|| cpu_frequency_default.into() > cpu_frequency_locked.into()
663+
{
664+
return Err(Error::UnsupportedFreqConfig);
665+
}
666+
656667
self.cpu_source_default = cpu_source_default;
657668
self.cpu_frequency_default =
658669
self.round_cpu_frequency(cpu_source_default, cpu_frequency_default);
@@ -762,6 +773,10 @@ impl ClockControl {
762773
frequency: T,
763774
keep_pll_enabled: bool,
764775
) -> Result<&mut Self, Error> {
776+
if source == self.cpu_source && frequency.into() == self.cpu_frequency {
777+
return Ok(self);
778+
}
779+
765780
match source {
766781
CPUSource::Xtal => self.set_cpu_frequency_to_xtal(frequency)?,
767782
CPUSource::PLL => self.set_cpu_frequency_to_pll(frequency)?,

0 commit comments

Comments
 (0)