@@ -217,8 +217,16 @@ impl ITM {
217217 /// See (coresight, B2.3.10).
218218 #[ inline]
219219 pub fn unlock ( & mut self ) {
220+ if !self . locked ( ) {
221+ return ;
222+ }
223+
220224 // 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 ( ) { }
222230 }
223231
224232 /// Engages the software lock on the [`ITM`]. Should be called after
@@ -227,8 +235,14 @@ impl ITM {
227235 /// See (coresight, B2.3.10).
228236 #[ inline]
229237 pub fn lock ( & mut self ) {
238+ if self . locked ( ) {
239+ return ;
240+ }
241+
230242 // NOTE(unsafe) atomic write to a stateless, write-only register
231243 unsafe { self . lar . write ( 0 ) }
244+
245+ while !self . locked ( ) { }
232246 }
233247
234248 /// Checks whether the target implements the software lock
@@ -246,7 +260,7 @@ impl ITM {
246260 /// See (coresight, B2.3.10).
247261 #[ inline]
248262 pub fn locked ( & self ) -> bool {
249- self . lsr . read ( ) . slk ( )
263+ self . has_software_lock ( ) && self . lsr . read ( ) . slk ( )
250264 }
251265
252266 /// Indicates whether the [`ITM`] is currently processing events.
@@ -264,10 +278,7 @@ impl ITM {
264278 use ITMConfigurationError as Error ;
265279
266280 // 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 ( ) ;
271282
272283 // The ITM must then be disabled before altering certain fields
273284 // in order to avoid trace stream corruption.
@@ -355,9 +366,7 @@ impl ITM {
355366 } ) ;
356367 }
357368
358- if self . has_software_lock ( ) {
359- self . lock ( ) ;
360- }
369+ self . lock ( ) ;
361370
362371 Ok ( ( ) )
363372 }
0 commit comments