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

Commit 9f2fcd8

Browse files
committed
Updated examples to latest changes in xtensa_lx6
1 parent 306556a commit 9f2fcd8

File tree

6 files changed

+36
-251
lines changed

6 files changed

+36
-251
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ rt = ["esp32/rt", "xtensa-lx6-rt"]
3232
[dependencies]
3333
esp32-hal-proc-macros = { path = "procmacros" }
3434

35-
xtensa-lx6-rt = { version = "0.2.0", optional = true }
36-
xtensa-lx6 = "0.1.0"
37-
esp32 = { version = "0.5.0", default-features = false }
35+
xtensa-lx6-rt = { version = "0.3.0", optional = true }
36+
xtensa-lx6 = "0.2.0"
37+
esp32 = "0.5.0"
3838
bare-metal = "0.2"
3939
nb = "0.1.2"
4040
spin = "0.5.2"

examples/blinky.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ extern crate esp32_hal as hal;
55
extern crate panic_halt;
66
extern crate xtensa_lx6_rt;
77

8+
use esp32_hal::target;
89
use hal::prelude::*;
9-
use hal::target;
10-
use xtensa_lx6::get_cycle_count;
10+
use xtensa_lx6::timer::get_cycle_count;
1111

1212
/// The default clock source is the onboard crystal
1313
/// In most cases 40mhz (but can be as low as 2mhz depending on the board)

examples/multicore.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use esp32_hal::dprintln;
1212
use esp32_hal::serial::{config::Config, NoRx, NoTx, Serial};
1313
use esp32_hal::target;
1414

15-
use xtensa_lx6::{get_cycle_count, get_stack_pointer};
15+
use xtensa_lx6::{get_stack_pointer, timer::get_cycle_count};
1616

1717
const BLINK_HZ: Hertz = Hertz(1);
1818

