<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Read the time between two lines of code</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/71768/read-the-time-between-two-lines-of-code</link><description>Hi I am using app timer in my software and in the timer handler i do multiple things which can take up random amount of time. I would like to know the time taken from the start of a line of code to the end of the line of code I could see that I can use</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 21 Oct 2022 08:54:39 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/71768/read-the-time-between-two-lines-of-code" /><item><title>RE: Read the time between two lines of code</title><link>https://devzone.nordicsemi.com/thread/391811?ContentTypeID=1</link><pubDate>Fri, 21 Oct 2022 08:54:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1a7993c7-d94e-420d-8c11-8bb6ec84a36c</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I found the problem after a bit more testing. What happens is that the TRCENA bit written in cyccnt_enable() gets cleared when the debugger forces the chip out of &lt;span&gt;&lt;a title="Debug Interface mode" href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/dif.html?cp=4_2_0_15_3#debuginterfacemode"&gt;Debug Interface mode&lt;/a&gt;&lt;/span&gt;. For instance, after programming of the app is complete. As you might guess, this will disable the cycle counter. Though only if it happens &lt;span style="text-decoration:underline;"&gt;after&lt;/span&gt; the cyccnt_enable() function has been run by your app. &lt;/p&gt;
&lt;p&gt;You can do a power cycle, pinreset, etc after programming your test app to confirm this on your side.&lt;/p&gt;
&lt;p&gt;printf I added to monitor the TRCENA status:&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1666338914011v4.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;And the expected log output if you test with the same code snippet:&lt;/p&gt;
&lt;p&gt;time = 1000810&lt;br /&gt;Debug and Trace enabled&lt;br /&gt;time = 1000667&lt;br /&gt;Debug and Trace enabled&lt;br /&gt;time = 1000841&lt;br /&gt;Debug and Trace enabled&lt;br /&gt;time = 1000841&lt;br /&gt;Debug and Trace enabled&lt;br /&gt;time = 1000841&lt;br /&gt;Debug and Trace enabled&lt;br /&gt;time = 1000841&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Read the time between two lines of code</title><link>https://devzone.nordicsemi.com/thread/391383?ContentTypeID=1</link><pubDate>Wed, 19 Oct 2022 10:21:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0ee24c5f-9e51-44c1-9f0d-ba00fb51fd4b</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;I will have to check internally and get back to you. I suspect the module is powered down when not in debug interface mode, but maybe there is a way to force it on.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Read the time between two lines of code</title><link>https://devzone.nordicsemi.com/thread/391380?ContentTypeID=1</link><pubDate>Wed, 19 Oct 2022 10:15:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:66acb20c-c1ce-48bc-81b2-003a3de321c3</guid><dc:creator>dimiz-gcx</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;&lt;br /&gt;yes if I use a debuger to execute the example it works. Is there a way to achieve this without a debugger attached? &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Read the time between two lines of code</title><link>https://devzone.nordicsemi.com/thread/391369?ContentTypeID=1</link><pubDate>Wed, 19 Oct 2022 09:11:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7ed4ee10-8922-4361-8daf-89cef047403e</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Does it work better if you put the chip in &lt;span&gt;&lt;a title="Debug Interface mode" href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/dif.html?cp=4_2_0_15_3#debuginterfacemode"&gt;Debug Interface mode&lt;/a&gt;&lt;/span&gt;? You can do so by starting a debug session in your IDE/debugger.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Read the time between two lines of code</title><link>https://devzone.nordicsemi.com/thread/391236?ContentTypeID=1</link><pubDate>Tue, 18 Oct 2022 14:27:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:80e483dc-b6e6-4f35-be4b-bc58be9a777d</guid><dc:creator>dimiz-gcx</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;&lt;br /&gt;if I take your example and modify it in the follwoing manner this example is not really wokring:&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static inline void busy_loop(void) {
    volatile size_t foo = 0;
    for (size_t i = 0; i &amp;lt; (UINT32_MAX / 128); i++)
    {
        foo += i;
    }
}

