@@ -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,32 @@ 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:: enable_irq ( 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
+ for _ in 0 ..150_000 {
136
+ hal:: arch:: nop ( ) ;
137
+ }
141
138
print_aot_status ( & mut powman) ;
142
139
rollover_test ( & mut powman) ;
143
- loop_test ( & mut powman) ;
140
+ loop_test ( & mut powman, & mtimer ) ;
144
141
alarm_test ( & mut powman) ;
145
142
146
143
let source = AotClockSource :: Xosc ( FractionalFrequency :: from_hz ( 12_000_000 ) ) ;
147
144
_ = writeln ! ( & GLOBAL_UART , "Switching AOT to {}" , source) ;
148
145
powman. aot_set_clock ( source) . expect ( "selecting XOSC" ) ;
149
146
print_aot_status ( & mut powman) ;
150
- loop_test ( & mut powman) ;
147
+ loop_test ( & mut powman, & mtimer ) ;
151
148
152
149
let source = AotClockSource :: Lposc ( FractionalFrequency :: from_hz ( 32768 ) ) ;
153
150
_ = writeln ! ( & GLOBAL_UART , "Switching AOT to {}" , source) ;
154
151
powman. aot_set_clock ( source) . expect ( "selecting LPOSC" ) ;
155
152
print_aot_status ( & mut powman) ;
156
- loop_test ( & mut powman) ;
153
+ loop_test ( & mut powman, & mtimer ) ;
157
154
158
155
_ = writeln ! ( & GLOBAL_UART , "Rebooting now" ) ;
159
156
@@ -202,7 +199,7 @@ fn rollover_test(powman: &mut Powman) {
202
199
}
203
200
204
201
/// In this function, we see how long it takes to pass a certain number of ticks.
205
- fn loop_test ( powman : & mut Powman ) {
202
+ fn loop_test ( powman : & mut Powman , mtimer : & hal :: sio :: MachineTimer ) {
206
203
let start_loop = 0 ;
207
204
let end_loop = 2_000 ; // 2 seconds
208
205
_ = writeln ! (
@@ -214,24 +211,24 @@ fn loop_test(powman: &mut Powman) {
214
211
powman. aot_set_time ( start_loop) ;
215
212
powman. aot_start ( ) ;
216
213
217
- let start_clocks = cortex_m :: peripheral :: DWT :: cycle_count ( ) ;
214
+ let start_clocks = mtimer . read ( ) ;
218
215
loop {
219
216
let now = powman. aot_get_time ( ) ;
220
217
if now == end_loop {
221
218
break ;
222
219
}
223
220
}
224
- let end_clocks = cortex_m :: peripheral :: DWT :: cycle_count ( ) ;
225
- // Compare our AOT against our CPU clock speed
221
+ let end_clocks = mtimer . read ( ) ;
222
+ // Compare our AOT against our Machine Timer
226
223
let delta_clocks = end_clocks. wrapping_sub ( start_clocks) as u64 ;
227
224
let delta_ticks = end_loop - start_loop;
228
225
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;
226
+ // Assume we're running at 1 MHz MTimer
227
+ let ms_per_tick = ( cycles_per_tick as f32 * 1000.0 ) / 1_000_000 .0;
231
228
let percent = ( ( ms_per_tick - 1.0 ) / 1.0 ) * 100.0 ;
232
229
_ = writeln ! (
233
230
& 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}%)" ,
231
+ "Loop complete ... {delta_ticks} ticks in {delta_clocks} MTimer cycles = {cycles_per_tick} cycles/tick ~= {ms_per_tick} ms/tick ({percent:.3}%)" ,
235
232
)
236
233
;
237
234
}
@@ -258,8 +255,9 @@ fn alarm_test(powman: &mut Powman) {
258
255
& GLOBAL_UART ,
259
256
"Sleeping until alarm (* = wakeup, ! = POWMAN interrupt)..." ,
260
257
) ;
258
+
261
259
while !powman. aot_alarm_ringing ( ) {
262
- cortex_m :: asm :: wfe ( ) ;
260
+ hal :: arch :: wfe ( ) ;
263
261
_ = write ! ( & GLOBAL_UART , "*" , ) ;
264
262
}
265
263
@@ -278,25 +276,12 @@ fn alarm_test(powman: &mut Powman) {
278
276
_ = writeln ! ( & GLOBAL_UART , "Alarm cleared OK" ) ;
279
277
}
280
278
281
- #[ interrupt ]
279
+ #[ no_mangle ]
282
280
#[ allow( non_snake_case) ]
283
281
fn POWMAN_IRQ_TIMER ( ) {
284
282
Powman :: static_aot_alarm_interrupt_disable ( ) ;
285
283
_ = 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
- ) ;
284
+ hal:: arch:: sev ( ) ;
300
285
}
301
286
302
287
#[ panic_handler]
0 commit comments