@@ -105,9 +105,7 @@ fn main() -> ! {
105105
let _lock = clock_control_config.lock_cpu_frequency();
106106

107107
// start core 1 (APP_CPU)
108-
clock_control_config
109-
.start_core(esp32_hal::Core::APP, cpu1_start)
110-
.unwrap();
108+
clock_control_config.start_app_core(cpu1_start).unwrap();
111109

112110
// main loop, which in turn lock and unlocks apb and cpu locks
113111
let mut x: u32 = 0;

examples/rtccntl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ fn main() -> ! {
118118

119119
x = x.wrapping_add(1);
120120

121-
let ccount = xtensa_lx6::get_cycle_count();
121+
let ccount = xtensa_lx6::timer::get_cycle_count();
122122
let ccount_diff = ccount.wrapping_sub(prev_ccount);
123123

124124
writeln!(

examples/timer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ fn main() -> ! {
148148
timer0.get_value(),
149149
timer1.get_value(),
150150
timer2.get_value(),
151-
xtensa_lx6::get_cycle_count()
151+
xtensa_lx6::timer::get_cycle_count()
152152
)
153153
.unwrap();
154154
if let Ok(_) = timer1.wait() {

src/clock_control/mod.rs

Lines changed: 27 additions & 240 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
//! - Implement light sleep
1414
//! - 32kHz Xtal support
1515
//! - Allow 8.5MHz clock to be tuned
16-
//! - Global RTC clock reading
1716
//! - Automatic enabling/disabling of 8MHz source (when not in use for rtc_fast_clk or cpu frequency)
1817
1918
use crate::prelude::*;
@@ -24,8 +23,9 @@ use crate::target::rtccntl::clk_conf::*;
2423
use crate::target::rtccntl::cntl::*;
2524
use crate::target::{APB_CTRL, RTCCNTL, TIMG0};
2625
use core::fmt;
27-
use xtensa_lx6::get_cycle_count;
26+
use xtensa_lx6::timer::{delay, get_cycle_count};
2827

28+
pub mod config;
2929
pub mod cpu;
3030
pub mod dfs;
3131
mod pll;
@@ -199,243 +199,6 @@ static CLOCK_CONTROL_MUTEX: spin::Mutex<()> = spin::Mutex::new(());
199199
#[derive(Copy, Clone)]
200200
pub struct ClockControlConfig {}
201201

202-
impl<'a> ClockControlConfig {
203-
// All the single word reads of frequencies and sources are thread and interrupt safe
204-
// as these are atomic.
205-
206-
/// The current CPU frequency
207-
pub fn cpu_frequency(&self) -> Hertz {
208-
unsafe { CLOCK_CONTROL.as_ref().unwrap().cpu_frequency }
209-
}
210-
211-
/// The current APB frequency
212-
pub fn apb_frequency(&self) -> Hertz {
213-
unsafe { CLOCK_CONTROL.as_ref().unwrap().apb_frequency }
214-
}
215-
216-
/// The CPU frequency in the default state
217-
pub fn cpu_frequency_default(&self) -> Hertz {
218-
unsafe { CLOCK_CONTROL.as_ref().unwrap().cpu_frequency_default }
219-
}
220-
221-
/// The CPU frequency in the CPU lock state
222-
pub fn cpu_frequency_locked(&self) -> Hertz {
223-
unsafe { CLOCK_CONTROL.as_ref().unwrap().cpu_frequency_locked }
224-
}
225-
226-
/// The CPU frequency in the APB lock state
227-
pub fn cpu_frequency_apb_locked(&self) -> Hertz {
228-
unsafe { CLOCK_CONTROL.as_ref().unwrap().cpu_frequency_apb_locked }
229-
}
230-
231-
/// The APB frequency in the APB lock state
232-
pub fn apb_frequency_apb_locked(&self) -> Hertz {
233-
unsafe { CLOCK_CONTROL.as_ref().unwrap().apb_frequency_apb_locked }
234-
}
235-
236-
/// Is the reference clock 1MHz under all clock conditions
237-
pub fn is_ref_clock_stable(&self) -> bool {
238-
unsafe { CLOCK_CONTROL.as_ref().unwrap().ref_clock_stable }
239-
}
240-
241-
/// The current reference frequency
242-
pub fn ref_frequency(&self) -> Hertz {
243-
unsafe { CLOCK_CONTROL.as_ref().unwrap().ref_frequency }
244-
}
245-
246-
/// The current slow RTC frequency
247-
pub fn slow_rtc_frequency(&self) -> Hertz {
248-
unsafe { CLOCK_CONTROL.as_ref().unwrap().slow_rtc_frequency }
249-
}
250-
251-
/// The current fast RTC frequency
252-
pub fn fast_rtc_frequency(&self) -> Hertz {
253-
unsafe { CLOCK_CONTROL.as_ref().unwrap().fast_rtc_frequency }
254-
}
255-
256-
/// The current APLL frequency
257-
pub fn apll_frequency(&self) -> Hertz {
258-
unsafe { CLOCK_CONTROL.as_ref().unwrap().apll_frequency }
259-
}
260-
261-
/// The current PLL/2 frequency
262-
pub fn pll_d2_frequency(&self) -> Hertz {
263-
unsafe { CLOCK_CONTROL.as_ref().unwrap().pll_d2_frequency }
264-
}
265-
266-
/// The Xtal frequency
267-
pub fn xtal_frequency(&self) -> Hertz {
268-
unsafe { CLOCK_CONTROL.as_ref().unwrap().xtal_frequency }
269-
}
270-
271-
/// The 32kHz Xtal frequency
272-
pub fn xtal32k_frequency(&self) -> Hertz {
273-
unsafe { CLOCK_CONTROL.as_ref().unwrap().xtal32k_frequency }
274-
}
275-
276-
/// The current PLL frequency
277-
pub fn pll_frequency(&self) -> Hertz {
278-
unsafe { CLOCK_CONTROL.as_ref().unwrap().pll_frequency }
279-
}
280-
281-
/// The current 8MHz oscillator frequency
282-
pub fn rtc8m_frequency(&self) -> Hertz {
283-
unsafe { CLOCK_CONTROL.as_ref().unwrap().rtc8m_frequency }
284-
}
285-
286-
/// The current 8MHz oscillator frequency / 256
287-
pub fn rtc8md256_frequency(&self) -> Hertz {
288-
unsafe { CLOCK_CONTROL.as_ref().unwrap().rtc8md256_frequency }
289-
}
290-
291-
/// The current 150kHz oscillator frequency
292-
pub fn rtc_frequency(&self) -> Hertz {
293-
unsafe { CLOCK_CONTROL.as_ref().unwrap().rtc_frequency }
294-
}
295-
296-
/// The current source for the CPU and APB frequencies
297-
pub fn cpu_source(&self) -> CPUSource {
298-
unsafe { CLOCK_CONTROL.as_ref().unwrap().cpu_source }
299-
}
300-
301-
/// The current source for the slow RTC frequency
302-
pub fn slow_rtc_source(&self) -> SlowRTCSource {
303-
unsafe { CLOCK_CONTROL.as_ref().unwrap().slow_rtc_source }
304-
}
305-
306-
/// The current source for the fast RTC frequency
307-
pub fn fast_rtc_source(&self) -> FastRTCSource {
308-
unsafe { CLOCK_CONTROL.as_ref().unwrap().fast_rtc_source }
309-
}
310-
311-
// The lock and unlock calls are thread and interrupt safe because this is handled inside
312-
// the DFS routines
313-
314-
/// Obtain a RAII lock to use the high CPU frequency
315-
pub fn lock_cpu_frequency(&self) -> dfs::LockCPU {
316-
unsafe { CLOCK_CONTROL.as_mut().unwrap().lock_cpu_frequency() }
317-
}
318-
319-
/// Obtain a RAII lock to use the APB CPU frequency
320-
pub fn lock_apb_frequency(&self) -> dfs::LockAPB {
321-
unsafe { CLOCK_CONTROL.as_mut().unwrap().lock_apb_frequency() }
322-
}
323-
324-
/// Obtain a RAII lock to keep the CPU from sleeping
325-
pub fn lock_awake(&self) -> dfs::LockAwake {
326-
unsafe { CLOCK_CONTROL.as_mut().unwrap().lock_awake() }
327-
}
328-
329-
/// Obtain a RAII lock to keep the PLL/2 from being turned off
330-
pub fn lock_plld2(&self) -> dfs::LockPllD2 {
331-
unsafe { CLOCK_CONTROL.as_mut().unwrap().lock_plld2() }
332-
}
333-
334-
/// Add callback which will be called when clock speeds are changed.
335-
///
336-
/// NOTE: these callbacks are called in an interrupt free environment,
337-
/// so should be as short as possible
338-
// TODO: at the moment only static lifetime callbacks are allow
339-
pub fn add_callback<F>(&self, f: &'static F) -> Result<(), Error>
340-
where
341-
F: Fn(),
342-
{
343-
unsafe { CLOCK_CONTROL.as_mut().unwrap().add_callback(f) }
344-
}
345-
346-
/// Get the current count of the PCU, APB, Awake and PLL/2 locks
347-
pub fn get_lock_count(&self) -> dfs::Locks {
348-
unsafe { CLOCK_CONTROL.as_mut().unwrap().get_lock_count() }
349-
}
350-
351-
// The following routines are made thread and interrupt safe here
352-
353-
/// Halt the designated core
354-
pub unsafe fn park_core(&mut self, core: crate::Core) {
355-
interrupt::free(|_| {
356-
CLOCK_CONTROL_MUTEX.lock();
357-
CLOCK_CONTROL.as_mut().unwrap().park_core(core);
358-
})
359-
}
360-
361-
/// Start the APP (second) core
362-
///
363-
/// The second core will start running with the function `entry`.
364-
pub fn unpark_core(&mut self, core: crate::Core) {
365-
interrupt::free(|_| {
366-
CLOCK_CONTROL_MUTEX.lock();
367-
unsafe { CLOCK_CONTROL.as_mut().unwrap().unpark_core(core) }
368-
})
369-
}
370-
371-
/// Start the APP (second) core
372-
///
373-
/// The second core will start running with the function `entry`.
374-
pub fn start_app_core(&mut self, entry: fn() -> !) -> Result<(), Error> {
375-
interrupt::free(|_| {
376-
CLOCK_CONTROL_MUTEX.lock();
377-
unsafe { CLOCK_CONTROL.as_mut().unwrap().start_app_core(entry) }
378-
})
379-
}
380-
381-
// The following routines handle thread and interrupt safety themselves
382-
383-
/// Get RTC tick count since boot
384-
///
385-
/// *Note: this function takes up to one slow RTC clock cycle (can be up to 300us) and
386-
/// interrupts are blocked during this time.*
387-
pub fn rtc_tick_count(&self) -> TicksU64 {
388-
unsafe { CLOCK_CONTROL.as_mut().unwrap().rtc_tick_count() }
389-
}
390-
391-
/// Get nanoseconds since boot based on RTC tick count
392-
///
393-
/// *Note: this function takes up to one slow RTC clock cycle (can be up to 300us) and
394-
/// interrupts are blocked during this time.*
395-
pub fn rtc_nanoseconds(&self) -> NanoSecondsU64 {
396-
unsafe { CLOCK_CONTROL.as_mut().unwrap().rtc_nanoseconds() }
397-
}
398-
}
399-
400-
impl fmt::Debug for ClockControlConfig {
401-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
402-
unsafe { CLOCK_CONTROL.as_ref().unwrap().fmt(f) }
403-
}
404-
}
405-
406-
/// cycle accurate delay using the cycle counter register
407-
pub fn delay_cycles(clocks: u32) {
408-
let start = get_cycle_count();
409-
loop {
410-
if get_cycle_count().wrapping_sub(start) >= clocks {
411-
break;
412-
}
413-
}
414-
}
415-
416-
impl fmt::Debug for ClockControl {
417-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
418-
f.debug_struct("ClockControl")
419-
.field("cpu_frequency", &self.cpu_frequency)
420-
.field("apb_frequency", &self.apb_frequency)
421-
.field("ref_frequency", &self.ref_frequency)
422-
.field("apll_frequency", &self.apll_frequency)
423-
.field("pll_d2_frequency", &self.pll_d2_frequency)
424-
.field("slow_rtc_frequency", &self.slow_rtc_frequency)
425-
.field("fast_rtc_frequency", &self.fast_rtc_frequency)
426-
.field("xtal_frequency", &self.xtal_frequency)
427-
.field("xtal32k_frequency", &self.xtal32k_frequency)
428-
.field("rtc8m_frequency", &self.rtc8m_frequency)
429-
.field("rtc8md256_frequency", &self.rtc8md256_frequency)
430-
.field("rtc_frequency", &self.rtc_frequency)
431-
.field("pll_frequency", &self.pll_frequency)
432-
.field("cpu_source", &self.cpu_source)
433-
.field("slow_rtc_source", &self.slow_rtc_source)
434-
.field("fast_rtc_source", &self.fast_rtc_source)
435-
.finish()
436-
}
437-
}
438-
439202
/// Clock Control for initialization. Once initialization is done, call the freeze function to lock
440203
/// the clock configuration. This will return a [ClockControlConfig](ClockControlConfig), which can
441204
/// be copied for e.g. use in multiple peripherals.
@@ -828,7 +591,7 @@ impl ClockControl {
828591

829592
/// delay a certain time by spinning
830593
fn delay<T: Into<NanoSeconds>>(&self, time: T) {
831-
delay_cycles(self.time_to_cpu_cycles(time));
594+
delay(self.time_to_cpu_cycles(time));
832595
}
833596

834597
/// Check if a value from RTC_XTAL_FREQ_REG or RTC_APB_FREQ_REG are valid clocks
@@ -1565,3 +1328,27 @@ impl ClockControl {
15651328
self.rtc_tick_count() / self.slow_rtc_frequency
15661329
}
15671330
}
1331+
1332+
/// Custom debug formatter
1333+
impl fmt::Debug for ClockControl {
1334+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1335+
f.debug_struct("ClockControl")
1336+
.field("cpu_frequency", &self.cpu_frequency)
1337+
.field("apb_frequency", &self.apb_frequency)
1338+
.field("ref_frequency", &self.ref_frequency)
1339+
.field("apll_frequency", &self.apll_frequency)
1340+
.field("pll_d2_frequency", &self.pll_d2_frequency)
1341+
.field("slow_rtc_frequency", &self.slow_rtc_frequency)
1342+
.field("fast_rtc_frequency", &self.fast_rtc_frequency)
1343+
.field("xtal_frequency", &self.xtal_frequency)
1344+
.field("xtal32k_frequency", &self.xtal32k_frequency)
1345+
.field("rtc8m_frequency", &self.rtc8m_frequency)
1346+
.field("rtc8md256_frequency", &self.rtc8md256_frequency)
1347+
.field("rtc_frequency", &self.rtc_frequency)
1348+
.field("pll_frequency", &self.pll_frequency)
1349+
.field("cpu_source", &self.cpu_source)
1350+
.field("slow_rtc_source", &self.slow_rtc_source)
1351+
.field("fast_rtc_source", &self.fast_rtc_source)
1352+
.finish()
1353+
}
1354+
}

0 commit comments

Comments
 (0)