static uint32_t cycle_cnt_before;
static uint32_t cycle_cnt_after;
static uint32_t exec_time_us;

uint32_t cyccnt_get(void)
{
    return DWT-&amp;gt;CYCCNT;
}

uint32_t cyccnt_enable()
{
    CoreDebug-&amp;gt;DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
    DWT-&amp;gt;CTRL |= DWT_CTRL_CYCCNTENA_Msk;
    DWT-&amp;gt;CYCCNT = 0;
}


int p_main(void)
{
    /* Start 16MHz crystal oscillator for better accuracy (HFXO) */
    NRF_CLOCK-&amp;gt;TASKS_LFCLKSTART = 1;
    while (NRF_CLOCK-&amp;gt;EVENTS_LFCLKSTARTED == 0)
      {};
    
    cyccnt_enable();
    
    /* Start measurement */
    for (size_t i = 0; i &amp;lt; 5; i++)
    {
        cycle_cnt_before = cyccnt_get();
        // NRF_TIMER1-&amp;gt;TASKS_START = 1;
        
        // NRFX_DELAY_MS(1000);
        busy_loop();
        
        /* Stop measurement */
        cycle_cnt_after = cyccnt_get();
        // NRF_TIMER1-&amp;gt;TASKS_CAPTURE[0] = 1; // Captured counter value is stored to NRF_TIMER1-&amp;gt;CC[0]
        
        exec_time_us = (cycle_cnt_after - cycle_cnt_before) &amp;gt;&amp;gt; 6; /* Divide # of cycles with 64*/
        printf(&amp;quot;time = %lu\n&amp;quot;, exec_time_us);
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The output would looke something like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;time = 404
time = 0
time = 0
time = 0
time = 0
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;This is kind of wrong :/. the busy loop is takeing about 1-2 seconds on the nrf52840.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Read the time between two lines of code</title><link>https://devzone.nordicsemi.com/thread/295083?ContentTypeID=1</link><pubDate>Thu, 18 Feb 2021 12:26:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b72d00a6-a218-4f7d-ac97-119232b9a74b</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;For profiling of exuection time during developement I&amp;#39;d rather recommend using a high frequency &lt;span&gt;&lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/timer.html?cp=4_2_0_23#concept_xbd_hqp_sr"&gt;TIMER &amp;mdash; Timer/counter&lt;/a&gt;&lt;/span&gt; instance or the built in cycle counter. The app timer library runs off the&amp;nbsp; 32KHz RTC which limits the accuracy.&lt;/p&gt;
&lt;p&gt;Here is a small code snippet showing how you may use a TIMER or cyclecounter for measuring execution time for a code snippet:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static uint32_t cycle_cnt_before;
static uint32_t cycle_cnt_after;
static uint32_t exec_time_us;

uint32_t cyccnt_get(void)
{
    return DWT-&amp;gt;CYCCNT;
}

uint32_t cyccnt_enable()
{
    CoreDebug-&amp;gt;DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
    DWT-&amp;gt;CTRL |= DWT_CTRL_CYCCNTENA_Msk;
    DWT-&amp;gt;CYCCNT = 0;
}


int main(void)
{
    /* Start 16MHz crystal oscillator for better accuracy (HFXO) */
    NRF_CLOCK-&amp;gt;TASKS_HFCLKSTART = 1;
    while (NRF_CLOCK-&amp;gt;EVENTS_HFCLKSTARTED == 0)
      {};
    
    cyccnt_enable();
    
    /* Start measurement */
    cycle_cnt_before = cyccnt_get();
    NRF_TIMER1-&amp;gt;TASKS_START = 1;
    
    /* Place the code you want to measure the execution time of here */
    
    /* Stop measurement */
    cycle_cnt_after = cyccnt_get();
    NRF_TIMER1-&amp;gt;TASKS_CAPTURE[0] = 1; // Captured counter value is stored to NRF_TIMER1-&amp;gt;CC[0]
    
    exec_time_us = (cycle_cnt_after - cycle_cnt_before) &amp;gt;&amp;gt; 6; /* Divide # of cycles with 64*/
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Vidar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>