ARM_DWT_CYCCNT

sophiemor

New member
Hello everyone!

I am a beginner coder and I need to figure out how to use ARM_DWT_CYCCNT. Would someone be able to give me a detailed rundown? Thanks!
 
It’s super simple, just an unsigned 32-bit counter that increments on every CPU cycle (hence the CYCCNT). On Teensy 4.x at the default 600MHz it thus rolls over to zero about every 7 seconds.

I really can’t think of anything else to say about it…
 
Ordinary usage looks something like this.

Code:
uint32_t begin_cycles = ARM_DWT_CYCCNT;
// do something that takes time
uint32_t end_cycles = ARM_DWT_CYCCNT;
Serial.print("cycles = ");
Serial.println(end_cycles - begin_cycles);

Subtracting this way automatically handles the roll over case, so it's really very simple to use.
 
Just a dumb question: Which kind of microcontroller application would require to measure period bigger than 7s, with 1.6ns resolution ?
 
measure period bigger than 7s, with 1.6ns resolution
The problem remembering the 7 second limit. It is easy/efficient to use ARM_DWT_CYCCNT 'all the time' - but keeping in mind that after 32 bit rollover the results are bad.

@luni did extend a way to use ARM_DWT_CYCCNT to 64 bits.

Reading ARM_DWT_CYCCNT takes 3 cycles for the data transfer and micros() takes 36(+) cycles for the function/calculation. And it relies on millis() sys_tick for reference.
 
Back
Top