1
votes

I have a Cortex M0+ (SAML21) board that I'm using for performance testing. I'd like to measure how many cycles a given piece of code takes. I tried using DWT (DWT_CONTROL), but it never produced a result; it returned 0 cycles regardless of what code ran.

  // enable the use DWT
  *DEMCR = *DEMCR | 0x01000000;

  // Reset cycle counter
  *DWT_CYCCNT = 0;

  // enable cycle counter
  *DWT_CONTROL = *DWT_CONTROL | 1 ;
  // some code here
  // .....

  // number of cycles stored in count variable
  count = *DWT_CYCCNT;

Is there a way to count cycles (perhaps with an interrupt and counter?) much like I can query for milliseconds (eg. millis() on Arduino)?

1
this processor doesnt necessarily work that way, if the chip vendor (atmel now microchip) does not do any flash caching (like st) then you can time some code at a specific address with specific settings and get a count. but move that code to another alignment, change the clock settings, etc and the number of cpu clocks for that code can change a little or a lot. - old_timer
The cycle counter is an optional feature on the M0+. Are you sure this implementation actually has the feature? - Realtime Rik
@RealtimeRik, I am not sure. I'm dealing with the ATSAML21E18B, which dues have debug capabilities. But, I think andy mango is correct; I have to read those registers out via JTAG. - MarkP
Possibly. I am not sure without looking it up. I know on a M3/M4 you can read the cycle counter programatically as I have used it. - Realtime Rik

1 Answers

1
votes

According to the CMSIS header file for the M0+ (core_cm0plus.h), the Core Debug Registers are only accessible over the Debug Access Port and not via the processor. I can only suggest using some free running timer (maybe SysTick) or perhaps your debugger can be of some help to get access to the required registers.