Skip to content

Commit 0da1028

Browse files
committed
add methods needed for cycle count comparisons
1 parent 441cb87 commit 0da1028

File tree

2 files changed

+66
-15
lines changed

2 files changed

+66
-15
lines changed

src/peripheral/dcb.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::peripheral::DCB;
66
use core::ptr;
77

88
const DCB_DEMCR_TRCENA: u32 = 1 << 24;
9+
const DCB_DEMCR_MON_EN: u32 = 1 << 16;
910

1011
/// Register block
1112
#[repr(C)]
@@ -46,6 +47,20 @@ impl DCB {
4647
}
4748
}
4849

50+
/// Enables the [`DebugMonitor`](crate::peripheral::scb::Exception::DebugMonitor) exception
51+
pub fn enable_debug_monitor(&mut self) {
52+
unsafe {
53+
self.demcr.modify(|w| w | DCB_DEMCR_MON_EN);
54+
}
55+
}
56+
57+
/// Disables the [`DebugMonitor`](crate::peripheral::scb::Exception::DebugMonitor) exception
58+
pub fn disable_debug_monitor(&mut self) {
59+
unsafe {
60+
self.demcr.modify(|w| w & !DCB_DEMCR_MON_EN);
61+
}
62+
}
63+
4964
/// Is there a debugger attached? (see note)
5065
///
5166
/// Note: This function is [reported not to

src/peripheral/dwt.rs

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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]
362371
pub 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

Comments
 (0)