@@ -13,9 +13,6 @@ use rp235x_hal::{
13
13
uart:: { DataBits , StopBits , UartConfig , UartPeripheral } ,
14
14
} ;
15
15
16
- use cortex_m_rt:: exception;
17
- use pac:: interrupt;
18
-
19
16
// Some traits we need
20
17
use core:: fmt:: Write ;
21
18
use hal:: fugit:: RateExtU32 ;
@@ -67,7 +64,7 @@ impl GlobalUart {
67
64
68
65
/// Entry point to our bare-metal application.
69
66
///
70
- /// The `#[hal::entry]` macro ensures the Cortex-M start-up code calls this function
67
+ /// The `#[hal::entry]` macro ensures the start-up code calls this function
71
68
/// as soon as all global variables and the spinlock are initialised.
72
69
///
73
70
/// The function configures the rp235x peripherals, then writes to the UART in
@@ -76,11 +73,6 @@ impl GlobalUart {
76
73
fn main ( ) -> ! {
77
74
// Grab our singleton objects
78
75
let mut pac = pac:: Peripherals :: take ( ) . unwrap ( ) ;
79
- let mut cp = cortex_m:: Peripherals :: take ( ) . unwrap ( ) ;
80
-
81
- // Enable the cycle counter
82
- cp. DCB . enable_trace ( ) ;
83
- cp. DWT . enable_cycle_counter ( ) ;
84
76
85
77
// Set up the watchdog driver - needed by the clock setup code
86
78
let mut watchdog = hal:: Watchdog :: new ( pac. WATCHDOG ) ;
@@ -99,6 +91,8 @@ fn main() -> ! {
99
91
100
92
// The single-cycle I/O block controls our GPIO pins
101
93
let sio = hal:: Sio :: new ( pac. SIO ) ;
94
+ let mut mtimer = sio. machine_timer ;
95
+ mtimer. set_enabled ( true ) ;
102
96
103
97
// Set the pins to their default state
104
98
let pins = gpio:: Pins :: new (
@@ -131,29 +125,30 @@ fn main() -> ! {
131
125
_ = writeln ! ( & GLOBAL_UART , "AOT time: 0x{:016x}" , powman. aot_get_time( ) ) ;
132
126
133
127
unsafe {
134
- cortex_m:: peripheral:: NVIC :: unmask ( pac:: Interrupt :: POWMAN_IRQ_TIMER ) ;
128
+ hal:: arch:: interrupt_unmask ( pac:: Interrupt :: POWMAN_IRQ_TIMER ) ;
129
+ hal:: arch:: interrupt_enable ( ) ;
135
130
}
136
131
137
132
_ = writeln ! ( & GLOBAL_UART , "Starting AOT..." ) ;
138
133
powman. aot_start ( ) ;
139
134
// we don't know what oscillator we're on, so give it time to start whatever it is
140
- cortex_m :: asm :: delay ( 150_000 ) ;
135
+ hal :: arch :: delay ( 150_000 ) ;
141
136
print_aot_status ( & mut powman) ;
142
137
rollover_test ( & mut powman) ;
143
- loop_test ( & mut powman) ;
138
+ loop_test ( & mut powman, & mtimer ) ;
144
139
alarm_test ( & mut powman) ;
145
140
146
141
let source = AotClockSource :: Xosc ( FractionalFrequency :: from_hz ( 12_000_000 ) ) ;
147
142
_ = writeln ! ( & GLOBAL_UART , "Switching AOT to {}" , source) ;
148
143
powman. aot_set_clock ( source) . expect ( "selecting XOSC" ) ;
149
144
print_aot_status ( & mut powman) ;
150
- loop_test ( & mut powman) ;
145
+ loop_test ( & mut powman, & mtimer ) ;
151
146
152
147
let source = AotClockSource :: Lposc ( FractionalFrequency :: from_hz ( 32768 ) ) ;
153
148
_ = writeln ! ( & GLOBAL_UART , "Switching AOT to {}" , source) ;
154
149
powman. aot_set_clock ( source) . expect ( "selecting LPOSC" ) ;
155
150
print_aot_status ( & mut powman) ;
156
- loop_test ( & mut powman) ;
151
+ loop_test ( & mut powman, & mtimer ) ;
157
152
158
153
_ = writeln ! ( & GLOBAL_UART , "Rebooting now" ) ;
159
154
@@ -202,7 +197,7 @@ fn rollover_test(powman: &mut Powman) {
202
197
}
203
198
204
199
/// In this function, we see how long it takes to pass a certain number of ticks.
205
- fn loop_test ( powman : & mut Powman ) {
200
+ fn loop_test ( powman : & mut Powman , mtimer : & hal :: sio :: MachineTimer ) {
206
201
let start_loop = 0 ;
207
202
let end_loop = 2_000 ; // 2 seconds
208
203
_ = writeln ! (
@@ -214,24 +209,24 @@ fn loop_test(powman: &mut Powman) {
214
209
powman. aot_set_time ( start_loop) ;
215
210
powman. aot_start ( ) ;
216
211
217
- let start_clocks = cortex_m :: peripheral :: DWT :: cycle_count ( ) ;
212
+ let start_clocks = mtimer . read ( ) ;
218
213
loop {
219
214
let now = powman. aot_get_time ( ) ;
220
215
if now == end_loop {
221
216
break ;
222
217
}
223
218
}
224
- let end_clocks = cortex_m :: peripheral :: DWT :: cycle_count ( ) ;
225
- // Compare our AOT against our CPU clock speed
226
- let delta_clocks = end_clocks. wrapping_sub ( start_clocks) as u64 ;
219
+ let end_clocks = mtimer . read ( ) ;
220
+ // Compare our AOT against our Machine Timer
221
+ let delta_clocks = end_clocks. wrapping_sub ( start_clocks) ;
227
222
let delta_ticks = end_loop - start_loop;
228
223
let cycles_per_tick = delta_clocks / delta_ticks;
229
- // Assume we're running at 150 MHz
230
- let ms_per_tick = ( cycles_per_tick as f32 * 1000.0 ) / 150_000_000 .0;
224
+ // Assume we're running at 1 MHz MTimer
225
+ let ms_per_tick = ( cycles_per_tick as f32 * 1000.0 ) / 1_000_000 .0;
231
226
let percent = ( ( ms_per_tick - 1.0 ) / 1.0 ) * 100.0 ;
232
227
_ = writeln ! (
233
228
& GLOBAL_UART ,
234
- "Loop complete ... {delta_ticks} ticks in {delta_clocks} CPU clock cycles = {cycles_per_tick} cycles/tick ~= {ms_per_tick} ms/tick ({percent:.3}%)" ,
229
+ "Loop complete ... {delta_ticks} ticks in {delta_clocks} MTimer cycles = {cycles_per_tick} cycles/tick ~= {ms_per_tick} ms/tick ({percent:.3}%)" ,
235
230
)
236
231
;
237
232
}
@@ -258,8 +253,9 @@ fn alarm_test(powman: &mut Powman) {
258
253
& GLOBAL_UART ,
259
254
"Sleeping until alarm (* = wakeup, ! = POWMAN interrupt)..." ,
260
255
) ;
256
+
261
257
while !powman. aot_alarm_ringing ( ) {
262
- cortex_m :: asm :: wfe ( ) ;
258
+ hal :: arch :: wfe ( ) ;
263
259
_ = write ! ( & GLOBAL_UART , "*" , ) ;
264
260
}
265
261
@@ -278,25 +274,12 @@ fn alarm_test(powman: &mut Powman) {
278
274
_ = writeln ! ( & GLOBAL_UART , "Alarm cleared OK" ) ;
279
275
}
280
276
281
- #[ interrupt ]
277
+ #[ no_mangle ]
282
278
#[ allow( non_snake_case) ]
283
279
fn POWMAN_IRQ_TIMER ( ) {
284
280
Powman :: static_aot_alarm_interrupt_disable ( ) ;
285
281
_ = write ! ( & GLOBAL_UART , "!" ) ;
286
- cortex_m:: asm:: sev ( ) ;
287
- }
288
-
289
- #[ exception]
290
- unsafe fn HardFault ( ef : & cortex_m_rt:: ExceptionFrame ) -> ! {
291
- let _ = writeln ! ( & GLOBAL_UART , "HARD FAULT:\n {:#?}" , ef) ;
292
-
293
- hal:: reboot:: reboot (
294
- hal:: reboot:: RebootKind :: BootSel {
295
- msd_disabled : false ,
296
- picoboot_disabled : false ,
297
- } ,
298
- hal:: reboot:: RebootArch :: Normal ,
299
- ) ;
282
+ hal:: arch:: sev ( ) ;
300
283
}
301
284
302
285
#[ panic_handler]
0 commit comments