Due: How can I use CP15 MCR/MRC for instruction profiling ?

On the ARM site,

A coprocessor C15 is mentioned, which supports a cycle (and other) counters.

I want to profile my ‘sketch’. I’m familiar with processor performance counters on other platforms.

To get started, I added a read of the Cycle Counter (to somewhere deep in loop, on receipt of my magic password over serial). The Arduino hangs after printing out half the println that precedes the instruction:

loop() {

// page 3-117
uint32_t cycleCounter = dcc_rd(); // is undefined :frowning:

Serial.print(“CCR value:”);
// Serial.println(cycleCounter);

delayMicroseconds(1000000);
}

uint32_t dcc_rd(void)
{
volatile uint32_t dcc_reg;

do {

// MRC p15, 0, , c15, c12, 1 ;Read Cycle Counter Register
asm volatile (“mrc p15, 0, %0, c15, c12, 1” : “=r” (dcc_reg) :);
Serial.print(".");
} while ((dcc_reg&1) == 0);

asm volatile (“mrc p15, 0, %0, C15, C12, 1” : “=r” (dcc_reg) :);
return dcc_reg;
}

So:

  • Does the CP15 exist in the Due ARM chip?
  • Are there any examples (googling didn’t help, except for the github links below)
  • Is the above code correct?
  • How can I diagnose why it hung?

My theory is that the instruction needs privileged mode, and maybe I’m not in it (I haven’t set it or anything).

But that’s just a wild guess.

https://github.com/pchickey/gcc-sam3u-build/blob/master/at91lib/peripherals/cp15/cp15_asm_keil.s
https://github.com/arduino/OpenOCD/blob/2d4ae3f4c881696c60f2eb1d1c3b4b6a9577bbd6/contrib/loaders/flash/at91sam7x/dcc.c

http://infocenter.arm.com/help/topic/com.arm.doc.ddi0338g/DDI0338G_arm1156t2s_r0p4_trm.pdf

That’s the TRM for the ARM11 microprocessor architecture. Due is a microcontroller that uses a Cortex-M3 core, and it’s quite different. No “co-processor” at all, AFAICT. http://infocenter.arm.com/help/topic/com.arm.doc.100165_0201_00_en/index.html

I’d expect that your program falls into an “unhandled trap” loop when you try to execute the non-existent instruction or non-existent memory location.