Skip to content

Commit

Permalink
fix bug making cycle counting affect micros() on some platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
profezzorn committed Mar 26, 2022
1 parent be40f79 commit 18cd97e
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions common/scoped_cycle_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,35 @@

class ScopedCycleCounter {
public:
ScopedCycleCounter(uint64_t& dest) :
dest_(dest) {
#ifndef DISABLE_DIAGNOSTIC_COMMANDS
static inline uint32_t getCycles() {
#ifdef TEENSYDUINO
cycles_ = ARM_DWT_CYCCNT;
return ARM_DWT_CYCCNT - counted_cycles_;
#else
cycles_ = DWT->CYCCNT;
return DWT->CYCCNT - counted_cycles_;
#endif
}
ScopedCycleCounter(uint64_t& dest) :
dest_(dest) {
#ifndef DISABLE_DIAGNOSTIC_COMMANDS
cycles_ = getCycles();
#endif
}
~ScopedCycleCounter() {
#ifndef DISABLE_DIAGNOSTIC_COMMANDS
uint32_t cycles;
#ifdef TEENSYDUINO
cycles = ARM_DWT_CYCCNT - cycles_;
ARM_DWT_CYCCNT = cycles_;
#else
cycles = DWT->CYCCNT - cycles_;
DWT->CYCCNT = cycles_;
#endif
cycles = getCycles() - cycles_;
noInterrupts();
counted_cycles_ += cycles;
interrupts();
dest_ += cycles;
#endif
}
private:
static uint32_t counted_cycles_;
uint32_t cycles_;
uint64_t& dest_;
};

uint32_t ScopedCycleCounter::counted_cycles_ = 0;

#endif

0 comments on commit 18cd97e

Please sign in to comment.