UNO R4 Access to cpu cycle counter?

On the Teensy boards, precise timings can be accomplishing using the cpu cycle counter and a macro for the clock frequency.

Here is an example that uses the cpu cycle counter to time the conversion and readout for an ADC

// cpu cycles for 700 nanoseconds
uint32_t wait_counts = (700 * (F_CPU/1000))/1000000;

uint32_t counts_at_start;

// Start the conversion in the ADC
digitalWrite( CNVST, HIGH);

// Wait 700 nsecs for the conversion to complete
counts_at_start = ARM_DWT_CYCCNT;
while(( ARM_DWT_CYCCNT-counts_at_start)<wait_counts);

// Enable the readout
digitalWrite( CNVST, LOW);

// Read the data
u16result = SPI.transfer16(0xFFFF);

Is there similar access to a cpu counter and a macro for the cpu frequency, in the UNO R4?

How about for the R3?

Thank you

P/S Aside, in case someone is interested, the wait is actually a little less than 700 nsecs, because of the setup time for the spi. And that depends on the platform. That is probably not so relevant to the question. But it goes to the point that if you want to get the best speed for the external ADC, you need to be able to time this closely.

I think I found it.

For the Renesas processors, it is DWT->CYCCNT, or at least it compiles. And F_CPU seems to compile as well.

Nope, that gives the same value every time, 0x40000000

So, on the Renesas forum, it says to turn on debug do the following:

CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
ITM->LAR = 0xc5acce55;
DWT->CYCCNT = 0;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;

However, that produces an error:

.arduino15/packages/arduino/hardware/renesas_uno/1.2.0/variants/MINIMA/includes/ra/arm/CMSIS_5/CMSIS/Core/Include/core_cm4.h:1566:46: error: expected ')' before '*' token
#define CoreDebug ((CoreDebug_Type *) CoreDebug_BASE)

No Help?

1 Like

It compiled OK for me. (in setup())
Check the surrounding lines for missing semicolons or something.
(Note that this a Cortex core stuff; it should work on any ARMCM3 or higher that has the DWT unit.)