@@ -356,12 +356,23 @@ pub struct ComparatorAddressSettings {
356356 pub access_type : AccessType ,
357357}
358358
359+ /// Settings for cycle count matching
360+ #[ derive( Debug , Eq , PartialEq , Copy , Clone ) ]
361+ pub struct CycleCountSettings {
362+ /// The cycle count value to compare against.
363+ pub compare : u32 ,
364+ /// The cycle count mask value to use.
365+ pub mask : u32 ,
366+ }
367+
359368/// The available functions of a DWT comparator.
360369#[ derive( Debug , Eq , PartialEq , Copy , Clone ) ]
361370#[ non_exhaustive]
362371pub enum ComparatorFunction {
363372 /// Compare accessed memory addresses.
364373 Address ( ComparatorAddressSettings ) ,
374+ /// Compare cycle count & target value.
375+ CycleCount ( CycleCountSettings ) ,
365376}
366377
367378/// Possible error values returned on [Comparator::configure].
@@ -376,8 +387,8 @@ impl Comparator {
376387 /// Configure the function of the comparator
377388 #[ allow( clippy:: missing_inline_in_public_items) ]
378389 pub fn configure ( & self , settings : ComparatorFunction ) -> Result < ( ) , DwtError > {
379- match settings {
380- ComparatorFunction :: Address ( settings) => unsafe {
390+ let ( func , emit , data_match , cyc_match , comp , mask ) = match settings {
391+ ComparatorFunction :: Address ( settings) => {
381392 // FUNCTION, EMITRANGE
382393 // See Table C1-14
383394 let ( function, emit_range) = match ( & settings. access_type , & settings. emit ) {
@@ -400,25 +411,50 @@ impl Comparator {
400411 ( _, EmitOption :: PC ) => return Err ( DwtError :: InvalidFunction ) ,
401412 } ;
402413
403- self . function . modify ( |mut r| {
404- r. set_function ( function) ;
405- r. set_emitrange ( emit_range) ;
406-
414+ (
415+ function,
416+ emit_range,
407417 // don't compare data value
408- r. set_datavmatch ( false ) ;
409-
418+ false ,
410419 // don't compare cycle counter value
411420 // NOTE: only needed for comparator 0, but is SBZP.
412- r. set_cycmatch ( false ) ;
421+ false ,
422+ settings. address ,
423+ settings. mask ,
424+ )
425+ }
426+ ComparatorFunction :: CycleCount ( settings) => {
427+ (
428+ // emit a Debug Watchpoint event, either halting execution or
429+ // firing a `DebugMonitor` exception
430+ 0b0111 ,
431+ // don't emit (we're going to fire an exception not trace)
432+ false ,
433+ // don't compare data
434+ false ,
435+ // compare cyccnt
436+ true ,
437+ settings. compare ,
438+ settings. mask ,
439+ )
440+ }
441+ } ;
413442
414- r
415- } ) ;
443+ unsafe {
444+ self . function . modify ( |mut r| {
445+ r. set_function ( func) ;
446+ r. set_emitrange ( emit) ;
447+ r. set_datavmatch ( data_match) ;
416448
417- self . comp . write ( settings. address ) ;
418- self . mask . write ( settings. mask ) ;
419- } ,
420- }
449+ // NOTE: only valid for comparator 0, but is SBZP.
450+ r. set_cycmatch ( cyc_match) ;
451+
452+ r
453+ } ) ;
421454
455+ self . comp . write ( comp) ;
456+ self . mask . write ( mask) ;
457+ }
422458 Ok ( ( ) )
423459 }
424460}
0 commit comments