3
3
4
4
extern crate panic_rtt;
5
5
6
- use core:: borrow:: Borrow ;
7
6
use cortex_m:: mutex:: CriticalSectionMutex as Mutex ;
8
7
use cortex_m_rt:: entry;
9
8
use embedded_time:: { self as time, Clock , Instant , Period , TimeInt } ;
@@ -16,13 +15,13 @@ pub mod nrf52 {
16
15
target:: { self as pac, Peripherals } ,
17
16
} ;
18
17
19
- pub struct Timer64 {
18
+ pub struct Clock64 {
20
19
low : pac:: TIMER0 ,
21
20
high : pac:: TIMER1 ,
22
21
capture_task : pac:: EGU0 ,
23
22
}
24
23
25
- impl Timer64 {
24
+ impl Clock64 {
26
25
pub fn take ( low : pac:: TIMER0 , high : pac:: TIMER1 , capture_task : pac:: EGU0 ) -> Self {
27
26
Self {
28
27
low,
@@ -45,16 +44,16 @@ impl time::Clock for SysClock {
45
44
const PERIOD : Period = Period :: new ( 1 , 16_000_000 ) ;
46
45
47
46
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 ( ) ,
50
49
None => 0 ,
51
50
} ) ;
52
51
53
52
Instant :: new ( ticks as Self :: Rep )
54
53
}
55
54
}
56
55
57
- static SYSTEM_TICKS : Mutex < Option < nrf52:: Timer64 > > = Mutex :: new ( None ) ;
56
+ static CLOCK64 : Mutex < Option < nrf52:: Clock64 > > = Mutex :: new ( None ) ;
58
57
59
58
#[ entry]
60
59
fn main ( ) -> ! {
@@ -76,35 +75,21 @@ fn main() -> ! {
76
75
77
76
unsafe {
78
77
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 )
83
79
} ) ;
84
80
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 )
88
82
} ) ;
89
83
device. PPI . chen . modify ( |_, w| w. ch0 ( ) . enabled ( ) ) ;
90
84
91
85
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 )
96
87
} ) ;
97
88
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 )
102
90
} ) ;
103
91
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 )
108
93
} ) ;
109
94
device. PPI . chen . modify ( |_, w| w. ch1 ( ) . enabled ( ) ) ;
110
95
}
@@ -118,8 +103,11 @@ fn main() -> ! {
118
103
. tasks_start
119
104
. write ( |write| unsafe { write. bits ( 1 ) } ) ;
120
105
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) ) ;
123
111
124
112
let port0 = nrf52:: gpio:: p0:: Parts :: new ( device. P0 ) ;
125
113
0 commit comments