<?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>Time taken by a code snippet</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/57703/time-taken-by-a-code-snippet</link><description>Hi 
 I want to know the time taken(in us) by a code snippet. I had a look at app_timer functionality with the help of this post . But the timer value always shows 0. I am not sure on how to create a dummy timer. Can someone help me with this? Also which</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 14 Feb 2020 11:15:05 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/57703/time-taken-by-a-code-snippet" /><item><title>RE: Time taken by a code snippet</title><link>https://devzone.nordicsemi.com/thread/234368?ContentTypeID=1</link><pubDate>Fri, 14 Feb 2020 11:15:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5629ff13-6a02-42ad-ba7e-d8e3c38ea779</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Sorry. I wrote that code without testing it. I was missed the part that the tasks has to be set to 1 (like the NRF_TIMER3-&amp;gt;TASKS_START = 1;)&lt;/p&gt;
&lt;p&gt;Try the following:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void timer_init(void)
{
    NRF_TIMER3-&amp;gt;BITMODE         = TIMER_BITMODE_BITMODE_32Bit &amp;lt;&amp;lt; TIMER_BITMODE_BITMODE_Pos;
    NRF_TIMER3-&amp;gt;PRESCALER       = 4;
    NRF_TIMER3-&amp;gt;SHORTS          = TIMER_SHORTS_COMPARE0_CLEAR_Disabled &amp;lt;&amp;lt; TIMER3_CC_NUM;
    NRF_TIMER3-&amp;gt;MODE            = TIMER_MODE_MODE_Timer &amp;lt;&amp;lt; TIMER_MODE_MODE_Pos;
    
    NRF_TIMER3-&amp;gt;TASKS_START = 1;
}

