Skip to content

Commit d1ae2c2

Browse files
committed
refactoring
1 parent 3f91aa0 commit d1ae2c2

File tree

3 files changed

+17
-31
lines changed

3 files changed

+17
-31
lines changed

examples/nrf52_dk/main.rs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
extern crate panic_rtt;
55

6-
use core::borrow::Borrow;
76
use cortex_m::mutex::CriticalSectionMutex as Mutex;
87
use cortex_m_rt::entry;
98
use embedded_time::{self as time, Clock, Instant, Period, TimeInt};
@@ -16,13 +15,13 @@ pub mod nrf52 {
1615
target::{self as pac, Peripherals},
1716
};
1817

19-
pub struct Timer64 {
18+
pub struct Clock64 {
2019
low: pac::TIMER0,
2120
high: pac::TIMER1,
2221
capture_task: pac::EGU0,
2322
}
2423

25-
impl Timer64 {
24+
impl Clock64 {
2625
pub fn take(low: pac::TIMER0, high: pac::TIMER1, capture_task: pac::EGU0) -> Self {
2726
Self {
2827
low,
@@ -45,16 +44,16 @@ impl time::Clock for SysClock {
4544
const PERIOD: Period = Period::new(1, 16_000_000);
4645

4746
fn now() -> Instant<Self> {
48-
let ticks = (&SYSTEM_TICKS).lock(|timer64| match timer64 {
49-
Some(timer64) => timer64.read(),
47+
let ticks = (&CLOCK64).lock(|clock| match clock {
48+
Some(clock) => clock.read(),
5049
None => 0,
5150
});
5251

5352
Instant::new(ticks as Self::Rep)
5453
}
5554
}
5655

57-
static SYSTEM_TICKS: Mutex<Option<nrf52::Timer64>> = Mutex::new(None);
56+
static CLOCK64: Mutex<Option<nrf52::Clock64>> = Mutex::new(None);
5857

5958
#[entry]
6059
fn main() -> ! {
@@ -76,35 +75,21 @@ fn main() -> ! {
7675

7776
unsafe {
7877
device.PPI.ch[0].eep.write(|w| {
79-
w.bits(
80-
device.TIMER0.events_compare[1].borrow() as *const nrf52::pac::generic::Reg<_, _>
81-
as u32,
82-
)
78+
w.bits(&device.TIMER0.events_compare[1] as *const nrf52::pac::generic::Reg<_, _> as u32)
8379
});
8480
device.PPI.ch[0].tep.write(|w| {
85-
w.bits(
86-
device.TIMER1.tasks_count.borrow() as *const nrf52::pac::generic::Reg<_, _> as u32,
87-
)
81+
w.bits(&device.TIMER1.tasks_count as *const nrf52::pac::generic::Reg<_, _> as u32)
8882
});
8983
device.PPI.chen.modify(|_, w| w.ch0().enabled());
9084

9185
device.PPI.ch[1].eep.write(|w| {
92-
w.bits(
93-
device.EGU0.events_triggered[0].borrow() as *const nrf52::pac::generic::Reg<_, _>
94-
as u32,
95-
)
86+
w.bits(&device.EGU0.events_triggered[0] as *const nrf52::pac::generic::Reg<_, _> as u32)
9687
});
9788
device.PPI.ch[1].tep.write(|w| {
98-
w.bits(
99-
device.TIMER0.tasks_capture[0].borrow() as *const nrf52::pac::generic::Reg<_, _>
100-
as u32,
101-
)
89+
w.bits(&device.TIMER0.tasks_capture[0] as *const nrf52::pac::generic::Reg<_, _> as u32)
10290
});
10391
device.PPI.fork[1].tep.write(|w| {
104-
w.bits(
105-
device.TIMER1.tasks_capture[0].borrow() as *const nrf52::pac::generic::Reg<_, _>
106-
as u32,
107-
)
92+
w.bits(&device.TIMER1.tasks_capture[0] as *const nrf52::pac::generic::Reg<_, _> as u32)
10893
});
10994
device.PPI.chen.modify(|_, w| w.ch1().enabled());
11095
}
@@ -118,8 +103,11 @@ fn main() -> ! {
118103
.tasks_start
119104
.write(|write| unsafe { write.bits(1) });
120105

121-
let timer64 = nrf52::Timer64::take(device.TIMER0, device.TIMER1, device.EGU0);
122-
(&SYSTEM_TICKS).lock(|ticks| *ticks = Some(timer64));
106+
// This moves these peripherals to prevent conflicting usage, however not the entire EGU0 is
107+
// used. A ref to EGU0 could be sent instead, although that provides no protection for the
108+
// fields that are being used by Clock64.
109+
let clock64 = nrf52::Clock64::take(device.TIMER0, device.TIMER1, device.EGU0);
110+
(&CLOCK64).lock(|ticks| *ticks = Some(clock64));
123111

124112
let port0 = nrf52::gpio::p0::Parts::new(device.P0);
125113

src/clock.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ pub trait Clock: Sized {
1313
fn now() -> Instant<Self>;
1414

1515
/// Blocking delay
16-
fn delay<Dur>(dur: Dur)
16+
fn delay<Dur: Duration>(dur: Dur)
1717
where
18-
Dur: Duration,
19-
Dur::Rep: TimeInt,
2018
Self::Rep: TryFrom<Dur::Rep>,
2119
{
2220
let start = Self::now();

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! [`Milliseconds`]: units::Milliseconds
1414
//!
1515
//! ## Definitions
16-
//! **Clock**: Any entity that periodically counts (ie a hardware timer peripheral). Generally,
16+
//! **Clock**: Any entity that periodically counts (ie a hardware timer/counter peripheral). Generally,
1717
//! this needs to be monotonic. A wrapping clock is considered monotonic in this context as long as
1818
//! it fulfills the other requirements.
1919
//!

0 commit comments

Comments
 (0)