<?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>How can I get more time from the millis function?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/95899/how-can-i-get-more-time-from-the-millis-function</link><description>Hi, 
 I created a function like Arduino&amp;#39;s millis which returns the number of milliseconds elapsed since the micro started executing the current program. Look down: 
 
 
 
 The problem is that the timer overflows every 17 minutes or so. The Arduino millis</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 24 Jan 2023 09:47:28 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/95899/how-can-i-get-more-time-from-the-millis-function" /><item><title>RE: How can I get more time from the millis function?</title><link>https://devzone.nordicsemi.com/thread/406190?ContentTypeID=1</link><pubDate>Tue, 24 Jan 2023 09:47:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2dfb245a-3d93-4add-b44a-1b9c7a8a3938</guid><dc:creator>Stefano1984</dc:creator><description>&lt;p&gt;Thank you&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How can I get more time from the millis function?</title><link>https://devzone.nordicsemi.com/thread/406036?ContentTypeID=1</link><pubDate>Mon, 23 Jan 2023 13:04:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5cc3cd73-aa75-4e6b-aa50-0fecb11cbb38</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Yes, if this never needs to run for as long as 49 days, then there is no problem to cast it to uint32_t.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How can I get more time from the millis function?</title><link>https://devzone.nordicsemi.com/thread/406003?ContentTypeID=1</link><pubDate>Mon, 23 Jan 2023 10:55:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f40e1288-8c5b-4cab-aaa1-194a9ac52e79</guid><dc:creator>Stefano1984</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;Einar Thorsrud,&lt;/p&gt;
&lt;p&gt;thanks for the answer.&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Currently I use the following code to make timings:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;#define OVERFLOW			((uint32_t)(0xFFFFFF / APP_TIMER_TICKS(1)))

uint32_t millis(void)
{
    return (app_timer_cnt_get() / APP_TIMER_TICKS(1));
}

uint32_t ElapsedTime(uint32_t prevMillis)
{
    uint32_t currMillis = millis();

	// This statement is needed because millis() overflows approximately every 17 minutes.
    if (currMillis &amp;lt; prevMillis)
        return (currMillis + OVERFLOW + 1 - prevMillis);
    
    return (currMillis - prevMillis);
}

// Example:

uint32_t timer1 = millis();

while(1)
{
	if (ElapsedTime(timer1) &amp;gt;= 10000)
	{
		// Does something exactly after 10 seconds.
	}
}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Can I change my code and use the following?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;uint32_t uptime_ms(void)
{
    return (get_now() / (uint32_t)APP_TIMER_TICKS(1));
}

uint32_t ElapsedTime(uint32_t prev_ms)
{
    uint32_t curr_ms = uptime_ms();
    
    return (curr_ms - prev_ms);
}

// Example:

uint32_t timer1 = uptime_ms();

while(1)
{
	if (ElapsedTime(timer1) &amp;gt;= 10000)
	{
		// Does something exactly after 10 seconds.
	}
}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Specifically can I cast it to uint32_t?&lt;br /&gt;In this case there is no need to manage the overflow because the overflow time is very long (my device can stay on for max 2 days) right?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thanks for your time&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How can I get more time from the millis function?</title><link>https://devzone.nordicsemi.com/thread/405969?ContentTypeID=1</link><pubDate>Mon, 23 Jan 2023 08:45:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ebc327a1-35ef-4c03-85f0-661e402eb33c</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;No, it returns the internal counter value. Essentially the same as&amp;nbsp;app_timer_cnt_get(), but where that returns a uint32_t with the 24 bit counter directly from the COUNTER register of the RTC,&amp;nbsp;get_now() returns a uint64_t that is always increasing. It comes from the COUNTER though and has the same frequency, so you can calculate the&amp;nbsp;time in milliseconds just as you did with&amp;nbsp;app_timer_cnt_get(). I suggest you try both, and you should see that before the 24 bit counter wraps around, you will get the same result with both.&lt;/p&gt;
&lt;p&gt;So that would be something like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static uint64_t uptime_ms(void)
{
    return (get_now() / (uint64_t)APP_TIMER_TICKS(1));
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Note that here I return an uint64_t. You can cast it to uint32_t and use that, but note that counting milliseconds in a uint32_t will mean that it will overflow after a bit more than 49 days.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How can I get more time from the millis function?</title><link>https://devzone.nordicsemi.com/thread/405873?ContentTypeID=1</link><pubDate>Fri, 20 Jan 2023 15:01:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:22a2525f-5919-45b8-b3a3-ed469e547e8f</guid><dc:creator>Stefano1984</dc:creator><description>&lt;p&gt;I&amp;#39;m using a NINA-B3 module where the nRF52840 microcontroller is present.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How can I get more time from the millis function?</title><link>https://devzone.nordicsemi.com/thread/405864?ContentTypeID=1</link><pubDate>Fri, 20 Jan 2023 14:46:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4f7436e7-81b3-4ecb-ba15-9c161181b62a</guid><dc:creator>Stefano1984</dc:creator><description>&lt;p&gt;Thanks for the answer but I don&amp;#39;t understand what get_now returns. Milliseconds like millis?&lt;br /&gt;How do I get milliseconds?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How can I get more time from the millis function?</title><link>https://devzone.nordicsemi.com/thread/405839?ContentTypeID=1</link><pubDate>Fri, 20 Jan 2023 13:02:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:75d768e5-2929-45e1-8d7b-5c42bdb1c311</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;If you are using a recent SDK and the app_timer implementation is app_timer2.c, then this is quite straight-forward, but you need to do a small change in the app_timer. The app_timer2 implementation use a 64 bit counter internally, so what you could do is to make get_now() non-static, and add that to&amp;nbsp; app_timer.h as shown in this diff:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/app_5F00_timer_5F00_get_5F00_now.diff"&gt;devzone.nordicsemi.com/.../app_5F00_timer_5F00_get_5F00_now.diff&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>