<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Get tick count (CPU cycle count) with SysTick in Kinetis Microcontrollers</title>
    <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Get-tick-count-CPU-cycle-count-with-SysTick/m-p/241364#M5965</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK, I figured this out. I wasn't taking into account that the counter counts down and not up. I set the RVR register to 0x0 and since the CVR register counts from RVR to zero than loads RVR (which in my case was zero) I would always get zero.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Proper method:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SYST_RVR = 0xFFFFFFFF;&lt;/P&gt;&lt;P&gt;SYST_CSR = 0x7;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;int nStart = SYST_CVR;&lt;/P&gt;&lt;P&gt;//Do something&lt;/P&gt;&lt;P&gt;int nStop = SYST_CVR;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;int nChange = nStart - nStop;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Start value will always be larger because the CVR register counts down.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 26 Jun 2013 22:02:10 GMT</pubDate>
    <dc:creator>concerned12345</dc:creator>
    <dc:date>2013-06-26T22:02:10Z</dc:date>
    <item>
      <title>Get tick count (CPU cycle count) with SysTick</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Get-tick-count-CPU-cycle-count-with-SysTick/m-p/241363#M5964</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;BR /&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I want to get some timing information from my code and see how long certain algorithms run. I do not want to do this through debug. So, I have been looking at SysTick and I'm trying read the CVR register with the macro SYST_CVR. I keep getting zero as a result. From reading I am under the impression that this by default increments every clock cycle. Please let me know if I need to setup any of the other registers, if so which ones and with what values.&lt;/P&gt;&lt;P&gt;Also, if there is a nother way to get accurate CPU clock cycles please let me know.&lt;/P&gt;&lt;P&gt;I am running a K70 on a TWR-K70F120M rev. B tower board.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt;"&gt;printf( "CVR: 0x%X\n", (unsigned int)SYST_CVR );&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Always prints: CVR: 0x0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Jun 2013 21:42:45 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Get-tick-count-CPU-cycle-count-with-SysTick/m-p/241363#M5964</guid>
      <dc:creator>concerned12345</dc:creator>
      <dc:date>2013-06-26T21:42:45Z</dc:date>
    </item>
    <item>
      <title>Re: Get tick count (CPU cycle count) with SysTick</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Get-tick-count-CPU-cycle-count-with-SysTick/m-p/241364#M5965</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK, I figured this out. I wasn't taking into account that the counter counts down and not up. I set the RVR register to 0x0 and since the CVR register counts from RVR to zero than loads RVR (which in my case was zero) I would always get zero.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Proper method:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SYST_RVR = 0xFFFFFFFF;&lt;/P&gt;&lt;P&gt;SYST_CSR = 0x7;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;int nStart = SYST_CVR;&lt;/P&gt;&lt;P&gt;//Do something&lt;/P&gt;&lt;P&gt;int nStop = SYST_CVR;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;int nChange = nStart - nStop;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Start value will always be larger because the CVR register counts down.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Jun 2013 22:02:10 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Get-tick-count-CPU-cycle-count-with-SysTick/m-p/241364#M5965</guid>
      <dc:creator>concerned12345</dc:creator>
      <dc:date>2013-06-26T22:02:10Z</dc:date>
    </item>
    <item>
      <title>Re: Get tick count (CPU cycle count) with SysTick</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Get-tick-count-CPU-cycle-count-with-SysTick/m-p/241365#M5966</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am using a K60 twr board, and i have a delay function like this.&lt;/P&gt;&lt;P&gt;void delay()&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp; unsigned int i, n;&lt;/P&gt;&lt;P&gt;&amp;nbsp; for(i=0;i&amp;lt;1000;i++)&lt;/P&gt;&lt;P&gt;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; asm("nop");&lt;/P&gt;&lt;P&gt;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When i checked the number of cpu cycles it takes,it showed around 3000, which is theoretically correct (optimized code for speed, so 1000nop + 1000 add + 1000 cmp)&lt;/P&gt;&lt;P&gt;Since my cpu is 120Mhz, if i set and reset a gpio before and after calling this delay, i expect &lt;/P&gt;&lt;P&gt;theoritically -&amp;gt; 3000 cpu cycles X 0.0083micro seconds = 25 micro seconds&lt;/P&gt;&lt;P&gt;but, practically (by setting and resetting gpio) i am seeing it as&amp;nbsp; around 143 micro seconds. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any idea why this difference? I am really running my core @ 125?, or is my calcultion wrong somewhere?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanks in advance&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 30 Mar 2015 16:26:40 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Get-tick-count-CPU-cycle-count-with-SysTick/m-p/241365#M5966</guid>
      <dc:creator>vipindas</dc:creator>
      <dc:date>2015-03-30T16:26:40Z</dc:date>
    </item>
    <item>
      <title>Re: Get tick count (CPU cycle count) with SysTick</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Get-tick-count-CPU-cycle-count-with-SysTick/m-p/241366#M5967</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Show us the assembly-result for this routine, and tell us whether you are running it in RAM or FLASH, and if FLASH whether you have the hardware speed-ups enabled.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 30 Mar 2015 19:59:24 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Get-tick-count-CPU-cycle-count-with-SysTick/m-p/241366#M5967</guid>
      <dc:creator>egoodii</dc:creator>
      <dc:date>2015-03-30T19:59:24Z</dc:date>
    </item>
    <item>
      <title>Re: Get tick count (CPU cycle count) with SysTick</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Get-tick-count-CPU-cycle-count-with-SysTick/m-p/241367#M5968</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please find the below routine, and i am running from RAM.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A question, if i run in flash, how do i enable hardware speed-ups?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;c code&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;while(1) {&lt;/P&gt;&lt;P&gt;toggle led();&lt;/P&gt;&lt;P&gt;delay();&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;Assembly routine&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; delay:&lt;/P&gt;&lt;P&gt;1fff02b6:&amp;nbsp;&amp;nbsp; nop&lt;/P&gt;&lt;P&gt;1fff02b8:&amp;nbsp;&amp;nbsp; subs r0,r4,#1&lt;/P&gt;&lt;P&gt;1fff02ba:&amp;nbsp;&amp;nbsp; nop&lt;/P&gt;&lt;P&gt;1fff02bc:&amp;nbsp;&amp;nbsp; subs r4,r0,#1&lt;/P&gt;&lt;P&gt;1fff02be:&amp;nbsp;&amp;nbsp; bne delay (0x1fff02b6)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ; 0x1fff02b6&lt;/P&gt;&lt;P&gt;1fff02c0:&amp;nbsp;&amp;nbsp; b main+0x38 (0x1fff0294)&amp;nbsp; ; 0x1fff0294&lt;/P&gt;&lt;P&gt;1fff02c2:&amp;nbsp;&amp;nbsp; nop&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; exit:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;delay routine&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;void delay()&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;{&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&amp;nbsp; unsigned int i, n;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&amp;nbsp; for(i=0;i&amp;lt;1000;i++)&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&amp;nbsp; {&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; asm("nop");&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&amp;nbsp; }&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;}&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Mar 2015 14:55:52 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Get-tick-count-CPU-cycle-count-with-SysTick/m-p/241367#M5968</guid>
      <dc:creator>vipindas</dc:creator>
      <dc:date>2015-03-31T14:55:52Z</dc:date>
    </item>
    <item>
      <title>Re: Get tick count (CPU cycle count) with SysTick</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Get-tick-count-CPU-cycle-count-with-SysTick/m-p/241368#M5969</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Flash memory controls are in FMC.&amp;nbsp; For the K60, you have to watch 'old' mask 0M33 parts -- several FMC errata like 2448, 2647 and 2671 mean you have to turn-off these little pre-fetch, local cache, etc. 'speed up' features.&amp;nbsp; However, since you mention 120MHz I think we can assume you are NOT.&amp;nbsp; I can't say what the details are on your particular silicon -- but I might imagine that at 120MHz there might be an SRAM wait-state involved.&amp;nbsp; You show 5 words in your loop, but I don't quite find a register R4 initialization.&amp;nbsp; You should also be aware that in 'highly pipelined' architectures like ARM, 'branch' instructions have a cost 'far in excess of the obvious', probably 3 to 5 clocks for a 'branch taken' to re-seed the pipeline.&amp;nbsp; That all being said, I might see each loop taking 15 clocks.&amp;nbsp; Suffice it to say that in these modern, fast processors it becomes 'much more difficult' to just count instructions and predict performance.&amp;nbsp; I do agree, though, that you should check your CLOCK_DIV contents, and from the 'bus clock' some 'known division' to a baud-rate (for example), and work back to see that your core is indeed 120MHz.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Mar 2015 19:29:35 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Get-tick-count-CPU-cycle-count-with-SysTick/m-p/241368#M5969</guid>
      <dc:creator>egoodii</dc:creator>
      <dc:date>2015-03-31T19:29:35Z</dc:date>
    </item>
  </channel>
</rss>

