@@ -217,8 +217,16 @@ impl ITM {
217
217
/// See (coresight, B2.3.10).
218
218
#[ inline]
219
219
pub fn unlock ( & mut self ) {
220
+ if !self . locked ( ) {
221
+ return ;
222
+ }
223
+
220
224
// NOTE(unsafe) atomic write to a stateless, write-only register
221
- unsafe { self . lar . write ( 0xC5AC_CE55 ) }
225
+ unsafe {
226
+ self . lar . write ( 0xC5AC_CE55 ) ;
227
+ }
228
+
229
+ while self . locked ( ) { }
222
230
}
223
231
224
232
/// Engages the software lock on the [`ITM`]. Should be called after
@@ -227,8 +235,14 @@ impl ITM {
227
235
/// See (coresight, B2.3.10).
228
236
#[ inline]
229
237
pub fn lock ( & mut self ) {
238
+ if self . locked ( ) {
239
+ return ;
240
+ }
241
+
230
242
// NOTE(unsafe) atomic write to a stateless, write-only register
231
243
unsafe { self . lar . write ( 0 ) }
244
+
245
+ while !self . locked ( ) { }
232
246
}
233
247
234
248
/// Checks whether the target implements the software lock
@@ -246,7 +260,7 @@ impl ITM {
246
260
/// See (coresight, B2.3.10).
247
261
#[ inline]
248
262
pub fn locked ( & self ) -> bool {
249
- self . lsr . read ( ) . slk ( )
263
+ self . has_software_lock ( ) && self . lsr . read ( ) . slk ( )
250
264
}
251
265
252
266
/// Indicates whether the [`ITM`] is currently processing events.
@@ -264,10 +278,7 @@ impl ITM {
264
278
use ITMConfigurationError as Error ;
265
279
266
280
// The ITM must be unlocked before we apply any changes.
267
- if self . has_software_lock ( ) && self . locked ( ) {
268
- self . unlock ( ) ;
269
- while self . locked ( ) { }
270
- }
281
+ self . unlock ( ) ;
271
282
272
283
// The ITM must then be disabled before altering certain fields
273
284
// in order to avoid trace stream corruption.
@@ -355,9 +366,7 @@ impl ITM {
355
366
} ) ;
356
367
}
357
368
358
- if self . has_software_lock ( ) {
359
- self . lock ( ) ;
360
- }
369
+ self . lock ( ) ;
361
370
362
371
Ok ( ( ) )
363
372
}
0 commit comments