int main(void)
{
    bsp_board_init(BSP_INIT_LEDS);

    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();
    
    timer_init();
    
    NRF_LOG_INFO(&amp;quot;test&amp;quot;);
    while (1)
    {
        bsp_board_led_invert(0);
        NRF_TIMER3-&amp;gt;TASKS_CAPTURE[0] = 1;
        nrf_delay_ms(1000);
        NRF_TIMER3-&amp;gt;TASKS_CAPTURE[1] = 1;
        NRF_LOG_INFO(&amp;quot;diff_time: %08d &amp;#181;s&amp;quot;, (NRF_TIMER3-&amp;gt;CC[1] - NRF_TIMER3-&amp;gt;CC[0]));
        
        NRF_LOG_FLUSH();
        __WFE();
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;BR,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Time taken by a code snippet</title><link>https://devzone.nordicsemi.com/thread/234299?ContentTypeID=1</link><pubDate>Fri, 14 Feb 2020 04:22:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9a26ca54-18fa-4a06-95de-c4c1429589cc</guid><dc:creator>Sai Kiran</dc:creator><description>&lt;p&gt;I have put delay. I am printing in the log. I still get 0 as output. This is the code I have used.&lt;pre class="ui-code" data-mode="text"&gt;    timer_init();
    nrf_delay_ms(100);
    NRF_TIMER3-&amp;gt;TASKS_CAPTURE[0];
    nrf_delay_us(100);
    my_function();
    NRF_TIMER3-&amp;gt;TASKS_CAPTURE[1];
    float diff_time = (float)NRF_TIMER3-&amp;gt;CC[1] - (float)NRF_TIMER3-&amp;gt;CC[0];
    NRF_LOG_INFO(&amp;quot;CC[0]: %08x, CC[1]: %08x&amp;quot;, NRF_TIMER3-&amp;gt;CC[0], NRF_TIMER3-&amp;gt;CC[1]);
    NRF_LOG_INFO(&amp;quot;Time in us: &amp;quot; NRF_LOG_FLOAT_MARKER &amp;quot;\r\n&amp;quot;,  NRF_LOG_FLOAT(diff_time));&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Time taken by a code snippet</title><link>https://devzone.nordicsemi.com/thread/234239?ContentTypeID=1</link><pubDate>Thu, 13 Feb 2020 15:34:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f29e93e0-dff1-48f5-9c68-d778eb9446be</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;It takes some time to actually start the timer. Can you try:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;timer_init();
nrf_delay_ms(100);
NRF_TIMER3-&amp;gt;TASKS_CAPTURE[0];
my_function();
NRF_TIMER3-&amp;gt;TASKS_CAPTURE[1];
uint32_t diff_time = NRF_TIMER3-&amp;gt;CC[1] - NRF_TIMER3-&amp;gt;CC[0];
NRF_LOG_INFO(&amp;quot;Time taken: %d&amp;quot;, diff_time);
NRF_LOG_INFO(&amp;quot;CC[0]: %08x, CC[1]: %08x&amp;quot;, NRF_TIMER3-&amp;gt;CC[0], NRF_TIMER3-&amp;gt;CC[1]);
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;You also need to include &amp;quot;nrf_delay.h&amp;quot; in order to use the nrf_delay_ms function.&lt;/p&gt;
&lt;p&gt;What does the log say now?&lt;/p&gt;
&lt;p&gt;Are you actually getting this printed in your log, or are you debugging to see what diff_time is? If you are debugging, have you remembered to turn off the optimization?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Time taken by a code snippet</title><link>https://devzone.nordicsemi.com/thread/234089?ContentTypeID=1</link><pubDate>Thu, 13 Feb 2020 05:09:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:20bd8777-f238-4d23-8776-a20a44a401a0</guid><dc:creator>Sai Kiran</dc:creator><description>&lt;p&gt;I have tried the code you have posted. Modified a couple of things as there were some issues while compiling. Here is my code.&lt;pre class="ui-code" data-mode="text"&gt; void timer_init()
{
    NRF_TIMER3-&amp;gt;BITMODE                 = TIMER_BITMODE_BITMODE_32Bit &amp;lt;&amp;lt; TIMER_BITMODE_BITMODE_Pos;
    NRF_TIMER3-&amp;gt;PRESCALER               = 4; // will give 1tick/&amp;#181;s
    NRF_TIMER3-&amp;gt;SHORTS                  = TIMER_SHORTS_COMPARE0_CLEAR_Disabled &amp;lt;&amp;lt; TIMER3_CC_NUM;
    NRF_TIMER3-&amp;gt;MODE                    = TIMER_MODE_MODE_Timer &amp;lt;&amp;lt; TIMER_MODE_MODE_Pos;
    
    NRF_TIMER3-&amp;gt;TASKS_START = 1;
}

int main(void)
{
    timer_init();
    NRF_TIMER3-&amp;gt;TASKS_CAPTURE[0];
    my_function();
    NRF_TIMER3-&amp;gt;TASKS_CAPTURE[1];
    uint32_t diff_time = NRF_TIMER3-&amp;gt;CC[1] - NRF_TIMER3-&amp;gt;CC[0];
    NRF_LOG_INFO(&amp;quot;Time taken: %d&amp;quot;, diff_time);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I get &amp;quot;Time taken: 0&amp;quot;. Any idea what&amp;#39;s happening? Something to do with sdk_config.h?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Time taken by a code snippet</title><link>https://devzone.nordicsemi.com/thread/234087?ContentTypeID=1</link><pubDate>Thu, 13 Feb 2020 04:50:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e2475f03-7694-42d1-8e3d-7913cc2b40db</guid><dc:creator>Sai Kiran</dc:creator><description>&lt;p&gt;Hi Edvin,&lt;/p&gt;
&lt;p&gt;Thanks for your reply. Will try to use timers and get the time taken. I have softdevice S132. Do I still have to write timer_init() function?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Time taken by a code snippet</title><link>https://devzone.nordicsemi.com/thread/234055?ContentTypeID=1</link><pubDate>Wed, 12 Feb 2020 16:43:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cdc8c65a-3762-46f6-86e3-e5a64d8eb7eb</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello Sai Kiran,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user=""]Also which is more accurate, RTC or APP_TIMER?[/quote]
&lt;p&gt;&amp;nbsp;The app_timer uses the RTC, So they should be equally accurate, but it depends on how you use it.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The easiest way to measure how long something takes is to toggle a pin before and after that piece of code, and then use a logic analyzer to measure how long the pin was high/low.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Secondly, if you want to measure in µs how long something takes, I recommend that you use a TIMER, instead of the RTC (the app_timer uses the RTC). The difference is that the timers can run on 32MHz, while the RTC is running on the 32kHz clock, giving you a resolution of ~30µs.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If you do not have a logic analyzer, you can try something like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void timer_init()
{
    NRF_TIMER3-&amp;gt;BITMODE                 = TIMER_BITMODE_BITMODE_32Bit &amp;lt;&amp;lt; TIMER_BITMODE_BITMODE_Pos;
    NRF_TIMER3-&amp;gt;PRESCALER               = 4; // will give 1tick/&amp;#181;s
    NRF_TIMER3-&amp;gt;SHORTS                  = TIMER_SHORTS_COMPARE0_CLEAR_Disabled &amp;lt;&amp;lt; TIMER_RELOAD_CC_NUM;
    NRF_TIMER3-&amp;gt;MODE                    = TIMER_MODE_MODE_Timer &amp;lt;&amp;lt; TIMER_MODE_MODE_Pos;
    
    
    TIMER3-&amp;gt;TASKS_START = 1;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Call this some time before you want to measure what you will measure, because the timer uses some time to start up the XTAL.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Then, when you want to measure, you need to capture two timestamps:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;NRF_TIMER3-&amp;gt;TASKS_CAPTURE[0];
do_tasks_I_want_to_time();
NRF_TIMER3-&amp;gt;TASKS_CAPTURE[1];

uint32_t diff_time = NRF_TIMER3-&amp;gt;CC[1] - NRF_TIMER3-&amp;gt;CC[0];

// diff_time should hold the time different in &amp;#181;s. NB: If this took more than 2^32 &amp;#181;s, 
// you will not know whether or not the timer has rolled over, or how many times. 
// As long as you use the timer in 32bit mode, it shouldn&amp;#39;t matter if the timer&amp;#39;s counter
// register has ticked from 0xFFFFFFFF to 0x0000000, as long as you take the diff_time
// = CC[1] - CC[0]&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&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></channel></rss>